Skip to content

Commit c18958e

Browse files
author
Mengqi Yu
committed
address 2nd round comments
1 parent e3ce169 commit c18958e

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

example/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ func main() {
103103
as, err := webhook.NewServer("foo-admission-server", mgr, webhook.ServerOptions{
104104
Port: 443,
105105
CertDir: "/tmp/cert",
106-
//Client: mgr.GetClient(),
107-
KVMap: map[string]interface{}{"foo": "bar"},
106+
KVMap: map[string]interface{}{"foo": "bar"},
108107
BootstrapOptions: &webhook.BootstrapOptions{
109108
Secret: &apitypes.NamespacedName{
110109
Namespace: "default",

pkg/webhook/bootstrap.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ func (s *Server) setBootstrappingDefault() {
123123
}
124124
}
125125

126-
// bootstrap writes the configuration of admissionWebhookConfiguration in yaml format if dryrun is true.
126+
// installWebhookConfig writes the configuration of admissionWebhookConfiguration in yaml format if dryrun is true.
127127
// Otherwise, it creates the the admissionWebhookConfiguration objects and service if any.
128128
// It also provisions the certificate for the admission server.
129-
func (s *Server) bootstrap(dryrun bool) error {
129+
func (s *Server) installWebhookConfig() error {
130130
// do defaulting if necessary
131131
s.once.Do(s.setDefault)
132132
if s.err != nil {
@@ -149,13 +149,13 @@ func (s *Server) bootstrap(dryrun bool) error {
149149
_, err = s.certProvisioner.Provision(cert.Options{
150150
ClientConfig: cc,
151151
Objects: s.webhookConfigurations,
152-
Dryrun: dryrun,
152+
Dryrun: s.Dryrun,
153153
})
154154
if err != nil {
155155
return err
156156
}
157157

158-
if dryrun {
158+
if s.Dryrun {
159159
// TODO: print here
160160
// if dryrun, return the AdmissionWebhookConfiguration in yaml format.
161161
return s.genYamlConfig(objects)

pkg/webhook/internal/cert/provisioner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Options struct {
4545
ClientConfig *admissionregistrationv1beta1.WebhookClientConfig
4646
// Objects are the objects that will use the ClientConfig above.
4747
Objects []runtime.Object
48-
// Dryrun controls if the objects are sent to the API server or output in the io.Writer
48+
// Dryrun controls if the objects are sent to the API server or write to io.Writer
4949
Dryrun bool
5050
}
5151

pkg/webhook/server.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ type ServerOptions struct {
5858
// Client will be injected by the manager if not set.
5959
Client client.Client
6060

61+
// Dryrun controls if the server will install the webhookConfiguration.
62+
// If true, it will print the objects in yaml format.
63+
// If false, it will install the objects in the cluster.
64+
Dryrun bool
65+
6166
// BootstrapOptions contains the options for bootstrapping the admission server.
6267
*BootstrapOptions
6368
}
@@ -177,10 +182,12 @@ func (s *Server) Register(webhooks ...Webhook) error {
177182

178183
var _ manager.Runnable = &Server{}
179184

180-
// Start runs the server.
185+
// Start runs the server if s.Dryrun is false.
186+
// Otherwise, it will print the objects in yaml format.
181187
func (s *Server) Start(stop <-chan struct{}) error {
182-
err := s.bootstrap(false)
183-
if err != nil {
188+
err := s.installWebhookConfig()
189+
// if encounter an error or it's in dryrun mode, return.
190+
if err != nil || s.Dryrun {
184191
return err
185192
}
186193

@@ -190,21 +197,17 @@ func (s *Server) Start(stop <-chan struct{}) error {
190197
}
191198
errCh := make(chan error)
192199
serveFn := func() {
193-
err := srv.ListenAndServeTLS(path.Join(s.CertDir, writer.ServerCertName), path.Join(s.CertDir, writer.ServerKeyName))
194-
errCh <- err
200+
errCh <- srv.ListenAndServeTLS(path.Join(s.CertDir, writer.ServerCertName), path.Join(s.CertDir, writer.ServerKeyName))
195201
}
196202

197-
changed := true
203+
go serveFn()
198204
for {
199205
// TODO(mengqiy): add jitter to the timer
200206
// Could use https://godoc.org/k8s.io/apimachinery/pkg/util/wait#Jitter
201207
timer := time.Tick(6 * 30 * 24 * time.Hour)
202-
if changed {
203-
go serveFn()
204-
}
205208
select {
206209
case <-timer:
207-
changed, err = s.RefreshCert()
210+
changed, err := s.RefreshCert()
208211
if err != nil {
209212
log.Error(err, "encountering error when refreshing the certificate")
210213
return err
@@ -218,6 +221,7 @@ func (s *Server) Start(stop <-chan struct{}) error {
218221
log.Error(err, "encountering error when shutting down")
219222
return err
220223
}
224+
go serveFn()
221225
case <-stop:
222226
return nil
223227
case e := <-errCh:
@@ -226,11 +230,6 @@ func (s *Server) Start(stop <-chan struct{}) error {
226230
}
227231
}
228232

229-
// DryRun outputs k8s AdmissionWebhookConfiguration in yaml format instead of installing them to the APIServer.
230-
func (s *Server) DryRun() error {
231-
return s.bootstrap(true)
232-
}
233-
234233
// RefreshCert refreshes the certificate using Server's Provisioner if the certificate is expiring.
235234
func (s *Server) RefreshCert() (bool, error) {
236235
cc, err := s.getClientConfig()

0 commit comments

Comments
 (0)