/
pods.go
133 lines (114 loc) · 4.33 KB
/
pods.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
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 seedadmission
import (
"context"
"fmt"
"strings"
extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardenlogger "github.com/gardener/gardener/pkg/logger"
"github.com/sirupsen/logrus"
"gomodules.xyz/jsonpatch/v2"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// MutatePod mutates pod if mutation conditions are met
func MutatePod(ctx context.Context, c client.Client, logger *logrus.Logger, request *admissionv1beta1.AdmissionRequest) ([]jsonpatch.JsonPatchOperation, error) {
// Ignore all resources other than our expected ones
switch request.Resource {
case
metav1.GroupVersionResource{Group: corev1.SchemeGroupVersion.Group, Version: corev1.SchemeGroupVersion.Version, Resource: "pods"}:
default:
return nil, nil
}
obj, err := getRequestObject(ctx, c, request)
if err != nil {
return nil, client.IgnoreNotFound(err)
}
entryLogger := gardenlogger.
NewFieldLogger(logger, "resource", fmt.Sprintf("%s/%s/%s", request.Kind.Group, request.Kind.Version, request.Kind.Kind)).
WithField("operation", request.Operation).
WithField("namespace", request.Namespace)
entryLogger.Info("Handling request")
patch, err := mutateShootControlPlanePodAnnotations(ctx, c, entryLogger, obj, request.Namespace)
if err != nil {
logger.Errorf("Error mutating pod %s/%s: %v", request.Name, request.Namespace, err)
return nil, err
}
return patch, nil
}
// mutateShootControlPlanePodAnnotations checks if the object mutation is allowed and return the needed patches .
func mutateShootControlPlanePodAnnotations(ctx context.Context, c client.Client, logger *logrus.Entry, pod runtime.Object, namespace string) ([]jsonpatch.JsonPatchOperation, error) {
var patch []jsonpatch.JsonPatchOperation
// determine whether to perform mutation
isMutationRequired, err := mutationRequired(ctx, c, namespace)
if err != nil {
return nil, err
}
acc, err := meta.Accessor(pod)
if err != nil {
return nil, err
}
if !isMutationRequired {
logger.Debugf("Skipping mutation for %s/%s due to policy check", acc.GetNamespace(), acc.GetName())
return nil, nil
}
originalAnnotations := acc.GetAnnotations()
annotationsToAdd := map[string]string{
"fluentbit.io/exclude": "true",
}
for key, value := range annotationsToAdd {
if originalAnnotations == nil || originalAnnotations[key] == "" {
patch = append(patch, jsonpatch.JsonPatchOperation{
Operation: "add",
Path: "/metadata/annotations",
Value: map[string]string{
key: value,
},
})
} else {
patch = append(patch, jsonpatch.JsonPatchOperation{
Operation: "replace",
Path: "/metadata/annotations/" + strings.ReplaceAll(key, "/", "~1"),
Value: value,
})
}
}
return patch, nil
}
func mutationRequired(ctx context.Context, c client.Client, namespace string) (bool, error) {
cluster, err := extensionscontroller.GetCluster(ctx, c, namespace)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
return false, err
}
if cluster.Shoot == nil {
return false, nil
}
if cluster.Shoot.Spec.Hibernation != nil && cluster.Shoot.Spec.Hibernation.Enabled != nil && *cluster.Shoot.Spec.Hibernation.Enabled {
return true, nil
}
if cluster.Shoot.Spec.Purpose != nil && *cluster.Shoot.Spec.Purpose == gardencorev1beta1.ShootPurposeTesting {
return true, nil
}
return false, nil
}