/
agentclusterinstall_validation_hook.go
367 lines (321 loc) · 13.5 KB
/
agentclusterinstall_validation_hook.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
package v1beta1
import (
"fmt"
"net/http"
"strings"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
hivev1 "github.com/openshift/hive/apis/hive/v1"
log "github.com/sirupsen/logrus"
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
const (
agentClusterInstallGroup = "extensions.hive.openshift.io"
agentClusterInstallVersion = "v1beta1"
agentClusterInstallResource = "agentclusterinstalls"
agentClusterInstallAdmissionGroup = "admission.agentinstall.openshift.io"
agentClusterInstallAdmissionVersion = "v1"
)
var (
mutableFields = []string{"ClusterMetadata"}
)
// AgentClusterInstallValidatingAdmissionHook is a struct that is used to reference what code should be run by the generic-admission-server.
type AgentClusterInstallValidatingAdmissionHook struct {
decoder *admission.Decoder
}
// NewAgentClusterInstallValidatingAdmissionHook constructs a new AgentClusterInstallValidatingAdmissionHook
func NewAgentClusterInstallValidatingAdmissionHook(decoder *admission.Decoder) *AgentClusterInstallValidatingAdmissionHook {
return &AgentClusterInstallValidatingAdmissionHook{decoder: decoder}
}
// ValidatingResource is called by generic-admission-server on startup to register the returned REST resource through which the
// webhook is accessed by the kube apiserver.
// For example, generic-admission-server uses the data below to register the webhook on the REST resource "/apis/admission.agentinstall.openshift.io/v1/agentclusterinstallvalidators".
// When the kube apiserver calls this registered REST resource, the generic-admission-server calls the Validate() method below.
func (a *AgentClusterInstallValidatingAdmissionHook) ValidatingResource() (plural schema.GroupVersionResource, singular string) {
log.WithFields(log.Fields{
"group": agentClusterInstallAdmissionGroup,
"version": agentClusterInstallAdmissionVersion,
"resource": "agentclusterinstallvalidator",
}).Info("Registering validation REST resource")
// NOTE: This GVR is meant to be different than the AgentClusterInstall CRD GVR which has group "hivextension.openshift.io".
return schema.GroupVersionResource{
Group: agentClusterInstallAdmissionGroup,
Version: agentClusterInstallAdmissionVersion,
Resource: "agentclusterinstallvalidators",
},
"agentclusterinstallvalidator"
}
// Initialize is called by generic-admission-server on startup to setup any special initialization that your webhook needs.
func (a *AgentClusterInstallValidatingAdmissionHook) Initialize(kubeClientConfig *rest.Config, stopCh <-chan struct{}) error {
log.WithFields(log.Fields{
"group": agentClusterInstallAdmissionGroup,
"version": agentClusterInstallAdmissionVersion,
"resource": "agentclusterinstallvalidator",
}).Info("Initializing validation REST resource")
return nil // No initialization needed right now.
}
// Validate is called by generic-admission-server when the registered REST resource above is called with an admission request.
// Usually it's the kube apiserver that is making the admission validation request.
func (a *AgentClusterInstallValidatingAdmissionHook) Validate(admissionSpec *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
contextLogger := log.WithFields(log.Fields{
"operation": admissionSpec.Operation,
"group": admissionSpec.Resource.Group,
"version": admissionSpec.Resource.Version,
"resource": admissionSpec.Resource.Resource,
"method": "Validate",
})
if !shouldValidate(admissionSpec) {
contextLogger.Info("Skipping validation for request")
// The request object isn't something that this validator should validate.
// Therefore, we say that it's allowed.
return &admissionv1.AdmissionResponse{
Allowed: true,
}
}
contextLogger.Info("Validating request")
if admissionSpec.Operation == admissionv1.Update {
return a.validateUpdate(admissionSpec)
}
if admissionSpec.Operation == admissionv1.Create {
return a.validateCreate(admissionSpec)
}
// all other operations are explicitly allowed
contextLogger.Info("Successful validation")
return &admissionv1.AdmissionResponse{
Allowed: true,
}
}
// shouldValidate explicitly checks if the request should be validated. For example, this webhook may have accidentally been registered to check
// the validity of some other type of object with a different GVR.
func shouldValidate(admissionSpec *admissionv1.AdmissionRequest) bool {
contextLogger := log.WithFields(log.Fields{
"operation": admissionSpec.Operation,
"group": admissionSpec.Resource.Group,
"version": admissionSpec.Resource.Version,
"resource": admissionSpec.Resource.Resource,
"method": "shouldValidate",
})
if admissionSpec.Resource.Group != agentClusterInstallGroup {
contextLogger.Debug("Returning False, not our group")
return false
}
if admissionSpec.Resource.Version != agentClusterInstallVersion {
contextLogger.Debug("Returning False, it's our group, but not the right version")
return false
}
if admissionSpec.Resource.Resource != agentClusterInstallResource {
contextLogger.Debug("Returning False, it's our group and version, but not the right resource")
return false
}
// If we get here, then we're supposed to validate the object.
contextLogger.Debug("Returning True, passed all prerequisites.")
return true
}
func (a *AgentClusterInstallValidatingAdmissionHook) validateCreate(admissionSpec *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
contextLogger := log.WithFields(log.Fields{
"operation": admissionSpec.Operation,
"group": admissionSpec.Resource.Group,
"version": admissionSpec.Resource.Version,
"resource": admissionSpec.Resource.Resource,
"method": "validateCreate",
})
newObject := &hiveext.AgentClusterInstall{}
if err := a.decoder.DecodeRaw(admissionSpec.Object, newObject); err != nil {
contextLogger.Errorf("Failed unmarshaling Object: %v", err.Error())
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: err.Error(),
},
}
}
// Add the new data to the contextLogger
contextLogger.Data["object.Name"] = newObject.Name
// verify that UserNetworkManagement is not set to false with SNO.
// if the user leave this field empty it is fine because the AI knows
// what to set as default
if isUserManagedNetworkingSetToFalseWithSNO(newObject) {
message := "UserManagedNetworking must be set to true with SNO"
contextLogger.Errorf("Failed validation: %v", message)
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusConflict, Reason: metav1.StatusReasonConflict,
Message: message,
},
}
}
// If we get here, then all checks passed, so the object is valid.
contextLogger.Info("Successful validation")
return &admissionv1.AdmissionResponse{
Allowed: true,
}
}
// validateUpdate specifically validates update operations for AgentClusterInstall objects.
func (a *AgentClusterInstallValidatingAdmissionHook) validateUpdate(admissionSpec *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
contextLogger := log.WithFields(log.Fields{
"operation": admissionSpec.Operation,
"group": admissionSpec.Resource.Group,
"version": admissionSpec.Resource.Version,
"resource": admissionSpec.Resource.Resource,
"method": "validateUpdate",
})
newObject := &hiveext.AgentClusterInstall{}
if err := a.decoder.DecodeRaw(admissionSpec.Object, newObject); err != nil {
contextLogger.Errorf("Failed unmarshaling Object: %v", err.Error())
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: err.Error(),
},
}
}
// Add the new data to the contextLogger
contextLogger.Data["object.Name"] = newObject.Name
oldObject := &hiveext.AgentClusterInstall{}
if err := a.decoder.DecodeRaw(admissionSpec.OldObject, oldObject); err != nil {
contextLogger.Errorf("Failed unmarshaling OldObject: %v", err.Error())
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: err.Error(),
},
}
}
if !areImageSetRefsEqual(oldObject.Spec.ImageSetRef, newObject.Spec.ImageSetRef) {
message := "Attempted to change AgentClusterInstall.ImageSetRef which is immutable"
contextLogger.Errorf("Failed validation: %v", message)
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: message,
},
}
}
if isUserManagedNetworkingSetToFalseWithSNO(newObject) {
message := "UserManagedNetworking must be set to true with SNO"
contextLogger.Errorf("Failed validation: %v", message)
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusConflict, Reason: metav1.StatusReasonConflict,
Message: message,
},
}
}
if installAlreadyStarted(newObject.Status.Conditions) {
ignoreChanges := mutableFields
// MGMT-12794 This function returns true if the ProvisionRequirements field
// has changed after installation completion. A change to this section has no effect
// at this stage, but it is needed to serve some CI/CD gitops flows.
if installCompleted(newObject.Status.Conditions) {
ignoreChanges = append(ignoreChanges, "ProvisionRequirements")
}
hasChangedImmutableField, unsupportedDiff := hasChangedImmutableField(&oldObject.Spec, &newObject.Spec, ignoreChanges)
if hasChangedImmutableField {
message := fmt.Sprintf("Attempted to change AgentClusterInstall.Spec which is immutable after install started, except for %s fields. Unsupported change: \n%s", strings.Join(mutableFields, ","), unsupportedDiff)
contextLogger.Infof("Failed validation: %v", message)
contextLogger.Error(message)
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: metav1.StatusFailure, Code: http.StatusBadRequest, Reason: metav1.StatusReasonBadRequest,
Message: message,
},
}
}
}
// If we get here, then all checks passed, so the object is valid.
contextLogger.Info("Successful validation")
return &admissionv1.AdmissionResponse{
Allowed: true,
}
}
func installAlreadyStarted(conditions []hivev1.ClusterInstallCondition) bool {
cond := FindStatusCondition(conditions, hiveext.ClusterCompletedCondition)
if cond == nil {
return false
}
switch cond.Reason {
case hiveext.ClusterInstallationFailedReason, hiveext.ClusterInstalledReason, hiveext.ClusterInstallationInProgressReason, hiveext.ClusterAlreadyInstallingReason:
return true
default:
return false
}
}
func installCompleted(conditions []hivev1.ClusterInstallCondition) bool {
cond := FindStatusCondition(conditions, hiveext.ClusterCompletedCondition)
if cond == nil {
return false
}
return cond.Reason == hiveext.ClusterInstalledReason || cond.Reason == hiveext.ClusterInstallationFailedReason
}
// FindStatusCondition finds the conditionType in conditions.
func FindStatusCondition(conditions []hivev1.ClusterInstallCondition, conditionType string) *hivev1.ClusterInstallCondition {
for i := range conditions {
if conditions[i].Type == conditionType {
return &conditions[i]
}
}
return nil
}
// hasChangedImmutableField determines if a AgentClusterInstall.spec immutable field was changed.
// it returns the diff string that shows the changes that are not supported
func hasChangedImmutableField(oldObject, cd *hiveext.AgentClusterInstallSpec, mutableFields []string) (bool, string) {
r := &diffReporter{}
opts := cmp.Options{
cmpopts.EquateEmpty(),
cmpopts.IgnoreFields(hiveext.AgentClusterInstallSpec{}, mutableFields...),
cmp.Reporter(r),
}
return !cmp.Equal(oldObject, cd, opts), r.String()
}
func areImageSetRefsEqual(imageSetRef1 *hivev1.ClusterImageSetReference, imageSetRef2 *hivev1.ClusterImageSetReference) bool {
if imageSetRef1 == nil && imageSetRef2 == nil {
return true
} else if imageSetRef1 != nil && imageSetRef2 != nil {
return imageSetRef1.Name == imageSetRef2.Name
} else {
return false
}
}
func isUserManagedNetworkingSetToFalseWithSNO(newObject *hiveext.AgentClusterInstall) bool {
return isSNO(newObject) &&
newObject.Spec.Networking.UserManagedNetworking != nil &&
!*newObject.Spec.Networking.UserManagedNetworking
}
func isSNO(newObject *hiveext.AgentClusterInstall) bool {
return newObject.Spec.ProvisionRequirements.ControlPlaneAgents == 1 &&
newObject.Spec.ProvisionRequirements.WorkerAgents == 0
}
// diffReporter is a simple custom reporter that only records differences
// detected during comparison.
type diffReporter struct {
path cmp.Path
diffs []string
}
func (r *diffReporter) PushStep(ps cmp.PathStep) {
r.path = append(r.path, ps)
}
func (r *diffReporter) Report(rs cmp.Result) {
if !rs.Equal() {
p := r.path.String()
vx, vy := r.path.Last().Values()
r.diffs = append(r.diffs, fmt.Sprintf("\t%s: (%+v => %+v)", p, vx, vy))
}
}
func (r *diffReporter) PopStep() {
r.path = r.path[:len(r.path)-1]
}
func (r *diffReporter) String() string {
return strings.Join(r.diffs, "\n")
}