Skip to content

Commit fb008f8

Browse files
Bug: 2 ansiblejobs were created when disabled policy, "Disabled+Manual" mode
Solution: Add resourceV verification Signed-off-by: Yi Rae Kim <[email protected]>
1 parent 0d164a2 commit fb008f8

File tree

3 files changed

+316
-140
lines changed

3 files changed

+316
-140
lines changed

controllers/automation/policyautomation_controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ func (r *PolicyAutomationReconciler) Reconcile(
303303
}
304304

305305
if policyAutomation.Annotations["policy.open-cluster-management.io/rerun"] == "true" {
306+
AjExist, err := common.MatchPAResouceV(policyAutomation,
307+
r.DynamicClient, policyAutomation.GetResourceVersion())
308+
if err != nil {
309+
log.Error(err, "Failed to compare Ansible job's resourceVersion")
310+
311+
return reconcile.Result{}, err
312+
}
313+
314+
if AjExist {
315+
log.Info("Ansiblejob already exist under this policyautomation resourceVersion")
316+
317+
return reconcile.Result{}, nil
318+
}
319+
306320
targetList := common.FindNonCompliantClustersForPolicy(policy)
307321
log.Info(
308322
"Creating an Ansible job", "mode", "manual",

controllers/common/ansible.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
ControllerName string = "policy-automation"
2525
PolicyAutomationLabel string = "policy.open-cluster-management.io/policyautomation-name"
2626
PolicyAutomationGeneration string = "policy.open-cluster-management.io/policyautomation-generation"
27+
// policyautomation-ResouceVersion
28+
PolicyAutomationResouceV string = "policy.open-cluster-management.io/policyautomation-resource-version"
2729
)
2830

2931
var log = ctrl.Log.WithName(ControllerName)
@@ -67,6 +69,38 @@ func MatchPAGeneration(policyAutomation *policyv1beta1.PolicyAutomation,
6769
return false, nil
6870
}
6971

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+
70104
// CreateAnsibleJob creates ansiblejob with given PolicyAutomation
71105
func CreateAnsibleJob(policyAutomation *policyv1beta1.PolicyAutomation,
72106
dynamicClient dynamic.Interface, mode string, violationContext policyv1beta1.ViolationContext,
@@ -79,6 +113,7 @@ func CreateAnsibleJob(policyAutomation *policyv1beta1.PolicyAutomation,
79113
"annotations": map[string]interface{}{
80114
PolicyAutomationGeneration: strconv.
81115
FormatInt(policyAutomation.GetGeneration(), 10),
116+
PolicyAutomationResouceV: policyAutomation.GetResourceVersion(),
82117
},
83118
},
84119
"spec": map[string]interface{}{

0 commit comments

Comments
 (0)