sigs.k8s.io/controller-runtime@v0.18.2/pkg/builder/example_test.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 builder_test 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 24 appsv1 "k8s.io/api/apps/v1" 25 corev1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 28 "sigs.k8s.io/controller-runtime/pkg/builder" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 "sigs.k8s.io/controller-runtime/pkg/client/config" 31 logf "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/reconcile" 36 ) 37 38 func ExampleBuilder_metadata_only() { 39 logf.SetLogger(zap.New()) 40 41 log := logf.Log.WithName("builder-examples") 42 43 mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{}) 44 if err != nil { 45 log.Error(err, "could not create manager") 46 os.Exit(1) 47 } 48 49 cl := mgr.GetClient() 50 err = builder. 51 ControllerManagedBy(mgr). // Create the ControllerManagedBy 52 For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API 53 Owns(&corev1.Pod{}, builder.OnlyMetadata). // ReplicaSet owns Pods created by it, and caches them as metadata only 54 Complete(reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { 55 // Read the ReplicaSet 56 rs := &appsv1.ReplicaSet{} 57 err := cl.Get(ctx, req.NamespacedName, rs) 58 if err != nil { 59 return reconcile.Result{}, client.IgnoreNotFound(err) 60 } 61 62 // List the Pods matching the PodTemplate Labels, but only their metadata 63 var podsMeta metav1.PartialObjectMetadataList 64 err = cl.List(ctx, &podsMeta, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels)) 65 if err != nil { 66 return reconcile.Result{}, client.IgnoreNotFound(err) 67 } 68 69 // Update the ReplicaSet 70 rs.Labels["pod-count"] = fmt.Sprintf("%v", len(podsMeta.Items)) 71 err = cl.Update(ctx, rs) 72 if err != nil { 73 return reconcile.Result{}, err 74 } 75 76 return reconcile.Result{}, nil 77 })) 78 if err != nil { 79 log.Error(err, "could not create controller") 80 os.Exit(1) 81 } 82 83 if err := mgr.Start(signals.SetupSignalHandler()); err != nil { 84 log.Error(err, "could not start manager") 85 os.Exit(1) 86 } 87 } 88 89 // This example creates a simple application ControllerManagedBy that is configured for ReplicaSets and Pods. 90 // 91 // * Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into 92 // ReplicaSetReconciler. 93 // 94 // * Start the application. 95 func ExampleBuilder() { 96 logf.SetLogger(zap.New()) 97 98 log := logf.Log.WithName("builder-examples") 99 100 mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{}) 101 if err != nil { 102 log.Error(err, "could not create manager") 103 os.Exit(1) 104 } 105 106 err = builder. 107 ControllerManagedBy(mgr). // Create the ControllerManagedBy 108 For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API 109 Owns(&corev1.Pod{}). // ReplicaSet owns Pods created by it 110 Complete(&ReplicaSetReconciler{ 111 Client: mgr.GetClient(), 112 }) 113 if err != nil { 114 log.Error(err, "could not create controller") 115 os.Exit(1) 116 } 117 118 if err := mgr.Start(signals.SetupSignalHandler()); err != nil { 119 log.Error(err, "could not start manager") 120 os.Exit(1) 121 } 122 } 123 124 // ReplicaSetReconciler is a simple ControllerManagedBy example implementation. 125 type ReplicaSetReconciler struct { 126 client.Client 127 } 128 129 // Implement the business logic: 130 // This function will be called when there is a change to a ReplicaSet or a Pod with an OwnerReference 131 // to a ReplicaSet. 132 // 133 // * Read the ReplicaSet 134 // * Read the Pods 135 // * Set a Label on the ReplicaSet with the Pod count. 136 func (a *ReplicaSetReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { 137 // Read the ReplicaSet 138 rs := &appsv1.ReplicaSet{} 139 err := a.Get(ctx, req.NamespacedName, rs) 140 if err != nil { 141 return reconcile.Result{}, err 142 } 143 144 // List the Pods matching the PodTemplate Labels 145 pods := &corev1.PodList{} 146 err = a.List(ctx, pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels)) 147 if err != nil { 148 return reconcile.Result{}, err 149 } 150 151 // Update the ReplicaSet 152 rs.Labels["pod-count"] = fmt.Sprintf("%v", len(pods.Items)) 153 err = a.Update(ctx, rs) 154 if err != nil { 155 return reconcile.Result{}, err 156 } 157 158 return reconcile.Result{}, nil 159 }