/
util.go
140 lines (118 loc) · 4.51 KB
/
util.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
/*
Copyright 2022.
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 v1
import (
"context"
"path/filepath"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//+kubebuilder:object:generate=false
// ObjectWithCompliance contains the usual client.Object metadata interface, as
// well as a user-implemented method to get the compliance state, which is used
// when creating compliance events to propagate status back to the hub cluster.
type ObjectWithCompliance interface {
client.Object
GetComplianceState() ComplianceState
}
// SortString returns a string which can help sort RelatedObjects.
func (o RelatedObject) SortString() string {
return o.Object.APIVersion + o.Object.Kind + o.Object.Metadata.Namespace +
o.Object.Metadata.Name + o.Reason
}
// GetNamespaces fetches all namespaces in the cluster and returns a list of the
// namespaces that match the NamespaceSelector. The client.Reader needs access
// for viewing namespaces, like the access given by this kubebuilder tag:
// `//+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch`
func (sel NamespaceSelector) GetNamespaces(ctx context.Context, r client.Reader) ([]string, error) {
matchingNamespaces := make([]string, 0)
namespaceList := &corev1.NamespaceList{}
if err := r.List(ctx, namespaceList); err != nil {
return matchingNamespaces, err
}
namespaces := make([]string, len(namespaceList.Items))
for i, ns := range namespaceList.Items {
namespaces[i] = ns.GetName()
}
return sel.matches(namespaces)
}
// matches filters a slice of strings, and returns ones that match the selector
func (sel NamespaceSelector) matches(namespaces []string) ([]string, error) {
matchingNamespaces := make([]string, 0)
for _, namespace := range namespaces {
include := false
for _, includePattern := range sel.Include {
var err error
include, err = filepath.Match(string(includePattern), namespace)
if err != nil { // The only possible returned error is ErrBadPattern, when pattern is malformed.
return matchingNamespaces, err
}
if include {
break
}
}
if !include {
continue
}
exclude := false
for _, excludePattern := range sel.Exclude {
var err error
exclude, err = filepath.Match(string(excludePattern), namespace)
if err != nil { // The only possible returned error is ErrBadPattern, when pattern is malformed.
return matchingNamespaces, err
}
if exclude {
break
}
}
if exclude {
continue
}
matchingNamespaces = append(matchingNamespaces, namespace)
}
return matchingNamespaces, nil
}
// RecordComplianceEvent creates an event on the "parent" policy of the given
// object (found through ownerReferences, which is set by the policy framework)
// which can be recognized by the policy framework to update the parent policy's
// status. This is the way that compliance information gets sent to the hub.
// The provided message will be prepended with "Compliant; " or "NonCompliant; "
// as required by the policy framework. The record.EventRecorder needs access to
// create and update events, like the access given by this kubebuilder tag:
// `//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch`
func RecordComplianceEvent(r record.EventRecorder, policy ObjectWithCompliance, msg string) {
if len(policy.GetOwnerReferences()) != 0 {
ownerRef := policy.GetOwnerReferences()[0]
parentPolicy := &PolicyCore{
ObjectMeta: metav1.ObjectMeta{
Name: ownerRef.Name,
Namespace: policy.GetNamespace(), // K8s ensures that owning objects are in the same namespace
UID: ownerRef.UID,
},
TypeMeta: metav1.TypeMeta{
Kind: ownerRef.Kind,
APIVersion: ownerRef.APIVersion,
},
}
eventType := "Normal"
msgPrefix := "Compliant; "
if policy.GetComplianceState() == NonCompliant {
eventType = "Warning"
msgPrefix = "NonCompliant; "
}
reason := "policy: " + policy.GetNamespace() + "/" + policy.GetName()
r.Event(parentPolicy, eventType, reason, msgPrefix+msg)
}
}