Skip to content

Commit 2596de1

Browse files
authored
Merge pull request #2422 from sbueringer/pr-deprecate-opts
⚠ Remove deprecated manager, webhook and cluster options
2 parents cc1862e + e92eadb commit 2596de1

File tree

7 files changed

+61
-218
lines changed

7 files changed

+61
-218
lines changed

pkg/cluster/cluster.go

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ import (
2828
"k8s.io/client-go/kubernetes/scheme"
2929
"k8s.io/client-go/rest"
3030
"k8s.io/client-go/tools/record"
31-
"k8s.io/utils/pointer"
32-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
33-
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3431

3532
"sigs.k8s.io/controller-runtime/pkg/cache"
3633
"sigs.k8s.io/controller-runtime/pkg/client"
34+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
35+
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3736
intrec "sigs.k8s.io/controller-runtime/pkg/internal/recorder"
3837
)
3938

@@ -95,17 +94,9 @@ type Options struct {
9594
// value only if you know what you are doing. Defaults to 10 hours if unset.
9695
// there will a 10 percent jitter between the SyncPeriod of all controllers
9796
// so that all controllers will not send list requests simultaneously.
98-
SyncPeriod *time.Duration
99-
100-
// Namespace if specified restricts the manager's cache to watch objects in
101-
// the desired namespace Defaults to all namespaces
10297
//
103-
// Note: If a namespace is specified, controllers can still Watch for a
104-
// cluster-scoped resource (e.g Node). For namespaced resources the cache
105-
// will only hold objects from the desired namespace.
106-
//
107-
// Deprecated: Use Cache.Namespaces instead.
108-
Namespace string
98+
// Deprecated: Use Cache.SyncPeriod instead.
99+
SyncPeriod *time.Duration
109100

110101
// HTTPClient is the http client that will be used to create the default
111102
// Cache and Client. If not set the rest.HTTPClientFor function will be used
@@ -141,18 +132,6 @@ type Options struct {
141132
// Only use a custom NewClient if you know what you are doing.
142133
NewClient client.NewClientFunc
143134

144-
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
145-
// for the given objects.
146-
//
147-
// Deprecated: Use Client.Cache.DisableFor instead.
148-
ClientDisableCacheFor []client.Object
149-
150-
// DryRunClient specifies whether the client should be configured to enforce
151-
// dryRun mode.
152-
//
153-
// Deprecated: Use Client.DryRun instead.
154-
DryRunClient bool
155-
156135
// EventBroadcaster records Events emitted by the manager and sends them to the Kubernetes API
157136
// Use this to customize the event correlator and spam filter
158137
//
@@ -211,9 +190,6 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
211190
if cacheOpts.SyncPeriod == nil {
212191
cacheOpts.SyncPeriod = options.SyncPeriod
213192
}
214-
if len(cacheOpts.Namespaces) == 0 && options.Namespace != "" {
215-
cacheOpts.Namespaces = []string{options.Namespace}
216-
}
217193
}
218194
cache, err := options.NewCache(config, cacheOpts)
219195
if err != nil {
@@ -240,16 +216,6 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
240216
if clientOpts.Cache.Reader == nil {
241217
clientOpts.Cache.Reader = cache
242218
}
243-
244-
// For backward compatibility, the ClientDisableCacheFor option should
245-
// be appended to the DisableFor option in the client.
246-
clientOpts.Cache.DisableFor = append(clientOpts.Cache.DisableFor, options.ClientDisableCacheFor...)
247-
248-
if clientOpts.DryRun == nil && options.DryRunClient {
249-
// For backward compatibility, the DryRunClient (if set) option should override
250-
// the DryRun option in the client (if unset).
251-
clientOpts.DryRun = pointer.Bool(true)
252-
}
253219
}
254220
clientWriter, err := options.NewClient(config, clientOpts)
255221
if err != nil {

pkg/envtest/webhook_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
corev1 "k8s.io/api/core/v1"
3030
apierrors "k8s.io/apimachinery/pkg/api/errors"
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
3233
"sigs.k8s.io/controller-runtime/pkg/client"
3334
"sigs.k8s.io/controller-runtime/pkg/manager"
3435
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -40,12 +41,12 @@ var _ = Describe("Test", func() {
4041
Describe("Webhook", func() {
4142
It("should reject create request for webhook that rejects all requests", func() {
4243
m, err := manager.New(env.Config, manager.Options{
43-
Port: env.WebhookInstallOptions.LocalServingPort,
44-
Host: env.WebhookInstallOptions.LocalServingHost,
45-
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
46-
TLSOpts: []func(*tls.Config){
47-
func(config *tls.Config) {},
48-
},
44+
WebhookServer: webhook.NewServer(webhook.Options{
45+
Port: env.WebhookInstallOptions.LocalServingPort,
46+
Host: env.WebhookInstallOptions.LocalServingHost,
47+
CertDir: env.WebhookInstallOptions.LocalServingCertDir,
48+
TLSOpts: []func(*tls.Config){func(config *tls.Config) {}},
49+
}),
4950
}) // we need manager here just to leverage manager.SetFields
5051
Expect(err).NotTo(HaveOccurred())
5152
server := m.GetWebhookServer()

pkg/manager/manager.go

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

1919
import (
2020
"context"
21-
"crypto/tls"
2221
"fmt"
2322
"net"
2423
"net/http"
@@ -140,35 +139,6 @@ type Options struct {
140139
// Only use a custom NewClient if you know what you are doing.
141140
NewClient client.NewClientFunc
142141

143-
// SyncPeriod determines the minimum frequency at which watched resources are
144-
// reconciled. A lower period will correct entropy more quickly, but reduce
145-
// responsiveness to change if there are many watched resources. Change this
146-
// value only if you know what you are doing. Defaults to 10 hours if unset.
147-
// there will a 10 percent jitter between the SyncPeriod of all controllers
148-
// so that all controllers will not send list requests simultaneously.
149-
//
150-
// This applies to all controllers.
151-
//
152-
// A period sync happens for two reasons:
153-
// 1. To insure against a bug in the controller that causes an object to not
154-
// be requeued, when it otherwise should be requeued.
155-
// 2. To insure against an unknown bug in controller-runtime, or its dependencies,
156-
// that causes an object to not be requeued, when it otherwise should be
157-
// requeued, or to be removed from the queue, when it otherwise should not
158-
// be removed.
159-
//
160-
// If you want
161-
// 1. to insure against missed watch events, or
162-
// 2. to poll services that cannot be watched,
163-
// then we recommend that, instead of changing the default period, the
164-
// controller requeue, with a constant duration `t`, whenever the controller
165-
// is "done" with an object, and would otherwise not requeue it, i.e., we
166-
// recommend the `Reconcile` function return `reconcile.Result{RequeueAfter: t}`,
167-
// instead of `reconcile.Result{}`.
168-
//
169-
// Deprecated: Use Cache.SyncPeriod instead.
170-
SyncPeriod *time.Duration
171-
172142
// Logger is the logger that should be used by this manager.
173143
// If none is set, it defaults to log.Log global logger.
174144
Logger logr.Logger
@@ -239,23 +209,15 @@ type Options struct {
239209
// wait to force acquire leadership. This is measured against time of
240210
// last observed ack. Default is 15 seconds.
241211
LeaseDuration *time.Duration
212+
242213
// RenewDeadline is the duration that the acting controlplane will retry
243214
// refreshing leadership before giving up. Default is 10 seconds.
244215
RenewDeadline *time.Duration
216+
245217
// RetryPeriod is the duration the LeaderElector clients should wait
246218
// between tries of actions. Default is 2 seconds.
247219
RetryPeriod *time.Duration
248220

249-
// Namespace, if specified, restricts the manager's cache to watch objects in
250-
// the desired namespace. Defaults to all namespaces.
251-
//
252-
// Note: If a namespace is specified, controllers can still Watch for a
253-
// cluster-scoped resource (e.g Node). For namespaced resources, the cache
254-
// will only hold objects from the desired namespace.
255-
//
256-
// Deprecated: Use Cache.Namespaces instead.
257-
Namespace string
258-
259221
// MetricsBindAddress is the TCP address that the controller should bind to
260222
// for serving prometheus metrics.
261223
// It can be set to "0" to disable the metrics serving.
@@ -279,31 +241,6 @@ type Options struct {
279241
// before exposing it to public.
280242
PprofBindAddress string
281243

282-
// Port is the port that the webhook server serves at.
283-
// It is used to set webhook.Server.Port if WebhookServer is not set.
284-
//
285-
// Deprecated: Use WebhookServer instead. A WebhookServer can be created via webhook.NewServer.
286-
Port int
287-
// Host is the hostname that the webhook server binds to.
288-
// It is used to set webhook.Server.Host if WebhookServer is not set.
289-
//
290-
// Deprecated: Use WebhookServer instead. A WebhookServer can be created via webhook.NewServer.
291-
Host string
292-
293-
// CertDir is the directory that contains the server key and certificate.
294-
// If not set, webhook server would look up the server key and certificate in
295-
// {TempDir}/k8s-webhook-server/serving-certs. The server key and certificate
296-
// must be named tls.key and tls.crt, respectively.
297-
// It is used to set webhook.Server.CertDir if WebhookServer is not set.
298-
//
299-
// Deprecated: Use WebhookServer instead. A WebhookServer can be created via webhook.NewServer.
300-
CertDir string
301-
302-
// TLSOpts is used to allow configuring the TLS config used for the webhook server.
303-
//
304-
// Deprecated: Use WebhookServer instead. A WebhookServer can be created via webhook.NewServer.
305-
TLSOpts []func(*tls.Config)
306-
307244
// WebhookServer is an externally configured webhook.Server. By default,
308245
// a Manager will create a default server using Port, Host, and CertDir;
309246
// if this is set, the Manager will use this server instead.
@@ -314,18 +251,6 @@ type Options struct {
314251
// will receive a new Background Context instead.
315252
BaseContext BaseContextFunc
316253

317-
// ClientDisableCacheFor tells the client that, if any cache is used, to bypass it
318-
// for the given objects.
319-
//
320-
// Deprecated: Use Client.Cache.DisableCacheFor instead.
321-
ClientDisableCacheFor []client.Object
322-
323-
// DryRunClient specifies whether the client should be configured to enforce
324-
// dryRun mode.
325-
//
326-
// Deprecated: Use Client.DryRun instead.
327-
DryRunClient bool
328-
329254
// EventBroadcaster records Events emitted by the manager and sends them to the Kubernetes API
330255
// Use this to customize the event correlator and spam filter
331256
//
@@ -401,15 +326,11 @@ func New(config *rest.Config, options Options) (Manager, error) {
401326
clusterOptions.Scheme = options.Scheme
402327
clusterOptions.MapperProvider = options.MapperProvider
403328
clusterOptions.Logger = options.Logger
404-
clusterOptions.SyncPeriod = options.SyncPeriod
405329
clusterOptions.NewCache = options.NewCache
406330
clusterOptions.NewClient = options.NewClient
407331
clusterOptions.Cache = options.Cache
408332
clusterOptions.Client = options.Client
409-
clusterOptions.Namespace = options.Namespace //nolint:staticcheck
410-
clusterOptions.ClientDisableCacheFor = options.ClientDisableCacheFor //nolint:staticcheck
411-
clusterOptions.DryRunClient = options.DryRunClient //nolint:staticcheck
412-
clusterOptions.EventBroadcaster = options.EventBroadcaster //nolint:staticcheck
333+
clusterOptions.EventBroadcaster = options.EventBroadcaster //nolint:staticcheck
413334
})
414335
if err != nil {
415336
return nil, err
@@ -526,12 +447,12 @@ func (o Options) AndFrom(loader config.ControllerManagerConfiguration) (Options,
526447

527448
o = o.setLeaderElectionConfig(newObj)
528449

529-
if o.SyncPeriod == nil && newObj.SyncPeriod != nil {
530-
o.SyncPeriod = &newObj.SyncPeriod.Duration
450+
if o.Cache.SyncPeriod == nil && newObj.SyncPeriod != nil {
451+
o.Cache.SyncPeriod = &newObj.SyncPeriod.Duration
531452
}
532453

533-
if o.Namespace == "" && newObj.CacheNamespace != "" {
534-
o.Namespace = newObj.CacheNamespace
454+
if len(o.Cache.Namespaces) == 0 && newObj.CacheNamespace != "" {
455+
o.Cache.Namespaces = []string{newObj.CacheNamespace}
535456
}
536457

537458
if o.MetricsBindAddress == "" && newObj.Metrics.BindAddress != "" {
@@ -550,20 +471,15 @@ func (o Options) AndFrom(loader config.ControllerManagerConfiguration) (Options,
550471
o.LivenessEndpointName = newObj.Health.LivenessEndpointName
551472
}
552473

553-
if o.Port == 0 && newObj.Webhook.Port != nil {
554-
o.Port = *newObj.Webhook.Port
555-
}
556-
if o.Host == "" && newObj.Webhook.Host != "" {
557-
o.Host = newObj.Webhook.Host
558-
}
559-
if o.CertDir == "" && newObj.Webhook.CertDir != "" {
560-
o.CertDir = newObj.Webhook.CertDir
561-
}
562474
if o.WebhookServer == nil {
475+
port := 0
476+
if newObj.Webhook.Port != nil {
477+
port = *newObj.Webhook.Port
478+
}
563479
o.WebhookServer = webhook.NewServer(webhook.Options{
564-
Port: o.Port,
565-
Host: o.Host,
566-
CertDir: o.CertDir,
480+
Port: port,
481+
Host: newObj.Webhook.Host,
482+
CertDir: newObj.Webhook.CertDir,
567483
})
568484
}
569485

@@ -737,12 +653,7 @@ func setOptionsDefaults(options Options) Options {
737653
}
738654

739655
if options.WebhookServer == nil {
740-
options.WebhookServer = webhook.NewServer(webhook.Options{
741-
Host: options.Host,
742-
Port: options.Port,
743-
CertDir: options.CertDir,
744-
TLSOpts: options.TLSOpts,
745-
})
656+
options.WebhookServer = webhook.NewServer(webhook.Options{})
746657
}
747658

748659
return options

pkg/manager/manager_test.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,22 @@ var _ = Describe("manger.Manager", func() {
156156
m, err := Options{}.AndFrom(&fakeDeferredLoader{ccfg})
157157
Expect(err).ToNot(HaveOccurred())
158158

159-
Expect(*m.SyncPeriod).To(Equal(duration.Duration))
159+
Expect(*m.Cache.SyncPeriod).To(Equal(duration.Duration))
160160
Expect(m.LeaderElection).To(Equal(leaderElect))
161161
Expect(m.LeaderElectionResourceLock).To(Equal("leases"))
162162
Expect(m.LeaderElectionNamespace).To(Equal("default"))
163163
Expect(m.LeaderElectionID).To(Equal("ctrl-lease"))
164164
Expect(m.LeaseDuration.String()).To(Equal(duration.Duration.String()))
165165
Expect(m.RenewDeadline.String()).To(Equal(duration.Duration.String()))
166166
Expect(m.RetryPeriod.String()).To(Equal(duration.Duration.String()))
167-
Expect(m.Namespace).To(Equal("default"))
167+
Expect(m.Cache.Namespaces).To(Equal([]string{"default"}))
168168
Expect(m.MetricsBindAddress).To(Equal(":6000"))
169169
Expect(m.HealthProbeBindAddress).To(Equal("6060"))
170170
Expect(m.ReadinessEndpointName).To(Equal("/readyz"))
171171
Expect(m.LivenessEndpointName).To(Equal("/livez"))
172-
Expect(m.Port).To(Equal(port))
173-
Expect(m.Host).To(Equal("localhost"))
174-
Expect(m.CertDir).To(Equal("/certs"))
172+
Expect(m.WebhookServer.(*webhook.DefaultServer).Options.Port).To(Equal(port))
173+
Expect(m.WebhookServer.(*webhook.DefaultServer).Options.Host).To(Equal("localhost"))
174+
Expect(m.WebhookServer.(*webhook.DefaultServer).Options.CertDir).To(Equal("/certs"))
175175
})
176176

177177
It("should be able to keep Options when cfg.ControllerManagerConfiguration set", func() {
@@ -213,15 +213,17 @@ var _ = Describe("manger.Manager", func() {
213213
func(config *tls.Config) {},
214214
}
215215
m, err := Options{
216-
SyncPeriod: &optDuration,
216+
Cache: cache.Options{
217+
SyncPeriod: &optDuration,
218+
Namespaces: []string{"ctrl"},
219+
},
217220
LeaderElection: true,
218221
LeaderElectionResourceLock: "configmaps",
219222
LeaderElectionNamespace: "ctrl",
220223
LeaderElectionID: "ctrl-configmap",
221224
LeaseDuration: &optDuration,
222225
RenewDeadline: &optDuration,
223226
RetryPeriod: &optDuration,
224-
Namespace: "ctrl",
225227
MetricsBindAddress: ":7000",
226228
HealthProbeBindAddress: "5000",
227229
ReadinessEndpointName: "/readiness",
@@ -235,15 +237,15 @@ var _ = Describe("manger.Manager", func() {
235237
}.AndFrom(&fakeDeferredLoader{ccfg})
236238
Expect(err).ToNot(HaveOccurred())
237239

238-
Expect(m.SyncPeriod.String()).To(Equal(optDuration.String()))
240+
Expect(m.Cache.SyncPeriod.String()).To(Equal(optDuration.String()))
239241
Expect(m.LeaderElection).To(BeTrue())
240242
Expect(m.LeaderElectionResourceLock).To(Equal("configmaps"))
241243
Expect(m.LeaderElectionNamespace).To(Equal("ctrl"))
242244
Expect(m.LeaderElectionID).To(Equal("ctrl-configmap"))
243245
Expect(m.LeaseDuration.String()).To(Equal(optDuration.String()))
244246
Expect(m.RenewDeadline.String()).To(Equal(optDuration.String()))
245247
Expect(m.RetryPeriod.String()).To(Equal(optDuration.String()))
246-
Expect(m.Namespace).To(Equal("ctrl"))
248+
Expect(m.Cache.Namespaces).To(Equal([]string{"ctrl"}))
247249
Expect(m.MetricsBindAddress).To(Equal(":7000"))
248250
Expect(m.HealthProbeBindAddress).To(Equal("5000"))
249251
Expect(m.ReadinessEndpointName).To(Equal("/readiness"))
@@ -267,23 +269,10 @@ var _ = Describe("manger.Manager", func() {
267269
Expect(svr.(*webhook.DefaultServer).Options.Host).To(Equal("foo.com"))
268270
})
269271

270-
It("should lazily initialize a webhook server if needed (deprecated)", func() {
271-
By("creating a manager with options")
272-
m, err := New(cfg, Options{Port: 9440, Host: "foo.com"})
273-
Expect(err).NotTo(HaveOccurred())
274-
Expect(m).NotTo(BeNil())
275-
276-
By("checking options are passed to the webhook server")
277-
svr := m.GetWebhookServer()
278-
Expect(svr).NotTo(BeNil())
279-
Expect(svr.(*webhook.DefaultServer).Options.Port).To(Equal(9440))
280-
Expect(svr.(*webhook.DefaultServer).Options.Host).To(Equal("foo.com"))
281-
})
282-
283272
It("should not initialize a webhook server if Options.WebhookServer is set", func() {
284273
By("creating a manager with options")
285274
srv := webhook.NewServer(webhook.Options{Port: 9440})
286-
m, err := New(cfg, Options{Port: 9441, WebhookServer: srv})
275+
m, err := New(cfg, Options{WebhookServer: srv})
287276
Expect(err).NotTo(HaveOccurred())
288277
Expect(m).NotTo(BeNil())
289278

0 commit comments

Comments
 (0)