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 10, 2020
1 parent dfb60c9 commit 872756c
Show file tree
Hide file tree
Showing 11 changed files with 562 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

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

// 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
6 changes: 6 additions & 0 deletions examples/config/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: controller-runtime.config.sigs.k8s.io/v1alpha1
kind: GenericControllerConfiguration
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 *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
}
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 the ComponentConfig type for configuring
// controller-runtime
// +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 872756c

Please sign in to comment.