/
api.go
537 lines (431 loc) · 14.3 KB
/
api.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
/*
Copyright paskal.maksim@gmail.com
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 api
import (
"context"
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/distribution/reference"
"github.com/maksim-paskal/pod-admission-controller/pkg/client"
"github.com/maksim-paskal/pod-admission-controller/pkg/conditions"
"github.com/maksim-paskal/pod-admission-controller/pkg/config"
"github.com/maksim-paskal/pod-admission-controller/pkg/metrics"
"github.com/maksim-paskal/pod-admission-controller/pkg/patch"
"github.com/maksim-paskal/pod-admission-controller/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)
var (
runtimeScheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(runtimeScheme)
deserializer = codecs.UniversalDeserializer()
)
type MutateInput struct {
Namespace *corev1.Namespace
AdmissionReview *admissionv1.AdmissionReview
}
func (m *MutateInput) GetObjectName() string {
result := m.GetType()
if m != nil && m.AdmissionReview != nil && m.AdmissionReview.Request != nil {
req := m.AdmissionReview.Request
if len(req.Namespace) > 0 {
result += "/" + req.Namespace
}
if len(req.Name) > 0 {
result += "/" + req.Name
}
if len(req.UID) > 0 {
result += "/" + string(req.UID)
}
}
return result
}
func (m *MutateInput) GetNamespace(ctx context.Context) (*corev1.Namespace, error) {
if m.Namespace != nil {
return m.Namespace, nil
}
namespace, err := client.KubeClient().CoreV1().Namespaces().Get(ctx, m.AdmissionReview.Request.Namespace, metav1.GetOptions{}) //nolint:lll
if err != nil {
return nil, errors.Wrap(err, "namespace not found")
}
return namespace, nil
}
func (m *MutateInput) GetType() string {
resourceType := m.AdmissionReview.Request.Resource.Resource
if len(m.AdmissionReview.Request.Resource.Group) > 0 {
resourceType += "." + m.AdmissionReview.Request.Resource.Group
}
if len(m.AdmissionReview.Request.Resource.Version) > 0 {
resourceType += "." + m.AdmissionReview.Request.Resource.Version
}
return resourceType
}
type Mutation struct{}
func NewMutation() *Mutation {
return &Mutation{}
}
func (m *Mutation) Mutate(ctx context.Context, input *MutateInput) *admissionv1.AdmissionResponse {
log.Infof("mutate %s", input.GetObjectName())
switch input.GetType() {
case "pods.v1":
return m.mutatePod(ctx, input)
case "namespaces.v1":
return m.mutateNamespace(ctx, input)
case "ingresses.networking.k8s.io.v1":
return m.mutateIngress(ctx, input)
}
return m.mutateError(string(input.AdmissionReview.Request.UID), errors.Errorf("unknown resource type %s", input.GetType())) //nolint:lll
}
func (m *Mutation) createSecret(ctx context.Context, namespace string, secret *types.CreateSecret) error {
_, err := client.KubeClient().CoreV1().Secrets(namespace).Get(ctx, secret.Name, metav1.GetOptions{})
// return error if operation have some errors except not found
if err != nil && !k8sErrors.IsNotFound(err) {
return errors.Wrap(err, "error getting secret")
}
// delete secret if exists
if !k8sErrors.IsNotFound(err) {
err := client.KubeClient().CoreV1().Secrets(namespace).Delete(ctx, secret.Name, metav1.DeleteOptions{})
if err != nil {
return errors.Wrap(err, "error deleting secret")
}
}
newSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secret.Name,
Labels: map[string]string{
"app": "pod-admission-controller",
},
},
Type: corev1.SecretType(secret.Type),
Data: secret.Data,
}
_, err = client.KubeClient().CoreV1().Secrets(namespace).Create(ctx, newSecret, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "error creating secret")
}
return nil
}
func (m *Mutation) replaceIngressHost(ingress networkingv1.Ingress, host string) (string, bool) {
ingressSuffix := *config.Get().IngressSuffix
if ingress.Annotations != nil {
if suffix, ok := ingress.Annotations[types.AnnotationDefaultIngressSuffix]; ok {
ingressSuffix = suffix
}
}
if len(ingressSuffix) == 0 {
return host, false
}
if strings.HasSuffix(host, ".") {
return host + ingressSuffix, true
}
return host, false
}
func (m *Mutation) mutateIngressHosts(ingress networkingv1.Ingress) []types.PatchOperation {
mutationPatch := make([]types.PatchOperation, 0)
for ruleID, rule := range ingress.Spec.Rules {
if host, ok := m.replaceIngressHost(ingress, rule.Host); ok {
mutationPatch = append(mutationPatch, types.PatchOperation{
Op: "replace",
Path: fmt.Sprintf("/spec/rules/%d/host", ruleID),
Value: host,
})
}
}
for tlsID, tls := range ingress.Spec.TLS {
for hostID, host := range tls.Hosts {
if newHost, ok := m.replaceIngressHost(ingress, host); ok {
mutationPatch = append(mutationPatch, types.PatchOperation{
Op: "replace",
Path: fmt.Sprintf("/spec/tls/%d/hosts/%d", tlsID, hostID),
Value: newHost,
})
}
}
}
return mutationPatch
}
func (m *Mutation) mutateIngress(_ context.Context, input *MutateInput) *admissionv1.AdmissionResponse {
req := input.AdmissionReview.Request
ingress := networkingv1.Ingress{}
if err := json.Unmarshal(req.Object.Raw, &ingress); err != nil {
return m.mutateError(ingress.Name, err)
}
if m.checkIgnoreAnnotation(ingress.Annotations) {
metrics.MutationsIgnored.WithLabelValues(ingress.Name).Inc()
return &admissionv1.AdmissionResponse{
Allowed: true,
Warnings: []string{
fmt.Sprintf("%s, ingress %s", types.WarningObjectDoedNotNeedMutation, ingress.Name),
},
}
}
mutationPatch := make([]types.PatchOperation, 0)
mutationPatch = append(mutationPatch, m.mutateIngressHosts(ingress)...)
mutationPatch = append(mutationPatch, m.injectAnnotation(ingress.Annotations))
patchBytes, err := json.Marshal(mutationPatch)
if err != nil {
return m.mutateError(ingress.Name, err)
}
return &admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Status: metav1.StatusSuccess,
},
Patch: patchBytes,
PatchType: func() *admissionv1.PatchType {
pt := admissionv1.PatchTypeJSONPatch
return &pt
}(),
}
}
func (m *Mutation) mutateNamespace(ctx context.Context, input *MutateInput) *admissionv1.AdmissionResponse { //nolint:lll
req := input.AdmissionReview.Request
namespace := corev1.Namespace{}
if err := json.Unmarshal(req.Object.Raw, &namespace); err != nil {
return m.mutateError(namespace.Name, err)
}
if m.checkIgnoreAnnotation(namespace.Annotations) {
metrics.MutationsIgnored.WithLabelValues(namespace.Name).Inc()
return &admissionv1.AdmissionResponse{
Allowed: true,
Warnings: []string{
fmt.Sprintf("%s, namespace %s", types.WarningObjectDoedNotNeedMutation, namespace.Name),
},
}
}
for _, secret := range config.Get().CreateSecrets {
if err := m.createSecret(ctx, namespace.Name, secret); err != nil {
return m.mutateError(namespace.Name, err)
}
}
return &admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Status: metav1.StatusSuccess,
},
}
}
// mutate pod.
func (m *Mutation) mutatePod(ctx context.Context, input *MutateInput) *admissionv1.AdmissionResponse { //nolint:funlen,cyclop,lll
namespace, err := input.GetNamespace(ctx)
if err != nil {
return m.mutateError("namespace not found", err)
}
req := input.AdmissionReview.Request
pod := corev1.Pod{}
if err := json.Unmarshal(req.Object.Raw, &pod); err != nil {
return m.mutateError(namespace.Name, err)
}
if m.checkIgnoreAnnotation(pod.Annotations) {
metrics.MutationsIgnored.WithLabelValues(namespace.Name).Inc()
return &admissionv1.AdmissionResponse{
Allowed: true,
Warnings: []string{
fmt.Sprintf("%s, pod %s/%s", types.WarningObjectDoedNotNeedMutation, namespace.Name, pod.Name),
},
}
}
mutationPatch := make([]types.PatchOperation, 0)
for _, podContainer := range types.PodContainersFromPod(namespace, &pod) {
containerInfo := &types.ContainerInfo{
PodContainer: podContainer,
ContainerName: podContainer.Container.Name,
ContainerType: podContainer.Type,
Namespace: namespace.Name,
NamespaceAnnotations: namespace.Annotations,
NamespaceLabels: namespace.Labels,
PodAnnotations: pod.Annotations,
PodLabels: pod.Labels,
SelectedRules: []*types.Rule{},
}
imageInfo, err := GetImageInfo(podContainer.Container.Image)
if err != nil {
return m.mutateError(namespace.Name, err)
}
containerInfo.Image = imageInfo
log.Debugf("containerInfo.Image=%+v", containerInfo.Image)
// check rule that corresponds to container
for _, rule := range config.Get().Rules {
match, err := conditions.Check(containerInfo, rule.Conditions)
if err != nil {
return m.mutateError(namespace.Name, err)
}
if match {
containerInfo.SelectedRules = append(containerInfo.SelectedRules, rule)
}
}
// if no rules found for container continue to next container
if len(containerInfo.SelectedRules) == 0 {
continue
}
pathOps, err := patch.NewPatch(ctx, containerInfo)
if err != nil {
return m.mutateError(namespace.Name, err)
}
for _, pathOp := range pathOps {
if m.patchContains(mutationPatch, pathOp) {
log.Debugf("patch already exists: %s", pathOp)
} else {
mutationPatch = append(mutationPatch, pathOp)
}
}
}
// if no patches found return empty response
if len(mutationPatch) == 0 {
return &admissionv1.AdmissionResponse{
Allowed: true,
Warnings: []string{types.WarningNoPatchGenerated},
}
}
mutationPatch = append(mutationPatch, m.injectAnnotation(pod.Annotations))
patchBytes, err := json.Marshal(mutationPatch)
if err != nil {
return m.mutateError(namespace.Name, err)
}
log.Debugf("patch=%s", string(patchBytes))
metrics.MutationsTotal.WithLabelValues(namespace.Name).Inc()
return &admissionv1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Status: metav1.StatusSuccess,
},
Patch: patchBytes,
PatchType: func() *admissionv1.PatchType {
pt := admissionv1.PatchTypeJSONPatch
return &pt
}(),
}
}
func (m *Mutation) patchContains(patches []types.PatchOperation, patch types.PatchOperation) bool {
for _, p := range patches {
if p.Path == patch.Path && p.Op == patch.Op {
if reflect.DeepEqual(p.Value, patch.Value) {
return true
}
}
}
return false
}
// some objects does not need mutation
// pod-admission-controller/ignore=true.
func (m *Mutation) checkIgnoreAnnotation(annotations map[string]string) bool {
if ignore, ok := annotations[types.AnnotationIgnore]; ok {
if strings.EqualFold(ignore, "true") {
return true
}
}
return false
}
func (m *Mutation) injectAnnotation(annotations map[string]string) types.PatchOperation {
objAnnotations := make(map[string]string)
if annotations != nil {
objAnnotations = annotations
}
objAnnotations[types.AnnotationInjected] = "true"
return types.PatchOperation{
Op: "add",
Path: "/metadata/annotations",
Value: objAnnotations,
}
}
// throw mutaion errors.
func (m *Mutation) mutateError(namespaceName string, err error) *admissionv1.AdmissionResponse {
log.WithError(err).Error("Error mutating")
metrics.MutationsError.WithLabelValues(namespaceName).Inc()
return &admissionv1.AdmissionResponse{
Result: &metav1.Status{
Status: metav1.StatusFailure,
Message: err.Error(),
},
}
}
// parse http request.
func ParseRequest(ctx context.Context, body []byte) ([]byte, error) {
obj, gvk, err := deserializer.Decode(body, nil, &admissionv1.AdmissionReview{})
if err != nil {
return nil, errors.Wrap(err, "Request could not be decoded")
}
var responseObj runtime.Object
requestedAdmissionReview, ok := obj.(*admissionv1.AdmissionReview)
if !ok {
return nil, errors.Errorf("Expected v1.AdmissionReview but got: %T", obj)
}
input := &MutateInput{
AdmissionReview: requestedAdmissionReview,
}
responseAdmissionReview := &admissionv1.AdmissionReview{}
responseAdmissionReview.SetGroupVersionKind(*gvk)
responseAdmissionReview.Response = NewMutation().Mutate(ctx, input)
responseAdmissionReview.Response.UID = requestedAdmissionReview.Request.UID
responseObj = responseAdmissionReview
respBytes, err := json.Marshal(responseObj)
if err != nil {
return nil, errors.Wrap(err, "can not marshal response")
}
return respBytes, nil
}
// parse image to repo, slug path and tag.
func GetImageInfo(image string) (*types.ContainerImage, error) {
// check if image is has fully qualified name
refName, err := reference.ParseNormalizedNamed(image)
if err != nil {
return nil, errors.Wrapf(err, "error parsing image name %s", image)
}
// get only lowered the image name
imageName := strings.ToLower(reference.Path(refName))
// replace all non alphanumeric characters with a dash
imageName = regexp.MustCompile("[^A-Za-z0-9]+").ReplaceAllString(imageName, "-")
result := types.ContainerImage{
Domain: reference.Domain(refName),
Name: image,
Slug: strings.Trim(imageName, "-"),
Tag: "latest",
}
if tag, ok := refName.(reference.Tagged); ok {
result.Tag = tag.Tag()
}
return &result, nil
}
func TestPOD(ctx context.Context, namespace, podName string) ([]byte, error) {
pod, err := client.KubeClient().CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrap(err, "Could not get namespace")
}
podJSON, err := json.Marshal(pod)
if err != nil {
return nil, errors.Wrap(err, "Could not marshal pod")
}
input := &MutateInput{
AdmissionReview: &admissionv1.AdmissionReview{
Request: &admissionv1.AdmissionRequest{
Object: runtime.RawExtension{
Raw: podJSON,
},
},
},
}
log.Infof("req=%+v", input)
resp := NewMutation().Mutate(ctx, input)
log.Infof("warnings=%+v", resp.Warnings)
return resp.Patch, nil
}