/* 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 builder_test import ( "context" "fmt" "os" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/config" logf "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/reconcile" ) func ExampleBuilder_metadata_only() { logf.SetLogger(zap.New()) var log = logf.Log.WithName("builder-examples") mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{}) if err != nil { log.Error(err, "could not create manager") os.Exit(1) } cl := mgr.GetClient() err = builder. ControllerManagedBy(mgr). // Create the ControllerManagedBy For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API Owns(&corev1.Pod{}, builder.OnlyMetadata). // ReplicaSet owns Pods created by it, and caches them as metadata only Complete(reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { // Read the ReplicaSet rs := &appsv1.ReplicaSet{} err := cl.Get(ctx, req.NamespacedName, rs) if err != nil { return reconcile.Result{}, client.IgnoreNotFound(err) } // List the Pods matching the PodTemplate Labels, but only their metadata var podsMeta metav1.PartialObjectMetadataList err = cl.List(ctx, &podsMeta, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels)) if err != nil { return reconcile.Result{}, client.IgnoreNotFound(err) } // Update the ReplicaSet rs.Labels["pod-count"] = fmt.Sprintf("%v", len(podsMeta.Items)) err = cl.Update(ctx, rs) if err != nil { return reconcile.Result{}, err } return reconcile.Result{}, nil })) if err != nil { log.Error(err, "could not create controller") os.Exit(1) } if err := mgr.Start(signals.SetupSignalHandler()); err != nil { log.Error(err, "could not start manager") os.Exit(1) } } // This example creates a simple application ControllerManagedBy that is configured for ReplicaSets and Pods. // // * Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into // ReplicaSetReconciler. // // * Start the application. func ExampleBuilder() { logf.SetLogger(zap.New()) var log = logf.Log.WithName("builder-examples") mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{}) if err != nil { log.Error(err, "could not create manager") os.Exit(1) } err = builder. ControllerManagedBy(mgr). // Create the ControllerManagedBy For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API Owns(&corev1.Pod{}). // ReplicaSet owns Pods created by it Complete(&ReplicaSetReconciler{}) if err != nil { log.Error(err, "could not create controller") os.Exit(1) } if err := mgr.Start(signals.SetupSignalHandler()); err != nil { log.Error(err, "could not start manager") os.Exit(1) } } // ReplicaSetReconciler is a simple ControllerManagedBy example implementation. type ReplicaSetReconciler struct { client.Client } // Implement the business logic: // This function will be called when there is a change to a ReplicaSet or a Pod with an OwnerReference // to a ReplicaSet. // // * Read the ReplicaSet // * Read the Pods // * Set a Label on the ReplicaSet with the Pod count. func (a *ReplicaSetReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { // Read the ReplicaSet rs := &appsv1.ReplicaSet{} err := a.Get(ctx, req.NamespacedName, rs) if err != nil { return reconcile.Result{}, err } // List the Pods matching the PodTemplate Labels pods := &corev1.PodList{} err = a.List(ctx, pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels)) if err != nil { return reconcile.Result{}, err } // Update the ReplicaSet rs.Labels["pod-count"] = fmt.Sprintf("%v", len(pods.Items)) err = a.Update(ctx, rs) if err != nil { return reconcile.Result{}, err } return reconcile.Result{}, nil } func (a *ReplicaSetReconciler) InjectClient(c client.Client) error { a.Client = c return nil }