Skip to content

Commit 03c3f1b

Browse files
author
Mengqi Yu
committed
move webhook self installer to CT as generator
1 parent 2027a41 commit 03c3f1b

36 files changed

+325
-4736
lines changed

Gopkg.lock

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/main.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ import (
2020
"flag"
2121
"os"
2222

23-
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
2423
appsv1 "k8s.io/api/apps/v1"
2524
corev1 "k8s.io/api/core/v1"
26-
apitypes "k8s.io/apimachinery/pkg/types"
2725
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2826
"sigs.k8s.io/controller-runtime/pkg/client/config"
2927
"sigs.k8s.io/controller-runtime/pkg/controller"
3028
"sigs.k8s.io/controller-runtime/pkg/handler"
31-
"sigs.k8s.io/controller-runtime/pkg/manager"
3229
logf "sigs.k8s.io/controller-runtime/pkg/log"
3330
"sigs.k8s.io/controller-runtime/pkg/log/zap"
31+
"sigs.k8s.io/controller-runtime/pkg/manager"
3432
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
3533
"sigs.k8s.io/controller-runtime/pkg/source"
3634
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -84,9 +82,6 @@ func main() {
8482
mutatingWebhook, err := builder.NewWebhookBuilder().
8583
Name("mutating.k8s.io").
8684
Mutating().
87-
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
88-
WithManager(mgr).
89-
ForType(&corev1.Pod{}).
9085
Handlers(&podAnnotator{}).
9186
Build()
9287
if err != nil {
@@ -97,9 +92,6 @@ func main() {
9792
validatingWebhook, err := builder.NewWebhookBuilder().
9893
Name("validating.k8s.io").
9994
Validating().
100-
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
101-
WithManager(mgr).
102-
ForType(&corev1.Pod{}).
10395
Handlers(&podValidator{}).
10496
Build()
10597
if err != nil {
@@ -109,24 +101,8 @@ func main() {
109101

110102
entryLog.Info("setting up webhook server")
111103
as, err := webhook.NewServer("foo-admission-server", mgr, webhook.ServerOptions{
112-
Port: 9876,
113-
CertDir: "/tmp/cert",
114-
DisableWebhookConfigInstaller: &disableWebhookConfigInstaller,
115-
BootstrapOptions: &webhook.BootstrapOptions{
116-
Secret: &apitypes.NamespacedName{
117-
Namespace: "default",
118-
Name: "foo-admission-server-secret",
119-
},
120-
121-
Service: &webhook.Service{
122-
Namespace: "default",
123-
Name: "foo-admission-server-service",
124-
// Selectors should select the pods that runs this webhook server.
125-
Selectors: map[string]string{
126-
"app": "foo-admission-server",
127-
},
128-
},
129-
},
104+
Port: 9876,
105+
CertDir: "/tmp/cert",
130106
})
131107
if err != nil {
132108
entryLog.Error(err, "unable to create a new webhook server")

pkg/webhook/internal/cert/generator/certgenerator_test.go renamed to pkg/internal/webhookgenerator/admission/doc.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,4 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package generator
18-
19-
import "fmt"
20-
21-
func ExampleServiceToCommonName() {
22-
fmt.Println(ServiceToCommonName("myservicenamespace", "myservicename"))
23-
// Output: myservicename.myservicenamespace.svc
24-
}
17+
package admission
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2018 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+
"errors"
21+
"fmt"
22+
"regexp"
23+
"strings"
24+
"sync"
25+
26+
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"sigs.k8s.io/controller-runtime/pkg/internal/webhookgenerator/types"
29+
)
30+
31+
// Webhook represents each individual webhook.
32+
type Webhook struct {
33+
// Name is the name of the webhook
34+
Name string
35+
// Type is the webhook type, i.e. mutating, validating
36+
Type types.WebhookType
37+
// Path is the path this webhook will serve.
38+
Path string
39+
// Rules maps to the Rules field in admissionregistrationv1beta1.Webhook
40+
Rules []admissionregistrationv1beta1.RuleWithOperations
41+
// FailurePolicy maps to the FailurePolicy field in admissionregistrationv1beta1.Webhook
42+
// This optional. If not set, will be defaulted to Ignore (fail-open) by the server.
43+
// More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147
44+
FailurePolicy *admissionregistrationv1beta1.FailurePolicyType
45+
// NamespaceSelector maps to the NamespaceSelector field in admissionregistrationv1beta1.Webhook
46+
// This optional.
47+
NamespaceSelector *metav1.LabelSelector
48+
49+
once sync.Once
50+
}
51+
52+
func (w *Webhook) setDefaults() {
53+
if len(w.Path) == 0 {
54+
if len(w.Rules) == 0 || len(w.Rules[0].Resources) == 0 {
55+
// can't do defaulting, skip it.
56+
return
57+
}
58+
if w.Type == types.WebhookTypeMutating {
59+
w.Path = "/mutate-" + w.Rules[0].Resources[0]
60+
} else if w.Type == types.WebhookTypeValidating {
61+
w.Path = "/validate-" + w.Rules[0].Resources[0]
62+
}
63+
}
64+
if len(w.Name) == 0 {
65+
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
66+
processedPath := strings.ToLower(reg.ReplaceAllString(w.Path, ""))
67+
w.Name = processedPath + ".example.com"
68+
}
69+
}
70+
71+
// GetName returns the name of the webhook.
72+
func (w *Webhook) GetName() string {
73+
w.once.Do(w.setDefaults)
74+
return w.Name
75+
}
76+
77+
// GetPath returns the path that the webhook registered.
78+
func (w *Webhook) GetPath() string {
79+
w.once.Do(w.setDefaults)
80+
return w.Path
81+
}
82+
83+
// GetType returns the type of the webhook.
84+
func (w *Webhook) GetType() types.WebhookType {
85+
w.once.Do(w.setDefaults)
86+
return w.Type
87+
}
88+
89+
// Validate validates if the webhook is valid.
90+
func (w *Webhook) Validate() error {
91+
w.once.Do(w.setDefaults)
92+
if len(w.Rules) == 0 {
93+
return errors.New("field Rules should not be empty")
94+
}
95+
if len(w.Name) == 0 {
96+
return errors.New("field Name should not be empty")
97+
}
98+
if w.Type != types.WebhookTypeMutating && w.Type != types.WebhookTypeValidating {
99+
return fmt.Errorf("unsupported Type: %v, only WebhookTypeMutating and WebhookTypeValidating are supported", w.Type)
100+
}
101+
if len(w.Path) == 0 {
102+
return errors.New("field Path should not be empty")
103+
}
104+
return nil
105+
}

pkg/webhook/internal/cert/generator/doc.go renamed to pkg/internal/webhookgenerator/doc.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,4 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
/*
18-
Package generator provides an interface and implementation to provision certificates.
19-
20-
Create an instance of certGenerator.
21-
22-
cg := SelfSignedCertGenerator{}
23-
24-
Generate the certificates.
25-
certs, err := cg.Generate("foo.bar.com")
26-
if err != nil {
27-
// handle error
28-
}
29-
*/
30-
package generator
17+
package webhook

0 commit comments

Comments
 (0)