-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamplepolicy_controller.go
609 lines (551 loc) · 21.8 KB
/
samplepolicy_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright 2019 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package samplepolicy
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
policiesv1 "github.com/open-cluster-management/multicloud-operators-policy-controller/pkg/apis/policies/v1"
"github.com/open-cluster-management/multicloud-operators-policy-controller/pkg/common"
//testclient "k8s.io/client-go/kubernetes/fake"
)
var log = logf.Log.WithName("controller_samplepolicy")
// Finalizer used to ensure consistency when deleting a CRD.
const Finalizer = "finalizer.policy.open-cluster-management.io"
const grcCategory = "system-and-information-integrity"
// availablePolicies is a cach all all available polices.
var availablePolicies common.SyncedPolicyMap
// PlcChan a channel used to pass policies ready for update.
var PlcChan chan *policiesv1.SamplePolicy
// KubeClient a k8s client used for k8s native resources.
var KubeClient *kubernetes.Interface
var reconcilingAgent *ReconcileSamplePolicy
// NamespaceWatched defines which namespace we can watch for the GRC policies and ignore others.
var NamespaceWatched string
// EventOnParent specifies if we also want to send events to the parent policy. Available options are yes/no/ifpresent.
var EventOnParent string
// PrometheusAddr port addr for prom metrics.
var PrometheusAddr string
// Add creates a new SamplePolicy Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler.
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileSamplePolicy{client: mgr.GetClient(), scheme: mgr.GetScheme(), recorder: mgr.GetEventRecorderFor("samplepolicy-controller")}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler.
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("samplepolicy-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource SamplePolicy
err = c.Watch(&source.Kind{Type: &policiesv1.SamplePolicy{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// Watch for changes to secondary resource Pods and requeue the owner SamplePolicy
err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: &policiesv1.SamplePolicy{},
})
if err != nil {
return err
}
return nil
}
// Initialize to initialize some controller variables.
func Initialize(kClient *kubernetes.Interface, mgr manager.Manager, namespace, eventParent string) {
KubeClient = kClient
PlcChan = make(chan *policiesv1.SamplePolicy, 100) //buffering up to 100 policies for update
NamespaceWatched = namespace
EventOnParent = strings.ToLower(eventParent)
}
// blank assignment to verify that ReconcileSamplePolicy implements reconcile.Reconciler.
var _ reconcile.Reconciler = &ReconcileSamplePolicy{}
// ReconcileSamplePolicy reconciles a SamplePolicy object.
type ReconcileSamplePolicy struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
}
// Reconcile reads that state of the cluster for a SamplePolicy object and makes changes based on the state read
// and what is in the SamplePolicy.Spec
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileSamplePolicy) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling SamplePolicy")
// Fetch the SamplePolicy instance
instance := &policiesv1.SamplePolicy{}
if reconcilingAgent == nil {
reconcilingAgent = r
}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
// name of our mcm custom finalizer
myFinalizerName := Finalizer
if instance.ObjectMeta.DeletionTimestamp.IsZero() {
updateNeeded := false
// The object is not being deleted, so if it might not have our finalizer,
// then lets add the finalizer and update the object.
if !containsString(instance.ObjectMeta.Finalizers, myFinalizerName) {
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, myFinalizerName)
updateNeeded = true
}
if !ensureDefaultLabel(instance) {
updateNeeded = true
}
if updateNeeded {
if err := r.client.Update(context.Background(), instance); err != nil {
return reconcile.Result{Requeue: true}, nil
}
}
instance.Status.CompliancyDetails = nil //reset CompliancyDetails
err := handleAddingPolicy(instance)
if err != nil {
glog.V(3).Infof("Failed to handleAddingPolicy")
}
} else {
handleRemovingPolicy(instance)
// The object is being deleted
if containsString(instance.ObjectMeta.Finalizers, myFinalizerName) {
// our finalizer is present, so lets handle our external dependency
if err := r.deleteExternalDependency(instance); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return reconcile.Result{}, err
}
// remove our finalizer from the list and update it.
instance.ObjectMeta.Finalizers = removeString(instance.ObjectMeta.Finalizers, myFinalizerName)
if err := r.client.Update(context.Background(), instance); err != nil {
return reconcile.Result{Requeue: true}, nil
}
}
// Our finalizer has finished, so the reconciler can do nothing.
return reconcile.Result{}, nil
}
glog.V(3).Infof("reason: successful processing, subject: policy/%v, namespace: %v, according to policy: %v, additional-info: none",
instance.Name, instance.Namespace, instance.Name)
// Pod already exists - don't requeue
// reqLogger.Info("Skip reconcile: Pod already exists", "Pod.Namespace", found.Namespace, "Pod.Name", found.Name)
return reconcile.Result{}, nil
}
// PeriodicallyExecSamplePolicies always check status.
func PeriodicallyExecSamplePolicies(freq uint) {
var plcToUpdateMap map[string]*policiesv1.SamplePolicy
for {
start := time.Now()
printMap(availablePolicies.PolicyMap)
plcToUpdateMap = make(map[string]*policiesv1.SamplePolicy)
for resource, policy := range availablePolicies.PolicyMap {
namespace := strings.Split(resource, "/")[0]
//For each namespace, fetch all the RoleBindings in that NS according to the policy selector
//For each RoleBindings get the number of users
//update the status internal map
//no difference between enforce and inform here
roleBindingList, err := (*common.KubeClient).RbacV1().RoleBindings(namespace).
List(metav1.ListOptions{LabelSelector: labels.Set(policy.Spec.LabelSelector).String()})
if err != nil {
glog.Errorf("reason: communication error, subject: k8s API server, namespace: %v, according to policy: %v, additional-info: %v\n",
namespace, policy.Name, err)
continue
}
userViolationCount, GroupViolationCount := checkViolationsPerNamespace(roleBindingList, policy)
if strings.EqualFold(string(policy.Spec.RemediationAction), string(policiesv1.Enforce)) {
glog.V(5).Infof("Enforce is set, but ignored :-)")
}
if addViolationCount(policy, userViolationCount, GroupViolationCount, namespace) {
plcToUpdateMap[policy.Name] = policy
}
checkComplianceBasedOnDetails(policy)
}
err := checkUnNamespacedPolicies(plcToUpdateMap)
if err != nil {
glog.V(3).Infof("Failed to checkUnNamespacedPolicies")
}
//update status of all policies that changed:
faultyPlc, err := updatePolicyStatus(plcToUpdateMap)
if err != nil {
glog.Errorf("reason: policy update error, subject: policy/%v, namespace: %v, according to policy: %v, additional-info: %v\n",
faultyPlc.Name, faultyPlc.Namespace, faultyPlc.Name, err)
}
// making sure that if processing is > freq we don't sleep
// if freq > processing we sleep for the remaining duration
elapsed := time.Since(start) / 1000000000 // convert to seconds
if float64(freq) > float64(elapsed) {
remainingSleep := float64(freq) - float64(elapsed)
time.Sleep(time.Duration(remainingSleep) * time.Second)
}
if KubeClient == nil {
return
}
}
}
func ensureDefaultLabel(instance *policiesv1.SamplePolicy) (updateNeeded bool) {
//we need to ensure this label exists -> category: "System and Information Integrity"
if instance.ObjectMeta.Labels == nil {
newlbl := make(map[string]string)
newlbl["category"] = grcCategory
instance.ObjectMeta.Labels = newlbl
return true
}
if _, ok := instance.ObjectMeta.Labels["category"]; !ok {
instance.ObjectMeta.Labels["category"] = grcCategory
return true
}
if instance.ObjectMeta.Labels["category"] != grcCategory {
instance.ObjectMeta.Labels["category"] = grcCategory
return true
}
return false
}
func checkUnNamespacedPolicies(plcToUpdateMap map[string]*policiesv1.SamplePolicy) error {
plcMap := convertMaptoPolicyNameKey()
// group the policies with cluster users and the ones with groups
// take the plc with min users and groups and make it your baseline
ClusteRoleBindingList, err := (*common.KubeClient).RbacV1().ClusterRoleBindings().List(metav1.ListOptions{})
if err != nil {
glog.Errorf("reason: communication error, subject: k8s API server, namespace: all, according to policy: none, additional-info: %v\n", err)
return err
}
clusterLevelUsers, clusterLevelGroups := checkAllClusterLevel(ClusteRoleBindingList)
for _, policy := range plcMap {
var userViolationCount, groupViolationCount int
if policy.Spec.MaxClusterRoleBindingUsers < clusterLevelUsers && policy.Spec.MaxClusterRoleBindingUsers >= 0 {
userViolationCount = clusterLevelUsers - policy.Spec.MaxClusterRoleBindingUsers
}
if policy.Spec.MaxClusterRoleBindingGroups < clusterLevelGroups && policy.Spec.MaxClusterRoleBindingGroups >= 0 {
groupViolationCount = clusterLevelGroups - policy.Spec.MaxClusterRoleBindingGroups
}
if addViolationCount(policy, userViolationCount, groupViolationCount, "cluster-wide") {
plcToUpdateMap[policy.Name] = policy
}
checkComplianceBasedOnDetails(policy)
}
return nil
}
func checkAllClusterLevel(clusterRoleBindingList *v1.ClusterRoleBindingList) (userV, groupV int) {
usersMap := make(map[string]bool)
groupsMap := make(map[string]bool)
for _, clusterRoleBinding := range clusterRoleBindingList.Items {
for _, subject := range clusterRoleBinding.Subjects {
if subject.Kind == "User" {
usersMap[subject.Name] = true
}
if subject.Kind == "Group" {
groupsMap[subject.Name] = true
}
}
}
return len(usersMap), len(groupsMap)
}
func convertMaptoPolicyNameKey() map[string]*policiesv1.SamplePolicy {
plcMap := make(map[string]*policiesv1.SamplePolicy)
for _, policy := range availablePolicies.PolicyMap {
plcMap[policy.Name] = policy
}
return plcMap
}
func checkViolationsPerNamespace(roleBindingList *v1.RoleBindingList, plc *policiesv1.SamplePolicy) (userV, groupV int) {
usersMap := make(map[string]bool)
groupsMap := make(map[string]bool)
for _, roleBinding := range roleBindingList.Items {
for _, subject := range roleBinding.Subjects {
if subject.Kind == "User" {
usersMap[subject.Name] = true
}
if subject.Kind == "Group" {
groupsMap[subject.Name] = true
}
}
}
var userViolationCount, groupViolationCount int
if plc.Spec.MaxRoleBindingUsersPerNamespace < len(usersMap) && plc.Spec.MaxRoleBindingUsersPerNamespace >= 0 {
userViolationCount = (len(usersMap) - plc.Spec.MaxRoleBindingUsersPerNamespace)
}
if plc.Spec.MaxRoleBindingGroupsPerNamespace < len(groupsMap) && plc.Spec.MaxRoleBindingGroupsPerNamespace >= 0 {
groupViolationCount = (len(groupsMap) - plc.Spec.MaxRoleBindingGroupsPerNamespace)
}
return userViolationCount, groupViolationCount
}
func addViolationCount(plc *policiesv1.SamplePolicy, userCount int, groupCount int, namespace string) bool {
changed := false
msg := fmt.Sprintf("%s violations detected in namespace `%s`, there are %v users violations and %v groups violations",
fmt.Sprint(userCount+groupCount),
namespace,
userCount,
groupCount)
if plc.Status.CompliancyDetails == nil {
plc.Status.CompliancyDetails = make(map[string]map[string][]string)
}
if _, ok := plc.Status.CompliancyDetails[plc.Name]; !ok {
plc.Status.CompliancyDetails[plc.Name] = make(map[string][]string)
}
if plc.Status.CompliancyDetails[plc.Name][namespace] == nil {
plc.Status.CompliancyDetails[plc.Name][namespace] = []string{}
}
if len(plc.Status.CompliancyDetails[plc.Name][namespace]) == 0 {
plc.Status.CompliancyDetails[plc.Name][namespace] = []string{msg}
changed = true
return changed
}
firstNum := strings.Split(plc.Status.CompliancyDetails[plc.Name][namespace][0], " ")
if len(firstNum) > 0 {
if firstNum[0] == fmt.Sprint(userCount+groupCount) {
return false
}
}
plc.Status.CompliancyDetails[plc.Name][namespace][0] = msg
changed = true
return changed
}
func checkComplianceBasedOnDetails(plc *policiesv1.SamplePolicy) {
plc.Status.ComplianceState = policiesv1.Compliant
if plc.Status.CompliancyDetails == nil {
return
}
if _, ok := plc.Status.CompliancyDetails[plc.Name]; !ok {
return
}
if len(plc.Status.CompliancyDetails[plc.Name]) == 0 {
return
}
for namespace, msgList := range plc.Status.CompliancyDetails[plc.Name] {
if len(msgList) > 0 {
violationNum := strings.Split(plc.Status.CompliancyDetails[plc.Name][namespace][0], " ")
if len(violationNum) > 0 {
if violationNum[0] != fmt.Sprint(0) {
plc.Status.ComplianceState = policiesv1.NonCompliant
}
}
} else {
return
}
}
}
func checkComplianceChangeBasedOnDetails(plc *policiesv1.SamplePolicy) (complianceChanged bool) {
//used in case we also want to know not just the compliance state, but also whether the compliance changed or not.
previous := plc.Status.ComplianceState
if plc.Status.CompliancyDetails == nil {
plc.Status.ComplianceState = policiesv1.UnknownCompliancy
return reflect.DeepEqual(previous, plc.Status.ComplianceState)
}
if _, ok := plc.Status.CompliancyDetails[plc.Name]; !ok {
plc.Status.ComplianceState = policiesv1.UnknownCompliancy
return reflect.DeepEqual(previous, plc.Status.ComplianceState)
}
if len(plc.Status.CompliancyDetails[plc.Name]) == 0 {
plc.Status.ComplianceState = policiesv1.UnknownCompliancy
return reflect.DeepEqual(previous, plc.Status.ComplianceState)
}
plc.Status.ComplianceState = policiesv1.Compliant
for namespace, msgList := range plc.Status.CompliancyDetails[plc.Name] {
if len(msgList) > 0 {
violationNum := strings.Split(plc.Status.CompliancyDetails[plc.Name][namespace][0], " ")
if len(violationNum) > 0 {
if violationNum[0] != fmt.Sprint(0) {
plc.Status.ComplianceState = policiesv1.NonCompliant
}
}
} else {
return reflect.DeepEqual(previous, plc.Status.ComplianceState)
}
}
if plc.Status.ComplianceState != policiesv1.NonCompliant {
plc.Status.ComplianceState = policiesv1.Compliant
}
return reflect.DeepEqual(previous, plc.Status.ComplianceState)
}
func updatePolicyStatus(policies map[string]*policiesv1.SamplePolicy) (*policiesv1.SamplePolicy, error) {
for _, instance := range policies { // policies is a map where: key = plc.Name, value = pointer to plc
err := reconcilingAgent.client.Status().Update(context.TODO(), instance)
if err != nil {
return instance, err
}
if EventOnParent != "no" {
createParentPolicyEvent(instance)
}
if reconcilingAgent.recorder != nil {
reconcilingAgent.recorder.Event(instance, "Normal", "Policy updated", fmt.Sprintf("Policy status is: %v", instance.Status.ComplianceState))
}
}
return nil, nil
}
func getContainerID(pod corev1.Pod, containerName string) string {
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Name == containerName {
return containerStatus.ContainerID
}
}
return ""
}
func handleRemovingPolicy(plc *policiesv1.SamplePolicy) {
for k, v := range availablePolicies.PolicyMap {
if v.Name == plc.Name {
availablePolicies.RemoveObject(k)
}
}
}
func handleAddingPolicy(plc *policiesv1.SamplePolicy) error {
allNamespaces, err := common.GetAllNamespaces()
if err != nil {
glog.Errorf("reason: error fetching the list of available namespaces, subject: K8s API server, namespace: all, according to policy: %v, additional-info: %v",
plc.Name, err)
return err
}
//clean up that policy from the existing namepsaces, in case the modification is in the namespace selector
for _, ns := range allNamespaces {
key := fmt.Sprintf("%s/%s", ns, plc.Name)
if policy, found := availablePolicies.GetObject(key); found {
if policy.Name == plc.Name {
availablePolicies.RemoveObject(ns)
}
}
}
selectedNamespaces := common.GetSelectedNamespaces(plc.Spec.NamespaceSelector.Include, plc.Spec.NamespaceSelector.Exclude, allNamespaces)
for _, ns := range selectedNamespaces {
key := fmt.Sprintf("%s/%s", ns, plc.Name)
availablePolicies.AddObject(key, plc)
}
return err
}
//=================================================================
//deleteExternalDependency in case the CRD was related to non-k8s resource
//nolint
func (r *ReconcileSamplePolicy) deleteExternalDependency(instance *policiesv1.SamplePolicy) error {
glog.V(0).Infof("reason: CRD deletion, subject: policy/%v, namespace: %v, according to policy: none, additional-info: none\n",
instance.Name,
instance.Namespace)
// Ensure that delete implementation is idempotent and safe to invoke
// multiple types for same object.
return nil
}
//=================================================================
// Helper functions to check if a string exists in a slice of strings.
func containsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
//=================================================================
// Helper functions to remove a string from a slice of strings.
func removeString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}
//=================================================================
// Helper functions that pretty prints a map.
func printMap(myMap map[string]*policiesv1.SamplePolicy) {
if len(myMap) == 0 {
fmt.Println("Waiting for policies to be available for processing... ")
return
}
fmt.Println("Available policies in namespaces: ")
for k, v := range myMap {
fmt.Printf("namespace = %v; policy = %v \n", k, v.Name)
}
}
func createParentPolicyEvent(instance *policiesv1.SamplePolicy) {
if len(instance.OwnerReferences) == 0 {
return //there is nothing to do, since no owner is set
}
// we are making an assumption that the GRC policy has a single owner, or we chose the first owner in the list
if string(instance.OwnerReferences[0].UID) == "" {
return //there is nothing to do, since no owner UID is set
}
parentPlc := createParentPolicy(instance)
reconcilingAgent.recorder.Event(&parentPlc,
corev1.EventTypeNormal,
fmt.Sprintf("policy: %s/%s", instance.Namespace, instance.Name),
convertPolicyStatusToString(instance))
}
func createParentPolicy(instance *policiesv1.SamplePolicy) policiesv1.Policy {
ns := common.ExtractNamespaceLabel(instance)
if ns == "" {
ns = NamespaceWatched
}
plc := policiesv1.Policy{
ObjectMeta: metav1.ObjectMeta{
Name: instance.OwnerReferences[0].Name,
Namespace: ns, // we are making an assumption here that the parent policy is in the watched-namespace passed as flag
UID: instance.OwnerReferences[0].UID,
},
TypeMeta: metav1.TypeMeta{
Kind: "Policy",
APIVersion: " policy.open-cluster-management.io/v1",
},
}
return plc
}
//=================================================================
// convertPolicyStatusToString to be able to pass the status as event.
func convertPolicyStatusToString(plc *policiesv1.SamplePolicy) (results string) {
result := "ComplianceState is still undetermined"
if plc.Status.ComplianceState == "" {
return result
}
result = string(plc.Status.ComplianceState)
if plc.Status.CompliancyDetails == nil {
return result
}
if _, ok := plc.Status.CompliancyDetails[plc.Name]; !ok {
return result
}
for _, v := range plc.Status.CompliancyDetails[plc.Name] {
result += fmt.Sprintf("; %s", strings.Join(v, ", "))
}
return result
}