-
Notifications
You must be signed in to change notification settings - Fork 462
/
shoot.go
85 lines (70 loc) · 2.82 KB
/
shoot.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package shoot
import (
"fmt"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
)
const (
// WebhookName is the name of the shoot webhook.
WebhookName = "shoot"
// KindSystem is used for webhooks which should only apply to the to the kube-system namespace.
KindSystem = "system"
)
var logger = log.Log.WithName("shoot-webhook")
// Args are arguments for creating a webhook targeting a shoot.
type Args struct {
// Types is a list of resource types.
Types []extensionswebhook.Type
// Mutator is a mutator to be used by the admission handler. It doesn't need the shoot client.
Mutator extensionswebhook.Mutator
// MutatorWithShootClient is a mutator to be used by the admission handler. It needs the shoot client.
MutatorWithShootClient extensionswebhook.MutatorWithShootClient
// FailurePolicy is the failure policy for the webhook (defaults to Ignore).
FailurePolicy *admissionregistrationv1.FailurePolicyType
}
// New creates a new webhook with the shoot as target cluster.
func New(mgr manager.Manager, args Args) (*extensionswebhook.Webhook, error) {
logger.Info("Creating webhook", "name", WebhookName)
wh := &extensionswebhook.Webhook{
Name: WebhookName,
Types: args.Types,
Path: WebhookName,
Target: extensionswebhook.TargetShoot,
NamespaceSelector: buildSelector(),
FailurePolicy: args.FailurePolicy,
}
switch {
case args.Mutator != nil:
handler, err := extensionswebhook.NewBuilder(mgr, logger).WithMutator(args.Mutator, args.Types...).Build()
if err != nil {
return nil, err
}
wh.Webhook = &admission.Webhook{Handler: handler, RecoverPanic: true}
return wh, nil
case args.MutatorWithShootClient != nil:
handler, err := extensionswebhook.NewHandlerWithShootClient(mgr, args.Types, args.MutatorWithShootClient, logger)
if err != nil {
return nil, err
}
wh.Handler = handler
return wh, nil
}
return nil, fmt.Errorf("neither mutator nor mutator with shoot client is set")
}
// buildSelector creates and returns a LabelSelector for the given webhook kind and provider.
func buildSelector() *metav1.LabelSelector {
// Create and return LabelSelector
return &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: v1beta1constants.GardenerPurpose, Operator: metav1.LabelSelectorOpIn, Values: []string{metav1.NamespaceSystem}},
},
}
}