forked from gatekeeper/gatekeeper-operator
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config_helper.go
289 lines (235 loc) · 7.2 KB
/
config_helper.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
package controllers
import (
"context"
"reflect"
"github.com/go-logr/logr"
"github.com/open-policy-agent/gatekeeper/v3/apis/config/v1alpha1"
"github.com/open-policy-agent/gatekeeper/v3/pkg/wildcard"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
cacheRuntime "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
operatorv1alpha1 "github.com/stolostron/gatekeeper-operator/api/v1alpha1"
)
// Default config data
func getDefaultConfig(namespace string) *v1alpha1.Config {
config := &v1alpha1.Config{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "config",
},
Spec: v1alpha1.ConfigSpec{
Match: []v1alpha1.MatchEntry{
{
ExcludedNamespaces: []wildcard.Wildcard{
"kube-*", "multicluster-engine",
"hypershift", "hive", "rhacs-operator", "open-cluster-*", "openshift-*",
},
Processes: []string{
"webhook", "mutation-webhook",
},
},
},
},
}
return config
}
func createDefaultConfig(ctx context.Context, c client.Client, namespace string,
gatekeeper *operatorv1alpha1.Gatekeeper, scheme *runtime.Scheme,
) error {
var config *v1alpha1.Config
// When it is DisableDefaultMatches = true
if gatekeeper.Spec.Config != nil && gatekeeper.Spec.Config.DisableDefaultMatches {
config = &v1alpha1.Config{
ObjectMeta: metav1.ObjectMeta{
Name: "config",
Namespace: namespace,
},
}
} else {
config = getDefaultConfig(namespace)
}
matches := []v1alpha1.MatchEntry{}
if gatekeeper.Spec.Config != nil && gatekeeper.Spec.Config.Matches != nil {
// Will append gatekeeper.Spec.Config.Matches
matches = gatekeeper.Spec.Config.Matches
}
// Append matched from Gatekeeper CR spec.config.matches
config.Spec.Match = append(config.Spec.Match, matches...)
// Set OwnerReference
if err := controllerutil.SetOwnerReference(gatekeeper, config, scheme); err != nil {
return err
}
err := c.Create(ctx, config)
if err != nil {
return err
}
return nil
}
// Reset Config.Spec.Match and append the default exempt namespaces and
// provided matches from the Gatekeeper CR
func setExemptNamespaces(
ctx context.Context,
existingConfig *v1alpha1.Config,
gatekeeper *operatorv1alpha1.Gatekeeper,
log logr.Logger,
c client.Client,
scheme *runtime.Scheme,
) error {
// Find OwnerReference
ownerRefFound := false
for _, ownerRef := range existingConfig.GetOwnerReferences() {
if ownerRef.UID == gatekeeper.UID {
ownerRefFound = true
break
}
}
// The ownerRefFound which is false means the Config resource was not created by gatekeeper-operator
if !ownerRefFound && len(existingConfig.Spec.Match) != 0 {
log.V(1).Info("The gatekeeper matches already exist. Skip adding DefaultExemptNamespaces")
return nil
}
// Reset Config Match
var newMatch []v1alpha1.MatchEntry
var configDefault *v1alpha1.Config
// When it is DisableDefaultMatches = false or nil then append default exempt namespaces
if gatekeeper.Spec.Config == nil || !gatekeeper.Spec.Config.DisableDefaultMatches {
configDefault = getDefaultConfig("")
newMatch = append(newMatch, configDefault.Spec.Match...)
}
// Avoid gatekeeper.Spec.Config nil error
if gatekeeper.Spec.Config != nil {
// Append matched from Gatekeeper CR spec.config.matches
newMatch = append(newMatch, gatekeeper.Spec.Config.Matches...)
}
// When ownerRefFound is false, config will be updated for adding ownerRef
if reflect.DeepEqual(existingConfig.Spec.Match, newMatch) && ownerRefFound {
log.V(1).Info("No need to Update")
return nil
}
existingConfig.Spec.Match = newMatch
// Set OwnerReference
if !ownerRefFound {
if err := controllerutil.SetOwnerReference(gatekeeper, existingConfig, scheme); err != nil {
return err
}
}
err := c.Update(ctx, existingConfig, &client.UpdateOptions{})
if err != nil {
return err
}
log.Info("Updated Config object with excluded namespaces")
return nil
}
func (r *GatekeeperReconciler) initConfig(
ctx context.Context,
gatekeeper *operatorv1alpha1.Gatekeeper,
) error {
configGVR := schema.GroupVersionResource{
Group: "config.gatekeeper.sh",
Version: "v1alpha1",
Resource: "configs",
}
config, err := r.DynamicClient.Resource(configGVR).
Namespace(r.Namespace).Get(ctx, "config", metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
err = createDefaultConfig(ctx, r.Client, r.Namespace, gatekeeper, r.Scheme)
if err != nil {
return err
}
r.Log.Info("The Gatekeeper Config resource is created.")
return nil
}
return err
}
var updatedConfig *v1alpha1.Config
err = runtime.DefaultUnstructuredConverter.
FromUnstructured(config.Object, &updatedConfig)
if err != nil {
return err
}
err = setExemptNamespaces(ctx, updatedConfig, gatekeeper, r.Log, r.Client, r.Scheme)
if err != nil {
r.Log.V(1).Error(err, "Adding default exempt namespaces to the Config has failed")
return err
}
return nil
}
func (r *GatekeeperReconciler) handleConfigController(ctx context.Context) error {
isCRDReady, err := checkCrdAvailable(ctx, r.DynamicClient, "Config", "configs.config.gatekeeper.sh")
if err != nil {
return err
}
if !isCRDReady {
return errCrdNotReady
}
if r.isConfigCtrlRunning {
return nil
}
var configCtrlCtx context.Context
configCtrlCtx, r.configCtrlCtxCancel = context.WithCancel(ctx)
configMgr, err := ctrl.NewManager(r.KubeConfig, ctrl.Options{
Scheme: r.Scheme,
Metrics: server.Options{
BindAddress: "0",
},
LeaderElection: false,
Cache: cacheRuntime.Options{
ByObject: map[client.Object]cacheRuntime.ByObject{
&v1alpha1.Config{}: {
Namespaces: map[string]cacheRuntime.Config{
r.Namespace: {
FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": "config"}),
},
},
},
},
},
})
if err != nil {
setupLog.Error(err, "Failed to setup NewManager for Config controller")
return err
}
if err := (&ConfigReconciler{
Scheme: r.Scheme,
Client: configMgr.GetClient(),
Log: ctrl.Log.WithName("Config"),
Namespace: r.Namespace,
}).SetupWithManager(configMgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Config")
return err
}
r.isConfigCtrlRunning = true
r.subControllerWait.Add(1)
// Use another go routine for the Config controller
go func() {
err := configMgr.Start(configCtrlCtx)
if err != nil {
setupLog.Error(err, "A problem running Config manager. Triggering a reconcile to restart it.")
}
defer r.configCtrlCtxCancel()
r.isConfigCtrlRunning = false
r.ManualReconcileTrigger <- event.GenericEvent{
Object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": v1alpha1.GroupVersion.String(),
"kind": "Gatekeeper",
"metadata": map[string]interface{}{
"name": defaultGatekeeperCrName,
},
},
},
}
r.subControllerWait.Done()
}()
return nil
}