Skip to content

Commit

Permalink
remove options.AndFrom deprecation
Browse files Browse the repository at this point in the history
Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
  • Loading branch information
troy0820 committed Jan 10, 2024
1 parent 8f8247f commit d7b2929
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 317 deletions.
123 changes: 0 additions & 123 deletions pkg/manager/manager.go
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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" {
Expand Down
54 changes: 0 additions & 54 deletions pkg/manager/manager_options_test.go

This file was deleted.

140 changes: 0 additions & 140 deletions pkg/manager/manager_test.go
Expand Up @@ -18,7 +18,6 @@ package manager

import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
Expand All @@ -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"
Expand Down Expand Up @@ -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"})})
Expand Down

0 comments on commit d7b2929

Please sign in to comment.