diff --git a/examples/config/example.yaml b/examples/config/example.yaml index ea13d2f882..30d83025af 100644 --- a/examples/config/example.yaml +++ b/examples/config/example.yaml @@ -1,5 +1,6 @@ apiVersion: controller-runtime.config.sigs.k8s.io/v1alpha1 kind: GenericControllerConfiguration +syncPeriod: 24h port: 9443 metricsBindAddress: ":8080" leaderElection: diff --git a/pkg/api/config/conversion.go b/pkg/api/config/conversion.go new file mode 100644 index 0000000000..32bdd5660b --- /dev/null +++ b/pkg/api/config/conversion.go @@ -0,0 +1,19 @@ +/* +The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +func (*ControllerRuntimeConfiguration) Hub() {} diff --git a/pkg/api/config/groupversion_info.go b/pkg/api/config/groupversion_info.go new file mode 100644 index 0000000000..0f52a207cc --- /dev/null +++ b/pkg/api/config/groupversion_info.go @@ -0,0 +1,18 @@ +/* +The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package config provides the Internal ComponentConfig type for configuring +// controller-runtime +// +kubebuilder:object:generate=true +package config diff --git a/pkg/api/config/types.go b/pkg/api/config/types.go new file mode 100644 index 0000000000..c8f7cf9317 --- /dev/null +++ b/pkg/api/config/types.go @@ -0,0 +1,83 @@ +/* +The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + configv1alpha1 "k8s.io/component-base/config/v1alpha1" +) + +// ControllerConfiguration defines the desired state of GenericControllerConfiguration +type ControllerConfiguration struct { + // SyncPeriod returns the SyncPeriod + // +optional + SyncPeriod *metav1.Duration `json:"syncPeriod,omitempty"` + + // LeaderElection returns the LeaderElection config + // +optional + LeaderElection configv1alpha1.LeaderElectionConfiguration `json:"leaderElection,omitempty"` + + // Namespace returns the namespace for the controller + // +optional + Namespace string `json:"namespace,omitempty"` + + // MetricsBindAddress returns the bind address for the metrics server + // +optional + MetricsBindAddress string `json:"metricsBindAddress,omitempty"` + + // MetricsBindAddress returns the bind address for the metrics server + // +optional + Health ControllerHealth `json:"health,omitempty"` + + // Port returns the Port for the server + // +optional + Port *int `json:"port,omitempty"` + + // Host returns the Host for the server + // +optional + Host string `json:"host,omitempty"` + + // CertDir returns the CertDir + // +optional + CertDir string `json:"certDir,omitempty"` +} + +// ControllerHealth defines the health configs +type ControllerHealth struct { + // HealthProbeBindAddress returns the bind address for the health probe + // +optional + HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"` + + // ReadinessEndpointName returns the readiness endpoint name + // +optional + ReadinessEndpointName string `json:"readinessEndpointName,omitempty"` + + // LivenessEndpointName returns the liveness endpoint name + // +optional + LivenessEndpointName string `json:"livenessEndpointName,omitempty"` +} + +// +kubebuilder:object:root=true + +// ControllerRuntimeConfiguration is the Schema for the GenericControllerConfigurations API +type ControllerRuntimeConfiguration struct { + metav1.TypeMeta `json:",inline"` + + // ControllerConfiguration returns the contfigurations for controllers + ControllerConfiguration `json:",inline"` +} diff --git a/pkg/api/config/v1alpha1/componentconfig.go b/pkg/api/config/v1alpha1/componentconfig.go deleted file mode 100644 index dfb41219c6..0000000000 --- a/pkg/api/config/v1alpha1/componentconfig.go +++ /dev/null @@ -1,205 +0,0 @@ -/* -The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1alpha1 - -import ( - "reflect" - "time" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - configv1alpha1 "k8s.io/component-base/config/v1alpha1" -) - -// Getters - -// GetSyncPeriod returns the sync period in time.Duration -func (in *ControllerConfiguration) GetSyncPeriod() *time.Duration { - if in.SyncPeriod != nil { - return &in.SyncPeriod.Duration - } - return nil -} - -// GetLeaderElection returns the LeaderElection -func (in *ControllerConfiguration) GetLeaderElection() *bool { - return in.LeaderElection.LeaderElect -} - -// GetLeaderElectionNamespace returns the LeaderElectionNamespace -func (in *ControllerConfiguration) GetLeaderElectionNamespace() string { - return in.LeaderElection.ResourceNamespace -} - -// GetLeaderElectionID returns the LeaderElectionID -func (in *ControllerConfiguration) GetLeaderElectionID() string { - return in.LeaderElection.ResourceName -} - -// GetLeaseDuration returns the LeaseDuration -func (in *ControllerConfiguration) GetLeaseDuration() *time.Duration { - return &in.LeaderElection.LeaseDuration.Duration -} - -// GetRenewDeadline returns the RenewDeadline -func (in *ControllerConfiguration) GetRenewDeadline() *time.Duration { - return &in.LeaderElection.RenewDeadline.Duration -} - -// GetRetryPeriod returns the RetryPeriod -func (in *ControllerConfiguration) GetRetryPeriod() *time.Duration { - return &in.LeaderElection.RetryPeriod.Duration -} - -// GetNamespace returns the Namespace -func (in *ControllerConfiguration) GetNamespace() string { - return in.Namespace -} - -// GetMetricsBindAddress returns the MetricsBindAddress -func (in *ControllerConfiguration) GetMetricsBindAddress() string { - return in.MetricsBindAddress -} - -// GetHealthProbeBindAddress returns the HealthProbeBindAddress -func (in *ControllerConfiguration) GetHealthProbeBindAddress() string { - return in.Health.HealthProbeBindAddress -} - -// GetReadinessEndpointName returns the ReadinessEndpointName -func (in *ControllerConfiguration) GetReadinessEndpointName() string { - return in.Health.ReadinessEndpointName -} - -// GetLivenessEndpointName returns the LivenessEndpointName -func (in *ControllerConfiguration) GetLivenessEndpointName() string { - return in.Health.LivenessEndpointName -} - -// GetPort returns the Port -func (in *ControllerConfiguration) GetPort() *int { - return in.Port -} - -// GetHost returns the Host -func (in *ControllerConfiguration) GetHost() string { - return in.Host -} - -// GetCertDir returns the CertDir -func (in *ControllerConfiguration) GetCertDir() string { - return in.CertDir -} - -// Setters - -// SetSyncPeriod sets the sync period in time.Duration -func (in *ControllerConfiguration) SetSyncPeriod(syncPeriod *metav1.Duration) { - in.SyncPeriod = syncPeriod -} - -// SetLeaderElectionConfiguration sets the leader election configuration -func (in *ControllerConfiguration) SetLeaderElectionConfiguration(leaderElection configv1alpha1.LeaderElectionConfiguration) { - in.LeaderElection = leaderElection -} - -// SetLeaderElection sets the LeaderElection config -func (in *ControllerConfiguration) SetLeaderElection(leaderElection bool) { - if reflect.DeepEqual(in.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { - in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{}) - } - in.LeaderElection.LeaderElect = &leaderElection -} - -// SetLeaderElectionNamespace sets the LeaderElectionNamespace -func (in *ControllerConfiguration) SetLeaderElectionNamespace(resourceNamespace string) { - if reflect.DeepEqual(in.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { - in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{}) - } - in.LeaderElection.ResourceNamespace = resourceNamespace -} - -// SetLeaderElectionID sets the LeaderElectionID -func (in *ControllerConfiguration) SetLeaderElectionID(resourceName string) { - if reflect.DeepEqual(in.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { - in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{}) - } - in.LeaderElection.ResourceName = resourceName -} - -// SetLeaseDuration sets the LeaseDuration -func (in *ControllerConfiguration) SetLeaseDuration(leaseDuration metav1.Duration) { - if reflect.DeepEqual(in.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { - in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{}) - } - in.LeaderElection.LeaseDuration = leaseDuration -} - -// SetRenewDeadline sets the RenewDeadline -func (in *ControllerConfiguration) SetRenewDeadline(renewDeadline metav1.Duration) { - if reflect.DeepEqual(in.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { - in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{}) - } - in.LeaderElection.RenewDeadline = renewDeadline -} - -// SetRetryPeriod sets the RetryPeriod -func (in *ControllerConfiguration) SetRetryPeriod(retryPeriod metav1.Duration) { - if reflect.DeepEqual(in.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { - in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{}) - } - in.LeaderElection.RetryPeriod = retryPeriod -} - -// SetNamespace sets the Namespace -func (in *ControllerConfiguration) SetNamespace(namespace string) { - in.Namespace = namespace -} - -// SetMetricsBindAddress sets the MetricsBindAddress -func (in *ControllerConfiguration) SetMetricsBindAddress(metricsBindAddress string) { - in.MetricsBindAddress = metricsBindAddress -} - -// SetHealthProbeBindAddress sets the HealthProbeBindAddress -func (in *ControllerConfiguration) SetHealthProbeBindAddress(healthProbeBindAddress string) { - in.Health.HealthProbeBindAddress = healthProbeBindAddress -} - -// SetReadinessEndpointName sets the ReadinessEndpointName -func (in *ControllerConfiguration) SetReadinessEndpointName(readinessEndpointName string) { - in.Health.ReadinessEndpointName = readinessEndpointName -} - -// SetLivenessEndpointName sets the LivenessEndpointName -func (in *ControllerConfiguration) SetLivenessEndpointName(livenessEndpointName string) { - in.Health.LivenessEndpointName = livenessEndpointName -} - -// SetPort sets the Port -func (in *ControllerConfiguration) SetPort(port *int) { - in.Port = port -} - -// SetHost sets the Host -func (in *ControllerConfiguration) SetHost(host string) { - in.Host = host -} - -// SetCertDir sets the CertDir -func (in *ControllerConfiguration) SetCertDir(certDir string) { - in.CertDir = certDir -} diff --git a/pkg/api/config/v1alpha1/conversion.go b/pkg/api/config/v1alpha1/conversion.go new file mode 100644 index 0000000000..3825d04863 --- /dev/null +++ b/pkg/api/config/v1alpha1/conversion.go @@ -0,0 +1,66 @@ +/* +The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha1 + +import ( + "reflect" + + "sigs.k8s.io/controller-runtime/pkg/api/config" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +// ConvertTo converts this GenericControllerConfiguration to the Hub version (v1). +func (src *GenericControllerConfiguration) ConvertTo(dstRaw conversion.Hub) error { + dst := dstRaw.(*config.ControllerRuntimeConfiguration) + + dst.TypeMeta = src.TypeMeta + dst.SyncPeriod = src.SyncPeriod + dst.LeaderElection = src.LeaderElection + dst.Namespace = src.Namespace + dst.MetricsBindAddress = src.MetricsBindAddress + if !reflect.DeepEqual(src.Health, ControllerHealth{}) { + dst.Health.HealthProbeBindAddress = src.Health.HealthProbeBindAddress + dst.Health.ReadinessEndpointName = src.Health.ReadinessEndpointName + dst.Health.LivenessEndpointName = src.Health.LivenessEndpointName + } + dst.Port = src.Port + dst.Host = src.Host + dst.CertDir = src.CertDir + + return nil +} + +// ConvertFrom converts from the Hub version (internal type) to this version. +func (dst *GenericControllerConfiguration) ConvertFrom(srcRaw conversion.Hub) error { + src := srcRaw.(*config.ControllerRuntimeConfiguration) + + dst.TypeMeta = src.TypeMeta + dst.SyncPeriod = src.SyncPeriod + dst.LeaderElection = src.LeaderElection + dst.Namespace = src.Namespace + dst.MetricsBindAddress = src.MetricsBindAddress + if !reflect.DeepEqual(src.Health, ControllerHealth{}) { + dst.Health.HealthProbeBindAddress = src.Health.HealthProbeBindAddress + dst.Health.ReadinessEndpointName = src.Health.ReadinessEndpointName + dst.Health.LivenessEndpointName = src.Health.LivenessEndpointName + } + dst.Port = src.Port + dst.Host = src.Host + dst.CertDir = src.CertDir + + return nil +} diff --git a/pkg/api/config/v1alpha1/types.go b/pkg/api/config/v1alpha1/types.go index 495b6413b9..576d5a54db 100644 --- a/pkg/api/config/v1alpha1/types.go +++ b/pkg/api/config/v1alpha1/types.go @@ -42,7 +42,7 @@ type ControllerConfiguration struct { // MetricsBindAddress returns the bind address for the metrics server // +optional - Health GenericControllerHealth `json:"health,omitempty"` + Health ControllerHealth `json:"health,omitempty"` // Port returns the Port for the server // +optional @@ -57,8 +57,8 @@ type ControllerConfiguration struct { CertDir string `json:"certDir,omitempty"` } -// GenericControllerHealth defines the health configs -type GenericControllerHealth struct { +// ControllerHealth defines the health configs +type ControllerHealth struct { // HealthProbeBindAddress returns the bind address for the health probe // +optional HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"` diff --git a/pkg/api/config/v1alpha1/zz_generated.deepcopy.go b/pkg/api/config/v1alpha1/zz_generated.deepcopy.go index c00e0c4cc7..dde26bdac9 100644 --- a/pkg/api/config/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/api/config/v1alpha1/zz_generated.deepcopy.go @@ -52,6 +52,21 @@ func (in *ControllerConfiguration) DeepCopy() *ControllerConfiguration { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ControllerHealth) DeepCopyInto(out *ControllerHealth) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ControllerHealth. +func (in *ControllerHealth) DeepCopy() *ControllerHealth { + if in == nil { + return nil + } + out := new(ControllerHealth) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GenericControllerConfiguration) DeepCopyInto(out *GenericControllerConfiguration) { *out = *in @@ -76,18 +91,3 @@ func (in *GenericControllerConfiguration) DeepCopyObject() runtime.Object { } return nil } - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *GenericControllerHealth) DeepCopyInto(out *GenericControllerHealth) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GenericControllerHealth. -func (in *GenericControllerHealth) DeepCopy() *GenericControllerHealth { - if in == nil { - return nil - } - out := new(GenericControllerHealth) - in.DeepCopyInto(out) - return out -} diff --git a/pkg/api/config/zz_generated.deepcopy.go b/pkg/api/config/zz_generated.deepcopy.go new file mode 100644 index 0000000000..76daea2e29 --- /dev/null +++ b/pkg/api/config/zz_generated.deepcopy.go @@ -0,0 +1,93 @@ +// +build !ignore_autogenerated + +/* +The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by controller-gen. DO NOT EDIT. + +package config + +import ( + "k8s.io/apimachinery/pkg/apis/meta/v1" + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ControllerConfiguration) DeepCopyInto(out *ControllerConfiguration) { + *out = *in + if in.SyncPeriod != nil { + in, out := &in.SyncPeriod, &out.SyncPeriod + *out = new(v1.Duration) + **out = **in + } + in.LeaderElection.DeepCopyInto(&out.LeaderElection) + out.Health = in.Health + if in.Port != nil { + in, out := &in.Port, &out.Port + *out = new(int) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ControllerConfiguration. +func (in *ControllerConfiguration) DeepCopy() *ControllerConfiguration { + if in == nil { + return nil + } + out := new(ControllerConfiguration) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ControllerHealth) DeepCopyInto(out *ControllerHealth) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ControllerHealth. +func (in *ControllerHealth) DeepCopy() *ControllerHealth { + if in == nil { + return nil + } + out := new(ControllerHealth) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ControllerRuntimeConfiguration) DeepCopyInto(out *ControllerRuntimeConfiguration) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ControllerConfiguration.DeepCopyInto(&out.ControllerConfiguration) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ControllerRuntimeConfiguration. +func (in *ControllerRuntimeConfiguration) DeepCopy() *ControllerRuntimeConfiguration { + if in == nil { + return nil + } + out := new(ControllerRuntimeConfiguration) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ControllerRuntimeConfiguration) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index e10e6ae451..ab20604292 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -20,6 +20,7 @@ import ( "fmt" "io/ioutil" "net" + "reflect" "time" "github.com/go-logr/logr" @@ -31,9 +32,12 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/leaderelection/resourcelock" "k8s.io/client-go/tools/record" + configv1alpha1 "k8s.io/component-base/config/v1alpha1" + apicfg "sigs.k8s.io/controller-runtime/pkg/api/config" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" + "sigs.k8s.io/controller-runtime/pkg/conversion" "sigs.k8s.io/controller-runtime/pkg/healthz" internalrecorder "sigs.k8s.io/controller-runtime/pkg/internal/recorder" "sigs.k8s.io/controller-runtime/pkg/leaderelection" @@ -99,55 +103,6 @@ type Manager interface { GetWebhookServer() *webhook.Server } -// Configuration defines what the ComponentConfig object for controller -// runtime needs in order to run -type Configuration interface { - runtime.Object - - // GetSyncPeriod returns the SyncPeriod - // Typically ComponentConfig types will store this as matav1.Duration - GetSyncPeriod() *time.Duration - - // GetSyncPeriod returns if LeaderElection is turned on - GetLeaderElection() *bool - // GetLeaderElectionNamespace returns the namespace for LeaderElection - GetLeaderElectionNamespace() string - // GetLeaderElectionID returns the LeaderElectionID - GetLeaderElectionID() string - - // GetLeaseDuration returns the LeaseDuration - // Typically ComponentConfig types will store this as matav1.Duration - GetLeaseDuration() *time.Duration - // GetRenewDeadline returns the RenewDeadline - // Typically ComponentConfig types will store this as matav1.Duration - GetRenewDeadline() *time.Duration - // GetRetryPeriod returns the RetryPeriod - // Typically ComponentConfig types will store this as matav1.Duration - GetRetryPeriod() *time.Duration - - // GetNamespace returns the Namespace - // TODO(christopherhein,joelan) explore adding Plural to support - GetNamespace() string - - // GetMetricsBindAddress returns the MetricsBindAddress - GetMetricsBindAddress() string - // GetHealthProbeBindAddress returns the HealthProbeBindAddress - GetHealthProbeBindAddress() string - - // GetReadinessEndpointName returns the ReadinessEndpointName - GetReadinessEndpointName() string - // GetLivenessEndpointName returns the LivenessEndpointName - GetLivenessEndpointName() string - - // GetPort returns the Port - GetPort() *int - // GetHost returns the Host - GetHost() string - - // GetCertDir returns the CertDir - GetCertDir() string -} - // Options are the arguments for creating a new Manager type Options struct { // Scheme is the scheme used to resolve runtime.Objects to GroupVersionKinds / Resources @@ -369,14 +324,14 @@ func New(config *rest.Config, options Options) (Manager, error) { // NewOptionsFromComponentConfig returns a populated Options based on the // ManagerConfiguation object. -func NewOptionsFromComponentConfig(scheme *runtime.Scheme, filename string, config Configuration) (options Options, err error) { +func NewOptionsFromComponentConfig(scheme *runtime.Scheme, filename string, object conversion.Convertible) (options Options, err error) { // Check if Scheme is set if scheme == nil { return options, fmt.Errorf("must specify Scheme") } - // Check if Configuration is set - if config == nil { + // Check if convertible object is set + if object == nil { return options, fmt.Errorf("must specify Configuration") } @@ -393,67 +348,44 @@ func NewOptionsFromComponentConfig(scheme *runtime.Scheme, filename string, conf // Regardless of if the bytes are of any external version, // it will be read successfully and converted into the internal version - if err := runtime.DecodeInto(codecs.UniversalDecoder(), content, config); err != nil { + if err := runtime.DecodeInto(codecs.UniversalDecoder(), content, object); err != nil { return options, err } - options.SyncPeriod = config.GetSyncPeriod() - - if config.GetLeaderElection() != nil { - options.LeaderElection = *config.GetLeaderElection() - } - - if config.GetLeaderElectionNamespace() != "" { - options.LeaderElectionNamespace = config.GetLeaderElectionNamespace() - } - - if config.GetLeaderElectionID() != "" { - options.LeaderElectionID = config.GetLeaderElectionID() - } - - if config.GetLeaseDuration() != nil { - options.LeaseDuration = config.GetLeaseDuration() - } - - if config.GetRenewDeadline() != nil { - options.RenewDeadline = config.GetRenewDeadline() - } - - if config.GetRetryPeriod() != nil { - options.RetryPeriod = config.GetRetryPeriod() - } - - if config.GetNamespace() != "" { - options.Namespace = config.GetNamespace() - } - - if config.GetMetricsBindAddress() != "" { - options.MetricsBindAddress = config.GetMetricsBindAddress() - } - - if config.GetHealthProbeBindAddress() != "" { - options.HealthProbeBindAddress = config.GetHealthProbeBindAddress() + config := apicfg.ControllerRuntimeConfiguration{} + if err := object.ConvertTo(&config); err != nil { + return options, err } - if config.GetReadinessEndpointName() != "" { - options.ReadinessEndpointName = config.GetReadinessEndpointName() + if config.SyncPeriod != nil { + options.SyncPeriod = &config.SyncPeriod.Duration } - if config.GetLivenessEndpointName() != "" { - options.LivenessEndpointName = config.GetLivenessEndpointName() + if !reflect.DeepEqual(config.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) { + if config.LeaderElection.LeaderElect != nil { + options.LeaderElection = *config.LeaderElection.LeaderElect + } + options.LeaderElectionNamespace = config.LeaderElection.ResourceNamespace + options.LeaderElectionID = config.LeaderElection.ResourceName + options.LeaseDuration = &config.LeaderElection.LeaseDuration.Duration + options.RenewDeadline = &config.LeaderElection.RenewDeadline.Duration + options.RetryPeriod = &config.LeaderElection.RetryPeriod.Duration } - if config.GetPort() != nil { - options.Port = *config.GetPort() - } + options.Namespace = config.Namespace + options.MetricsBindAddress = config.MetricsBindAddress - if config.GetHost() != "" { - options.Host = config.GetHost() + if !reflect.DeepEqual(config.Health, apicfg.ControllerHealth{}) { + options.HealthProbeBindAddress = config.Health.HealthProbeBindAddress + options.ReadinessEndpointName = config.Health.ReadinessEndpointName + options.LivenessEndpointName = config.Health.LivenessEndpointName } - if config.GetCertDir() != "" { - options.CertDir = config.GetCertDir() + if config.Port != nil { + options.Port = *config.Port } + options.Host = config.Host + options.CertDir = config.CertDir return options, nil } diff --git a/pkg/manager/manager_test.go b/pkg/manager/manager_test.go index bd440ad82e..14c26eb8df 100644 --- a/pkg/manager/manager_test.go +++ b/pkg/manager/manager_test.go @@ -262,21 +262,21 @@ var _ = Describe("manger.Manager", func() { opts, err := NewOptionsFromComponentConfig(scheme, "../../examples/config/example.yaml", cfg) Expect(err).To(BeNil()) - Expect(opts.SyncPeriod).To(Equal(cfg.GetSyncPeriod())) - Expect(opts.LeaderElection).To(Equal(*cfg.GetLeaderElection())) - Expect(opts.LeaderElectionNamespace).To(Equal(cfg.GetLeaderElectionNamespace())) - Expect(opts.LeaderElectionID).To(Equal(cfg.GetLeaderElectionID())) - Expect(opts.LeaseDuration).To(Equal(cfg.GetLeaseDuration())) - Expect(opts.RenewDeadline).To(Equal(cfg.GetRenewDeadline())) - Expect(opts.RetryPeriod).To(Equal(cfg.GetRetryPeriod())) - Expect(opts.Namespace).To(Equal(cfg.GetNamespace())) - Expect(opts.MetricsBindAddress).To(Equal(cfg.GetMetricsBindAddress())) - Expect(opts.HealthProbeBindAddress).To(Equal(cfg.GetHealthProbeBindAddress())) - Expect(opts.ReadinessEndpointName).To(Equal(cfg.GetReadinessEndpointName())) - Expect(opts.LivenessEndpointName).To(Equal(cfg.GetLivenessEndpointName())) - Expect(opts.Port).To(Equal(*cfg.GetPort())) - Expect(opts.Host).To(Equal(cfg.GetHost())) - Expect(opts.CertDir).To(Equal(cfg.GetCertDir())) + Expect(opts.SyncPeriod).To(Equal(&cfg.SyncPeriod.Duration)) + Expect(opts.LeaderElection).To(Equal(*cfg.LeaderElection.LeaderElect)) + Expect(opts.LeaderElectionNamespace).To(Equal(cfg.LeaderElection.ResourceNamespace)) + Expect(opts.LeaderElectionID).To(Equal(cfg.LeaderElection.ResourceName)) + Expect(opts.LeaseDuration).To(Equal(&cfg.LeaderElection.LeaseDuration.Duration)) + Expect(opts.RenewDeadline).To(Equal(&cfg.LeaderElection.RenewDeadline.Duration)) + Expect(opts.RetryPeriod).To(Equal(&cfg.LeaderElection.RetryPeriod.Duration)) + Expect(opts.Namespace).To(Equal(cfg.Namespace)) + Expect(opts.MetricsBindAddress).To(Equal(cfg.MetricsBindAddress)) + Expect(opts.HealthProbeBindAddress).To(Equal(cfg.Health.HealthProbeBindAddress)) + Expect(opts.ReadinessEndpointName).To(Equal(cfg.Health.ReadinessEndpointName)) + Expect(opts.LivenessEndpointName).To(Equal(cfg.Health.LivenessEndpointName)) + Expect(opts.Port).To(Equal(*cfg.Port)) + Expect(opts.Host).To(Equal(cfg.Host)) + Expect(opts.CertDir).To(Equal(cfg.CertDir)) }) })