@@ -5,18 +5,68 @@ package common
5
5
import (
6
6
"context"
7
7
"encoding/json"
8
+ "fmt"
8
9
"reflect"
9
10
"regexp"
11
+ "strconv"
10
12
"strings"
11
13
12
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
15
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14
16
"k8s.io/apimachinery/pkg/runtime/schema"
15
17
"k8s.io/client-go/dynamic"
18
+ ctrl "sigs.k8s.io/controller-runtime"
16
19
17
20
policyv1beta1 "open-cluster-management.io/governance-policy-propagator/api/v1beta1"
18
21
)
19
22
23
+ const (
24
+ ControllerName string = "policy-automation"
25
+ PolicyAutomationLabel string = "policy.open-cluster-management.io/policyautomation-name"
26
+ PolicyAutomationGeneration string = "policy.open-cluster-management.io/policyautomation-generation"
27
+ )
28
+
29
+ var log = ctrl .Log .WithName (ControllerName )
30
+
31
+ var ansibleJobRes = schema.GroupVersionResource {
32
+ Group : "tower.ansible.com" , Version : "v1alpha1" ,
33
+ Resource : "ansiblejobs" ,
34
+ }
35
+
36
+ // Check any ansiblejob is made by input genteration number
37
+ // Returning "true" means the policy automation already created ansiblejob with the generation
38
+ func MatchPAGeneration (policyAutomation * policyv1beta1.PolicyAutomation ,
39
+ dynamicClient dynamic.Interface , generation int64 ,
40
+ ) (bool , error ) {
41
+ ansiblejobList , err := dynamicClient .Resource (ansibleJobRes ).Namespace (policyAutomation .GetNamespace ()).List (
42
+ context .TODO (), metav1.ListOptions {
43
+ LabelSelector : fmt .Sprintf ("%s=%s" , PolicyAutomationLabel , policyAutomation .GetName ()),
44
+ },
45
+ )
46
+ if err != nil {
47
+ log .Error (err , "Failed to get ansiblejob list" )
48
+
49
+ return false , err
50
+ }
51
+
52
+ ansiblejobLen := len (ansiblejobList .Items )
53
+ // Check whether new PolicyAutomation
54
+ if ansiblejobLen == 0 {
55
+ return false , nil
56
+ }
57
+
58
+ s := strconv .FormatInt (generation , 10 )
59
+
60
+ for _ , aj := range ansiblejobList .Items {
61
+ annotations := aj .GetAnnotations ()
62
+ if annotations [PolicyAutomationGeneration ] == s {
63
+ return true , nil
64
+ }
65
+ }
66
+
67
+ return false , nil
68
+ }
69
+
20
70
// CreateAnsibleJob creates ansiblejob with given PolicyAutomation
21
71
func CreateAnsibleJob (policyAutomation * policyv1beta1.PolicyAutomation ,
22
72
dynamicClient dynamic.Interface , mode string , violationContext policyv1beta1.ViolationContext ,
@@ -25,6 +75,12 @@ func CreateAnsibleJob(policyAutomation *policyv1beta1.PolicyAutomation,
25
75
Object : map [string ]interface {}{
26
76
"apiVersion" : "tower.ansible.com/v1alpha1" ,
27
77
"kind" : "AnsibleJob" ,
78
+ "metadata" : map [string ]interface {}{
79
+ "annotations" : map [string ]interface {}{
80
+ PolicyAutomationGeneration : strconv .
81
+ FormatInt (policyAutomation .GetGeneration (), 10 ),
82
+ },
83
+ },
28
84
"spec" : map [string ]interface {}{
29
85
"job_template_name" : policyAutomation .Spec .Automation .Name ,
30
86
"tower_auth_secret" : policyAutomation .Spec .Automation .TowerSecret ,
@@ -63,6 +119,11 @@ func CreateAnsibleJob(policyAutomation *policyv1beta1.PolicyAutomation,
63
119
mapExtraVars [fieldName ] = value
64
120
}
65
121
122
+ label := map [string ]string {
123
+ PolicyAutomationLabel : policyAutomation .GetName (),
124
+ }
125
+ ansibleJob .SetLabels (label )
126
+
66
127
ansibleJob .Object ["spec" ].(map [string ]interface {})["extra_vars" ] = mapExtraVars
67
128
68
129
if policyAutomation .Spec .Automation .JobTTL != nil {
0 commit comments