sigs.k8s.io/controller-runtime@v0.18.2/examples/builtins/main.go (about) 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 "os" 21 22 appsv1 "k8s.io/api/apps/v1" 23 corev1 "k8s.io/api/core/v1" 24 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" 25 "sigs.k8s.io/controller-runtime/pkg/builder" 26 "sigs.k8s.io/controller-runtime/pkg/client/config" 27 "sigs.k8s.io/controller-runtime/pkg/controller" 28 "sigs.k8s.io/controller-runtime/pkg/handler" 29 "sigs.k8s.io/controller-runtime/pkg/log" 30 "sigs.k8s.io/controller-runtime/pkg/log/zap" 31 "sigs.k8s.io/controller-runtime/pkg/manager" 32 "sigs.k8s.io/controller-runtime/pkg/manager/signals" 33 "sigs.k8s.io/controller-runtime/pkg/source" 34 ) 35 36 func init() { 37 log.SetLogger(zap.New()) 38 } 39 40 func main() { 41 entryLog := log.Log.WithName("entrypoint") 42 43 // Setup a Manager 44 entryLog.Info("setting up manager") 45 mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{}) 46 if err != nil { 47 entryLog.Error(err, "unable to set up overall controller manager") 48 os.Exit(1) 49 } 50 51 // Setup a new controller to reconcile ReplicaSets 52 entryLog.Info("Setting up controller") 53 c, err := controller.New("foo-controller", mgr, controller.Options{ 54 Reconciler: &reconcileReplicaSet{client: mgr.GetClient()}, 55 }) 56 if err != nil { 57 entryLog.Error(err, "unable to set up individual controller") 58 os.Exit(1) 59 } 60 61 // Watch ReplicaSets and enqueue ReplicaSet object key 62 if err := c.Watch(source.Kind(mgr.GetCache(), &appsv1.ReplicaSet{}, &handler.TypedEnqueueRequestForObject[*appsv1.ReplicaSet]{})); err != nil { 63 entryLog.Error(err, "unable to watch ReplicaSets") 64 os.Exit(1) 65 } 66 67 // Watch Pods and enqueue owning ReplicaSet key 68 if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, 69 handler.TypedEnqueueRequestForOwner[*corev1.Pod](mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.ReplicaSet{}, handler.OnlyControllerOwner()))); err != nil { 70 entryLog.Error(err, "unable to watch Pods") 71 os.Exit(1) 72 } 73 74 if err := builder.WebhookManagedBy(mgr). 75 For(&corev1.Pod{}). 76 WithDefaulter(&podAnnotator{}). 77 WithValidator(&podValidator{}). 78 Complete(); err != nil { 79 entryLog.Error(err, "unable to create webhook", "webhook", "Pod") 80 os.Exit(1) 81 } 82 83 entryLog.Info("starting manager") 84 if err := mgr.Start(signals.SetupSignalHandler()); err != nil { 85 entryLog.Error(err, "unable to run manager") 86 os.Exit(1) 87 } 88 }