Skip to content

Commit b3e65f7

Browse files
adding ComponentConfig type and Options parsing
Signed-off-by: Chris Hein <[email protected]>
1 parent 4e1e8ed commit b3e65f7

File tree

11 files changed

+566
-2
lines changed

11 files changed

+566
-2
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
run:
22
deadline: 5m
3+
skip-files:
4+
- pkg/api/config/v1alpha1/zz_generated.deepcopy.go
35
linters-settings:
46
lll:
57
line-length: 170

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export GOPROXY
3434
# Active module mode, as we use go modules to manage dependencies
3535
export GO111MODULE=on
3636

37+
ifeq (,$(shell go env GOBIN))
38+
GOBIN=$(shell go env GOPATH)/bin
39+
else
40+
GOBIN=$(shell go env GOBIN)
41+
endif
42+
3743
# Tools.
3844
TOOLS_DIR := hack/tools
3945
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
@@ -62,6 +68,25 @@ test: ## Run the script check-everything.sh which will check all.
6268
$(GOLANGCI_LINT): $(TOOLS_DIR)/go.mod # Build golangci-lint from tools folder.
6369
cd $(TOOLS_DIR); go build -tags=tools -o bin/golangci-lint github.com/golangci/golangci-lint/cmd/golangci-lint
6470

71+
controller-gen:
72+
ifeq (, $(shell which controller-gen))
73+
@{ \
74+
set -e ;\
75+
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
76+
cd $$CONTROLLER_GEN_TMP_DIR ;\
77+
go mod init tmp ;\
78+
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
79+
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
80+
}
81+
CONTROLLER_GEN=$(GOBIN)/controller-gen
82+
else
83+
CONTROLLER_GEN=$(shell which controller-gen)
84+
endif
85+
86+
generate: controller-gen
87+
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./pkg/api/..."
88+
89+
6590
## --------------------------------------
6691
## Linting
6792
## --------------------------------------

alias.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ var (
100100
// NewManager returns a new Manager for creating Controllers.
101101
NewManager = manager.New
102102

103+
// NewOptionsFromComponentConfig returns an updated Options struct from a ComponentConfig type
104+
NewOptionsFromComponentConfig = manager.NewOptionsFromComponentConfig
105+
103106
// CreateOrUpdate creates or updates the given object obj in the Kubernetes
104107
// cluster. The object's desired state should be reconciled with the existing
105108
// state using the passed in ReconcileFn. obj must be a struct pointer so that

examples/config/example.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: controller-runtime.config.sigs.k8s.io/v1alpha1
2+
kind: DefaultControllerConfiguration
3+
spec:
4+
port: 9443
5+
metricsBindAddress: ":8080"
6+
leaderElection:
7+
leaderElect: false

hack/boilerplate.go.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/

hack/verify.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ source $(dirname ${BASH_SOURCE})/common.sh
2121
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
2222
cd "${REPO_ROOT}"
2323

24+
header_text "deepcopy gen"
25+
make generate
26+
2427
header_text "running golangci-lint"
2528
make lint
2629

2730
header_text "verifying modules"
28-
make modules verify-modules
31+
make modules verify-modules
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"reflect"
21+
"time"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
25+
)
26+
27+
// Getters
28+
29+
// GetSyncPeriod returns the sync period in time.Duration
30+
func (in *DefaultControllerConfiguration) GetSyncPeriod() *time.Duration {
31+
if in.Spec.SyncPeriod != nil {
32+
return &in.Spec.SyncPeriod.Duration
33+
}
34+
return nil
35+
}
36+
37+
// GetLeaderElection returns the LeaderElection
38+
func (in *DefaultControllerConfiguration) GetLeaderElection() *bool {
39+
return in.Spec.LeaderElection.LeaderElect
40+
}
41+
42+
// GetLeaderElectionNamespace returns the LeaderElectionNamespace
43+
func (in *DefaultControllerConfiguration) GetLeaderElectionNamespace() string {
44+
return in.Spec.LeaderElection.ResourceNamespace
45+
}
46+
47+
// GetLeaderElectionID returns the LeaderElectionID
48+
func (in *DefaultControllerConfiguration) GetLeaderElectionID() string {
49+
return in.Spec.LeaderElection.ResourceName
50+
}
51+
52+
// GetLeaseDuration returns the LeaseDuration
53+
func (in *DefaultControllerConfiguration) GetLeaseDuration() *time.Duration {
54+
return &in.Spec.LeaderElection.LeaseDuration.Duration
55+
}
56+
57+
// GetRenewDeadline returns the RenewDeadline
58+
func (in *DefaultControllerConfiguration) GetRenewDeadline() *time.Duration {
59+
return &in.Spec.LeaderElection.RenewDeadline.Duration
60+
}
61+
62+
// GetRetryPeriod returns the RetryPeriod
63+
func (in *DefaultControllerConfiguration) GetRetryPeriod() *time.Duration {
64+
return &in.Spec.LeaderElection.RetryPeriod.Duration
65+
}
66+
67+
// GetNamespace returns the Namespace
68+
func (in *DefaultControllerConfiguration) GetNamespace() string {
69+
return in.Spec.Namespace
70+
}
71+
72+
// GetMetricsBindAddress returns the MetricsBindAddress
73+
func (in *DefaultControllerConfiguration) GetMetricsBindAddress() string {
74+
return in.Spec.MetricsBindAddress
75+
}
76+
77+
// GetHealthProbeBindAddress returns the HealthProbeBindAddress
78+
func (in *DefaultControllerConfiguration) GetHealthProbeBindAddress() string {
79+
return in.Spec.Health.HealthProbeBindAddress
80+
}
81+
82+
// GetReadinessEndpointName returns the ReadinessEndpointName
83+
func (in *DefaultControllerConfiguration) GetReadinessEndpointName() string {
84+
return in.Spec.Health.ReadinessEndpointName
85+
}
86+
87+
// GetLivenessEndpointName returns the LivenessEndpointName
88+
func (in *DefaultControllerConfiguration) GetLivenessEndpointName() string {
89+
return in.Spec.Health.LivenessEndpointName
90+
}
91+
92+
// GetPort returns the Port
93+
func (in *DefaultControllerConfiguration) GetPort() *int {
94+
return in.Spec.Port
95+
}
96+
97+
// GetHost returns the Host
98+
func (in *DefaultControllerConfiguration) GetHost() string {
99+
return in.Spec.Host
100+
}
101+
102+
// GetCertDir returns the CertDir
103+
func (in *DefaultControllerConfiguration) GetCertDir() string {
104+
return in.Spec.CertDir
105+
}
106+
107+
// Setters
108+
109+
// SetSyncPeriod sets the sync period in time.Duration
110+
func (in *DefaultControllerConfiguration) SetSyncPeriod(syncPeriod *metav1.Duration) {
111+
in.Spec.SyncPeriod = syncPeriod
112+
}
113+
114+
// SetLeaderElectionConfiguration sets the leader election configuration
115+
func (in *DefaultControllerConfiguration) SetLeaderElectionConfiguration(leaderElection configv1alpha1.LeaderElectionConfiguration) {
116+
in.Spec.LeaderElection = leaderElection
117+
}
118+
119+
// SetLeaderElection sets the LeaderElection config
120+
func (in *DefaultControllerConfiguration) SetLeaderElection(leaderElection bool) {
121+
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
122+
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
123+
}
124+
in.Spec.LeaderElection.LeaderElect = &leaderElection
125+
}
126+
127+
// SetLeaderElectionNamespace returns the LeaderElectionNamespace
128+
func (in *DefaultControllerConfiguration) SetLeaderElectionNamespace(resourceNamespace string) {
129+
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
130+
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
131+
}
132+
in.Spec.LeaderElection.ResourceNamespace = resourceNamespace
133+
}
134+
135+
// SetLeaderElectionID returns the LeaderElectionID
136+
func (in *DefaultControllerConfiguration) SetLeaderElectionID(resourceName string) {
137+
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
138+
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
139+
}
140+
in.Spec.LeaderElection.ResourceName = resourceName
141+
}
142+
143+
// SetLeaseDuration returns the LeaseDuration
144+
func (in *DefaultControllerConfiguration) SetLeaseDuration(leaseDuration metav1.Duration) {
145+
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
146+
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
147+
}
148+
in.Spec.LeaderElection.LeaseDuration = leaseDuration
149+
}
150+
151+
// SetRenewDeadline returns the RenewDeadline
152+
func (in *DefaultControllerConfiguration) SetRenewDeadline(renewDeadline metav1.Duration) {
153+
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
154+
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
155+
}
156+
in.Spec.LeaderElection.RenewDeadline = renewDeadline
157+
}
158+
159+
// SetRetryPeriod returns the RetryPeriod
160+
func (in *DefaultControllerConfiguration) SetRetryPeriod(retryPeriod metav1.Duration) {
161+
if reflect.DeepEqual(in.Spec.LeaderElection, configv1alpha1.LeaderElectionConfiguration{}) {
162+
in.SetLeaderElectionConfiguration(configv1alpha1.LeaderElectionConfiguration{})
163+
}
164+
in.Spec.LeaderElection.RetryPeriod = retryPeriod
165+
}
166+
167+
// SetNamespace returns the Namespace
168+
func (in *DefaultControllerConfiguration) SetNamespace(namespace string) {
169+
in.Spec.Namespace = namespace
170+
}
171+
172+
// SetMetricsBindAddress returns the MetricsBindAddress
173+
func (in *DefaultControllerConfiguration) SetMetricsBindAddress(metricsBindAddress string) {
174+
in.Spec.MetricsBindAddress = metricsBindAddress
175+
}
176+
177+
// SetHealthProbeBindAddress returns the HealthProbeBindAddress
178+
func (in *DefaultControllerConfiguration) SetHealthProbeBindAddress(healthProbeBindAddress string) {
179+
in.Spec.Health.HealthProbeBindAddress = healthProbeBindAddress
180+
}
181+
182+
// SetReadinessEndpointName returns the ReadinessEndpointName
183+
func (in *DefaultControllerConfiguration) SetReadinessEndpointName(readinessEndpointName string) {
184+
in.Spec.Health.ReadinessEndpointName = readinessEndpointName
185+
}
186+
187+
// SetLivenessEndpointName returns the LivenessEndpointName
188+
func (in *DefaultControllerConfiguration) SetLivenessEndpointName(livenessEndpointName string) {
189+
in.Spec.Health.LivenessEndpointName = livenessEndpointName
190+
}
191+
192+
// SetPort returns the Port
193+
func (in *DefaultControllerConfiguration) SetPort(port *int) {
194+
in.Spec.Port = port
195+
}
196+
197+
// SetHost returns the Host
198+
func (in *DefaultControllerConfiguration) SetHost(host string) {
199+
in.Spec.Host = host
200+
}
201+
202+
// SetCertDir returns the CertDir
203+
func (in *DefaultControllerConfiguration) SetCertDir(certDir string) {
204+
in.Spec.CertDir = certDir
205+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
// Package v1alpha1 provides a default ComponentConfig type for configuring
16+
// controller-runtime Options.
17+
// +kubebuilder:object:generate=true
18+
package v1alpha1
19+
20+
import (
21+
"k8s.io/apimachinery/pkg/runtime/schema"
22+
"sigs.k8s.io/controller-runtime/pkg/scheme"
23+
)
24+
25+
var (
26+
// GroupVersion is group version used to register these objects
27+
GroupVersion = schema.GroupVersion{Group: "controller-runtime.sigs.k8s.io", Version: "v1alpha1"}
28+
29+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
30+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
31+
32+
// AddToScheme adds the types in this group-version to the given scheme.
33+
AddToScheme = SchemeBuilder.AddToScheme
34+
)

0 commit comments

Comments
 (0)