-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding ComponentConfig type and Options parsing
Signed-off-by: Chris Hein <me@chrishein.com>
- Loading branch information
1 parent
c000ea8
commit cd3b990
Showing
25 changed files
with
1,255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ linters: | |
- unparam | ||
- ineffassign | ||
- nakedret | ||
- interfacer | ||
- gocyclo | ||
- lll | ||
- dupl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 | ||
kind: GenericControllerConfiguration | ||
namespace: default | ||
metricsBindAddress: :9091 | ||
leaderElection: | ||
leaderElect: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2018 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 main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// reconcileReplicaSet reconciles ReplicaSets | ||
type reconcileReplicaSet struct { | ||
// client can be used to retrieve objects from the APIServer. | ||
client client.Client | ||
} | ||
|
||
// Implement reconcile.Reconciler so the controller can reconcile objects | ||
var _ reconcile.Reconciler = &reconcileReplicaSet{} | ||
|
||
func (r *reconcileReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
// set up a convenient log object so we don't have to type request over and over again | ||
log := log.FromContext(ctx) | ||
|
||
// Fetch the ReplicaSet from the cache | ||
rs := &appsv1.ReplicaSet{} | ||
err := r.client.Get(context.TODO(), request.NamespacedName, rs) | ||
if errors.IsNotFound(err) { | ||
log.Error(nil, "Could not find ReplicaSet") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("could not fetch ReplicaSet: %+v", err) | ||
} | ||
|
||
// Print the ReplicaSet | ||
log.Info("Reconciling ReplicaSet", "container name", rs.Spec.Template.Spec.Containers[0].Name) | ||
|
||
// Set the label if it is missing | ||
if rs.Labels == nil { | ||
rs.Labels = map[string]string{} | ||
} | ||
if rs.Labels["hello"] == "world" { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Update the ReplicaSet | ||
rs.Labels["hello"] = "world" | ||
err = r.client.Update(context.TODO(), rs) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2020 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 main | ||
|
||
import ( | ||
"os" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
cfg "sigs.k8s.io/controller-runtime/pkg/config" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
var scheme = runtime.NewScheme() | ||
|
||
func init() { | ||
log.SetLogger(zap.New()) | ||
clientgoscheme.AddToScheme(scheme) | ||
} | ||
|
||
func main() { | ||
entryLog := log.Log.WithName("entrypoint") | ||
|
||
// Setup a Manager | ||
entryLog.Info("setting up manager") | ||
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{ | ||
Scheme: scheme, | ||
}.AndFromOrDie(cfg.FromFile())) | ||
if err != nil { | ||
entryLog.Error(err, "unable to set up overall controller manager") | ||
os.Exit(1) | ||
} | ||
|
||
// Setup a new controller to reconcile ReplicaSets | ||
entryLog.Info("Setting up controller") | ||
c, err := controller.New("foo-controller", mgr, controller.Options{ | ||
Reconciler: &reconcileReplicaSet{client: mgr.GetClient()}, | ||
}) | ||
if err != nil { | ||
entryLog.Error(err, "unable to set up individual controller") | ||
os.Exit(1) | ||
} | ||
|
||
// Watch ReplicaSets and enqueue ReplicaSet object key | ||
if err := c.Watch(&source.Kind{Type: &appsv1.ReplicaSet{}}, &handler.EnqueueRequestForObject{}); err != nil { | ||
entryLog.Error(err, "unable to watch ReplicaSets") | ||
os.Exit(1) | ||
} | ||
|
||
// Watch Pods and enqueue owning ReplicaSet key | ||
if err := c.Watch(&source.Kind{Type: &corev1.Pod{}}, | ||
&handler.EnqueueRequestForOwner{OwnerType: &appsv1.ReplicaSet{}, IsController: true}); err != nil { | ||
entryLog.Error(err, "unable to watch Pods") | ||
os.Exit(1) | ||
} | ||
|
||
entryLog.Info("starting manager") | ||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
entryLog.Error(err, "unable to run manager") | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: examples.x-k8s.io/v1alpha1 | ||
kind: CustomControllerConfiguration | ||
clusterName: example-test | ||
namespace: default | ||
metricsBindAddress: :8081 | ||
leaderElection: | ||
leaderElect: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2018 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 main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// reconcileReplicaSet reconciles ReplicaSets | ||
type reconcileReplicaSet struct { | ||
// client can be used to retrieve objects from the APIServer. | ||
client client.Client | ||
} | ||
|
||
// Implement reconcile.Reconciler so the controller can reconcile objects | ||
var _ reconcile.Reconciler = &reconcileReplicaSet{} | ||
|
||
func (r *reconcileReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
// set up a convenient log object so we don't have to type request over and over again | ||
log := log.FromContext(ctx) | ||
|
||
// Fetch the ReplicaSet from the cache | ||
rs := &appsv1.ReplicaSet{} | ||
err := r.client.Get(context.TODO(), request.NamespacedName, rs) | ||
if errors.IsNotFound(err) { | ||
log.Error(nil, "Could not find ReplicaSet") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("could not fetch ReplicaSet: %+v", err) | ||
} | ||
|
||
// Print the ReplicaSet | ||
log.Info("Reconciling ReplicaSet", "container name", rs.Spec.Template.Spec.Containers[0].Name) | ||
|
||
// Set the label if it is missing | ||
if rs.Labels == nil { | ||
rs.Labels = map[string]string{} | ||
} | ||
if rs.Labels["hello"] == "world" { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Update the ReplicaSet | ||
rs.Labels["hello"] = "world" | ||
err = r.client.Update(context.TODO(), rs) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2020 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 main | ||
|
||
import ( | ||
"os" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
"sigs.k8s.io/controller-runtime/examples/configfile/custom/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
cfg "sigs.k8s.io/controller-runtime/pkg/config" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
var scheme = runtime.NewScheme() | ||
|
||
func init() { | ||
log.SetLogger(zap.New()) | ||
clientgoscheme.AddToScheme(scheme) | ||
v1alpha1.AddToScheme(scheme) | ||
} | ||
|
||
func main() { | ||
entryLog := log.Log.WithName("entrypoint") | ||
|
||
// Setup a Manager | ||
entryLog.Info("setting up manager") | ||
ctrlConfig := v1alpha1.CustomControllerConfiguration{} | ||
|
||
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{ | ||
Scheme: scheme, | ||
}.AndFromOrDie(cfg.FromFile().OfKind(&ctrlConfig))) | ||
if err != nil { | ||
entryLog.Error(err, "unable to set up overall controller manager") | ||
os.Exit(1) | ||
} | ||
|
||
// Setup a new controller to reconcile ReplicaSets | ||
entryLog.Info("Setting up controller in cluster", "name", ctrlConfig.ClusterName) | ||
c, err := controller.New("foo-controller", mgr, controller.Options{ | ||
Reconciler: &reconcileReplicaSet{client: mgr.GetClient()}, | ||
}) | ||
if err != nil { | ||
entryLog.Error(err, "unable to set up individual controller") | ||
os.Exit(1) | ||
} | ||
|
||
// Watch ReplicaSets and enqueue ReplicaSet object key | ||
if err := c.Watch(&source.Kind{Type: &appsv1.ReplicaSet{}}, &handler.EnqueueRequestForObject{}); err != nil { | ||
entryLog.Error(err, "unable to watch ReplicaSets") | ||
os.Exit(1) | ||
} | ||
|
||
// Watch Pods and enqueue owning ReplicaSet key | ||
if err := c.Watch(&source.Kind{Type: &corev1.Pod{}}, | ||
&handler.EnqueueRequestForOwner{OwnerType: &appsv1.ReplicaSet{}, IsController: true}); err != nil { | ||
entryLog.Error(err, "unable to watch Pods") | ||
os.Exit(1) | ||
} | ||
|
||
entryLog.Info("starting manager") | ||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
entryLog.Error(err, "unable to run manager") | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.