-
Notifications
You must be signed in to change notification settings - Fork 91
/
config.go
376 lines (331 loc) · 9.99 KB
/
config.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
// Copyright 2019 Hewlett Packard Enterprise Development LP
// 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 validator
import (
"encoding/json"
"fmt"
"github.com/bluek8s/kubedirector/pkg/secretkeys"
"strings"
"github.com/bluek8s/kubedirector/pkg/controller/kubedirectorconfig"
"github.com/bluek8s/kubedirector/pkg/shared"
kdv1 "github.com/bluek8s/kubedirector/pkg/apis/kubedirector/v1beta1"
"github.com/bluek8s/kubedirector/pkg/observer"
av1beta1 "k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// configPatchSpec is used to create the PATCH operation for populating
// default values in the config as necessary.
type configPatchSpec struct {
Op string `json:"op"`
Path string `json:"path"`
Value configPatchValue `json:"value"`
}
type configPatchValue struct {
ValueStr *string
ValueBool *bool
// if no value is specified, will marshal an empty object instead
}
func (obj configPatchValue) MarshalJSON() ([]byte, error) {
if obj.ValueStr != nil {
return json.Marshal(obj.ValueStr)
}
if obj.ValueBool != nil {
return json.Marshal(obj.ValueBool)
}
return json.Marshal(struct{}{})
}
// ensureConfigSpec creates a PATCH if necessary to create the top-level spec
// object. (This can be missing since none of the spec properties are
// required.) The CR's Spec property will also be set to an empty struct if
// it is currently nil.
func ensureConfigSpec(
cr *kdv1.KubeDirectorConfig,
) []configPatchSpec {
if cr.Spec != nil {
return []configPatchSpec{}
}
cr.Spec = &kdv1.KubeDirectorConfigSpec{}
return []configPatchSpec{
{
Op: "add",
Path: "/spec",
},
}
}
// validateConfigStorageClass validates storageClassName by checking
// for a valid storageClass k8s resource.
func validateConfigStorageClass(
storageClassName *string,
valErrors []string,
) []string {
if storageClassName == nil {
return valErrors
}
_, err := observer.GetStorageClass(*storageClassName)
if err == nil {
return valErrors
}
valErrors = append(
valErrors,
fmt.Sprintf(
invalidStorageClass,
*storageClassName,
),
)
return valErrors
}
// validateOrPopulateMasterEncryptionKey checks key length to be supported by AES (16,24,32)
// or generates default 32 bytes encryption key for AES-256. Also, if there's
// an existing non-nil value, we currently don't allow changing the value while
// any kdclusters exist.
func validateOrPopulateMasterEncryptionKey(
prevConfigCR kdv1.KubeDirectorConfig,
configCR kdv1.KubeDirectorConfig,
patches []configPatchSpec,
valErrors []string,
) ([]configPatchSpec, []string) {
if (prevConfigCR.Spec != nil) && (prevConfigCR.Spec.MasterEncryptionKey != nil) {
if shared.AnyClusters() {
if (configCR.Spec.MasterEncryptionKey == nil) ||
(*configCR.Spec.MasterEncryptionKey != *prevConfigCR.Spec.MasterEncryptionKey) {
valErrors = append(valErrors, masterEncryptionKeyChange)
return patches, valErrors
}
}
}
if configCR.Spec.MasterEncryptionKey == nil {
patches = append(patches,
newStrPatch("/spec/masterEncryptionKey", secretkeys.GenerateEncryptionKey()),
)
} else {
err := secretkeys.ValidateEncryptionKey(*configCR.Spec.MasterEncryptionKey)
if err != nil {
valErrors = append(valErrors,
fmt.Sprintf(
invalidMasterEncryptionKey,
err,
),
)
}
}
return patches, valErrors
}
// admitKDConfigCR is the top-level config validation function, which invokes
// specific validation subroutines and composes the admission response. The
// admission response will include PATCH operations as necessary to populate
// values for missing properties.
func admitKDConfigCR(
ar *av1beta1.AdmissionReview,
) *av1beta1.AdmissionResponse {
var admitResponse = av1beta1.AdmissionResponse{
Allowed: false,
}
// If this is a delete, the admission handler only needs to check that
// there are no existing kdclusters.
if ar.Request.Operation == av1beta1.Delete {
if shared.AnyClusters() {
admitResponse.Result = &metav1.Status{
Message: "\n" + invalidConfigDelete,
}
} else {
admitResponse.Allowed = true
}
return &admitResponse
}
// Deserialize the object.
raw := ar.Request.Object.Raw
configCR := kdv1.KubeDirectorConfig{}
if jsonErr := json.Unmarshal(raw, &configCR); jsonErr != nil {
admitResponse.Result = &metav1.Status{
Message: "\n" + jsonErr.Error(),
}
return &admitResponse
}
// Only allow KubeDirectorConfig requests in the kubedirector namespace.
kdNamespace, err := shared.GetKubeDirectorNamespace()
if err != nil {
admitResponse.Result = &metav1.Status{
Message: "Failed to get kubedirector namespace",
}
return &admitResponse
}
if configCR.Namespace != kdNamespace {
admitResponse.Result = &metav1.Status{
Message: fmt.Sprintf("Invalid namespace '%s', must be '%s'.\n",
configCR.Namespace,
kdNamespace,
),
}
return &admitResponse
}
// If this is an update, get the previous version of the object ready for
// use in some checks.
prevConfigCR := kdv1.KubeDirectorConfig{}
if ar.Request.Operation == av1beta1.Update {
prevRaw := ar.Request.OldObject.Raw
if prevJSONErr := json.Unmarshal(prevRaw, &prevConfigCR); prevJSONErr != nil {
admitResponse.Result = &metav1.Status{
Message: "\n" + prevJSONErr.Error(),
}
return &admitResponse
}
}
// Don't allow Status to be updated except by KubeDirector. Do this by
// using one-time codes known by KubeDirector.
if configCR.Status != nil {
statusViolation := &metav1.Status{
Message: "\nKubeDirector-related status properties are read-only",
}
expectedStatusGen, ok := kubedirectorconfig.StatusGens.ReadStatusGen(configCR.UID)
// Reject this write if either of:
// - this status generation UID is not what we're expecting a write for
// - KubeDirector doesn't know about the CR & the status is changing
if ok {
if configCR.Status.GenerationUID != expectedStatusGen.UID {
admitResponse.Result = statusViolation
return &admitResponse
}
} else {
if !equality.Semantic.DeepEqual(configCR.Status, prevConfigCR.Status) {
admitResponse.Result = statusViolation
return &admitResponse
}
}
// If this status generation UID has already been admitted previously,
// it's OK to write the status again as long as nothing is changing.
// (For example we'll see this when a PATCH happens.)
if expectedStatusGen.Validated {
if !equality.Semantic.DeepEqual(configCR.Status, prevConfigCR.Status) {
admitResponse.Result = statusViolation
return &admitResponse
}
}
}
kubedirectorconfig.StatusGens.ValidateStatusGen(configCR.UID)
var valErrors []string
patches := ensureConfigSpec(&configCR)
// Validate storage class name if present.
valErrors = validateConfigStorageClass(configCR.Spec.StorageClass, valErrors)
// Populate default service type if necessary.
if configCR.Spec.ServiceType == nil {
patches = append(patches,
newStrPatch("/spec/defaultServiceType", shared.DefaultServiceType),
)
}
// Populate default systemd support if necessary.
if configCR.Spec.NativeSystemdSupport == nil {
patches = append(patches,
newBoolPatch("/spec/nativeSystemdSupport", defaultNativeSystemd),
)
}
// Populate the default ClusterSvcDomainBase if necessary
if configCR.Spec.ClusterSvcDomainBase == nil {
svcDomainBase := shared.DefaultSvcDomainBase
patches = append(
patches,
configPatchSpec{
Op: "add",
Path: "/spec/clusterSvcDomainBase",
Value: configPatchValue{
ValueStr: &svcDomainBase,
},
},
)
}
// Populate default naming scheme if necessary.
if configCR.Spec.DefaultNamingScheme == nil {
patches = append(patches,
newStrPatch("/spec/defaultNamingScheme", shared.DefaultNamingScheme),
)
}
// Populate master key if necessary.
patches, valErrors = validateOrPopulateMasterEncryptionKey(
prevConfigCR,
configCR,
patches,
valErrors,
)
// Check that all specified global labels/annotations have good syntax.
valErrors, _ = validateLabelsAndAnnotations(
field.NewPath("spec"),
configCR.Spec.PodLabels,
configCR.Spec.PodAnnotations,
configCR.Spec.ServiceLabels,
configCR.Spec.ServiceAnnotations,
valErrors,
)
// Populate backup-cluster-status and allow-restore-w/o-connections flags
// if necessary.
if configCR.Spec.BackupClusterStatus == nil {
patches = append(patches,
newBoolPatch(
"/spec/backupClusterStatus",
defaultBackupClusterStatus,
),
)
}
if configCR.Spec.AllowRestoreWithoutConnections == nil {
patches = append(patches,
newBoolPatch(
"/spec/allowRestoreWithoutConnections",
defaultAllowRestoreWithoutConnections,
),
)
}
if len(valErrors) == 0 {
if len(patches) != 0 {
patchResult, patchErr := json.Marshal(patches)
if patchErr == nil {
admitResponse.Patch = patchResult
patchType := av1beta1.PatchTypeJSONPatch
admitResponse.PatchType = &patchType
} else {
valErrors = append(valErrors, failedToPatch)
}
}
}
if len(valErrors) == 0 {
admitResponse.Allowed = true
} else {
admitResponse.Result = &metav1.Status{
Message: "\n" + strings.Join(valErrors, "\n"),
}
}
return &admitResponse
}
func newBoolPatch(
path string,
defaultVal bool,
) configPatchSpec {
valueBool := defaultVal
return configPatchSpec{
Op: "add",
Path: path,
Value: configPatchValue{
ValueBool: &valueBool,
},
}
}
func newStrPatch(
path string,
defaultVal string,
) configPatchSpec {
valueStr := defaultVal
return configPatchSpec{
Op: "add",
Path: path,
Value: configPatchValue{
ValueStr: &valueStr,
},
}
}