|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package v1 |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 29 | + |
| 30 | + apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1" |
| 31 | + "github.com/cloudnative-pg/cloudnative-pg/pkg/utils" |
| 32 | + |
| 33 | + . "github.com/onsi/ginkgo/v2" |
| 34 | + . "github.com/onsi/gomega" |
| 35 | +) |
| 36 | + |
| 37 | +func newClusterWithValidationAnnotation(value string) *apiv1.Cluster { |
| 38 | + return &apiv1.Cluster{ |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Annotations: map[string]string{ |
| 41 | + utils.WebhookValidationAnnotationName: value, |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +var _ = Describe("Validation webhook validation parser", func() { |
| 48 | + It("ensures that with no annotations the validation checking is enabled", func() { |
| 49 | + cluster := &apiv1.Cluster{} |
| 50 | + Expect(isValidationEnabled(cluster)).To(BeTrue()) |
| 51 | + }) |
| 52 | + |
| 53 | + It("ensures that with validation can be explicitly enabled", func() { |
| 54 | + cluster := newClusterWithValidationAnnotation(validationEnabledAnnotationValue) |
| 55 | + Expect(isValidationEnabled(cluster)).To(BeTrue()) |
| 56 | + }) |
| 57 | + |
| 58 | + It("ensures that with validation can be explicitly disabled", func() { |
| 59 | + cluster := newClusterWithValidationAnnotation(validationDisabledAnnotationValue) |
| 60 | + Expect(isValidationEnabled(cluster)).To(BeFalse()) |
| 61 | + }) |
| 62 | + |
| 63 | + It("ensures that with validation is enabled when the annotation value is unknown", func() { |
| 64 | + cluster := newClusterWithValidationAnnotation("idontknow") |
| 65 | + status, err := isValidationEnabled(cluster) |
| 66 | + Expect(err).To(HaveOccurred()) |
| 67 | + Expect(status).To(BeTrue()) |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +type fakeCustomValidator struct { |
| 72 | + calls []string |
| 73 | + |
| 74 | + createWarnings admission.Warnings |
| 75 | + createError error |
| 76 | + |
| 77 | + updateWarnings admission.Warnings |
| 78 | + updateError error |
| 79 | + |
| 80 | + deleteWarnings admission.Warnings |
| 81 | + deleteError error |
| 82 | +} |
| 83 | + |
| 84 | +func (f *fakeCustomValidator) ValidateCreate( |
| 85 | + _ context.Context, |
| 86 | + _ runtime.Object, |
| 87 | +) (admission.Warnings, error) { |
| 88 | + f.calls = append(f.calls, "create") |
| 89 | + return f.createWarnings, f.createError |
| 90 | +} |
| 91 | + |
| 92 | +func (f *fakeCustomValidator) ValidateUpdate( |
| 93 | + _ context.Context, |
| 94 | + _ runtime.Object, |
| 95 | + _ runtime.Object, |
| 96 | +) (admission.Warnings, error) { |
| 97 | + f.calls = append(f.calls, "update") |
| 98 | + return f.updateWarnings, f.updateError |
| 99 | +} |
| 100 | + |
| 101 | +func (f *fakeCustomValidator) ValidateDelete( |
| 102 | + _ context.Context, |
| 103 | + _ runtime.Object, |
| 104 | +) (admission.Warnings, error) { |
| 105 | + f.calls = append(f.calls, "delete") |
| 106 | + return f.deleteWarnings, f.deleteError |
| 107 | +} |
| 108 | + |
| 109 | +var _ = Describe("Bypassable validator", func() { |
| 110 | + fakeCreateError := fmt.Errorf("fake error") |
| 111 | + fakeUpdateError := fmt.Errorf("fake error") |
| 112 | + fakeDeleteError := fmt.Errorf("fake error") |
| 113 | + |
| 114 | + disabledCluster := newClusterWithValidationAnnotation(validationDisabledAnnotationValue) |
| 115 | + enabledCluster := newClusterWithValidationAnnotation(validationEnabledAnnotationValue) |
| 116 | + wrongCluster := newClusterWithValidationAnnotation("dontknow") |
| 117 | + |
| 118 | + fakeErrorValidator := &fakeCustomValidator{ |
| 119 | + createError: fakeCreateError, |
| 120 | + deleteError: fakeDeleteError, |
| 121 | + updateError: fakeUpdateError, |
| 122 | + } |
| 123 | + |
| 124 | + DescribeTable( |
| 125 | + "validator callbacks", |
| 126 | + func(ctx SpecContext, c *apiv1.Cluster, expectedError, withWarnings bool) { |
| 127 | + b := newBypassableValidator(fakeErrorValidator) |
| 128 | + |
| 129 | + By("creation entrypoint", func() { |
| 130 | + result, err := b.ValidateCreate(ctx, c) |
| 131 | + if expectedError { |
| 132 | + Expect(err).To(Equal(fakeCreateError)) |
| 133 | + } else { |
| 134 | + Expect(err).ToNot(HaveOccurred()) |
| 135 | + } |
| 136 | + |
| 137 | + if withWarnings { |
| 138 | + Expect(result).To(HaveLen(1)) |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + By("update entrypoint", func() { |
| 143 | + result, err := b.ValidateUpdate(ctx, enabledCluster, c) |
| 144 | + if expectedError { |
| 145 | + Expect(err).To(Equal(fakeUpdateError)) |
| 146 | + } else { |
| 147 | + Expect(err).ToNot(HaveOccurred()) |
| 148 | + } |
| 149 | + |
| 150 | + if withWarnings { |
| 151 | + Expect(result).To(HaveLen(1)) |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + By("delete entrypoint", func() { |
| 156 | + result, err := b.ValidateDelete(ctx, c) |
| 157 | + if expectedError { |
| 158 | + Expect(err).To(Equal(fakeDeleteError)) |
| 159 | + } else { |
| 160 | + Expect(err).ToNot(HaveOccurred()) |
| 161 | + } |
| 162 | + |
| 163 | + if withWarnings { |
| 164 | + Expect(result).To(HaveLen(1)) |
| 165 | + } |
| 166 | + }) |
| 167 | + }, |
| 168 | + Entry("validation is disabled", disabledCluster, false, true), |
| 169 | + Entry("validation is enabled", enabledCluster, true, false), |
| 170 | + Entry("validation value is not expected", wrongCluster, true, true), |
| 171 | + ) |
| 172 | +}) |
0 commit comments