Skip to content

Commit

Permalink
Fix ObjectMeta CRD validation
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
  • Loading branch information
mikkeloscar committed Aug 26, 2020
1 parent d161726 commit 7cf30e0
Show file tree
Hide file tree
Showing 9 changed files with 649 additions and 40 deletions.
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ E2E_IMAGE ?= $(IMAGE)-e2e
TAG ?= $(VERSION)
SOURCES = $(shell find . -name '*.go')
CRD_SOURCES = $(shell find pkg/apis/zalando.org -name '*.go')
CRD_TYPE_SOURCE = pkg/apis/zalando.org/v1/types.go
GENERATED_CRDS = docs/stackset_crd.yaml docs/stack_crd.yaml
GENERATED = pkg/apis/zalando.org/v1/zz_generated.deepcopy.go
CONTROLLER_GEN = ./build/controller-gen
Expand All @@ -31,24 +32,22 @@ check: $(GENERATED)
go mod download
golangci-lint run --timeout=2m ./...

$(GENERATED):
$(GENERATED): $(CRD_TYPE_SOURCE)
./hack/update-codegen.sh

$(CONTROLLER_GEN):
mkdir -p build
GOBIN=$(shell pwd)/build go install sigs.k8s.io/controller-tools/cmd/controller-gen

crds: $(GENERATED_CRDS)

$(GENERATED_CRDS): $(CONTROLLER_GEN) $(GENERATED) $(CRD_SOURCES)
$(CONTROLLER_GEN) crd:preserveUnknownFields=false paths=./pkg/apis/... output:crd:dir=docs || /bin/true || true
$(CONTROLLER_GEN) crd:crdVersions=v1beta1,preserveUnknownFields=false paths=./pkg/apis/... output:crd:dir=docs || /bin/true || true
mv docs/zalando.org_stacksets.yaml docs/stackset_crd.yaml
mv docs/zalando.org_stacks.yaml docs/stack_crd.yaml
# workaround for CRD issue with k8s 1.18 & controller-gen 0.3
# ref: https://github.com/kubernetes/kubernetes/issues/91395
perl -i -p0e 's/\s*x-kubernetes-list-map-keys:.*?x-kubernetes-list-type: map//seg' $(GENERATED_CRDS)

build.local: $(LOCAL_BINARIES)
build.local: $(LOCAL_BINARIES) $(GENERATED_CRDS)
build.linux: $(LINUX_BINARIES)

build/linux/e2e: $(GENERATED) $(SOURCES)
Expand Down
250 changes: 247 additions & 3 deletions docs/stack_crd.yaml

Large diffs are not rendered by default.

298 changes: 295 additions & 3 deletions docs/stackset_crd.yaml

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/emicklei/go-restful v2.11.1+incompatible // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/go-openapi/spec v0.19.4 // indirect
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 // indirect
github.com/google/go-cmp v0.5.1
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40
Expand All @@ -16,14 +15,16 @@ require (
github.com/prometheus/client_golang v1.7.1
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.6.1
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/tools v0.0.0-20200123022218-593de606220b // indirect
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/api v0.18.8
k8s.io/apimachinery v0.18.8
k8s.io/client-go v0.18.8
k8s.io/code-generator v0.18.8
sigs.k8s.io/controller-tools v0.3.0
sigs.k8s.io/controller-tools v0.4.0
)

// This PR: https://github.com/kubernetes-sigs/controller-tools/pull/395
// rebased on v0.4.0
replace sigs.k8s.io/controller-tools => github.com/mikkeloscar/controller-tools v0.4.1-0.20200826152554-54e4525f9990
48 changes: 39 additions & 9 deletions go.sum

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions pkg/apis/zalando.org/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,26 @@ type StackSetSpec struct {
Traffic []*DesiredTraffic `json:"traffic,omitempty"`
}

// EmbeddedObjectMetaWithAnnotations defines the metadata which can be attached
// to a resource. It's a slimmed down version of metav1.ObjectMeta only
// containing annotations.
// +k8s:deepcopy-gen=true
type EmbeddedObjectMetaWithAnnotations struct {
// Annotations is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata. They are not
// queryable and should be preserved when modifying objects.
// More info: http://kubernetes.io/docs/user-guide/annotations
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
}

// StackSetIngressSpec is the ingress defintion of an StackSet. This
// includes ingress annotations and a list of hostnames.
// +k8s:deepcopy-gen=true
type StackSetIngressSpec struct {
metav1.ObjectMeta `json:"metadata,omitempty"`
Hosts []string `json:"hosts"`
BackendPort intstr.IntOrString `json:"backendPort"`
EmbeddedObjectMetaWithAnnotations `json:"metadata,omitempty"`
Hosts []string `json:"hosts"`
BackendPort intstr.IntOrString `json:"backendPort"`
// +optional
Path string `json:"path"`
}
Expand Down Expand Up @@ -90,8 +103,8 @@ type StackLifecycle struct {
// StackSet definition.
// +k8s:deepcopy-gen=true
type StackTemplate struct {
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec StackSpecTemplate `json:"spec"`
EmbeddedObjectMetaWithAnnotations `json:"metadata,omitempty"`
Spec StackSpecTemplate `json:"spec"`
}

// MetricsEndpoint specified the endpoint where the custom endpoint where the metrics
Expand Down Expand Up @@ -145,7 +158,6 @@ type Autoscaler struct {
// defined an HPA will be created for the Stack.
// +k8s:deepcopy-gen=true
type HorizontalPodAutoscaler struct {
metav1.ObjectMeta `json:"metadata,omitempty"`
// minReplicas is the lower limit for the number of replicas to which the autoscaler can scale down.
// It defaults to 1 pod.
// +optional
Expand Down Expand Up @@ -278,7 +290,7 @@ type StackSpec struct {
// a stack.
// +k8s:deepcopy-gen=true
type StackServiceSpec struct {
metav1.ObjectMeta `json:"metadata,omitempty"`
EmbeddedObjectMetaWithAnnotations `json:"metadata,omitempty"`

// The list of ports that are exposed by this service.
// More info: https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies
Expand Down
41 changes: 37 additions & 4 deletions pkg/apis/zalando.org/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pkg/core/stack_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ func TestStackGenerateIngress(t *testing.T) {
},
stacksetName: "foo",
ingressSpec: &zv1.StackSetIngressSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"ignored": "label"},
EmbeddedObjectMetaWithAnnotations: zv1.EmbeddedObjectMetaWithAnnotations{
Annotations: map[string]string{"ingress": "annotation"},
},
Hosts: []string{"foo.example.org", "foo.example.com"},
Expand Down Expand Up @@ -342,7 +341,7 @@ func TestStackGenerateService(t *testing.T) {
ObjectMeta: testStackMeta,
Spec: zv1.StackSpec{
Service: &zv1.StackServiceSpec{
ObjectMeta: metav1.ObjectMeta{
EmbeddedObjectMetaWithAnnotations: zv1.EmbeddedObjectMetaWithAnnotations{
Annotations: svcAnnotations,
},
Ports: []v1.ServicePort{
Expand Down
3 changes: 1 addition & 2 deletions pkg/core/stackset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,7 @@ func TestStackSetGenerateIngress(t *testing.T) {
},
Spec: zv1.StackSetSpec{
Ingress: &zv1.StackSetIngressSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"ignored": "label"},
EmbeddedObjectMetaWithAnnotations: zv1.EmbeddedObjectMetaWithAnnotations{
Annotations: map[string]string{"ingress": "annotation"},
},
Hosts: []string{"example.org", "example.com"},
Expand Down

0 comments on commit 7cf30e0

Please sign in to comment.