From d7b2929a0bab03c930b36ed8619f9508ce7bf454 Mon Sep 17 00:00:00 2001 From: Troy Connor Date: Wed, 10 Jan 2024 18:42:23 -0500 Subject: [PATCH] remove options.AndFrom deprecation Signed-off-by: Troy Connor --- pkg/manager/manager.go | 123 ------------------------ pkg/manager/manager_options_test.go | 54 ----------- pkg/manager/manager_test.go | 140 ---------------------------- 3 files changed, 317 deletions(-) delete mode 100644 pkg/manager/manager_options_test.go diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 25c3c7375b..7b1bc605b1 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -22,14 +22,12 @@ import ( "fmt" "net" "net/http" - "reflect" "time" "github.com/go-logr/logr" coordinationv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" "k8s.io/client-go/tools/leaderelection/resourcelock" @@ -41,7 +39,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/cluster" "sigs.k8s.io/controller-runtime/pkg/config" - "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/healthz" intrec "sigs.k8s.io/controller-runtime/pkg/internal/recorder" "sigs.k8s.io/controller-runtime/pkg/leaderelection" @@ -438,126 +435,6 @@ func New(config *rest.Config, options Options) (Manager, error) { }, nil } -// AndFrom will use a supplied type and convert to Options -// any options already set on Options will be ignored, this is used to allow -// cli flags to override anything specified in the config file. -// -// Deprecated: This function has been deprecated and will be removed in a future release, -// The Component Configuration package has been unmaintained for over a year and is no longer -// actively developed. Users should migrate to their own configuration format -// and configure Manager.Options directly. -// See https://github.com/kubernetes-sigs/controller-runtime/issues/895 -// for more information, feedback, and comments. -func (o Options) AndFrom(loader config.ControllerManagerConfiguration) (Options, error) { - newObj, err := loader.Complete() - if err != nil { - return o, err - } - - o = o.setLeaderElectionConfig(newObj) - - if o.Cache.SyncPeriod == nil && newObj.SyncPeriod != nil { - o.Cache.SyncPeriod = &newObj.SyncPeriod.Duration - } - - if len(o.Cache.DefaultNamespaces) == 0 && newObj.CacheNamespace != "" { - o.Cache.DefaultNamespaces = map[string]cache.Config{newObj.CacheNamespace: {}} - } - - if o.Metrics.BindAddress == "" && newObj.Metrics.BindAddress != "" { - o.Metrics.BindAddress = newObj.Metrics.BindAddress - } - - if o.HealthProbeBindAddress == "" && newObj.Health.HealthProbeBindAddress != "" { - o.HealthProbeBindAddress = newObj.Health.HealthProbeBindAddress - } - - if o.ReadinessEndpointName == "" && newObj.Health.ReadinessEndpointName != "" { - o.ReadinessEndpointName = newObj.Health.ReadinessEndpointName - } - - if o.LivenessEndpointName == "" && newObj.Health.LivenessEndpointName != "" { - o.LivenessEndpointName = newObj.Health.LivenessEndpointName - } - - if o.WebhookServer == nil { - port := 0 - if newObj.Webhook.Port != nil { - port = *newObj.Webhook.Port - } - o.WebhookServer = webhook.NewServer(webhook.Options{ - Port: port, - Host: newObj.Webhook.Host, - CertDir: newObj.Webhook.CertDir, - }) - } - - if newObj.Controller != nil { - if o.Controller.CacheSyncTimeout == 0 && newObj.Controller.CacheSyncTimeout != nil { - o.Controller.CacheSyncTimeout = *newObj.Controller.CacheSyncTimeout - } - - if len(o.Controller.GroupKindConcurrency) == 0 && len(newObj.Controller.GroupKindConcurrency) > 0 { - o.Controller.GroupKindConcurrency = newObj.Controller.GroupKindConcurrency - } - } - - return o, nil -} - -// AndFromOrDie will use options.AndFrom() and will panic if there are errors. -// -// Deprecated: This function has been deprecated and will be removed in a future release, -// The Component Configuration package has been unmaintained for over a year and is no longer -// actively developed. Users should migrate to their own configuration format -// and configure Manager.Options directly. -// See https://github.com/kubernetes-sigs/controller-runtime/issues/895 -// for more information, feedback, and comments. -func (o Options) AndFromOrDie(loader config.ControllerManagerConfiguration) Options { - o, err := o.AndFrom(loader) - if err != nil { - panic(fmt.Sprintf("could not parse config file: %v", err)) - } - return o -} - -func (o Options) setLeaderElectionConfig(obj v1alpha1.ControllerManagerConfigurationSpec) Options { - if obj.LeaderElection == nil { - // The source does not have any configuration; noop - return o - } - - if !o.LeaderElection && obj.LeaderElection.LeaderElect != nil { - o.LeaderElection = *obj.LeaderElection.LeaderElect - } - - if o.LeaderElectionResourceLock == "" && obj.LeaderElection.ResourceLock != "" { - o.LeaderElectionResourceLock = obj.LeaderElection.ResourceLock - } - - if o.LeaderElectionNamespace == "" && obj.LeaderElection.ResourceNamespace != "" { - o.LeaderElectionNamespace = obj.LeaderElection.ResourceNamespace - } - - if o.LeaderElectionID == "" && obj.LeaderElection.ResourceName != "" { - o.LeaderElectionID = obj.LeaderElection.ResourceName - } - - if o.LeaseDuration == nil && !reflect.DeepEqual(obj.LeaderElection.LeaseDuration, metav1.Duration{}) { - o.LeaseDuration = &obj.LeaderElection.LeaseDuration.Duration - } - - if o.RenewDeadline == nil && !reflect.DeepEqual(obj.LeaderElection.RenewDeadline, metav1.Duration{}) { - o.RenewDeadline = &obj.LeaderElection.RenewDeadline.Duration - } - - if o.RetryPeriod == nil && !reflect.DeepEqual(obj.LeaderElection.RetryPeriod, metav1.Duration{}) { - o.RetryPeriod = &obj.LeaderElection.RetryPeriod.Duration - } - - return o -} - // defaultHealthProbeListener creates the default health probes listener bound to the given address. func defaultHealthProbeListener(addr string) (net.Listener, error) { if addr == "" || addr == "0" { diff --git a/pkg/manager/manager_options_test.go b/pkg/manager/manager_options_test.go deleted file mode 100644 index 3718bedcbe..0000000000 --- a/pkg/manager/manager_options_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package manager - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "sigs.k8s.io/controller-runtime/pkg/config" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - configv1alpha1 "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1" -) - -var _ = Describe("manager.Options", func() { - Describe("AndFrom", func() { - Describe("reading custom type using OfKind", func() { - var ( - o Options - c customConfig - err error - ) - - JustBeforeEach(func() { - s := runtime.NewScheme() - o = Options{Scheme: s} - c = customConfig{} - - _, err = o.AndFrom(config.File().AtPath("./testdata/custom-config.yaml").OfKind(&c)) - }) - - It("should not panic or fail", func() { - Expect(err).To(Succeed()) - }) - It("should set custom properties", func() { - Expect(c.CustomValue).To(Equal("foo")) - }) - }) - }) -}) - -type customConfig struct { - metav1.TypeMeta `json:",inline"` - configv1alpha1.ControllerManagerConfigurationSpec `json:",inline"` - CustomValue string `json:"customValue"` -} - -func (in *customConfig) DeepCopyObject() runtime.Object { - out := &customConfig{} - *out = *in - - in.ControllerManagerConfigurationSpec.DeepCopyInto(&out.ControllerManagerConfigurationSpec) - - return out -} diff --git a/pkg/manager/manager_test.go b/pkg/manager/manager_test.go index 90596e9ace..cefa08253d 100644 --- a/pkg/manager/manager_test.go +++ b/pkg/manager/manager_test.go @@ -18,7 +18,6 @@ package manager import ( "context" - "crypto/tls" "errors" "fmt" "io" @@ -37,12 +36,10 @@ import ( coordinationv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/rest" "k8s.io/client-go/tools/leaderelection/resourcelock" - configv1alpha1 "k8s.io/component-base/config/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/cache/informertest" "sigs.k8s.io/controller-runtime/pkg/client" @@ -120,143 +117,6 @@ var _ = Describe("manger.Manager", func() { Expect(err.Error()).To(ContainSubstring("expected error")) }) - It("should be able to load Options from cfg.ControllerManagerConfiguration type", func() { - duration := metav1.Duration{Duration: 48 * time.Hour} - port := int(6090) - leaderElect := false - - ccfg := &v1alpha1.ControllerManagerConfiguration{ - ControllerManagerConfigurationSpec: v1alpha1.ControllerManagerConfigurationSpec{ - SyncPeriod: &duration, - LeaderElection: &configv1alpha1.LeaderElectionConfiguration{ - LeaderElect: &leaderElect, - ResourceLock: "leases", - ResourceNamespace: "default", - ResourceName: "ctrl-lease", - LeaseDuration: duration, - RenewDeadline: duration, - RetryPeriod: duration, - }, - CacheNamespace: "default", - Metrics: v1alpha1.ControllerMetrics{ - BindAddress: ":6000", - }, - Health: v1alpha1.ControllerHealth{ - HealthProbeBindAddress: "6060", - ReadinessEndpointName: "/readyz", - LivenessEndpointName: "/livez", - }, - Webhook: v1alpha1.ControllerWebhook{ - Port: &port, - Host: "localhost", - CertDir: "/certs", - }, - }, - } - - m, err := Options{}.AndFrom(&fakeDeferredLoader{ccfg}) - Expect(err).ToNot(HaveOccurred()) - - Expect(*m.Cache.SyncPeriod).To(Equal(duration.Duration)) - Expect(m.LeaderElection).To(Equal(leaderElect)) - Expect(m.LeaderElectionResourceLock).To(Equal("leases")) - Expect(m.LeaderElectionNamespace).To(Equal("default")) - Expect(m.LeaderElectionID).To(Equal("ctrl-lease")) - Expect(m.LeaseDuration.String()).To(Equal(duration.Duration.String())) - Expect(m.RenewDeadline.String()).To(Equal(duration.Duration.String())) - Expect(m.RetryPeriod.String()).To(Equal(duration.Duration.String())) - Expect(m.Cache.DefaultNamespaces).To(Equal(map[string]cache.Config{"default": {}})) - Expect(m.Metrics.BindAddress).To(Equal(":6000")) - Expect(m.HealthProbeBindAddress).To(Equal("6060")) - Expect(m.ReadinessEndpointName).To(Equal("/readyz")) - Expect(m.LivenessEndpointName).To(Equal("/livez")) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.Port).To(Equal(port)) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.Host).To(Equal("localhost")) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.CertDir).To(Equal("/certs")) - }) - - It("should be able to keep Options when cfg.ControllerManagerConfiguration set", func() { - optDuration := time.Duration(2) - duration := metav1.Duration{Duration: 48 * time.Hour} - port := int(6090) - leaderElect := false - - ccfg := &v1alpha1.ControllerManagerConfiguration{ - ControllerManagerConfigurationSpec: v1alpha1.ControllerManagerConfigurationSpec{ - SyncPeriod: &duration, - LeaderElection: &configv1alpha1.LeaderElectionConfiguration{ - LeaderElect: &leaderElect, - ResourceLock: "leases", - ResourceNamespace: "default", - ResourceName: "ctrl-lease", - LeaseDuration: duration, - RenewDeadline: duration, - RetryPeriod: duration, - }, - CacheNamespace: "default", - Metrics: v1alpha1.ControllerMetrics{ - BindAddress: ":6000", - }, - Health: v1alpha1.ControllerHealth{ - HealthProbeBindAddress: "6060", - ReadinessEndpointName: "/readyz", - LivenessEndpointName: "/livez", - }, - Webhook: v1alpha1.ControllerWebhook{ - Port: &port, - Host: "localhost", - CertDir: "/certs", - }, - }, - } - - optionsTlSOptsFuncs := []func(*tls.Config){ - func(config *tls.Config) {}, - } - m, err := Options{ - Cache: cache.Options{ - SyncPeriod: &optDuration, - DefaultNamespaces: map[string]cache.Config{"ctrl": {}}, - }, - LeaderElection: true, - LeaderElectionResourceLock: "configmaps", - LeaderElectionNamespace: "ctrl", - LeaderElectionID: "ctrl-configmap", - LeaseDuration: &optDuration, - RenewDeadline: &optDuration, - RetryPeriod: &optDuration, - Metrics: metricsserver.Options{BindAddress: ":7000"}, - HealthProbeBindAddress: "5000", - ReadinessEndpointName: "/readiness", - LivenessEndpointName: "/liveness", - WebhookServer: webhook.NewServer(webhook.Options{ - Port: 8080, - Host: "example.com", - CertDir: "/pki", - TLSOpts: optionsTlSOptsFuncs, - }), - }.AndFrom(&fakeDeferredLoader{ccfg}) - Expect(err).ToNot(HaveOccurred()) - - Expect(m.Cache.SyncPeriod.String()).To(Equal(optDuration.String())) - Expect(m.LeaderElection).To(BeTrue()) - Expect(m.LeaderElectionResourceLock).To(Equal("configmaps")) - Expect(m.LeaderElectionNamespace).To(Equal("ctrl")) - Expect(m.LeaderElectionID).To(Equal("ctrl-configmap")) - Expect(m.LeaseDuration.String()).To(Equal(optDuration.String())) - Expect(m.RenewDeadline.String()).To(Equal(optDuration.String())) - Expect(m.RetryPeriod.String()).To(Equal(optDuration.String())) - Expect(m.Cache.DefaultNamespaces).To(Equal(map[string]cache.Config{"ctrl": {}})) - Expect(m.Metrics.BindAddress).To(Equal(":7000")) - Expect(m.HealthProbeBindAddress).To(Equal("5000")) - Expect(m.ReadinessEndpointName).To(Equal("/readiness")) - Expect(m.LivenessEndpointName).To(Equal("/liveness")) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.Port).To(Equal(8080)) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.Host).To(Equal("example.com")) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.CertDir).To(Equal("/pki")) - Expect(m.WebhookServer.(*webhook.DefaultServer).Options.TLSOpts).To(Equal(optionsTlSOptsFuncs)) - }) - It("should lazily initialize a webhook server if needed", func() { By("creating a manager with options") m, err := New(cfg, Options{WebhookServer: webhook.NewServer(webhook.Options{Port: 9440, Host: "foo.com"})})