Skip to content

Commit

Permalink
adding ComponentConfig type and Options parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Hein <me@chrishein.com>
  • Loading branch information
christopherhein committed Apr 8, 2020
1 parent 4e1e8ed commit d95d4d3
Show file tree
Hide file tree
Showing 11 changed files with 566 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
run:
deadline: 5m
skip-files:
- pkg/api/config/v1alpha1/zz_generated.deepcopy.go
linters-settings:
lll:
line-length: 170
Expand Down
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export GOPROXY
# Active module mode, as we use go modules to manage dependencies
export GO111MODULE=on

ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# Tools.
TOOLS_DIR := hack/tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
Expand Down Expand Up @@ -62,6 +68,25 @@ test: ## Run the script check-everything.sh which will check all.
$(GOLANGCI_LINT): $(TOOLS_DIR)/go.mod # Build golangci-lint from tools folder.
cd $(TOOLS_DIR); go build -tags=tools -o bin/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint

controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

generate: controller-gen
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./pkg/api/..."


## --------------------------------------
## Linting
## --------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ var (
// NewManager returns a new Manager for creating Controllers.
NewManager = manager.New

// UpdateOptionsFromComponentConfig returns an updated Options struct from a ComponentConfig type
UpdateOptionsFromComponentConfig = manager.UpdateOptionsFromComponentConfig

// CreateOrUpdate creates or updates the given object obj in the Kubernetes
// cluster. The object's desired state should be reconciled with the existing
// state using the passed in ReconcileFn. obj must be a struct pointer so that
Expand Down
7 changes: 7 additions & 0 deletions examples/config/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: controller-runtime.config.sigs.k8s.io/v1alpha1
kind: DefaultControllerConfiguration
spec:
port: 9443
metricsBindAddress: ":8080"
leaderElection:
leaderElect: false
15 changes: 15 additions & 0 deletions hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
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.
*/
5 changes: 4 additions & 1 deletion hack/verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ source $(dirname ${BASH_SOURCE})/common.sh
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
cd "${REPO_ROOT}"

header_text "deepcopy gen"
make generate

header_text "running golangci-lint"
make lint

header_text "verifying modules"
make modules verify-modules
make modules verify-modules
205 changes: 205 additions & 0 deletions pkg/api/config/v1alpha1/componentconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
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 *DefaultControllerConfiguration) GetSyncPeriod() *time.Duration {
if in.Spec.SyncPeriod != nil {
return &in.Spec.SyncPeriod.Duration
}
return nil
}

// GetLeaderElection returns the LeaderElection
func (in *DefaultControllerConfiguration) GetLeaderElection() *bool {
return in.Spec.LeaderElection.LeaderElect
}

// GetLeaderElectionNamespace returns the LeaderElectionNamespace
func (in *DefaultControllerConfiguration) GetLeaderElectionNamespace() string {
return in.Spec.LeaderElection.ResourceNamespace
}

// GetLeaderElectionID returns the LeaderElectionID
func (in *DefaultControllerConfiguration) GetLeaderElectionID() string {
return in.Spec.LeaderElection.ResourceName
}

// GetLeaseDuration returns the LeaseDuration
func (in *DefaultControllerConfiguration) GetLeaseDuration() *time.Duration {
return &in.Spec.LeaderElection.LeaseDuration.Duration
}

// GetRenewDeadline returns the RenewDeadline
func (in *DefaultControllerConfiguration) GetRenewDeadline() *time.Duration {
return &in.Spec.LeaderElection.RenewDeadline.Duration
}

// GetRetryPeriod returns the RetryPeriod
func (in *DefaultControllerConfiguration) GetRetryPeriod() *time.Duration {
return &in.Spec.LeaderElection.RetryPeriod.Duration
}

// GetNamespace returns the Namespace
func (in *DefaultControllerConfiguration) GetNamespace() string {
return in.Spec.Namespace
}

// GetMetricsBindAddress returns the MetricsBindAddress
func (in *DefaultControllerConfiguration) GetMetricsBindAddress() string {
return in.Spec.MetricsBindAddress
}

// GetHealthProbeBindAddress returns the HealthProbeBindAddress
func (in *DefaultControllerConfiguration) GetHealthProbeBindAddress() string {
return in.Spec.Health.HealthProbeBindAddress
}

// GetReadinessEndpointName returns the ReadinessEndpointName
func (in *DefaultControllerConfiguration) GetReadinessEndpointName() string {
return in.Spec.Health.ReadinessEndpointName
}

// GetLivenessEndpointName returns the LivenessEndpointName
func (in *DefaultControllerConfiguration) GetLivenessEndpointName() string {
return in.Spec.Health.LivenessEndpointName
}

// GetPort returns the Port
func (in *DefaultControllerConfiguration) GetPort() *int {
return in.Spec.Port
}

// GetHost returns the Host
func (in *DefaultControllerConfiguration) GetHost() string {
return in.Spec.Host
}

// GetCertDir returns the CertDir
func (in *DefaultControllerConfiguration) GetCertDir() string {
return in.Spec.CertDir
}

// Setters

// SetSyncPeriod sets the sync period in time.Duration
func (in *DefaultControllerConfiguration) SetSyncPeriod(syncPeriod *metav1.Duration) {
in.Spec.SyncPeriod = syncPeriod
}

// SetLeaderElectionConfiguration sets the leader election configuration
func (in *DefaultControllerConfiguration) SetLeaderElectionConfiguration(leaderElection configv1alpha1.LeaderElectionConfiguration) {
in.Spec.LeaderElection = leaderElection
}

// SetLeaderElection sets the LeaderElection config
func (in *DefaultControllerConfiguration) SetLeaderElection(leaderElection bool) {
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
}
in.Spec.LeaderElection.LeaderElect = &leaderElection
}

// SetLeaderElectionNamespace returns the LeaderElectionNamespace
func (in *DefaultControllerConfiguration) SetLeaderElectionNamespace(resourceNamespace string) {
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
}
in.Spec.LeaderElection.ResourceNamespace = resourceNamespace
}

// SetLeaderElectionID returns the LeaderElectionID
func (in *DefaultControllerConfiguration) SetLeaderElectionID(resourceName string) {
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
}
in.Spec.LeaderElection.ResourceName = resourceName
}

// SetLeaseDuration returns the LeaseDuration
func (in *DefaultControllerConfiguration) SetLeaseDuration(leaseDuration metav1.Duration) {
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
}
in.Spec.LeaderElection.LeaseDuration = leaseDuration
}

// SetRenewDeadline returns the RenewDeadline
func (in *DefaultControllerConfiguration) SetRenewDeadline(renewDeadline metav1.Duration) {
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
}
in.Spec.LeaderElection.RenewDeadline = renewDeadline
}

// SetRetryPeriod returns the RetryPeriod
func (in *DefaultControllerConfiguration) SetRetryPeriod(retryPeriod metav1.Duration) {
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
}
in.Spec.LeaderElection.RetryPeriod = retryPeriod
}

// SetNamespace returns the Namespace
func (in *DefaultControllerConfiguration) SetNamespace(namespace string) {
in.Spec.Namespace = namespace
}

// SetMetricsBindAddress returns the MetricsBindAddress
func (in *DefaultControllerConfiguration) SetMetricsBindAddress(metricsBindAddress string) {
in.Spec.MetricsBindAddress = metricsBindAddress
}

// SetHealthProbeBindAddress returns the HealthProbeBindAddress
func (in *DefaultControllerConfiguration) SetHealthProbeBindAddress(healthProbeBindAddress string) {
in.Spec.Health.HealthProbeBindAddress = healthProbeBindAddress
}

// SetReadinessEndpointName returns the ReadinessEndpointName
func (in *DefaultControllerConfiguration) SetReadinessEndpointName(readinessEndpointName string) {
in.Spec.Health.ReadinessEndpointName = readinessEndpointName
}

// SetLivenessEndpointName returns the LivenessEndpointName
func (in *DefaultControllerConfiguration) SetLivenessEndpointName(livenessEndpointName string) {
in.Spec.Health.LivenessEndpointName = livenessEndpointName
}

// SetPort returns the Port
func (in *DefaultControllerConfiguration) SetPort(port *int) {
in.Spec.Port = port
}

// SetHost returns the Host
func (in *DefaultControllerConfiguration) SetHost(host string) {
in.Spec.Host = host
}

// SetCertDir returns the CertDir
func (in *DefaultControllerConfiguration) SetCertDir(certDir string) {
in.Spec.CertDir = certDir
}
34 changes: 34 additions & 0 deletions pkg/api/config/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 provides a default ComponentConfig type for configuring
// controller-runtime Options.
// +kubebuilder:object:generate=true
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "controller-runtime.sigs.k8s.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Loading

0 comments on commit d95d4d3

Please sign in to comment.