sigs.k8s.io/cluster-api@v1.7.1/controllers/remote/cluster_cache_reconciler.go (about) 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 remote 18 19 import ( 20 "context" 21 22 "github.com/pkg/errors" 23 apierrors "k8s.io/apimachinery/pkg/api/errors" 24 ctrl "sigs.k8s.io/controller-runtime" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 "sigs.k8s.io/controller-runtime/pkg/controller" 27 "sigs.k8s.io/controller-runtime/pkg/reconcile" 28 29 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 30 "sigs.k8s.io/cluster-api/util/predicates" 31 ) 32 33 // ClusterCacheReconciler is responsible for stopping remote cluster caches when 34 // the cluster for the remote cache is being deleted. 35 type ClusterCacheReconciler struct { 36 Client client.Client 37 Tracker *ClusterCacheTracker 38 39 // WatchFilterValue is the label value used to filter events prior to reconciliation. 40 WatchFilterValue string 41 } 42 43 func (r *ClusterCacheReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 44 err := ctrl.NewControllerManagedBy(mgr). 45 Named("remote/clustercache"). 46 For(&clusterv1.Cluster{}). 47 WithOptions(options). 48 WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)). 49 Complete(r) 50 51 if err != nil { 52 return errors.Wrap(err, "failed setting up with a controller manager") 53 } 54 return nil 55 } 56 57 // Reconcile reconciles Clusters and removes ClusterCaches for any Cluster that cannot be retrieved from the 58 // management cluster. 59 func (r *ClusterCacheReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { 60 log := ctrl.LoggerFrom(ctx) 61 log.V(4).Info("Reconciling") 62 63 var cluster clusterv1.Cluster 64 65 err := r.Client.Get(ctx, req.NamespacedName, &cluster) 66 if err == nil { 67 log.V(4).Info("Cluster still exists") 68 return reconcile.Result{}, nil 69 } else if !apierrors.IsNotFound(err) { 70 log.Error(err, "Error retrieving cluster") 71 return reconcile.Result{}, err 72 } 73 74 log.V(2).Info("Cluster no longer exists") 75 76 r.Tracker.deleteAccessor(ctx, req.NamespacedName) 77 78 return reconcile.Result{}, nil 79 }