@@ -614,6 +614,127 @@ var _ = Describe("resource-consist-controller", func() {
614614 })
615615 })
616616
617+ Context ("validate employer" , func () {
618+ svc4 := corev1.Service {
619+ ObjectMeta : v1.ObjectMeta {
620+ Name : "resource-consist-ut-svc-4" ,
621+ Namespace : "default" ,
622+ Labels : map [string ]string {
623+ v1alpha1 .ControlledByKusionStackLabelKey : "true" ,
624+ "resource-consist.kusionstack.io/invalid" : "true" ,
625+ },
626+ },
627+ Spec : corev1.ServiceSpec {
628+ Ports : []corev1.ServicePort {
629+ {
630+ Name : "tcp-80" ,
631+ Port : 80 ,
632+ Protocol : corev1 .ProtocolTCP ,
633+ },
634+ },
635+ Selector : map [string ]string {
636+ "resource-consist-ut" : "resource-consist-ut-4" ,
637+ },
638+ },
639+ }
640+
641+ It ("employer with invalid label should not reconcile" , func () {
642+ rc .ExpectedCalls = nil
643+ rc .On ("QueryVip" , mock .Anything ).Return (& DemoResourceVipOps {}, nil )
644+ rc .On ("CreateVip" , mock .Anything ).Return (& DemoResourceVipOps {}, nil )
645+ rc .On ("UpdateVip" , mock .Anything ).Return (& DemoResourceVipOps {}, nil )
646+ rc .On ("DeleteVip" , mock .Anything ).Return (& DemoResourceVipOps {}, nil )
647+ rc .On ("QueryRealServer" , mock .Anything ).Return (& DemoResourceRsOps {}, nil )
648+ rc .On ("CreateRealServer" , mock .Anything ).Return (& DemoResourceRsOps {}, nil )
649+ rc .On ("UpdateRealServer" , mock .Anything ).Return (& DemoResourceRsOps {}, nil )
650+ rc .On ("DeleteRealServer" , mock .Anything ).Return (& DemoResourceRsOps {}, nil )
651+
652+ Expect (mgr .GetClient ().Create (context .Background (), & svc4 )).Should (BeNil ())
653+
654+ // Employer should NOT be created in backend provider because validation failed
655+ Consistently (func () bool {
656+ _ , exist := demoResourceVipStatusInProvider .Load (svc4 .Name )
657+ return ! exist
658+ }, 2 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
659+
660+ // Clean finalizer should NOT be added because validation failed
661+ Consistently (func () bool {
662+ svcTmp := corev1.Service {}
663+ err := mgr .GetClient ().Get (context .TODO (), types.NamespacedName {
664+ Name : svc4 .Name ,
665+ Namespace : svc4 .Namespace ,
666+ }, & svcTmp )
667+ if err != nil {
668+ return false
669+ }
670+ for _ , flz := range svcTmp .GetFinalizers () {
671+ if flz == cleanFinalizer {
672+ return false
673+ }
674+ }
675+ return true
676+ }, 2 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
677+ })
678+
679+ It ("after removing invalid label, employer should be reconciled" , func () {
680+ // Remove invalid label
681+ Eventually (func () bool {
682+ svcTmp := corev1.Service {}
683+ err := mgr .GetClient ().Get (context .TODO (), types.NamespacedName {
684+ Name : svc4 .Name ,
685+ Namespace : svc4 .Namespace ,
686+ }, & svcTmp )
687+ if err != nil {
688+ return false
689+ }
690+ delete (svcTmp .Labels , "resource-consist.kusionstack.io/invalid" )
691+ return mgr .GetClient ().Update (context .TODO (), & svcTmp ) == nil
692+ }, 3 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
693+
694+ // Clean finalizer should be added now
695+ Eventually (func () bool {
696+ svcTmp := corev1.Service {}
697+ err := mgr .GetClient ().Get (context .TODO (), types.NamespacedName {
698+ Name : svc4 .Name ,
699+ Namespace : svc4 .Namespace ,
700+ }, & svcTmp )
701+ if err != nil {
702+ return false
703+ }
704+ for _ , flz := range svcTmp .GetFinalizers () {
705+ if flz == cleanFinalizer {
706+ return true
707+ }
708+ }
709+ return false
710+ }, 3 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
711+
712+ // Now employer should be created in backend provider
713+ Eventually (func () bool {
714+ details , exist := demoResourceVipStatusInProvider .Load (svc4 .Name )
715+ return exist && details .(DemoServiceDetails ).RemoteVIP == "demo-remote-VIP" && details .(DemoServiceDetails ).RemoteVIPQPS == 100
716+ }, 3 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
717+ })
718+
719+ It ("clean up svc4" , func () {
720+ Expect (mgr .GetClient ().Delete (context .TODO (), & svc4 )).Should (BeNil ())
721+
722+ Eventually (func () bool {
723+ svcTmp := corev1.Service {}
724+ err := mgr .GetClient ().Get (context .TODO (), types.NamespacedName {
725+ Name : svc4 .Name ,
726+ Namespace : svc4 .Namespace ,
727+ }, & svcTmp )
728+ return errors .IsNotFound (err )
729+ }, 3 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
730+
731+ Eventually (func () bool {
732+ _ , exist := demoResourceVipStatusInProvider .Load (svc4 .Name )
733+ return ! exist
734+ }, 3 * time .Second , 100 * time .Millisecond ).Should (BeTrue ())
735+ })
736+ })
737+
617738 Context ("label/selector change" , func () {
618739 svc3 := corev1.Service {
619740 ObjectMeta : v1.ObjectMeta {
0 commit comments