Skip to content

Commit

Permalink
feat(crd): add crd validation
Browse files Browse the repository at this point in the history
Adding validations against most of the fields. Also, change the way
loading CRDs at controller start-up. This is because CRD manifests are
now generated using controller-gen, not Wrangler codegen. The reason
behind it is that, currently only controller-gen supports CRD
validation using CEL.

Signed-off-by: Zespre Chang <zespre.chang@suse.com>
  • Loading branch information
starbops committed Feb 16, 2024
1 parent 02a28c0 commit fd749fb
Show file tree
Hide file tree
Showing 17 changed files with 568 additions and 486 deletions.
48 changes: 47 additions & 1 deletion chart/crds/network.harvesterhci.io_ippools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ spec:
properties:
cidr:
type: string
x-kubernetes-validations:
- message: CIDR is immutable
rule: self == oldSelf
dns:
format: ipv4
items:
type: string
maxItems: 3
type: array
domainName:
type: string
Expand All @@ -81,27 +86,67 @@ spec:
ntp:
items:
type: string
maxItems: 4
type: array
pool:
properties:
end:
format: ipv4
type: string
x-kubernetes-validations:
- message: End is immutable
rule: self == oldSelf
exclude:
format: ipv4
items:
type: string
type: array
x-kubernetes-validations:
- message: Exclude is immutable
rule: self == oldSelf
start:
format: ipv4
type: string
x-kubernetes-validations:
- message: Start is immutable
rule: self == oldSelf
required:
- end
- start
type: object
x-kubernetes-validations:
- message: End is required once set
rule: '!has(oldSelf.exclude) || has(self.exclude)'
router:
format: ipv4
type: string
x-kubernetes-validations:
- message: Router is immutable
rule: self == oldSelf
serverIP:
format: ipv4
type: string
x-kubernetes-validations:
- message: ServerIP is immutable
rule: self == oldSelf
required:
- cidr
- pool
- serverIP
type: object
x-kubernetes-validations:
- message: Router is required once set
rule: '!has(oldSelf.router) || has(self.router)'
networkName:
maxLength: 64
type: string
x-kubernetes-validations:
- message: NetworkName is immutable
rule: self == oldSelf
paused:
type: boolean
required:
- networkName
type: object
status:
properties:
Expand Down Expand Up @@ -161,4 +206,5 @@ spec:
type: object
served: true
storage: true
subresources: {}
subresources:
status: {}
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,37 @@ spec:
type: object
spec:
properties:
networkConfig:
networkConfigs:
items:
properties:
ipAddress:
format: ipv4
type: string
macAddress:
maxLength: 17
type: string
networkName:
maxLength: 64
type: string
required:
- macAddress
- networkName
type: object
maxItems: 4
type: array
x-kubernetes-validations:
- message: NetworkConfig may only be added
rule: oldSelf.all(x, x in self)
paused:
type: boolean
vmName:
maxLength: 64
type: string
x-kubernetes-validations:
- message: VMName is immutable
rule: self == oldSelf
required:
- vmName
type: object
status:
properties:
Expand Down Expand Up @@ -113,4 +129,5 @@ spec:
type: object
served: true
storage: true
subresources: {}
subresources:
status: {}
67 changes: 34 additions & 33 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,84 +21,85 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.3.0
k8s.io/api v0.27.9
k8s.io/apiextensions-apiserver v0.27.9
k8s.io/apimachinery v0.27.9
golang.org/x/sync v0.5.0
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v12.0.0+incompatible
kubevirt.io/api v1.1.0
)

require github.com/pmezard/go-difflib v1.0.0 // indirect

require (
github.com/gorilla/mux v1.8.0
github.com/rancher/dynamiclistener v0.3.5 // indirect
golang.org/x/crypto v0.17.0 // indirect
k8s.io/klog v1.0.0 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-aggregator v0.25.4 // indirect
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-bindata/go-bindata/v3 v3.1.3
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kisielk/errcheck v1.5.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.14.0 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/openshift/api v0.0.0 // indirect
github.com/openshift/custom-resource-status v1.1.2 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rancher/dynamiclistener v0.3.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/code-generator v0.27.9 // indirect
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
k8s.io/apiextensions-apiserver v0.28.0-alpha.1 // indirect
k8s.io/code-generator v0.28.0-alpha.1 // indirect
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect
k8s.io/klog v1.0.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-aggregator v0.25.4 // indirect
k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
kubevirt.io/containerized-data-importer-api v1.58.0 // indirect
kubevirt.io/controller-lifecycle-operator-sdk/api v0.0.0-20220329064328-f3cc58c6ed90 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit fd749fb

Please sign in to comment.