Skip to content

Commit 956b68e

Browse files
adding ComponentConfig type and Options parsing
Signed-off-by: Chris Hein <[email protected]>
1 parent 5757a38 commit 956b68e

25 files changed

+1255
-1
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ linters:
2828
- unparam
2929
- ineffassign
3030
- nakedret
31-
- interfacer
3231
- gocyclo
3332
- lll
3433
- dupl

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TOOLS_DIR := hack/tools
3939
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
4040
GOLANGCI_LINT := $(abspath $(TOOLS_BIN_DIR)/golangci-lint)
4141
GO_APIDIFF := $(TOOLS_BIN_DIR)/go-apidiff
42+
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
4243

4344
# The help will print out all targets with their descriptions organized bellow their categories. The categories are represented by `##@` and the target descriptions by `##`.
4445
# The awk commands is responsible to read the entire set of makefiles included in this invocation, looking for lines of the file as xyz: ## something, and then pretty-format the target and help. Then, if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -66,6 +67,9 @@ $(GOLANGCI_LINT): $(TOOLS_DIR)/go.mod # Build golangci-lint from tools folder.
6667
$(GO_APIDIFF): $(TOOLS_DIR)/go.mod # Build go-apidiff from tools folder.
6768
cd $(TOOLS_DIR) && go build -tags=tools -o bin/go-apidiff github.com/joelanford/go-apidiff
6869

70+
$(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
71+
cd $(TOOLS_DIR) && go build -tags=tools -o bin/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen
72+
6973
## --------------------------------------
7074
## Linting
7175
## --------------------------------------
@@ -83,6 +87,10 @@ modules: ## Runs go mod to ensure modules are up to date.
8387
go mod tidy
8488
cd $(TOOLS_DIR); go mod tidy
8589

90+
.PHONY: generate
91+
generate: $(CONTROLLER_GEN) ## Runs controller-gen for internal types for config file
92+
$(CONTROLLER_GEN) object paths="./pkg/config/v1alpha1/..."
93+
8694
## --------------------------------------
8795
## Cleanup / Verification
8896
## --------------------------------------
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiServer: controller-runtime.sigs.k8s.io/v1alpha1
2+
kind: GenericControllerConfiguration
3+
namespace: default
4+
metricsBindAddress: :9091
5+
leaderElection:
6+
leaderElect: false
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2018 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 main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
appsv1 "k8s.io/api/apps/v1"
24+
"k8s.io/apimachinery/pkg/api/errors"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
"sigs.k8s.io/controller-runtime/pkg/log"
27+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
28+
)
29+
30+
// reconcileReplicaSet reconciles ReplicaSets
31+
type reconcileReplicaSet struct {
32+
// client can be used to retrieve objects from the APIServer.
33+
client client.Client
34+
}
35+
36+
// Implement reconcile.Reconciler so the controller can reconcile objects
37+
var _ reconcile.Reconciler = &reconcileReplicaSet{}
38+
39+
func (r *reconcileReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
40+
// set up a convenient log object so we don't have to type request over and over again
41+
log := log.FromContext(ctx)
42+
43+
// Fetch the ReplicaSet from the cache
44+
rs := &appsv1.ReplicaSet{}
45+
err := r.client.Get(context.TODO(), request.NamespacedName, rs)
46+
if errors.IsNotFound(err) {
47+
log.Error(nil, "Could not find ReplicaSet")
48+
return reconcile.Result{}, nil
49+
}
50+
51+
if err != nil {
52+
return reconcile.Result{}, fmt.Errorf("could not fetch ReplicaSet: %+v", err)
53+
}
54+
55+
// Print the ReplicaSet
56+
log.Info("Reconciling ReplicaSet", "container name", rs.Spec.Template.Spec.Containers[0].Name)
57+
58+
// Set the label if it is missing
59+
if rs.Labels == nil {
60+
rs.Labels = map[string]string{}
61+
}
62+
if rs.Labels["hello"] == "world" {
63+
return reconcile.Result{}, nil
64+
}
65+
66+
// Update the ReplicaSet
67+
rs.Labels["hello"] = "world"
68+
err = r.client.Update(context.TODO(), rs)
69+
if err != nil {
70+
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err)
71+
}
72+
73+
return reconcile.Result{}, nil
74+
}

examples/configfile/builtin/main.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2020 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 main
18+
19+
import (
20+
"os"
21+
22+
appsv1 "k8s.io/api/apps/v1"
23+
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
26+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
27+
"sigs.k8s.io/controller-runtime/pkg/client/config"
28+
cfg "sigs.k8s.io/controller-runtime/pkg/config"
29+
"sigs.k8s.io/controller-runtime/pkg/controller"
30+
"sigs.k8s.io/controller-runtime/pkg/handler"
31+
"sigs.k8s.io/controller-runtime/pkg/log"
32+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
33+
"sigs.k8s.io/controller-runtime/pkg/manager"
34+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
35+
"sigs.k8s.io/controller-runtime/pkg/source"
36+
)
37+
38+
var scheme = runtime.NewScheme()
39+
40+
func init() {
41+
log.SetLogger(zap.New())
42+
clientgoscheme.AddToScheme(scheme)
43+
}
44+
45+
func main() {
46+
entryLog := log.Log.WithName("entrypoint")
47+
48+
// Setup a Manager
49+
entryLog.Info("setting up manager")
50+
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{
51+
Scheme: scheme,
52+
}.AndFromOrDie(cfg.FromFile()))
53+
if err != nil {
54+
entryLog.Error(err, "unable to set up overall controller manager")
55+
os.Exit(1)
56+
}
57+
58+
// Setup a new controller to reconcile ReplicaSets
59+
entryLog.Info("Setting up controller")
60+
c, err := controller.New("foo-controller", mgr, controller.Options{
61+
Reconciler: &reconcileReplicaSet{client: mgr.GetClient()},
62+
})
63+
if err != nil {
64+
entryLog.Error(err, "unable to set up individual controller")
65+
os.Exit(1)
66+
}
67+
68+
// Watch ReplicaSets and enqueue ReplicaSet object key
69+
if err := c.Watch(&source.Kind{Type: &appsv1.ReplicaSet{}}, &handler.EnqueueRequestForObject{}); err != nil {
70+
entryLog.Error(err, "unable to watch ReplicaSets")
71+
os.Exit(1)
72+
}
73+
74+
// Watch Pods and enqueue owning ReplicaSet key
75+
if err := c.Watch(&source.Kind{Type: &corev1.Pod{}},
76+
&handler.EnqueueRequestForOwner{OwnerType: &appsv1.ReplicaSet{}, IsController: true}); err != nil {
77+
entryLog.Error(err, "unable to watch Pods")
78+
os.Exit(1)
79+
}
80+
81+
entryLog.Info("starting manager")
82+
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
83+
entryLog.Error(err, "unable to run manager")
84+
os.Exit(1)
85+
}
86+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiServer: examples.x-k8s.io/v1alpha1
2+
kind: CustomControllerConfiguration
3+
clusterName: example-test
4+
namespace: default
5+
metricsBindAddress: :8081
6+
leaderElection:
7+
leaderElect: false
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2018 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 main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
appsv1 "k8s.io/api/apps/v1"
24+
"k8s.io/apimachinery/pkg/api/errors"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
"sigs.k8s.io/controller-runtime/pkg/log"
27+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
28+
)
29+
30+
// reconcileReplicaSet reconciles ReplicaSets
31+
type reconcileReplicaSet struct {
32+
// client can be used to retrieve objects from the APIServer.
33+
client client.Client
34+
}
35+
36+
// Implement reconcile.Reconciler so the controller can reconcile objects
37+
var _ reconcile.Reconciler = &reconcileReplicaSet{}
38+
39+
func (r *reconcileReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
40+
// set up a convenient log object so we don't have to type request over and over again
41+
log := log.FromContext(ctx)
42+
43+
// Fetch the ReplicaSet from the cache
44+
rs := &appsv1.ReplicaSet{}
45+
err := r.client.Get(context.TODO(), request.NamespacedName, rs)
46+
if errors.IsNotFound(err) {
47+
log.Error(nil, "Could not find ReplicaSet")
48+
return reconcile.Result{}, nil
49+
}
50+
51+
if err != nil {
52+
return reconcile.Result{}, fmt.Errorf("could not fetch ReplicaSet: %+v", err)
53+
}
54+
55+
// Print the ReplicaSet
56+
log.Info("Reconciling ReplicaSet", "container name", rs.Spec.Template.Spec.Containers[0].Name)
57+
58+
// Set the label if it is missing
59+
if rs.Labels == nil {
60+
rs.Labels = map[string]string{}
61+
}
62+
if rs.Labels["hello"] == "world" {
63+
return reconcile.Result{}, nil
64+
}
65+
66+
// Update the ReplicaSet
67+
rs.Labels["hello"] = "world"
68+
err = r.client.Update(context.TODO(), rs)
69+
if err != nil {
70+
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err)
71+
}
72+
73+
return reconcile.Result{}, nil
74+
}

examples/configfile/custom/main.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright 2020 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 main
18+
19+
import (
20+
"os"
21+
22+
appsv1 "k8s.io/api/apps/v1"
23+
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
26+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
27+
"sigs.k8s.io/controller-runtime/examples/configfile/custom/v1alpha1"
28+
"sigs.k8s.io/controller-runtime/pkg/client/config"
29+
cfg "sigs.k8s.io/controller-runtime/pkg/config"
30+
"sigs.k8s.io/controller-runtime/pkg/controller"
31+
"sigs.k8s.io/controller-runtime/pkg/handler"
32+
"sigs.k8s.io/controller-runtime/pkg/log"
33+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
"sigs.k8s.io/controller-runtime/pkg/manager"
35+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
36+
"sigs.k8s.io/controller-runtime/pkg/source"
37+
)
38+
39+
var scheme = runtime.NewScheme()
40+
41+
func init() {
42+
log.SetLogger(zap.New())
43+
clientgoscheme.AddToScheme(scheme)
44+
v1alpha1.AddToScheme(scheme)
45+
}
46+
47+
func main() {
48+
entryLog := log.Log.WithName("entrypoint")
49+
50+
// Setup a Manager
51+
entryLog.Info("setting up manager")
52+
ctrlConfig := v1alpha1.CustomControllerConfiguration{}
53+
54+
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{
55+
Scheme: scheme,
56+
}.AndFromOrDie(cfg.FromFile().OfKind(&ctrlConfig)))
57+
if err != nil {
58+
entryLog.Error(err, "unable to set up overall controller manager")
59+
os.Exit(1)
60+
}
61+
62+
// Setup a new controller to reconcile ReplicaSets
63+
entryLog.Info("Setting up controller in cluster", "name", ctrlConfig.ClusterName)
64+
c, err := controller.New("foo-controller", mgr, controller.Options{
65+
Reconciler: &reconcileReplicaSet{client: mgr.GetClient()},
66+
})
67+
if err != nil {
68+
entryLog.Error(err, "unable to set up individual controller")
69+
os.Exit(1)
70+
}
71+
72+
// Watch ReplicaSets and enqueue ReplicaSet object key
73+
if err := c.Watch(&source.Kind{Type: &appsv1.ReplicaSet{}}, &handler.EnqueueRequestForObject{}); err != nil {
74+
entryLog.Error(err, "unable to watch ReplicaSets")
75+
os.Exit(1)
76+
}
77+
78+
// Watch Pods and enqueue owning ReplicaSet key
79+
if err := c.Watch(&source.Kind{Type: &corev1.Pod{}},
80+
&handler.EnqueueRequestForOwner{OwnerType: &appsv1.ReplicaSet{}, IsController: true}); err != nil {
81+
entryLog.Error(err, "unable to watch Pods")
82+
os.Exit(1)
83+
}
84+
85+
entryLog.Info("starting manager")
86+
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
87+
entryLog.Error(err, "unable to run manager")
88+
os.Exit(1)
89+
}
90+
}

0 commit comments

Comments
 (0)