@@ -11,6 +11,10 @@ import (
1111 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1212 "k8s.io/apimachinery/pkg/types"
1313
14+ "github.com/openstack-k8s-operators/lib-common/modules/common/annotations"
15+ baremetalv1 "github.com/openstack-k8s-operators/openstack-baremetal-operator/api/v1beta1"
16+ webhookv1beta1 "github.com/openstack-k8s-operators/openstack-baremetal-operator/internal/webhook/v1beta1"
17+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1418 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1519)
1620
@@ -151,6 +155,58 @@ var _ = Describe("OpenStackBaremetalSet Webhook", func() {
151155 })
152156 })
153157
158+ When ("When creating BaremetalSet with skip webhook validation annotation" , func () {
159+ BeforeEach (func () {
160+ DeferCleanup (th .DeleteInstance , CreateBaremetalHost (bmhName ))
161+ bmh := GetBaremetalHost (bmhName )
162+ Eventually (func (g Gomega ) {
163+ bmh .Status .Provisioning .State = metal3v1 .StateAvailable
164+ g .Expect (th .K8sClient .Status ().Update (th .Ctx , bmh )).To (Succeed ())
165+ }, th .Timeout , th .Interval ).Should (Succeed ())
166+ })
167+
168+ It ("It should not fail even with insufficient BMHs when annotation is set" , func () {
169+ spec := TwoNodeBaremetalSetSpec (baremetalSetName .Namespace )
170+ object := DefaultBaremetalSetTemplate (baremetalSetName , spec )
171+ metadata := object ["metadata" ].(map [string ]any )
172+ metadata ["annotations" ] = map [string ]any {
173+ annotations .SkipValidationAnnotation : "" ,
174+ }
175+ unstructuredObj := & unstructured.Unstructured {Object : object }
176+ _ , err := controllerutil .CreateOrPatch (
177+ th .Ctx , th .K8sClient , unstructuredObj , func () error { return nil })
178+ Expect (err ).ShouldNot (HaveOccurred ())
179+ })
180+
181+ It ("should return a warning when skip-validation annotation is set" , func () {
182+ validator := & webhookv1beta1.OpenStackBaremetalSetCustomValidator {}
183+ obj := & baremetalv1.OpenStackBaremetalSet {
184+ ObjectMeta : metav1.ObjectMeta {
185+ Name : baremetalSetName .Name ,
186+ Namespace : baremetalSetName .Namespace ,
187+ Annotations : map [string ]string {
188+ annotations .SkipValidationAnnotation : "" ,
189+ },
190+ },
191+ }
192+
193+ warnings , err := validator .ValidateCreate (th .Ctx , obj )
194+ Expect (err ).ShouldNot (HaveOccurred ())
195+ Expect (warnings ).To (HaveLen (1 ))
196+ Expect (warnings [0 ]).To (ContainSubstring (annotations .SkipValidationAnnotation ))
197+
198+ warnings , err = validator .ValidateUpdate (th .Ctx , obj , obj )
199+ Expect (err ).ShouldNot (HaveOccurred ())
200+ Expect (warnings ).To (HaveLen (1 ))
201+ Expect (warnings [0 ]).To (ContainSubstring (annotations .SkipValidationAnnotation ))
202+
203+ warnings , err = validator .ValidateDelete (th .Ctx , obj )
204+ Expect (err ).ShouldNot (HaveOccurred ())
205+ Expect (warnings ).To (HaveLen (1 ))
206+ Expect (warnings [0 ]).To (ContainSubstring (annotations .SkipValidationAnnotation ))
207+ })
208+ })
209+
154210 When ("When creating BaremetalSet with node selectors" , func () {
155211 BeforeEach (func () {
156212 DeferCleanup (th .DeleteInstance , CreateBaremetalHostWithNodeLabel (
0 commit comments