/
registration.go
191 lines (165 loc) · 6.98 KB
/
registration.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
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
// Copyright (c) 2019 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 webhook
import (
"context"
"fmt"
"strings"
"github.com/gardener/gardener/pkg/controllerutils"
"github.com/gardener/gardener/pkg/utils/kubernetes"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
const (
// NamePrefix is the prefix used for {Valida,Muta}tingWebhookConfigurations of extensions.
NamePrefix = "gardener-extension-"
// NameSuffixShoot is the suffix used for {Valida,Muta}tingWebhookConfigurations of extensions targeting a shoot.
NameSuffixShoot = "-shoot"
)
// RegisterWebhooks registers the given webhooks in the Kubernetes cluster targeted by the provided manager.
func RegisterWebhooks(ctx context.Context, mgr manager.Manager, namespace, providerName string, servicePort int, mode, url string, caBundle []byte, webhooks []*Webhook) (webhooksToRegisterSeed []admissionregistrationv1.MutatingWebhook, webhooksToRegisterShoot []admissionregistrationv1.MutatingWebhook, err error) {
var (
exact = admissionregistrationv1.Exact
mutatingWebhookConfigurationSeed = &admissionregistrationv1.MutatingWebhookConfiguration{ObjectMeta: metav1.ObjectMeta{Name: NamePrefix + providerName}}
)
for _, webhook := range webhooks {
var (
rules []admissionregistrationv1.RuleWithOperations
sideEffects = admissionregistrationv1.SideEffectClassNone
)
for _, t := range webhook.Types {
rule, err := buildRule(mgr, t)
if err != nil {
return nil, nil, err
}
rules = append(rules, *rule)
}
webhookToRegister := admissionregistrationv1.MutatingWebhook{
AdmissionReviewVersions: []string{"v1", "v1beta1"},
Name: fmt.Sprintf("%s.%s.extensions.gardener.cloud", webhook.Name, strings.TrimPrefix(providerName, "provider-")),
NamespaceSelector: webhook.Selector,
ObjectSelector: webhook.ObjectSelector,
Rules: rules,
SideEffects: &sideEffects,
TimeoutSeconds: pointer.Int32(10),
}
if webhook.TimeoutSeconds != nil {
webhookToRegister.TimeoutSeconds = webhook.TimeoutSeconds
}
shootMode := ModeURLWithServiceName
if mode == ModeURL {
shootMode = ModeURL
}
switch webhook.Target {
case TargetSeed:
webhookToRegister.FailurePolicy = getFailurePolicy(admissionregistrationv1.Fail, webhook.FailurePolicy)
webhookToRegister.MatchPolicy = &exact
webhookToRegister.ClientConfig = buildClientConfigFor(webhook, namespace, providerName, servicePort, mode, url, caBundle)
webhooksToRegisterSeed = append(webhooksToRegisterSeed, webhookToRegister)
case TargetShoot:
webhookToRegister.FailurePolicy = getFailurePolicy(admissionregistrationv1.Ignore, webhook.FailurePolicy)
webhookToRegister.MatchPolicy = &exact
webhookToRegister.ClientConfig = buildClientConfigFor(webhook, namespace, providerName, servicePort, shootMode, url, caBundle)
webhooksToRegisterShoot = append(webhooksToRegisterShoot, webhookToRegister)
default:
return nil, nil, fmt.Errorf("invalid webhook target: %s", webhook.Target)
}
}
if len(webhooksToRegisterSeed) > 0 {
c, err := getClient(mgr)
if err != nil {
return nil, nil, err
}
var ownerReference *metav1.OwnerReference
if len(namespace) > 0 {
ns := &corev1.Namespace{}
if err := c.Get(ctx, client.ObjectKey{Name: namespace}, ns); err != nil {
return nil, nil, err
}
ownerReference = metav1.NewControllerRef(ns, corev1.SchemeGroupVersion.WithKind("Namespace"))
ownerReference.BlockOwnerDeletion = pointer.Bool(false)
}
if _, err := controllerutils.GetAndCreateOrStrategicMergePatch(ctx, c, mutatingWebhookConfigurationSeed, func() error {
if ownerReference != nil {
mutatingWebhookConfigurationSeed.SetOwnerReferences(kubernetes.MergeOwnerReferences(mutatingWebhookConfigurationSeed.GetOwnerReferences(), *ownerReference))
}
mutatingWebhookConfigurationSeed.Webhooks = webhooksToRegisterSeed
return nil
}); err != nil {
return nil, nil, err
}
}
return webhooksToRegisterSeed, webhooksToRegisterShoot, nil
}
func getFailurePolicy(def admissionregistrationv1.FailurePolicyType, overwrite *admissionregistrationv1.FailurePolicyType) *admissionregistrationv1.FailurePolicyType {
if overwrite != nil {
return overwrite
}
return &def
}
// buildRule creates and returns a RuleWithOperations for the given object type.
func buildRule(mgr manager.Manager, t Type) (*admissionregistrationv1.RuleWithOperations, error) {
// Get GVK from the type
gvk, err := apiutil.GVKForObject(t.Obj, mgr.GetScheme())
if err != nil {
return nil, fmt.Errorf("could not get GroupVersionKind from object %v: %w", t.Obj, err)
}
// Get REST mapping from GVK
mapping, err := mgr.GetRESTMapper().RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("could not get REST mapping from GroupVersionKind '%s': %w", gvk.String(), err)
}
resource := mapping.Resource.Resource
if t.Subresource != nil {
resource += fmt.Sprintf("/%s", *t.Subresource)
}
// Create and return RuleWithOperations
return &admissionregistrationv1.RuleWithOperations{
Operations: []admissionregistrationv1.OperationType{
admissionregistrationv1.Create,
admissionregistrationv1.Update,
},
Rule: admissionregistrationv1.Rule{
APIGroups: []string{gvk.Group},
APIVersions: []string{gvk.Version},
Resources: []string{resource},
},
}, nil
}
func buildClientConfigFor(webhook *Webhook, namespace, providerName string, servicePort int, mode, url string, caBundle []byte) admissionregistrationv1.WebhookClientConfig {
path := "/" + webhook.Path
clientConfig := admissionregistrationv1.WebhookClientConfig{
CABundle: caBundle,
}
switch mode {
case ModeURL:
url := fmt.Sprintf("https://%s%s", url, path)
clientConfig.URL = &url
case ModeURLWithServiceName:
url := fmt.Sprintf("https://gardener-extension-%s.%s:%d%s", providerName, namespace, servicePort, path)
clientConfig.URL = &url
case ModeService:
clientConfig.Service = &admissionregistrationv1.ServiceReference{
Namespace: namespace,
Name: "gardener-extension-" + providerName,
Path: &path,
}
}
return clientConfig
}