diff --git a/Makefile b/Makefile index 8d0d811..c199d45 100644 --- a/Makefile +++ b/Makefile @@ -31,10 +31,7 @@ $(GENERATED): $(CRD_TYPE_SOURCE) bash -x ./hack/update-codegen.sh $(GENERATED_CRD): go.mod $(GENERATED) - go run sigs.k8s.io/controller-tools/cmd/controller-gen@v0.14.0 crd:crdVersions=v1 paths=./apis/... output:crd:dir=. - # workaround to add validation to array items. Not supported by controller-gen - # ref: https://github.com/kubernetes-sigs/controller-tools/issues/342 - go run hack/crd/add_hosts_validation.go $(GENERATED_CRD) + go run sigs.k8s.io/controller-tools/cmd/controller-gen@master crd:crdVersions=v1 paths=./apis/... output:crd:dir=. build.local: $(LOCAL_BINARIES) $(GENERATED_CRD) build.linux: $(LINUX_BINARIES) $(GENERATED_CRD) diff --git a/apis/zalando.org/v1/types.go b/apis/zalando.org/v1/types.go index d20021a..beb2c86 100644 --- a/apis/zalando.org/v1/types.go +++ b/apis/zalando.org/v1/types.go @@ -43,6 +43,8 @@ type RouteGroupList struct { type RouteGroupSpec struct { // List of hostnames for the RouteGroup // +kubebuilder:validation:MinItems=1 + // +kubebuilder:validation:items:MaxLength=255 + // +kubebuilder:validation:items:Pattern="^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" // +listType=set Hosts []string `json:"hosts,omitempty"` // List of backends that can be referenced in the routes @@ -180,6 +182,8 @@ type RouteGroupTLSSpec struct { // The values in this list must match the host name(s) used for // the RouteGroup in order to terminate TLS for the host(s). // +kubebuilder:validation:MinItems=1 + // +kubebuilder:validation:items:MaxLength=255 + // +kubebuilder:validation:items:Pattern="^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" // +listType=set Hosts []string `json:"hosts"` diff --git a/hack/crd/add_hosts_validation.go b/hack/crd/add_hosts_validation.go deleted file mode 100644 index c9aedb5..0000000 --- a/hack/crd/add_hosts_validation.go +++ /dev/null @@ -1,93 +0,0 @@ -// This program adds host validation to CRD yaml. -// -// # Why -// -// controller-gen does not support validating internal list items on list types, -// see https://github.com/kubernetes-sigs/controller-tools/issues/342 -package main - -import ( - "log" - "os" - - "sigs.k8s.io/yaml" -) - -const ( - hostPattern = `^[a-z0-9]([-a-z0-9]*[a-z0-9])?([.][a-z0-9]([-a-z0-9]*[a-z0-9])?)*$` - hostMaxLength = 255 // https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.4 -) - -func must(err error) { - if err != nil { - log.Fatal(err) - } -} - -func mustGet(v []byte, err error) []byte { - must(err) - return v -} - -type setter struct { - o interface{} -} - -func (s *setter) field(name string) *setter { - return &setter{s.o.(map[string]interface{})[name]} -} - -func (s *setter) item(i int) *setter { - return &setter{s.o.([]interface{})[i]} -} - -func (s *setter) setField(name string, value interface{}) *setter { - s.o.(map[string]interface{})[name] = value - return s -} - -func main() { - if len(os.Args) != 2 { - log.Fatalf("CRD filename required") - } - crdFilename := os.Args[1] - - yamlBytes := mustGet(os.ReadFile(crdFilename)) - - o := make(map[string]interface{}) - must(yaml.Unmarshal(yamlBytes, &o)) - - s := &setter{o} - - s.field("spec"). - field("versions"). - item(0). - field("schema"). - field("openAPIV3Schema"). - field("properties"). - field("spec"). - field("properties"). - field("hosts"). - field("items"). - setField("pattern", hostPattern). - setField("maxLength", hostMaxLength) - - s.field("spec"). - field("versions"). - item(0). - field("schema"). - field("openAPIV3Schema"). - field("properties"). - field("spec"). - field("properties"). - field("tls"). - field("items"). - field("properties"). - field("hosts"). - field("items"). - setField("pattern", hostPattern). - setField("maxLength", hostMaxLength) - - outYaml := mustGet(yaml.Marshal(o)) - must(os.WriteFile(crdFilename, outYaml, 0664)) -} diff --git a/zalando.org_routegroups.yaml b/zalando.org_routegroups.yaml index d0e0d4c..cb76943 100644 --- a/zalando.org_routegroups.yaml +++ b/zalando.org_routegroups.yaml @@ -1,8 +1,9 @@ +--- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.14.0 + controller-gen.kubebuilder.io/version: v0.14.1-0.20240403150007-6bbabbc0dad8 name: routegroups.zalando.org spec: group: zalando.org