Skip to content

Commit

Permalink
update TestClusterValidation
Browse files Browse the repository at this point in the history
Signed-off-by: hejianpeng <hejianpeng2@huawei.com>
  • Loading branch information
zirain committed Feb 28, 2023
1 parent 2f43933 commit 1413eb0
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 11 deletions.
3 changes: 2 additions & 1 deletion hack/update-crdgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ do
done

echo "running kustomize to generate the final CRD"
kubectl kustomize manifests/charts/ -o "${CRD_PATH}"/infrastructure.cluster.x-k8s.io_customclusters.yaml
kubectl kustomize manifests/charts/base -o "${CRD_PATH}"/infrastructure.cluster.x-k8s.io_customclusters.yaml
kubectl kustomize manifests/charts/cluster-operator -o "${WEBHOOK_PATH}"/manifests.yaml
6 changes: 6 additions & 0 deletions manifests/charts/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- templates/infrastructure.cluster.x-k8s.io_customclusters.yaml
commonLabels:
cluster.x-k8s.io/v1beta1: v1alpha1
5 changes: 5 additions & 0 deletions manifests/charts/cluster-operator/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- templates/manifests.yaml
namePrefix: cluster-operator-
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
creationTimestamp: null
name: mutating-webhook-configuration
name: cluster-operator-cluster-operatormutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1alpha1
Expand Down Expand Up @@ -31,7 +30,7 @@ apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
creationTimestamp: null
name: validating-webhook-configuration
name: cluster-operator-cluster-operatorvalidating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1alpha1
Expand Down
6 changes: 0 additions & 6 deletions manifests/charts/kustomization.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/webhooks/cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func validateInfra(in *v1.Cluster) field.ErrorList {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "infraType"), "must be set"))
}

if in.Spec.Region == "" {
if in.Spec.InfraType == v1.AWSClusterInfraType && in.Spec.Region == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec", "region"), "must be set"))
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/webhooks/cluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ func TestClusterDefault(t *testing.T) {
g.Expect(c.Spec.Network.ServiceCIDRs).To(Equal(v1.CIDRBlocks{defaultServiceCIDR}))
}

func TestClusterValidation(t *testing.T) {
// read configuration from examples directory
r := path.Join("../../manifests/examples", "infra")
caseNames := make([]string, 0)
err := filepath.WalkDir(r, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}

caseNames = append(caseNames, path)

return nil
})
assert.NoError(t, err)

wh := &ClusterWebhook{}
for _, tt := range caseNames {
t.Run(tt, func(t *testing.T) {
g := NewWithT(t)
c, err := readCluster(tt)
g.Expect(err).NotTo(HaveOccurred())

err = wh.validate(c)
g.Expect(err).NotTo(HaveOccurred())
})
}
}

func TestInvalidClusterValidation(t *testing.T) {
r := path.Join("testdata", "cluster")
caseNames := make([]string, 0)
Expand Down
25 changes: 25 additions & 0 deletions pkg/webhooks/testdata/cluster/aws-miss-region.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: cluster.kurator.dev/v1alpha1
kind: Cluster
metadata:
name: quickstart
namespace: default
spec:
infraType: aws
credential:
secretRef: quickstart
version: v1.23.0
network:
vpc:
cidrBlock: 10.10.0.0/16
podCIDRs:
- 192.168.0.0/16
serviceCIDRs:
- 10.96.0.0/12
cni:
type: calico
master:
replicas: 1
instanceType: t3.large
workers:
- replicas: 1
instanceType: t3.large

0 comments on commit 1413eb0

Please sign in to comment.