| 1 |
/* |
| 2 |
Copyright 2024 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 generic |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
|
| 24 |
admissionregistrationv1 "k8s.io/api/admissionregistration/v1" |
| 25 |
"k8s.io/apimachinery/pkg/api/meta" |
| 26 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 27 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 28 |
"k8s.io/apimachinery/pkg/util/sets" |
| 29 |
"k8s.io/apiserver/pkg/admission" |
| 30 |
"k8s.io/apiserver/pkg/admission/initializer" |
| 31 |
"k8s.io/apiserver/pkg/admission/plugin/policy/matching" |
| 32 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 33 |
"k8s.io/client-go/dynamic" |
| 34 |
"k8s.io/client-go/informers" |
| 35 |
"k8s.io/client-go/kubernetes" |
| 36 |
) |
| 37 |
|
| 38 |
// H is the Hook type generated by the source and consumed by the dispatcher. |
| 39 |
// !TODO: Just pass in a Plugin[H] with accessors to all this information |
| 40 |
type sourceFactory[H any] func(informers.SharedInformerFactory, kubernetes.Interface, dynamic.Interface, meta.RESTMapper) Source[H] |
| 41 |
type dispatcherFactory[H any] func(authorizer.Authorizer, *matching.Matcher, kubernetes.Interface) Dispatcher[H] |
| 42 |
|
| 43 |
// admissionResources is the list of resources related to CEL-based admission |
| 44 |
// features. |
| 45 |
var admissionResources = []schema.GroupResource{ |
| 46 |
{Group: admissionregistrationv1.GroupName, Resource: "validatingadmissionpolicies"}, |
| 47 |
{Group: admissionregistrationv1.GroupName, Resource: "validatingadmissionpolicybindings"}, |
| 48 |
{Group: admissionregistrationv1.GroupName, Resource: "mutatingadmissionpolicies"}, |
| 49 |
{Group: admissionregistrationv1.GroupName, Resource: "mutatingadmissionpolicybindings"}, |
| 50 |
} |
| 51 |
|
| 52 |
// AdmissionPolicyManager is an abstract admission plugin with all the |
| 53 |
// infrastructure to define Admit or Validate on-top. |
| 54 |
type Plugin[H any] struct { |
| 55 |
*admission.Handler |
| 56 |
|
| 57 |
sourceFactory sourceFactory[H] |
| 58 |
dispatcherFactory dispatcherFactory[H] |
| 59 |
|
| 60 |
source Source[H] |
| 61 |
dispatcher Dispatcher[H] |
| 62 |
matcher *matching.Matcher |
| 63 |
|
| 64 |
informerFactory informers.SharedInformerFactory |
| 65 |
client kubernetes.Interface |
| 66 |
restMapper meta.RESTMapper |
| 67 |
dynamicClient dynamic.Interface |
| 68 |
excludedResources sets.Set[schema.GroupResource] |
| 69 |
stopCh <-chan struct{} |
| 70 |
authorizer authorizer.Authorizer |
| 71 |
enabled bool |
| 72 |
} |
| 73 |
|
| 74 |
var ( |
| 75 |
_ initializer.WantsExternalKubeInformerFactory = &Plugin[any]{} |
| 76 |
_ initializer.WantsExternalKubeClientSet = &Plugin[any]{} |
| 77 |
_ initializer.WantsRESTMapper = &Plugin[any]{} |
| 78 |
_ initializer.WantsDynamicClient = &Plugin[any]{} |
| 79 |
_ initializer.WantsDrainedNotification = &Plugin[any]{} |
| 80 |
_ initializer.WantsAuthorizer = &Plugin[any]{} |
| 81 |
_ initializer.WantsExcludedAdmissionResources = &Plugin[any]{} |
| 82 |
_ admission.InitializationValidator = &Plugin[any]{} |
| 83 |
) |
| 84 |
|
| 85 |
func NewPlugin[H any]( |
| 86 |
handler *admission.Handler, |
| 87 |
sourceFactory sourceFactory[H], |
| 88 |
dispatcherFactory dispatcherFactory[H], |
| 89 |
) *Plugin[H] { |
| 90 |
return &Plugin[H]{ |
| 91 |
Handler: handler, |
| 92 |
sourceFactory: sourceFactory, |
| 93 |
dispatcherFactory: dispatcherFactory, |
| 94 |
|
| 95 |
// always exclude admission/mutating policies and bindings |
| 96 |
excludedResources: sets.New(admissionResources...), |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (c *Plugin[H]) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) { |
| 101 |
c.informerFactory = f |
| 102 |
} |
| 103 |
|
| 104 |
func (c *Plugin[H]) SetExternalKubeClientSet(client kubernetes.Interface) { |
| 105 |
c.client = client |
| 106 |
} |
| 107 |
|
| 108 |
func (c *Plugin[H]) SetRESTMapper(mapper meta.RESTMapper) { |
| 109 |
c.restMapper = mapper |
| 110 |
} |
| 111 |
|
| 112 |
func (c *Plugin[H]) SetDynamicClient(client dynamic.Interface) { |
| 113 |
c.dynamicClient = client |
| 114 |
} |
| 115 |
|
| 116 |
func (c *Plugin[H]) SetDrainedNotification(stopCh <-chan struct{}) { |
| 117 |
c.stopCh = stopCh |
| 118 |
} |
| 119 |
|
| 120 |
func (c *Plugin[H]) SetAuthorizer(authorizer authorizer.Authorizer) { |
| 121 |
c.authorizer = authorizer |
| 122 |
} |
| 123 |
|
| 124 |
func (c *Plugin[H]) SetMatcher(matcher *matching.Matcher) { |
| 125 |
c.matcher = matcher |
| 126 |
} |
| 127 |
|
| 128 |
func (c *Plugin[H]) SetEnabled(enabled bool) { |
| 129 |
c.enabled = enabled |
| 130 |
} |
| 131 |
|
| 132 |
func (c *Plugin[H]) SetExcludedAdmissionResources(excludedResources []schema.GroupResource) { |
| 133 |
c.excludedResources.Insert(excludedResources...) |
| 134 |
} |
| 135 |
|
| 136 |
// ValidateInitialization - once clientset and informer factory are provided, creates and starts the admission controller |
| 137 |
func (c *Plugin[H]) ValidateInitialization() error { |
| 138 |
// By default enabled is set to false. It is up to types which embed this |
| 139 |
// struct to set it to true (if feature gate is enabled, or other conditions) |
| 140 |
if !c.enabled { |
| 141 |
return nil |
| 142 |
} |
| 143 |
if c.Handler == nil { |
| 144 |
return errors.New("missing handler") |
| 145 |
} |
| 146 |
if c.informerFactory == nil { |
| 147 |
return errors.New("missing informer factory") |
| 148 |
} |
| 149 |
if c.client == nil { |
| 150 |
return errors.New("missing kubernetes client") |
| 151 |
} |
| 152 |
if c.restMapper == nil { |
| 153 |
return errors.New("missing rest mapper") |
| 154 |
} |
| 155 |
if c.dynamicClient == nil { |
| 156 |
return errors.New("missing dynamic client") |
| 157 |
} |
| 158 |
if c.stopCh == nil { |
| 159 |
return errors.New("missing stop channel") |
| 160 |
} |
| 161 |
if c.authorizer == nil { |
| 162 |
return errors.New("missing authorizer") |
| 163 |
} |
| 164 |
|
| 165 |
// Use default matcher |
| 166 |
namespaceInformer := c.informerFactory.Core().V1().Namespaces() |
| 167 |
c.matcher = matching.NewMatcher(namespaceInformer.Lister(), c.client) |
| 168 |
|
| 169 |
if err := c.matcher.ValidateInitialization(); err != nil { |
| 170 |
return err |
| 171 |
} |
| 172 |
|
| 173 |
c.source = c.sourceFactory(c.informerFactory, c.client, c.dynamicClient, c.restMapper) |
| 174 |
c.dispatcher = c.dispatcherFactory(c.authorizer, c.matcher, c.client) |
| 175 |
|
| 176 |
pluginContext, pluginContextCancel := context.WithCancel(context.Background()) |
| 177 |
go func() { |
| 178 |
defer pluginContextCancel() |
| 179 |
<-c.stopCh |
| 180 |
}() |
| 181 |
|
| 182 |
go func() { |
| 183 |
err := c.source.Run(pluginContext) |
| 184 |
if err != nil && !errors.Is(err, context.Canceled) { |
| 185 |
utilruntime.HandleError(fmt.Errorf("policy source context unexpectedly closed: %w", err)) |
| 186 |
} |
| 187 |
}() |
| 188 |
|
| 189 |
err := c.dispatcher.Start(pluginContext) |
| 190 |
if err != nil && !errors.Is(err, context.Canceled) { |
| 191 |
utilruntime.HandleError(fmt.Errorf("policy dispatcher context unexpectedly closed: %w", err)) |
| 192 |
} |
| 193 |
|
| 194 |
c.SetReadyFunc(func() bool { |
| 195 |
return namespaceInformer.Informer().HasSynced() && c.source.HasSynced() |
| 196 |
}) |
| 197 |
return nil |
| 198 |
} |
| 199 |
|
| 200 |
func (c *Plugin[H]) Dispatch( |
| 201 |
ctx context.Context, |
| 202 |
a admission.Attributes, |
| 203 |
o admission.ObjectInterfaces, |
| 204 |
) (err error) { |
| 205 |
if !c.enabled { |
| 206 |
return nil |
| 207 |
} else if c.shouldIgnoreResource(a) { |
| 208 |
return nil |
| 209 |
} else if !c.WaitForReady() { |
| 210 |
return admission.NewForbidden(a, fmt.Errorf("not yet ready to handle request")) |
| 211 |
} |
| 212 |
|
| 213 |
return c.dispatcher.Dispatch(ctx, a, o, c.source.Hooks()) |
| 214 |
} |
| 215 |
|
| 216 |
func (c *Plugin[H]) shouldIgnoreResource(attr admission.Attributes) bool { |
| 217 |
gvr := attr.GetResource() |
| 218 |
// exclusion decision ignores the version. |
| 219 |
gr := gvr.GroupResource() |
| 220 |
return c.excludedResources.Has(gr) |
| 221 |
} |