@@ -320,7 +320,18 @@ var _ = Describe("Service Webhook", func() {
320320 Expect (err ).To (HaveOccurred ())
321321 Expect (err .Error ()).To (ContainSubstring ("192.168.1.1" ))
322322 })
323- It ("Should reject creation when both ipam annotations are set" , func () {
323+ It ("Should allow creation when both ipam annotations are set to the same value" , func () {
324+ pool := & ciliumv2alpha1.CiliumLoadBalancerIPPool {
325+ ObjectMeta : metav1.ObjectMeta {
326+ Name : "my-pool" ,
327+ },
328+ Spec : ciliumv2alpha1.CiliumLoadBalancerIPPoolSpec {
329+ Blocks : []ciliumv2alpha1.CiliumLoadBalancerIPPoolIPBlock {
330+ {Cidr : "10.0.0.0/24" },
331+ },
332+ },
333+ }
334+
324335 obj := & corev1.Service {
325336 ObjectMeta : metav1.ObjectMeta {
326337 Name : "test-svc" ,
@@ -329,7 +340,32 @@ var _ = Describe("Service Webhook", func() {
329340 "network.snappcloud.io/address-pool" : "my-pool" ,
330341 },
331342 Annotations : map [string ]string {
332- "io.cilium/lb-ipam-ips" : "10.0.0.5,192.168.1.1" ,
343+ "io.cilium/lb-ipam-ips" : "10.0.0.5,10.0.0.10" ,
344+ "lbipam.cilium.io/ips" : "10.0.0.10,10.0.0.5" ,
345+ },
346+ },
347+ }
348+
349+ scheme := runtime .NewScheme ()
350+ Expect (ciliumv2alpha1 .AddToScheme (scheme )).To (Succeed ())
351+
352+ validator := ServiceCustomValidator {
353+ client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (pool ).Build (),
354+ }
355+ _ , err := validator .ValidateCreate (ctx , obj )
356+ Expect (err ).NotTo (HaveOccurred ())
357+ })
358+
359+ It ("Should reject creation when the two ipam annotations are set to different values" , func () {
360+ obj := & corev1.Service {
361+ ObjectMeta : metav1.ObjectMeta {
362+ Name : "test-svc" ,
363+ Namespace : "default" ,
364+ Labels : map [string ]string {
365+ "network.snappcloud.io/address-pool" : "my" ,
366+ },
367+ Annotations : map [string ]string {
368+ "io.cilium/lb-ipam-ips" : "10.0.0.4,192.168.1.1" ,
333369 "lbipam.cilium.io/ips" : "10.0.0.4,192.168.1.2" ,
334370 },
335371 },
@@ -339,10 +375,22 @@ var _ = Describe("Service Webhook", func() {
339375 }
340376 _ , err := validator .ValidateCreate (ctx , obj )
341377 Expect (err ).To (HaveOccurred ())
342- Expect (err .Error ()).To (ContainSubstring ("at most one" ))
378+ Expect (err .Error ()).To (ContainSubstring (lbIpamIpsAnnotation ))
379+ Expect (err .Error ()).To (ContainSubstring (lbIpamIpsAnnotationAlias ))
343380 })
344381
345- It ("Should reject creation when lbIpamIps annotation and loadBalancerIP are both set" , func () {
382+ It ("Should allow creation when lbIpamIps annotation and loadBalancerIP are set to the same value" , func () {
383+ pool := & ciliumv2alpha1.CiliumLoadBalancerIPPool {
384+ ObjectMeta : metav1.ObjectMeta {
385+ Name : "my-pool" ,
386+ },
387+ Spec : ciliumv2alpha1.CiliumLoadBalancerIPPoolSpec {
388+ Blocks : []ciliumv2alpha1.CiliumLoadBalancerIPPoolIPBlock {
389+ {Cidr : "10.0.0.0/24" },
390+ },
391+ },
392+ }
393+
346394 obj := & corev1.Service {
347395 ObjectMeta : metav1.ObjectMeta {
348396 Name : "test-svc" ,
@@ -351,22 +399,61 @@ var _ = Describe("Service Webhook", func() {
351399 "network.snappcloud.io/address-pool" : "my-pool" ,
352400 },
353401 Annotations : map [string ]string {
354- "io.cilium/lb-ipam-ips" : "10.0.0.5" ,
402+ "io.cilium/lb-ipam-ips" : "10.0.0.5,10.0.0.3 " ,
355403 },
356404 },
357405 Spec : corev1.ServiceSpec {
358- LoadBalancerIP : "10.0.0.5" ,
406+ LoadBalancerIP : "10.0.0.3,10.0.0.5" ,
407+ },
408+ }
409+
410+ scheme := runtime .NewScheme ()
411+ Expect (ciliumv2alpha1 .AddToScheme (scheme )).To (Succeed ())
412+
413+ validator := ServiceCustomValidator {
414+ client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (pool ).Build (),
415+ }
416+ _ , err := validator .ValidateCreate (ctx , obj )
417+ Expect (err ).NotTo (HaveOccurred ())
418+ })
419+
420+ It ("Should reject creation when lbIpamIps annotation and loadBalancerIP are set to different values" , func () {
421+ obj := & corev1.Service {
422+ ObjectMeta : metav1.ObjectMeta {
423+ Name : "test-svc" ,
424+ Namespace : "default" ,
425+ Labels : map [string ]string {
426+ "network.snappcloud.io/address-pool" : "my" ,
427+ },
428+ Annotations : map [string ]string {
429+ "io.cilium/lb-ipam-ips" : "10.0.0.5,192.168.1.2" ,
430+ },
431+ },
432+ Spec : corev1.ServiceSpec {
433+ LoadBalancerIP : "10.0.0.6,192.168.1.2" ,
359434 },
360435 }
361436 validator := ServiceCustomValidator {
362437 client : fake .NewClientBuilder ().Build (),
363438 }
364439 _ , err := validator .ValidateCreate (ctx , obj )
365440 Expect (err ).To (HaveOccurred ())
366- Expect (err .Error ()).To (ContainSubstring ("at most one" ))
441+ Expect (err .Error ()).To (ContainSubstring (lbIpamIpsAnnotation ))
442+ Expect (err .Error ()).To (ContainSubstring ("spec.loadBalancerIP" ))
367443 })
368444
369- It ("Should reject creation when lbIpamIps alias annotation and loadBalancerIP are both set" , func () {
445+ It ("Should allow update when lbIpamIps alias annotation and loadBalancerIP are set to the same value" , func () {
446+ pool := & ciliumv2alpha1.CiliumLoadBalancerIPPool {
447+ ObjectMeta : metav1.ObjectMeta {
448+ Name : "my-pool" ,
449+ },
450+ Spec : ciliumv2alpha1.CiliumLoadBalancerIPPoolSpec {
451+ Blocks : []ciliumv2alpha1.CiliumLoadBalancerIPPoolIPBlock {
452+ {Cidr : "10.0.0.0/24" },
453+ },
454+ },
455+ }
456+
370457 obj := & corev1.Service {
371458 ObjectMeta : metav1.ObjectMeta {
372459 Name : "test-svc" ,
@@ -382,12 +469,40 @@ var _ = Describe("Service Webhook", func() {
382469 LoadBalancerIP : "10.0.0.5" ,
383470 },
384471 }
472+
473+ scheme := runtime .NewScheme ()
474+ Expect (ciliumv2alpha1 .AddToScheme (scheme )).To (Succeed ())
475+
476+ validator := ServiceCustomValidator {
477+ client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (pool ).Build (),
478+ }
479+ _ , err := validator .ValidateUpdate (ctx , nil , obj )
480+ Expect (err ).NotTo (HaveOccurred ())
481+ })
482+
483+ It ("Should reject creation when lbIpamIps alias annotation and loadBalancerIP are set to different values" , func () {
484+ obj := & corev1.Service {
485+ ObjectMeta : metav1.ObjectMeta {
486+ Name : "test-svc" ,
487+ Namespace : "default" ,
488+ Labels : map [string ]string {
489+ "network.snappcloud.io/address-pool" : "my" ,
490+ },
491+ Annotations : map [string ]string {
492+ "lbipam.cilium.io/ips" : "10.0.0.5" ,
493+ },
494+ },
495+ Spec : corev1.ServiceSpec {
496+ LoadBalancerIP : "10.0.0.6" ,
497+ },
498+ }
385499 validator := ServiceCustomValidator {
386500 client : fake .NewClientBuilder ().Build (),
387501 }
388502 _ , err := validator .ValidateCreate (ctx , obj )
389503 Expect (err ).To (HaveOccurred ())
390- Expect (err .Error ()).To (ContainSubstring ("at most one" ))
504+ Expect (err .Error ()).To (ContainSubstring (lbIpamIpsAnnotationAlias ))
505+ Expect (err .Error ()).To (ContainSubstring ("spec.loadBalancerIP" ))
391506 })
392507 })
393508
0 commit comments