| 1 |
/* |
| 2 |
Copyright 2014 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package admission |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"io" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/runtime" |
| 24 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 25 |
auditinternal "k8s.io/apiserver/pkg/apis/audit" |
| 26 |
"k8s.io/apiserver/pkg/authentication/user" |
| 27 |
) |
| 28 |
|
| 29 |
// Attributes is an interface used by AdmissionController to get information about a request |
| 30 |
// that is used to make an admission decision. |
| 31 |
type Attributes interface { |
| 32 |
// GetName returns the name of the object as presented in the request. On a CREATE operation, the client |
| 33 |
// may omit name and rely on the server to generate the name. If that is the case, this method will return |
| 34 |
// the empty string |
| 35 |
GetName() string |
| 36 |
// GetNamespace is the namespace associated with the request (if any) |
| 37 |
GetNamespace() string |
| 38 |
// GetResource is the name of the resource being requested. This is not the kind. For example: pods |
| 39 |
GetResource() schema.GroupVersionResource |
| 40 |
// GetSubresource is the name of the subresource being requested. This is a different resource, scoped to the parent resource, but it may have a different kind. |
| 41 |
// For instance, /pods has the resource "pods" and the kind "Pod", while /pods/foo/status has the resource "pods", the sub resource "status", and the kind "Pod" |
| 42 |
// (because status operates on pods). The binding resource for a pod though may be /pods/foo/binding, which has resource "pods", subresource "binding", and kind "Binding". |
| 43 |
GetSubresource() string |
| 44 |
// GetOperation is the operation being performed |
| 45 |
GetOperation() Operation |
| 46 |
// GetOperationOptions is the options for the operation being performed |
| 47 |
GetOperationOptions() runtime.Object |
| 48 |
// IsDryRun indicates that modifications will definitely not be persisted for this request. This is to prevent |
| 49 |
// admission controllers with side effects and a method of reconciliation from being overwhelmed. |
| 50 |
// However, a value of false for this does not mean that the modification will be persisted, because it |
| 51 |
// could still be rejected by a subsequent validation step. |
| 52 |
IsDryRun() bool |
| 53 |
// GetObject is the object from the incoming request prior to default values being applied |
| 54 |
GetObject() runtime.Object |
| 55 |
// GetOldObject is the existing object. Only populated for UPDATE and DELETE requests. |
| 56 |
GetOldObject() runtime.Object |
| 57 |
// GetKind is the type of object being manipulated. For example: Pod |
| 58 |
GetKind() schema.GroupVersionKind |
| 59 |
// GetUserInfo is information about the requesting user |
| 60 |
GetUserInfo() user.Info |
| 61 |
|
| 62 |
// AddAnnotation sets annotation according to key-value pair. The key should be qualified, e.g., podsecuritypolicy.admission.k8s.io/admit-policy, where |
| 63 |
// "podsecuritypolicy" is the name of the plugin, "admission.k8s.io" is the name of the organization, "admit-policy" is the key name. |
| 64 |
// An error is returned if the format of key is invalid. When trying to overwrite annotation with a new value, an error is returned. |
| 65 |
// Both ValidationInterface and MutationInterface are allowed to add Annotations. |
| 66 |
// By default, an annotation gets logged into audit event if the request's audit level is greater or |
| 67 |
// equal to Metadata. |
| 68 |
AddAnnotation(key, value string) error |
| 69 |
|
| 70 |
// AddAnnotationWithLevel sets annotation according to key-value pair with additional intended audit level. |
| 71 |
// An Annotation gets logged into audit event if the request's audit level is greater or equal to the |
| 72 |
// intended audit level. |
| 73 |
AddAnnotationWithLevel(key, value string, level auditinternal.Level) error |
| 74 |
|
| 75 |
// GetReinvocationContext tracks the admission request information relevant to the re-invocation policy. |
| 76 |
GetReinvocationContext() ReinvocationContext |
| 77 |
} |
| 78 |
|
| 79 |
// ObjectInterfaces is an interface used by AdmissionController to get object interfaces |
| 80 |
// such as Converter or Defaulter. These interfaces are normally coming from Request Scope |
| 81 |
// to handle special cases like CRDs. |
| 82 |
type ObjectInterfaces interface { |
| 83 |
// GetObjectCreater is the ObjectCreator appropriate for the requested object. |
| 84 |
GetObjectCreater() runtime.ObjectCreater |
| 85 |
// GetObjectTyper is the ObjectTyper appropriate for the requested object. |
| 86 |
GetObjectTyper() runtime.ObjectTyper |
| 87 |
// GetObjectDefaulter is the ObjectDefaulter appropriate for the requested object. |
| 88 |
GetObjectDefaulter() runtime.ObjectDefaulter |
| 89 |
// GetObjectConvertor is the ObjectConvertor appropriate for the requested object. |
| 90 |
GetObjectConvertor() runtime.ObjectConvertor |
| 91 |
// GetEquivalentResourceMapper is the EquivalentResourceMapper appropriate for finding equivalent resources and expected kind for the requested object. |
| 92 |
GetEquivalentResourceMapper() runtime.EquivalentResourceMapper |
| 93 |
} |
| 94 |
|
| 95 |
// privateAnnotationsGetter is a private interface which allows users to get annotations from Attributes. |
| 96 |
type privateAnnotationsGetter interface { |
| 97 |
getAnnotations(maxLevel auditinternal.Level) map[string]string |
| 98 |
} |
| 99 |
|
| 100 |
// AnnotationsGetter allows users to get annotations from Attributes. An alternate Attribute should implement |
| 101 |
// this interface. |
| 102 |
type AnnotationsGetter interface { |
| 103 |
GetAnnotations(maxLevel auditinternal.Level) map[string]string |
| 104 |
} |
| 105 |
|
| 106 |
// ReinvocationContext provides access to the admission related state required to implement the re-invocation policy. |
| 107 |
type ReinvocationContext interface { |
| 108 |
// IsReinvoke returns true if the current admission check is a re-invocation. |
| 109 |
IsReinvoke() bool |
| 110 |
// SetIsReinvoke sets the current admission check as a re-invocation. |
| 111 |
SetIsReinvoke() |
| 112 |
// ShouldReinvoke returns true if any plugin has requested a re-invocation. |
| 113 |
ShouldReinvoke() bool |
| 114 |
// SetShouldReinvoke signals that a re-invocation is desired. |
| 115 |
SetShouldReinvoke() |
| 116 |
// AddValue set a value for a plugin name, possibly overriding a previous value. |
| 117 |
SetValue(plugin string, v interface{}) |
| 118 |
// Value reads a value for a webhook. |
| 119 |
Value(plugin string) interface{} |
| 120 |
} |
| 121 |
|
| 122 |
// Interface is an abstract, pluggable interface for Admission Control decisions. |
| 123 |
type Interface interface { |
| 124 |
// Handles returns true if this admission controller can handle the given operation |
| 125 |
// where operation can be one of CREATE, UPDATE, DELETE, or CONNECT |
| 126 |
Handles(operation Operation) bool |
| 127 |
} |
| 128 |
|
| 129 |
type MutationInterface interface { |
| 130 |
Interface |
| 131 |
|
| 132 |
// Admit makes an admission decision based on the request attributes. |
| 133 |
// Context is used only for timeout/deadline/cancellation and tracing information. |
| 134 |
Admit(ctx context.Context, a Attributes, o ObjectInterfaces) (err error) |
| 135 |
} |
| 136 |
|
| 137 |
// ValidationInterface is an abstract, pluggable interface for Admission Control decisions. |
| 138 |
type ValidationInterface interface { |
| 139 |
Interface |
| 140 |
|
| 141 |
// Validate makes an admission decision based on the request attributes. It is NOT allowed to mutate |
| 142 |
// Context is used only for timeout/deadline/cancellation and tracing information. |
| 143 |
Validate(ctx context.Context, a Attributes, o ObjectInterfaces) (err error) |
| 144 |
} |
| 145 |
|
| 146 |
// Operation is the type of resource operation being checked for admission control |
| 147 |
type Operation string |
| 148 |
|
| 149 |
// Operation constants |
| 150 |
const ( |
| 151 |
Create Operation = "CREATE" |
| 152 |
Update Operation = "UPDATE" |
| 153 |
Delete Operation = "DELETE" |
| 154 |
Connect Operation = "CONNECT" |
| 155 |
) |
| 156 |
|
| 157 |
// PluginInitializer is used for initialization of shareable resources between admission plugins. |
| 158 |
// After initialization the resources have to be set separately |
| 159 |
type PluginInitializer interface { |
| 160 |
Initialize(plugin Interface) |
| 161 |
} |
| 162 |
|
| 163 |
// InitializationValidator holds ValidateInitialization functions, which are responsible for validation of initialized |
| 164 |
// shared resources and should be implemented on admission plugins |
| 165 |
type InitializationValidator interface { |
| 166 |
ValidateInitialization() error |
| 167 |
} |
| 168 |
|
| 169 |
// ConfigProvider provides a way to get configuration for an admission plugin based on its name |
| 170 |
type ConfigProvider interface { |
| 171 |
ConfigFor(pluginName string) (io.Reader, error) |
| 172 |
} |