@@ -30,10 +30,10 @@ import (
30
30
"sync"
31
31
32
32
"k8s.io/apimachinery/pkg/runtime"
33
- "k8s.io/client-go/kubernetes/scheme"
33
+ kscheme "k8s.io/client-go/kubernetes/scheme"
34
34
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
35
35
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
36
- "sigs.k8s.io/controller-runtime/pkg/webhook/admission "
36
+ "sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics "
37
37
)
38
38
39
39
// DefaultPort is the default port that the webhook server serves.
@@ -105,67 +105,6 @@ func (s *Server) setDefaults() {
105
105
}
106
106
}
107
107
108
- // Options are the subset of fields on the controller that can be
109
- // configured when running an unmanaged webhook server (i.e. webhook.NewUnmanaged())
110
- type Options struct {
111
- // Host is the address that the server will listen on.
112
- // Defaults to "" - all addresses.
113
- Host string
114
-
115
- // Port is the port number that the server will serve.
116
- // It will be defaulted to 9443 if unspecified.
117
- Port int
118
-
119
- // CertDir is the directory that contains the server key and certificate. The
120
- // server key and certificate.
121
- CertDir string
122
-
123
- // CertName is the server certificate name. Defaults to tls.crt.
124
- CertName string
125
-
126
- // KeyName is the server key name. Defaults to tls.key.
127
- KeyName string
128
-
129
- // ClientCAName is the CA certificate name which server used to verify remote(client)'s certificate.
130
- // Defaults to "", which means server does not verify client's certificate.
131
- ClientCAName string
132
-
133
- // WebhookMux is the multiplexer that handles different webhooks.
134
- WebhookMux * http.ServeMux
135
-
136
- // Scheme is the scheme used to resolve runtime.Objects to GroupVersionKinds / Resources
137
- // Defaults to the kubernetes/client-go scheme.Scheme, but it's almost always better
138
- // idea to pass your own scheme in. See the documentation in pkg/scheme for more information.
139
- Scheme * runtime.Scheme
140
- }
141
-
142
- // NewUnmanaged provides a webhook server that can be ran without
143
- // a controller manager.
144
- func NewUnmanaged (options Options ) (* Server , error ) {
145
- server := & Server {
146
- Host : options .Host ,
147
- Port : options .Port ,
148
- CertDir : options .CertDir ,
149
- CertName : options .CertName ,
150
- KeyName : options .KeyName ,
151
- WebhookMux : options .WebhookMux ,
152
- }
153
- server .setDefaults ()
154
- // Use the Kubernetes client-go scheme if none is specified
155
- if options .Scheme == nil {
156
- options .Scheme = scheme .Scheme
157
- }
158
-
159
- // TODO: can we do this without dep injection?
160
- server .InjectFunc (func (i interface {}) error {
161
- if _ , err := inject .SchemeInto (options .Scheme , i ); err != nil {
162
- return err
163
- }
164
- return nil
165
- })
166
- return server , nil
167
- }
168
-
169
108
// NeedLeaderElection implements the LeaderElectionRunnable interface, which indicates
170
109
// the webhook server doesn't need leader election.
171
110
func (* Server ) NeedLeaderElection () bool {
@@ -185,7 +124,7 @@ func (s *Server) Register(path string, hook http.Handler) {
185
124
}
186
125
// TODO(directxman12): call setfields if we've already started the server
187
126
s .webhooks [path ] = hook
188
- s .WebhookMux .Handle (path , admission .InstrumentedHook (path , hook ))
127
+ s .WebhookMux .Handle (path , metrics .InstrumentedHook (path , hook ))
189
128
190
129
regLog := log .WithValues ("path" , path )
191
130
regLog .Info ("registering webhook" )
@@ -210,6 +149,26 @@ func (s *Server) Register(path string, hook http.Handler) {
210
149
}
211
150
}
212
151
152
+ // StartStandalone runs a webhook server without
153
+ // a controller manager.
154
+ func (s * Server ) StartStandalone (ctx context.Context , scheme * runtime.Scheme ) error {
155
+ // Use the Kubernetes client-go scheme if none is specified
156
+ if scheme == nil {
157
+ scheme = kscheme .Scheme
158
+ }
159
+
160
+ if err := s .InjectFunc (func (i interface {}) error {
161
+ if _ , err := inject .SchemeInto (scheme , i ); err != nil {
162
+ return err
163
+ }
164
+ return nil
165
+ }); err != nil {
166
+ return err
167
+ }
168
+
169
+ return s .Start (ctx )
170
+ }
171
+
213
172
// Start runs the server.
214
173
// It will install the webhook related resources depend on the server configuration.
215
174
func (s * Server ) Start (ctx context.Context ) error {
0 commit comments