forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmissionplugins.go
More file actions
220 lines (193 loc) · 9.71 KB
/
admissionplugins.go
File metadata and controls
220 lines (193 loc) · 9.71 KB
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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package admissionplugins
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
admissionapiv1 "k8s.io/pod-security-admission/admission/api/v1"
admissionapiv1alpha1 "k8s.io/pod-security-admission/admission/api/v1alpha1"
admissionapiv1beta1 "k8s.io/pod-security-admission/admission/api/v1beta1"
"k8s.io/utils/ptr"
"github.com/gardener/gardener/pkg/apis/core"
versionutils "github.com/gardener/gardener/pkg/utils/version"
)
var (
// admissionPluginsVersionRanges contains the version ranges for all Kubernetes admission plugins.
// Extracted from https://raw.githubusercontent.com/kubernetes/kubernetes/release-${version}/pkg/kubeapiserver/options/plugins.go
// and https://raw.githubusercontent.com/kubernetes/kubernetes/release-${version}/staging/src/k8s.io/apiserver/pkg/server/plugins.go.
// To maintain this list for each new Kubernetes version:
// - Run hack/compare-k8s-admission-plugins.sh <old-version> <new-version> (e.g. 'hack/compare-k8s-admission-plugins.sh 1.26 1.27').
// It will present 2 lists of admission plugins: those added and those removed in <new-version> compared to <old-version> and
// - Add all added admission plugins to the map with <new-version> as AddedInVersion and no RemovedInVersion.
// - Set `AllowsConfiguration` to `true` when the new admission plugin allows configuration.
// To determine whether the admission plugin allows configuration, check the plugin's `Register()` method, whether it uses configuration or not.
// Be careful that functions that are called within the method can take the config as part of their signature, but ultimately do not use it.
// - For any removed admission plugin, add <new-version> as RemovedInVersion to the already existing admission plugin in the map.
admissionPluginsVersionRanges = map[string]*AdmissionPluginVersionRange{
"AlwaysAdmit": {},
"AlwaysDeny": {},
"AlwaysPullImages": {},
"CertificateApproval": {},
"CertificateSigning": {},
"CertificateSubjectRestriction": {},
"ClusterTrustBundleAttest": {},
"DefaultIngressClass": {},
"DefaultStorageClass": {},
"DefaultTolerationSeconds": {},
"DenyServiceExternalIPs": {},
"EventRateLimit": {AllowsConfiguration: true},
"ExtendedResourceToleration": {},
"ImagePolicyWebhook": {AllowsConfiguration: true},
"LimitPodHardAntiAffinityTopology": {},
"LimitRanger": {},
"MutatingAdmissionPolicy": {VersionRange: versionutils.VersionRange{AddedInVersion: "1.32"}},
"MutatingAdmissionWebhook": {Required: true, AllowsConfiguration: true},
"NamespaceAutoProvision": {},
"NamespaceExists": {},
"NamespaceLifecycle": {Required: true},
"NodeRestriction": {Required: true},
"OwnerReferencesPermissionEnforcement": {},
"PersistentVolumeClaimResize": {},
"PersistentVolumeLabel": {VersionRange: versionutils.VersionRange{RemovedInVersion: "1.31"}},
"PodNodeSelector": {AllowsConfiguration: true},
"PodSecurity": {Required: true, AllowsConfiguration: true},
"PodTolerationRestriction": {AllowsConfiguration: true},
"PodTopologyLabels": {VersionRange: versionutils.VersionRange{AddedInVersion: "1.33"}},
"Priority": {Required: true},
"ResourceQuota": {AllowsConfiguration: true},
"RuntimeClass": {},
"SecurityContextDeny": {Forbidden: true, VersionRange: versionutils.VersionRange{RemovedInVersion: "1.30"}},
"ServiceAccount": {},
"StorageObjectInUseProtection": {Required: true},
"TaintNodesByCondition": {},
"ValidatingAdmissionPolicy": {},
"ValidatingAdmissionWebhook": {Required: true, AllowsConfiguration: true},
}
admissionPluginsSupportingExternalKubeconfig = sets.New("ValidatingAdmissionWebhook", "MutatingAdmissionWebhook", "ImagePolicyWebhook")
runtimeScheme *runtime.Scheme
codec runtime.Codec
)
func init() {
runtimeScheme = runtime.NewScheme()
utilruntime.Must(admissionapiv1alpha1.AddToScheme(runtimeScheme))
utilruntime.Must(admissionapiv1beta1.AddToScheme(runtimeScheme))
utilruntime.Must(admissionapiv1.AddToScheme(runtimeScheme))
var (
ser = json.NewSerializerWithOptions(json.DefaultMetaFactory, runtimeScheme, runtimeScheme, json.SerializerOptions{
Yaml: true,
Pretty: false,
Strict: false,
})
versions = schema.GroupVersions([]schema.GroupVersion{
admissionapiv1alpha1.SchemeGroupVersion,
admissionapiv1beta1.SchemeGroupVersion,
admissionapiv1.SchemeGroupVersion,
})
)
codec = serializer.NewCodecFactory(runtimeScheme).CodecForVersions(ser, ser, versions, versions)
}
// IsAdmissionPluginSupported returns true if the given admission plugin is supported for the given Kubernetes version.
// An admission plugin is only supported if it's a known admission plugin and its version range contains the given Kubernetes version.
func IsAdmissionPluginSupported(plugin, version string) (bool, error) {
vr := admissionPluginsVersionRanges[plugin]
if vr == nil {
return false, fmt.Errorf("unknown admission plugin %q", plugin)
}
return vr.Contains(version)
}
// AdmissionPluginVersionRange represents a version range of type [AddedInVersion, RemovedInVersion).
type AdmissionPluginVersionRange struct {
versionutils.VersionRange
Forbidden bool
Required bool
AllowsConfiguration bool
}
func getAllForbiddenPlugins(version string) ([]string, error) {
var allForbiddenPlugins []string
for name, vr := range admissionPluginsVersionRanges {
var (
inRange bool
err error
)
if inRange, err = vr.Contains(version); err != nil {
return nil, err
}
if inRange && vr.Forbidden {
allForbiddenPlugins = append(allForbiddenPlugins, name)
}
}
return allForbiddenPlugins, nil
}
// ValidateAdmissionPlugins validates the given Kubernetes admission plugins against the given Kubernetes version.
func ValidateAdmissionPlugins(admissionPlugins []core.AdmissionPlugin, version string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
forbiddenPlugins := []string{}
allForbiddenPlugins, pluginErr := getAllForbiddenPlugins(version)
if pluginErr != nil {
allErrs = append(allErrs, field.InternalError(fldPath, pluginErr))
return allErrs
}
for i, plugin := range admissionPlugins {
idxPath := fldPath.Index(i)
if len(plugin.Name) == 0 {
allErrs = append(allErrs, field.Required(idxPath.Child("name"), "must provide a name"))
return allErrs
}
supported, err := IsAdmissionPluginSupported(plugin.Name, version)
if err != nil {
allErrs = append(allErrs, field.Invalid(idxPath.Child("name"), plugin.Name, err.Error()))
} else if !supported {
allErrs = append(allErrs, field.Forbidden(idxPath.Child("name"), fmt.Sprintf("admission plugin %q is not supported in Kubernetes version %s", plugin.Name, version)))
} else {
if admissionPluginHasConfig(plugin) && !admissionPluginAllowsConfiguration(plugin) {
allErrs = append(allErrs, field.Forbidden(idxPath.Child("config"), fmt.Sprintf("admission plugin %q does not allow configuration", plugin.Name)))
}
if admissionPluginsVersionRanges[plugin.Name].Forbidden {
forbiddenPlugins = append(forbiddenPlugins, plugin.Name)
}
if ptr.Deref(plugin.Disabled, false) && admissionPluginsVersionRanges[plugin.Name].Required {
allErrs = append(allErrs, field.Forbidden(idxPath, fmt.Sprintf("admission plugin %q cannot be disabled", plugin.Name)))
}
if plugin.KubeconfigSecretName != nil && !admissionPluginsSupportingExternalKubeconfig.Has(plugin.Name) {
allErrs = append(allErrs, field.Forbidden(idxPath.Child("kubeconfigSecretName"), fmt.Sprintf("admission plugin %q does not allow specifying external kubeconfig", plugin.Name)))
}
if err := validateAdmissionPluginConfig(plugin, idxPath); err != nil {
allErrs = append(allErrs, err)
}
}
}
if len(forbiddenPlugins) > 0 {
allErrs = append(allErrs, field.Forbidden(fldPath, fmt.Sprintf("forbidden admission plugin(s) %+v - do not use plugins from the following list for Kubernetes version %s: %+v", forbiddenPlugins, version, allForbiddenPlugins)))
}
return allErrs
}
func admissionPluginHasConfig(plugin core.AdmissionPlugin) bool {
return plugin.Config != nil && len(plugin.Config.Raw) > 0
}
func admissionPluginAllowsConfiguration(plugin core.AdmissionPlugin) bool {
vr := admissionPluginsVersionRanges[plugin.Name]
return vr != nil && vr.AllowsConfiguration
}
func validateAdmissionPluginConfig(plugin core.AdmissionPlugin, fldPath *field.Path) *field.Error {
switch plugin.Name {
case "PodSecurity":
if plugin.Config == nil {
return nil
}
_, err := runtime.Decode(codec, plugin.Config.Raw)
if err != nil {
if runtime.IsNotRegisteredError(err) {
return field.Invalid(fldPath.Child("config"), string(plugin.Config.Raw), "expected pod-security.admission.config.k8s.io/v1.PodSecurityConfiguration")
}
return field.Invalid(fldPath.Child("config"), string(plugin.Config.Raw), "cannot decode the given config: "+err.Error())
}
}
return nil
}