@@ -24,6 +24,8 @@ const (
24
24
ControllerName string = "policy-automation"
25
25
PolicyAutomationLabel string = "policy.open-cluster-management.io/policyautomation-name"
26
26
PolicyAutomationGeneration string = "policy.open-cluster-management.io/policyautomation-generation"
27
+ // policyautomation-ResouceVersion
28
+ PolicyAutomationResouceV string = "policy.open-cluster-management.io/policyautomation-resource-version"
27
29
)
28
30
29
31
var log = ctrl .Log .WithName (ControllerName )
@@ -67,6 +69,38 @@ func MatchPAGeneration(policyAutomation *policyv1beta1.PolicyAutomation,
67
69
return false , nil
68
70
}
69
71
72
+ // Check any ansiblejob is made by input genteration number
73
+ // Returning "true" means the policy automation already created ansiblejob with the generation
74
+ func MatchPAResouceV (policyAutomation * policyv1beta1.PolicyAutomation ,
75
+ dynamicClient dynamic.Interface , resourceVersion string ,
76
+ ) (bool , error ) {
77
+ ansiblejobList , err := dynamicClient .Resource (ansibleJobRes ).Namespace (policyAutomation .GetNamespace ()).List (
78
+ context .TODO (), metav1.ListOptions {
79
+ LabelSelector : fmt .Sprintf ("%s=%s" , PolicyAutomationLabel , policyAutomation .GetName ()),
80
+ },
81
+ )
82
+ if err != nil {
83
+ log .Error (err , "Failed to get ansiblejob list" )
84
+
85
+ return false , err
86
+ }
87
+
88
+ ansiblejobLen := len (ansiblejobList .Items )
89
+ // Check whether new PolicyAutomation
90
+ if ansiblejobLen == 0 {
91
+ return false , nil
92
+ }
93
+
94
+ for _ , aj := range ansiblejobList .Items {
95
+ annotations := aj .GetAnnotations ()
96
+ if annotations [PolicyAutomationResouceV ] == resourceVersion {
97
+ return true , nil
98
+ }
99
+ }
100
+
101
+ return false , nil
102
+ }
103
+
70
104
// CreateAnsibleJob creates ansiblejob with given PolicyAutomation
71
105
func CreateAnsibleJob (policyAutomation * policyv1beta1.PolicyAutomation ,
72
106
dynamicClient dynamic.Interface , mode string , violationContext policyv1beta1.ViolationContext ,
@@ -79,6 +113,7 @@ func CreateAnsibleJob(policyAutomation *policyv1beta1.PolicyAutomation,
79
113
"annotations" : map [string ]interface {}{
80
114
PolicyAutomationGeneration : strconv .
81
115
FormatInt (policyAutomation .GetGeneration (), 10 ),
116
+ PolicyAutomationResouceV : policyAutomation .GetResourceVersion (),
82
117
},
83
118
},
84
119
"spec" : map [string ]interface {}{
0 commit comments