Skip to content

Commit 1795cc2

Browse files
committed
Add annotation for skipping webhook validation
1 parent ce00f67 commit 1795cc2

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

modules/common/annotations/annotations.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
// a reconciliation. Changing its value forces a new reconcile event.
3030
// Used in multiple operators, therefore defined as a shared constant.
3131
ReconcileTriggerAnnotation = "openstack.org/reconcile-trigger"
32+
33+
// SkipValidationAnnotation is set on a resource to skip webhook validation.
34+
// The annotation key presence is what matters; the value is ignored.
35+
SkipValidationAnnotation = "openstack.org/skip-webhook-validation"
3236
)
3337

3438
// IsPaused returns true if the PausedAnnotation key is present on the object.
@@ -40,3 +44,13 @@ func IsPaused(obj metav1.Object) bool {
4044
_, exists := annotations[PausedAnnotation]
4145
return exists
4246
}
47+
48+
// SkipValidation returns true if the SkipValidationAnnotation key is present on the object.
49+
func SkipValidation(obj metav1.Object) bool {
50+
annotations := obj.GetAnnotations()
51+
if annotations == nil {
52+
return false
53+
}
54+
_, exists := annotations[SkipValidationAnnotation]
55+
return exists
56+
}

modules/common/annotations/annotations_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,48 @@ func TestIsPaused(t *testing.T) {
6969
g.Expect(IsPaused(obj)).To(BeTrue())
7070
})
7171
}
72+
73+
func TestSkipValidation(t *testing.T) {
74+
t.Run("returns false when annotations are nil", func(t *testing.T) {
75+
g := NewWithT(t)
76+
obj := &corev1.ConfigMap{
77+
ObjectMeta: metav1.ObjectMeta{
78+
Name: "test",
79+
},
80+
}
81+
g.Expect(SkipValidation(obj)).To(BeFalse())
82+
})
83+
84+
t.Run("returns false when annotation is not present", func(t *testing.T) {
85+
g := NewWithT(t)
86+
obj := &corev1.ConfigMap{
87+
ObjectMeta: metav1.ObjectMeta{
88+
Name: "test",
89+
Annotations: map[string]string{"other": "value"},
90+
},
91+
}
92+
g.Expect(SkipValidation(obj)).To(BeFalse())
93+
})
94+
95+
t.Run("returns true when annotation is present with empty value", func(t *testing.T) {
96+
g := NewWithT(t)
97+
obj := &corev1.ConfigMap{
98+
ObjectMeta: metav1.ObjectMeta{
99+
Name: "test",
100+
Annotations: map[string]string{SkipValidationAnnotation: ""},
101+
},
102+
}
103+
g.Expect(SkipValidation(obj)).To(BeTrue())
104+
})
105+
106+
t.Run("returns true when annotation is present with any value", func(t *testing.T) {
107+
g := NewWithT(t)
108+
obj := &corev1.ConfigMap{
109+
ObjectMeta: metav1.ObjectMeta{
110+
Name: "test",
111+
Annotations: map[string]string{SkipValidationAnnotation: "true"},
112+
},
113+
}
114+
g.Expect(SkipValidation(obj)).To(BeTrue())
115+
})
116+
}

0 commit comments

Comments
 (0)