Skip to content

Commit 29feb54

Browse files
author
Mengqi Yu
committed
support disable installer
1 parent f3e97ad commit 29feb54

File tree

4 files changed

+49
-61
lines changed

4 files changed

+49
-61
lines changed

example/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,24 @@ import (
3939
var log = logf.Log.WithName("example-controller")
4040

4141
func main() {
42+
var enableInstaller bool
43+
flag.BoolVar(&enableInstaller, "enable-installer", false,
44+
"enable the installer in the webhook server, so it will install webhook related resources during bootstrapping")
45+
4246
flag.Parse()
4347
logf.SetLogger(logf.ZapLogger(false))
4448
entryLog := log.WithName("entrypoint")
4549

4650
// Setup a Manager
51+
entryLog.Info("setting up manager")
4752
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
4853
if err != nil {
4954
entryLog.Error(err, "unable to set up overall controller manager")
5055
os.Exit(1)
5156
}
5257

5358
// Setup a new controller to Reconciler ReplicaSets
59+
entryLog.Info("Setting up controller")
5460
c, err := controller.New("foo-controller", mgr, controller.Options{
5561
Reconciler: &reconcileReplicaSet{client: mgr.GetClient(), log: log.WithName("reconciler")},
5662
})
@@ -73,6 +79,7 @@ func main() {
7379
}
7480

7581
// Setup webhooks
82+
entryLog.Info("setting up webhooks")
7683
mutatingWebhook, err := builder.NewWebhookBuilder().
7784
Name("mutating.k8s.io").
7885
Mutating().
@@ -99,9 +106,11 @@ func main() {
99106
os.Exit(1)
100107
}
101108

109+
entryLog.Info("setting up webhook server")
102110
as, err := webhook.NewServer("foo-admission-server", mgr, webhook.ServerOptions{
103-
Port: 9876,
104-
CertDir: "/tmp/cert",
111+
Port: 9876,
112+
CertDir: "/tmp/cert",
113+
EnableInstaller: enableInstaller,
105114
BootstrapOptions: &webhook.BootstrapOptions{
106115
Secret: &apitypes.NamespacedName{
107116
Namespace: "default",
@@ -122,12 +131,15 @@ func main() {
122131
entryLog.Error(err, "unable to create a new webhook server")
123132
os.Exit(1)
124133
}
134+
135+
entryLog.Info("registering webhooks to the webhook server")
125136
err = as.Register(mutatingWebhook, validatingWebhook)
126137
if err != nil {
127138
entryLog.Error(err, "unable to register webhooks in the admission server")
128139
os.Exit(1)
129140
}
130141

142+
entryLog.Info("starting manager")
131143
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
132144
entryLog.Error(err, "unable to run manager")
133145
os.Exit(1)

pkg/webhook/bootstrap.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ import (
2222
"net"
2323
"net/http"
2424
"net/url"
25-
"os"
2625
"path"
2726
"strconv"
2827

29-
"github.com/ghodss/yaml"
30-
3128
"k8s.io/api/admissionregistration/v1beta1"
3229
admissionregistration "k8s.io/api/admissionregistration/v1beta1"
3330
corev1 "k8s.io/api/core/v1"
@@ -114,15 +111,11 @@ func (s *Server) setBootstrappingDefault() {
114111
s.certProvisioner = &cert.Provisioner{
115112
CertWriter: certWriter,
116113
}
117-
if s.Writer == nil {
118-
s.Writer = os.Stdout
119-
}
120114
}
121115

122-
// installWebhookConfig writes the configuration of admissionWebhookConfiguration in yaml format if dryrun is true.
123-
// Otherwise, it creates the the admissionWebhookConfiguration objects and service if any.
116+
// InstallWebhookManifests creates the admissionWebhookConfiguration objects and service if any.
124117
// It also provisions the certificate for the admission server.
125-
func (s *Server) installWebhookConfig() error {
118+
func (s *Server) InstallWebhookManifests() error {
126119
// do defaulting if necessary
127120
s.once.Do(s.setDefault)
128121
if s.err != nil {
@@ -145,40 +138,14 @@ func (s *Server) installWebhookConfig() error {
145138
_, err = s.certProvisioner.Provision(cert.Options{
146139
ClientConfig: cc,
147140
Objects: s.webhookConfigurations,
148-
Dryrun: s.Dryrun,
149141
})
150142
if err != nil {
151143
return err
152144
}
153145

154-
if s.Dryrun {
155-
// TODO: print here
156-
// if dryrun, return the AdmissionWebhookConfiguration in yaml format.
157-
return s.genYamlConfig(objects)
158-
}
159-
160146
return batchCreateOrReplace(s.Client, objects...)
161147
}
162148

163-
// genYamlConfig generates yaml config for admissionWebhookConfiguration
164-
func (s *Server) genYamlConfig(objs []runtime.Object) error {
165-
for _, obj := range objs {
166-
_, err := s.Writer.Write([]byte("---"))
167-
if err != nil {
168-
return err
169-
}
170-
b, err := yaml.Marshal(obj)
171-
if err != nil {
172-
return err
173-
}
174-
_, err = s.Writer.Write(b)
175-
if err != nil {
176-
return err
177-
}
178-
}
179-
return nil
180-
}
181-
182149
func (s *Server) getClientConfig() (*admissionregistration.WebhookClientConfig, error) {
183150
if s.Host != nil && s.Service != nil {
184151
return nil, errors.New("URL and Service can't be set at the same time")

pkg/webhook/server.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ type ServerOptions struct {
5454
// Client will be injected by the manager if not set.
5555
Client client.Client
5656

57-
// Dryrun controls if the server will install the webhookConfiguration and service if any.
58-
// If true, it will print the objects in yaml format.
59-
// If false, it will install the objects in the cluster.
60-
Dryrun bool
57+
// EnableInstaller controls if the server will automatically create webhook related objects
58+
// during bootstrapping. e.g. webhookConfiguration, service and secret.
59+
EnableInstaller bool
6160

6261
// BootstrapOptions contains the options for bootstrapping the admission server.
6362
*BootstrapOptions
@@ -75,7 +74,8 @@ type BootstrapOptions struct {
7574
// This is optional. If unspecified, it will write to the filesystem.
7675
// It the secret already exists and is different from the desired, it will be replaced.
7776
Secret *apitypes.NamespacedName
78-
// Writer is used in dryrun mode for writing the objects in yaml format.
77+
78+
// Deprecated: Writer will not be used anywhere.
7979
Writer io.Writer
8080

8181
// Service is k8s service fronting the webhook server pod(s).
@@ -187,13 +187,17 @@ func (s *Server) Handle(pattern string, handler http.Handler) {
187187

188188
var _ manager.Runnable = &Server{}
189189

190-
// Start runs the server if s.Dryrun is false.
191-
// Otherwise, it will print the objects in yaml format.
190+
// Start runs the server.
191+
// It will install the webhook related resources depend on the server configuration.
192192
func (s *Server) Start(stop <-chan struct{}) error {
193-
err := s.installWebhookConfig()
194-
// if encounter an error or it's in dryrun mode, return.
195-
if err != nil || s.Dryrun {
196-
return err
193+
if s.EnableInstaller {
194+
log.Info("webhook installer is enabled")
195+
err := s.InstallWebhookManifests()
196+
if err != nil {
197+
return err
198+
}
199+
} else {
200+
log.Info("webhook installer is disabled")
197201
}
198202

199203
srv := &http.Server{

pkg/webhook/util.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package webhook
1818

1919
import (
2020
"context"
21-
"fmt"
2221

2322
admissionregistration "k8s.io/api/admissionregistration/v1beta1"
2423
corev1 "k8s.io/api/core/v1"
@@ -27,29 +26,34 @@ import (
2726
"sigs.k8s.io/controller-runtime/pkg/client"
2827
)
2928

30-
type mutateFn func(current, desired runtime.Object) error
29+
type mutateFn func(current, desired *runtime.Object) error
3130

32-
var serviceFn = func(current, desired runtime.Object) error {
33-
typedC := current.(*corev1.Service)
34-
typedD := desired.(*corev1.Service)
31+
var serviceFn = func(current, desired *runtime.Object) error {
32+
typedC := (*current).(*corev1.Service)
33+
typedD := (*desired).(*corev1.Service)
3534
typedC.Spec.Selector = typedD.Spec.Selector
3635
return nil
3736
}
3837

39-
var mutatingWebhookConfigFn = func(current, desired runtime.Object) error {
40-
typedC := current.(*admissionregistration.MutatingWebhookConfiguration)
41-
typedD := desired.(*admissionregistration.MutatingWebhookConfiguration)
38+
var mutatingWebhookConfigFn = func(current, desired *runtime.Object) error {
39+
typedC := (*current).(*admissionregistration.MutatingWebhookConfiguration)
40+
typedD := (*desired).(*admissionregistration.MutatingWebhookConfiguration)
4241
typedC.Webhooks = typedD.Webhooks
4342
return nil
4443
}
4544

46-
var validatingWebhookConfigFn = func(current, desired runtime.Object) error {
47-
typedC := current.(*admissionregistration.ValidatingWebhookConfiguration)
48-
typedD := desired.(*admissionregistration.ValidatingWebhookConfiguration)
45+
var validatingWebhookConfigFn = func(current, desired *runtime.Object) error {
46+
typedC := (*current).(*admissionregistration.ValidatingWebhookConfiguration)
47+
typedD := (*desired).(*admissionregistration.ValidatingWebhookConfiguration)
4948
typedC.Webhooks = typedD.Webhooks
5049
return nil
5150
}
5251

52+
var genericFn = func(current, desired *runtime.Object) error {
53+
*current = *desired
54+
return nil
55+
}
56+
5357
// createOrReplaceHelper creates the object if it doesn't exist;
5458
// otherwise, it will replace it.
5559
// When replacing, fn should know how to preserve existing fields in the object GET from the APIServer.
@@ -70,7 +74,7 @@ func createOrReplaceHelper(c client.Client, obj runtime.Object, fn mutateFn) err
7074
if err != nil {
7175
return err
7276
}
73-
err = fn(existing, obj)
77+
err = fn(&existing, &obj)
7478
if err != nil {
7579
return err
7680
}
@@ -83,6 +87,7 @@ func createOrReplaceHelper(c client.Client, obj runtime.Object, fn mutateFn) err
8387
// otherwise, it will replace it.
8488
// When replacing, it knows how to preserve existing fields in the object GET from the APIServer.
8589
// It currently only support MutatingWebhookConfiguration, ValidatingWebhookConfiguration and Service.
90+
// For other kinds, it uses genericFn to replace the whole object.
8691
func createOrReplace(c client.Client, obj runtime.Object) error {
8792
if obj == nil {
8893
return nil
@@ -95,7 +100,7 @@ func createOrReplace(c client.Client, obj runtime.Object) error {
95100
case *corev1.Service:
96101
return createOrReplaceHelper(c, obj, serviceFn)
97102
default:
98-
return fmt.Errorf("unsupported GroupVersionKind: %#v", obj.GetObjectKind().GroupVersionKind())
103+
return createOrReplaceHelper(c, obj, genericFn)
99104
}
100105
}
101106

0 commit comments

Comments
 (0)