@@ -36,29 +36,29 @@ func AddPolicyController(mgr ctrl.Manager, inventoryRequester transport.Requeste
36
36
policyPredicate := predicate.Funcs {
37
37
UpdateFunc : func (e event.UpdateEvent ) bool {
38
38
// do not trigger the delete event for the replicated policies
39
- if _ , exist := e .ObjectOld .GetAnnotations ()[constants .PolicyEventRootPolicyNameLabelKey ]; exist {
39
+ if _ , exist := e .ObjectOld .GetLabels ()[constants .PolicyEventRootPolicyNameLabelKey ]; exist {
40
40
return false
41
41
}
42
42
return e .ObjectOld .GetResourceVersion () < e .ObjectNew .GetResourceVersion ()
43
43
},
44
44
CreateFunc : func (e event.CreateEvent ) bool {
45
+ // do not trigger the create event for the replicated policies
46
+ if _ , exist := e .Object .GetLabels ()[constants .PolicyEventRootPolicyNameLabelKey ]; exist {
47
+ return false
48
+ }
45
49
// add the annotation to indicate the request is a create request
46
50
// the annotation won't propagate to the etcd
47
51
annotations := e .Object .GetAnnotations ()
48
52
if annotations == nil {
49
53
annotations = map [string ]string {}
50
54
}
51
- // do not trigger the create event for the replicated policies
52
- if _ , exist := annotations [constants .PolicyEventRootPolicyNameLabelKey ]; exist {
53
- return false
54
- }
55
55
annotations [constants .InventoryResourceCreatingAnnotationlKey ] = ""
56
56
e .Object .SetAnnotations (annotations )
57
57
return true
58
58
},
59
59
DeleteFunc : func (e event.DeleteEvent ) bool {
60
60
// do not trigger the delete event for the replicated policies
61
- if _ , exist := e .Object .GetAnnotations ()[constants .PolicyEventRootPolicyNameLabelKey ]; exist {
61
+ if _ , exist := e .Object .GetLabels ()[constants .PolicyEventRootPolicyNameLabelKey ]; exist {
62
62
return false
63
63
}
64
64
return ! e .DeleteStateUnknown
@@ -84,11 +84,23 @@ func (p *PolicyController) Reconcile(ctx context.Context, req ctrl.Request) (ctr
84
84
}
85
85
return ctrl.Result {}, err
86
86
}
87
+ annotations := policy .GetAnnotations ()
88
+ if annotations != nil {
89
+ if _ , ok := annotations [constants .InventoryResourceCreatingAnnotationlKey ]; ok {
90
+ if resp , err := p .requester .GetHttpClient ().PolicyServiceClient .CreateK8SPolicy (
91
+ ctx , createK8SClusterPolicy (* policy , p .reporterInstanceId )); err != nil {
92
+ return ctrl.Result {}, fmt .Errorf ("failed to create k8s-policy %v: %w" , resp , err )
93
+ }
94
+ }
95
+ }
96
+
87
97
if policy .DeletionTimestamp .IsZero () {
88
98
// add a finalizer to the policy object
89
99
if ! controllerutil .ContainsFinalizer (policy , constants .InventoryResourceFinalizer ) {
90
100
controllerutil .AddFinalizer (policy , constants .InventoryResourceFinalizer )
91
- return ctrl.Result {}, p .runtimeClient .Update (ctx , policy )
101
+ if err := p .runtimeClient .Update (ctx , policy ); err != nil {
102
+ return ctrl.Result {}, err
103
+ }
92
104
}
93
105
} else {
94
106
// The policy object is being deleted
@@ -114,15 +126,6 @@ func (p *PolicyController) Reconcile(ctx context.Context, req ctrl.Request) (ctr
114
126
return ctrl.Result {}, nil
115
127
}
116
128
117
- annotations := policy .GetAnnotations ()
118
- if annotations != nil {
119
- if _ , ok := annotations [constants .InventoryResourceCreatingAnnotationlKey ]; ok {
120
- if resp , err := p .requester .GetHttpClient ().PolicyServiceClient .CreateK8SPolicy (
121
- ctx , createK8SClusterPolicy (* policy , p .reporterInstanceId )); err != nil {
122
- return ctrl.Result {}, fmt .Errorf ("failed to create k8s-policy %v: %w" , resp , err )
123
- }
124
- }
125
- }
126
129
if resp , err := p .requester .GetHttpClient ().PolicyServiceClient .UpdateK8SPolicy (
127
130
ctx , updateK8SClusterPolicy (* policy , p .reporterInstanceId )); err != nil {
128
131
return ctrl.Result {}, fmt .Errorf ("failed to update k8s-policy %v: %w" , resp , err )
@@ -145,9 +148,9 @@ func updateK8SPolicyIsPropagatedToK8SCluster(subjectId, objectId, status, report
145
148
var relationStatus kesselv1betarelations.K8SPolicyIsPropagatedToK8SClusterDetail_Status
146
149
switch status {
147
150
case "NonCompliant" :
148
- relationStatus = kesselv1betarelations .K8SPolicyIsPropagatedToK8SClusterDetail_NO_VIOLATIONS
149
- case "Compliant" :
150
151
relationStatus = kesselv1betarelations .K8SPolicyIsPropagatedToK8SClusterDetail_VIOLATIONS
152
+ case "Compliant" :
153
+ relationStatus = kesselv1betarelations .K8SPolicyIsPropagatedToK8SClusterDetail_NO_VIOLATIONS
151
154
default :
152
155
relationStatus = kesselv1betarelations .K8SPolicyIsPropagatedToK8SClusterDetail_STATUS_OTHER
153
156
}
0 commit comments