From 630f1e48bc0bbcaf2bc30aadcd0384f93bf8d869 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Thu, 24 Dec 2020 04:02:06 +0900 Subject: [PATCH] enable webhooks for AppConfig/Comp (#781) rename webhooks fix e2e tests Signed-off-by: roywang --- charts/vela-core/templates/webhook.yaml | 92 ++++++++++++++++++- .../validating_handler.go | 6 ++ test/e2e-test/suite_test.go | 16 +++- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/charts/vela-core/templates/webhook.yaml b/charts/vela-core/templates/webhook.yaml index 9760386ca..b688071af 100644 --- a/charts/vela-core/templates/webhook.yaml +++ b/charts/vela-core/templates/webhook.yaml @@ -3,11 +3,55 @@ apiVersion: admissionregistration.k8s.io/v1beta1 kind: MutatingWebhookConfiguration metadata: - name: mutating-webhook-configuration + name: kubevela-mutating-webhook-configuration namespace: {{ .Release.Namespace }} annotations: cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ .Values.certificate.certificateName }} webhooks: + - clientConfig: + caBundle: Cg== + service: + name: {{ template "kubevela.name" . }}-webhook + namespace: {{ .Release.Namespace }} + path: /mutating-core-oam-dev-v1alpha2-applicationconfigurations + failurePolicy: Fail + name: mutating.core.oam.dev.v1alpha2.applicationconfigurations + rules: + - apiGroups: + - core.oam.dev + apiVersions: + - v1alpha2 + operations: + - CREATE + - UPDATE + resources: + - applicationconfigurations + scope: Namespaced + admissionReviewVersions: + - v1beta1 + timeoutSeconds: 5 + - clientConfig: + caBundle: Cg== + service: + name: {{ template "kubevela.name" . }}-webhook + namespace: {{ .Release.Namespace }} + path: /mutating-core-oam-dev-v1alpha2-components + failurePolicy: Fail + name: mutating.core.oam-dev.v1alpha2.components + rules: + - apiGroups: + - core.oam.dev + apiVersions: + - v1alpha2 + operations: + - CREATE + - UPDATE + resources: + - components + scope: Namespaced + admissionReviewVersions: + - v1beta1 + timeoutSeconds: 5 - clientConfig: caBundle: Cg== service: @@ -49,11 +93,55 @@ webhooks: apiVersion: admissionregistration.k8s.io/v1beta1 kind: ValidatingWebhookConfiguration metadata: - name: validating-webhook-configuration + name: kubevela-validating-webhook-configuration namespace: {{ .Release.Namespace }} annotations: cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ .Values.certificate.certificateName }} webhooks: + - clientConfig: + caBundle: Cg== + service: + name: {{ template "kubevela.name" . }}-webhook + namespace: {{ .Release.Namespace }} + path: /validating-core-oam-dev-v1alpha2-applicationconfigurations + failurePolicy: Fail + name: validating.core.oam.dev.v1alpha2.applicationconfigurations + rules: + - apiGroups: + - core.oam.dev + apiVersions: + - v1alpha2 + operations: + - CREATE + - UPDATE + resources: + - applicationconfigurations + scope: Namespaced + admissionReviewVersions: + - v1beta1 + timeoutSeconds: 5 + - clientConfig: + caBundle: Cg== + service: + name: {{ template "kubevela.name" . }}-webhook + namespace: {{ .Release.Namespace }} + path: /validating-core-oam-dev-v1alpha2-components + failurePolicy: Fail + name: validating.core.oam.dev.v1alpha2.components + rules: + - apiGroups: + - core.oam.dev + apiVersions: + - v1alpha2 + operations: + - CREATE + - UPDATE + resources: + - components + scope: Namespaced + admissionReviewVersions: + - v1beta1 + timeoutSeconds: 5 - clientConfig: caBundle: Cg== service: diff --git a/pkg/webhook/core.oam.dev/v1alpha2/applicationconfiguration/validating_handler.go b/pkg/webhook/core.oam.dev/v1alpha2/applicationconfiguration/validating_handler.go index a10bbf774..ae7d6db77 100644 --- a/pkg/webhook/core.oam.dev/v1alpha2/applicationconfiguration/validating_handler.go +++ b/pkg/webhook/core.oam.dev/v1alpha2/applicationconfiguration/validating_handler.go @@ -89,6 +89,12 @@ func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) a if err != nil { return admission.Errored(http.StatusBadRequest, err) } + if !obj.ObjectMeta.DeletionTimestamp.IsZero() { + // skip validating the AppConfig being deleted + klog.Info("skip validating applicationConfiguration being deleted", " name: ", obj.Name, + " deletiongTimestamp: ", obj.GetDeletionTimestamp()) + return admission.ValidationResponse(true, "") + } vAppConfig := &ValidatingAppConfig{} if err := vAppConfig.PrepareForValidation(ctx, h.Client, h.Mapper, obj); err != nil { klog.Info("failed init appConfig before validation ", " name: ", obj.Name, " errMsg: ", err.Error()) diff --git a/test/e2e-test/suite_test.go b/test/e2e-test/suite_test.go index 60e149477..daff0cefd 100644 --- a/test/e2e-test/suite_test.go +++ b/test/e2e-test/suite_test.go @@ -145,7 +145,7 @@ var _ = BeforeSuite(func(done Done) { // For some reason, workloadDefinition is created as a Cluster scope object label := map[string]string{"workload": "containerized-workload"} - // create a workload definition + // create workload definition for 'containerizedworkload' wd := v1alpha2.WorkloadDefinition{ ObjectMeta: metav1.ObjectMeta{ Name: "containerizedworkloads.core.oam.dev", @@ -170,6 +170,20 @@ var _ = BeforeSuite(func(done Done) { Expect(k8sClient.Create(context.Background(), &wd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) By("Created containerizedworkload.core.oam.dev") + // create workload definition for 'deployments' + wdDeploy := v1alpha2.WorkloadDefinition{ + ObjectMeta: metav1.ObjectMeta{ + Name: "deployments.apps", + }, + Spec: v1alpha2.WorkloadDefinitionSpec{ + Reference: v1alpha2.DefinitionReference{ + Name: "deployments.apps", + }, + }, + } + Expect(k8sClient.Create(context.Background(), &wdDeploy)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{})) + By("Created deployments.apps") + exampleClusterRole := rbac.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ Name: roleName,