Skip to content

Commit 6a27543

Browse files
author
Mengqi Yu
committed
address comments
1 parent 2970c77 commit 6a27543

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

pkg/webhook/admission.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"regexp"
2323
"strings"
24-
"sync"
2524

2625
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
2726
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -44,8 +43,6 @@ type admissionWebhook struct {
4443
// namespaceSelector maps to the namespaceSelector field in admissionregistrationv1beta1.admissionWebhook
4544
// This optional.
4645
namespaceSelector *metav1.LabelSelector
47-
48-
once sync.Once
4946
}
5047

5148
func (w *admissionWebhook) setDefaults() {
@@ -69,25 +66,22 @@ func (w *admissionWebhook) setDefaults() {
6966

7067
// GetName returns the name of the webhook.
7168
func (w *admissionWebhook) GetName() string {
72-
w.once.Do(w.setDefaults)
7369
return w.name
7470
}
7571

7672
// GetPath returns the path that the webhook registered.
7773
func (w *admissionWebhook) GetPath() string {
78-
w.once.Do(w.setDefaults)
74+
w.setDefaults()
7975
return w.path
8076
}
8177

8278
// GetType returns the type of the webhook.
8379
func (w *admissionWebhook) GetType() webhookType {
84-
w.once.Do(w.setDefaults)
8580
return w.t
8681
}
8782

8883
// Validate validates if the webhook is valid.
8984
func (w *admissionWebhook) Validate() error {
90-
w.once.Do(w.setDefaults)
9185
if len(w.rules) == 0 {
9286
return errors.New("field rules should not be empty")
9387
}

pkg/webhook/generator.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ func (o *generatorOptions) getClientConfig() (*admissionregistration.WebhookClie
122122
return nil, errors.New("URL and service can't be set at the same time")
123123
}
124124
cc := &admissionregistration.WebhookClientConfig{
125-
CABundle: []byte{},
125+
// Put an non-empty and not harmful CABundle here.
126+
// Not doing this will cause the field
127+
CABundle: []byte(`\n`),
126128
}
127129
if o.host != nil {
128130
u := url.URL{
@@ -224,7 +226,10 @@ func (o *generatorOptions) mutatingWHConfigs() (runtime.Object, error) {
224226
ObjectMeta: metav1.ObjectMeta{
225227
Name: o.mutatingWebhookConfigName,
226228
Annotations: map[string]string{
227-
"admissionwebhook.alpha.kubebuilder.io/ca-secret-name": "webhook-cert",
229+
// The format is "namespace/secret-name"
230+
// This annotation will be understood by cert-manager.
231+
// TODO(mengqiy): point to the section in kubebuilder book when everything is ready.
232+
"alpha.admissionwebhook.kubebuilder.io/ca-secret-name": o.secret.String(),
228233
},
229234
},
230235
Webhooks: mutatingWebhooks,
@@ -262,7 +267,10 @@ func (o *generatorOptions) validatingWHConfigs() (runtime.Object, error) {
262267
ObjectMeta: metav1.ObjectMeta{
263268
Name: o.validatingWebhookConfigName,
264269
Annotations: map[string]string{
265-
"admission.alpha.kubebuilder.io/ca-secret-name": "webhook-cert",
270+
// The format is "namespace/secret-name"
271+
// This annotation will be understood by cert-manager.
272+
// TODO(mengqiy): point to the section in kubebuilder book when everything is ready.
273+
"alpha.admissionwebhook.kubebuilder.io/ca-secret-name": o.secret.String(),
266274
},
267275
},
268276
Webhooks: validatingWebhooks,
@@ -288,11 +296,6 @@ func (o *generatorOptions) admissionWebhook(path string, wh *admissionWebhook) (
288296
Rules: wh.rules,
289297
FailurePolicy: wh.failurePolicy,
290298
NamespaceSelector: wh.namespaceSelector,
291-
ClientConfig: admissionregistration.WebhookClientConfig{
292-
// The reason why we assign an empty byte array to CABundle is that
293-
// CABundle field will be updated by the Provisioner.
294-
CABundle: []byte{},
295-
},
296299
}
297300
cc, err := o.getClientConfigWithPath(path)
298301
if err != nil {
@@ -316,7 +319,10 @@ func (o *generatorOptions) getService() runtime.Object {
316319
Name: o.service.name,
317320
Namespace: o.service.namespace,
318321
Annotations: map[string]string{
319-
"service.alpha.kubebuilder.io/serving-cert-secret-name": "webhook-cert",
322+
// Secret here only need name, since it will be in the same namespace as the service.
323+
// This annotation will be understood by cert-manager.
324+
// TODO(mengqiy): point to the section in kubebuilder book when everything is ready.
325+
"alpha.service.kubebuilder.io/serving-cert-secret-name": o.secret.Name,
320326
},
321327
},
322328
Spec: corev1.ServiceSpec{

pkg/webhook/generator_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ var expected = map[string]string{
2828
kind: MutatingWebhookConfiguration
2929
metadata:
3030
annotations:
31-
admissionwebhook.alpha.kubebuilder.io/ca-secret-name: webhook-cert
31+
alpha.admissionwebhook.kubebuilder.io/ca-secret-name: test-system/webhook-secret
3232
creationTimestamp: null
3333
name: test-mutating-webhook-cfg
3434
webhooks:
3535
- clientConfig:
36+
caBundle: XG4=
3637
service:
3738
name: webhook-service
3839
namespace: test-system
@@ -56,11 +57,12 @@ apiVersion: admissionregistration.k8s.io/v1beta1
5657
kind: ValidatingWebhookConfiguration
5758
metadata:
5859
annotations:
59-
admission.alpha.kubebuilder.io/ca-secret-name: webhook-cert
60+
alpha.admissionwebhook.kubebuilder.io/ca-secret-name: test-system/webhook-secret
6061
creationTimestamp: null
6162
name: test-validating-webhook-cfg
6263
webhooks:
6364
- clientConfig:
65+
caBundle: XG4=
6466
service:
6567
name: webhook-service
6668
namespace: test-system
@@ -85,7 +87,7 @@ apiVersion: v1
8587
kind: Service
8688
metadata:
8789
annotations:
88-
service.alpha.kubebuilder.io/serving-cert-secret-name: webhook-cert
90+
alpha.service.kubebuilder.io/serving-cert-secret-name: webhook-secret
8991
creationTimestamp: null
9092
name: webhook-service
9193
namespace: test-system
@@ -107,8 +109,9 @@ spec:
107109
metadata:
108110
labels:
109111
app: webhook-server
112+
spec:
110113
containers:
111-
- name: webhook-server-container
114+
- name: manager
112115
ports:
113116
- containerPort: 7890
114117
name: webhook-server

pkg/webhook/manifests.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ spec:
7474
labels:
7575
{{ toYaml . | indent 8 }}
7676
{{- end }}
77+
spec:
7778
containers:
78-
- name: webhook-server-container
79+
- name: manager
7980
ports:
8081
- containerPort: {{ .Port }}
8182
name: webhook-server

0 commit comments

Comments
 (0)