@@ -35,6 +35,7 @@ import (
3535
3636 batchv1 "k8s.io/api/batch/v1"
3737 corev1 "k8s.io/api/core/v1"
38+ apierrors "k8s.io/apimachinery/pkg/api/errors"
3839 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3940 "k8s.io/client-go/kubernetes/scheme"
4041 "k8s.io/client-go/rest"
@@ -725,4 +726,103 @@ var _ = Describe("NodeRemediation", func() {
725726 return false
726727 }, eventuallyTimeout ).Should (BeTrue ())
727728 })
729+
730+ It ("deletes NodeOperation when the Node becomes remediated" , func () {
731+ ctx := context .Background ()
732+ nodeName := nodeNames [1 ]
733+
734+ template := nodeopsv1alpha1.NodeOperationTemplate {
735+ ObjectMeta : metav1.ObjectMeta {
736+ Name : "test-remediation-2" ,
737+ },
738+ Spec : nodeopsv1alpha1.NodeOperationTemplateSpec {
739+ Template : nodeopsv1alpha1.NodeOperationTemplateTemplateSpec {
740+ Spec : nodeopsv1alpha1.NodeOperationSpecTemplate {
741+ JobTemplate : nodeopsv1alpha1.JobTemplateSpec {
742+ Metadata : metav1.ObjectMeta {
743+ Namespace : "default" ,
744+ },
745+ Spec : batchv1.JobSpec {
746+ Template : corev1.PodTemplateSpec {
747+ Spec : corev1.PodSpec {
748+ Containers : []corev1.Container {{
749+ Name : "c" ,
750+ Image : "busybox" ,
751+ Command : []string {"sleep" , "infinity" },
752+ }},
753+ RestartPolicy : corev1 .RestartPolicyNever ,
754+ Tolerations : []corev1.Toleration {
755+ {Key : controllerTaint .Key , Operator : corev1 .TolerationOpExists },
756+ },
757+ },
758+ },
759+ },
760+ },
761+ },
762+ },
763+ },
764+ }
765+ Expect (k8sClient .Create (ctx , & template )).NotTo (HaveOccurred ())
766+
767+ remediation := nodeopsv1alpha1.NodeRemediation {
768+ ObjectMeta : metav1.ObjectMeta {
769+ Name : "test-remediation-2" ,
770+ },
771+ Spec : nodeopsv1alpha1.NodeRemediationSpec {
772+ NodeRemediationSpecTemplate : nodeopsv1alpha1.NodeRemediationSpecTemplate {
773+ Rule : nodeopsv1alpha1.NodeRemediationRule {
774+ Conditions : []nodeopsv1alpha1.NodeConditionMatcher {
775+ {Type : "TestRemediation2" , Status : corev1 .ConditionTrue },
776+ },
777+ },
778+ NodeOperationTemplateName : template .Name ,
779+ },
780+ NodeName : nodeName ,
781+ },
782+ }
783+ Expect (k8sClient .Create (ctx , & remediation )).NotTo (HaveOccurred ())
784+
785+ node := corev1.Node {}
786+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : nodeName }, & node )).NotTo (HaveOccurred ())
787+
788+ node .Status .Conditions = append (node .Status .Conditions , corev1.NodeCondition {
789+ Type : "TestRemediation2" ,
790+ Status : corev1 .ConditionTrue ,
791+ Reason : "testing" ,
792+ Message : "testing" ,
793+ LastHeartbeatTime : metav1 .NewTime (time .Now ()),
794+ LastTransitionTime : metav1 .NewTime (time .Now ()),
795+ })
796+ Expect (k8sClient .Status ().Update (ctx , & node )).NotTo (HaveOccurred ())
797+
798+ var nodeOp * nodeopsv1alpha1.NodeOperation
799+ Eventually (func () bool {
800+ nodeOpList := nodeopsv1alpha1.NodeOperationList {}
801+ Expect (k8sClient .List (ctx , & nodeOpList )).NotTo (HaveOccurred ())
802+
803+ for _ , op := range nodeOpList .Items {
804+ for _ , owner := range op .OwnerReferences {
805+ if owner .Kind == "NodeRemediation" && owner .Name == remediation .Name {
806+ nodeOp = & op
807+ return true
808+ }
809+ }
810+ }
811+
812+ return false
813+ }, eventuallyTimeout ).Should (BeTrue ())
814+
815+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : nodeName }, & node )).NotTo (HaveOccurred ())
816+ node .Status .Conditions [len (node .Status .Conditions )- 1 ].Status = corev1 .ConditionFalse
817+ Expect (k8sClient .Status ().Update (ctx , & node )).NotTo (HaveOccurred ())
818+
819+ Eventually (func () bool {
820+ err := k8sClient .Get (ctx , client.ObjectKey {
821+ Namespace : nodeOp .Namespace ,
822+ Name : nodeOp .Name ,
823+ }, & nodeopsv1alpha1.NodeOperation {})
824+
825+ return apierrors .IsNotFound (err )
826+ }, eventuallyTimeout ).Should (BeTrue ())
827+ })
728828})
0 commit comments