sigs.k8s.io/cluster-api@v1.7.1/controllers/alias.go (about) 1 /* 2 Copyright 2022 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 controllers 18 19 import ( 20 "context" 21 "time" 22 23 ctrl "sigs.k8s.io/controller-runtime" 24 "sigs.k8s.io/controller-runtime/pkg/client" 25 "sigs.k8s.io/controller-runtime/pkg/controller" 26 27 "sigs.k8s.io/cluster-api/controllers/remote" 28 clustercontroller "sigs.k8s.io/cluster-api/internal/controllers/cluster" 29 clusterclasscontroller "sigs.k8s.io/cluster-api/internal/controllers/clusterclass" 30 machinecontroller "sigs.k8s.io/cluster-api/internal/controllers/machine" 31 machinedeploymentcontroller "sigs.k8s.io/cluster-api/internal/controllers/machinedeployment" 32 machinehealthcheckcontroller "sigs.k8s.io/cluster-api/internal/controllers/machinehealthcheck" 33 machinesetcontroller "sigs.k8s.io/cluster-api/internal/controllers/machineset" 34 clustertopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/cluster" 35 machinedeploymenttopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/machinedeployment" 36 machinesettopologycontroller "sigs.k8s.io/cluster-api/internal/controllers/topology/machineset" 37 runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client" 38 ) 39 40 // Following types provides access to reconcilers implemented in internal/controllers, thus 41 // allowing users to provide a single binary "batteries included" with Cluster API and providers of choice. 42 43 // ClusterReconciler reconciles a Cluster object. 44 type ClusterReconciler struct { 45 Client client.Client 46 UnstructuredCachingClient client.Client 47 APIReader client.Reader 48 49 // WatchFilterValue is the label value used to filter events prior to reconciliation. 50 WatchFilterValue string 51 } 52 53 func (r *ClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 54 return (&clustercontroller.Reconciler{ 55 Client: r.Client, 56 UnstructuredCachingClient: r.UnstructuredCachingClient, 57 APIReader: r.APIReader, 58 WatchFilterValue: r.WatchFilterValue, 59 }).SetupWithManager(ctx, mgr, options) 60 } 61 62 // MachineReconciler reconciles a Machine object. 63 type MachineReconciler struct { 64 Client client.Client 65 UnstructuredCachingClient client.Client 66 APIReader client.Reader 67 Tracker *remote.ClusterCacheTracker 68 69 // WatchFilterValue is the label value used to filter events prior to reconciliation. 70 WatchFilterValue string 71 72 // NodeDrainClientTimeout timeout of the client used for draining nodes. 73 NodeDrainClientTimeout time.Duration 74 } 75 76 func (r *MachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 77 return (&machinecontroller.Reconciler{ 78 Client: r.Client, 79 UnstructuredCachingClient: r.UnstructuredCachingClient, 80 APIReader: r.APIReader, 81 Tracker: r.Tracker, 82 WatchFilterValue: r.WatchFilterValue, 83 NodeDrainClientTimeout: r.NodeDrainClientTimeout, 84 }).SetupWithManager(ctx, mgr, options) 85 } 86 87 // MachineSetReconciler reconciles a MachineSet object. 88 type MachineSetReconciler struct { 89 Client client.Client 90 UnstructuredCachingClient client.Client 91 APIReader client.Reader 92 Tracker *remote.ClusterCacheTracker 93 94 // WatchFilterValue is the label value used to filter events prior to reconciliation. 95 WatchFilterValue string 96 } 97 98 func (r *MachineSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 99 return (&machinesetcontroller.Reconciler{ 100 Client: r.Client, 101 UnstructuredCachingClient: r.UnstructuredCachingClient, 102 APIReader: r.APIReader, 103 Tracker: r.Tracker, 104 WatchFilterValue: r.WatchFilterValue, 105 }).SetupWithManager(ctx, mgr, options) 106 } 107 108 // MachineDeploymentReconciler reconciles a MachineDeployment object. 109 type MachineDeploymentReconciler struct { 110 Client client.Client 111 UnstructuredCachingClient client.Client 112 APIReader client.Reader 113 114 // WatchFilterValue is the label value used to filter events prior to reconciliation. 115 WatchFilterValue string 116 } 117 118 func (r *MachineDeploymentReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 119 return (&machinedeploymentcontroller.Reconciler{ 120 Client: r.Client, 121 UnstructuredCachingClient: r.UnstructuredCachingClient, 122 APIReader: r.APIReader, 123 WatchFilterValue: r.WatchFilterValue, 124 }).SetupWithManager(ctx, mgr, options) 125 } 126 127 // MachineHealthCheckReconciler reconciles a MachineHealthCheck object. 128 type MachineHealthCheckReconciler struct { 129 Client client.Client 130 Tracker *remote.ClusterCacheTracker 131 132 // WatchFilterValue is the label value used to filter events prior to reconciliation. 133 WatchFilterValue string 134 } 135 136 func (r *MachineHealthCheckReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 137 return (&machinehealthcheckcontroller.Reconciler{ 138 Client: r.Client, 139 Tracker: r.Tracker, 140 WatchFilterValue: r.WatchFilterValue, 141 }).SetupWithManager(ctx, mgr, options) 142 } 143 144 // ClusterTopologyReconciler reconciles a managed topology for a Cluster object. 145 type ClusterTopologyReconciler struct { 146 Client client.Client 147 Tracker *remote.ClusterCacheTracker 148 // APIReader is used to list MachineSets directly via the API server to avoid 149 // race conditions caused by an outdated cache. 150 APIReader client.Reader 151 152 RuntimeClient runtimeclient.Client 153 154 // WatchFilterValue is the label value used to filter events prior to reconciliation. 155 WatchFilterValue string 156 157 // UnstructuredCachingClient provides a client that forces caching of unstructured objects, 158 // thus allowing to optimize reads for templates or provider specific objects in a managed topology. 159 UnstructuredCachingClient client.Client 160 } 161 162 func (r *ClusterTopologyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 163 return (&clustertopologycontroller.Reconciler{ 164 Client: r.Client, 165 APIReader: r.APIReader, 166 Tracker: r.Tracker, 167 RuntimeClient: r.RuntimeClient, 168 UnstructuredCachingClient: r.UnstructuredCachingClient, 169 WatchFilterValue: r.WatchFilterValue, 170 }).SetupWithManager(ctx, mgr, options) 171 } 172 173 // MachineDeploymentTopologyReconciler deletes referenced templates during deletion of topology-owned MachineDeployments. 174 // The templates are only deleted, if they are not used in other MachineDeployments or MachineSets which are not in deleting state, 175 // i.e. the templates would otherwise be orphaned after the MachineDeployment deletion completes. 176 // Note: To achieve this the cluster topology controller sets a finalizer to hook into the MachineDeployment deletions. 177 type MachineDeploymentTopologyReconciler struct { 178 Client client.Client 179 // APIReader is used to list MachineSets directly via the API server to avoid 180 // race conditions caused by an outdated cache. 181 APIReader client.Reader 182 WatchFilterValue string 183 } 184 185 func (r *MachineDeploymentTopologyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 186 return (&machinedeploymenttopologycontroller.Reconciler{ 187 Client: r.Client, 188 APIReader: r.APIReader, 189 WatchFilterValue: r.WatchFilterValue, 190 }).SetupWithManager(ctx, mgr, options) 191 } 192 193 // MachineSetTopologyReconciler deletes referenced templates during deletion of topology-owned MachineSets. 194 // The templates are only deleted, if they are not used in other MachineDeployments or MachineSets which are not in deleting state, 195 // i.e. the templates would otherwise be orphaned after the MachineSet deletion completes. 196 // Note: To achieve this the reconciler sets a finalizer to hook into the MachineSet deletions. 197 type MachineSetTopologyReconciler struct { 198 Client client.Client 199 // APIReader is used to list MachineSets directly via the API server to avoid 200 // race conditions caused by an outdated cache. 201 APIReader client.Reader 202 WatchFilterValue string 203 } 204 205 func (r *MachineSetTopologyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 206 return (&machinesettopologycontroller.Reconciler{ 207 Client: r.Client, 208 APIReader: r.APIReader, 209 WatchFilterValue: r.WatchFilterValue, 210 }).SetupWithManager(ctx, mgr, options) 211 } 212 213 // ClusterClassReconciler reconciles the ClusterClass object. 214 type ClusterClassReconciler struct { 215 // internalReconciler is used to store the reconciler after SetupWithManager 216 // so that the Reconcile function can work. 217 internalReconciler *clusterclasscontroller.Reconciler 218 219 Client client.Client 220 221 // RuntimeClient is a client for calling runtime extensions. 222 RuntimeClient runtimeclient.Client 223 224 // WatchFilterValue is the label value used to filter events prior to reconciliation. 225 WatchFilterValue string 226 227 // UnstructuredCachingClient provides a client that forces caching of unstructured objects, 228 // thus allowing to optimize reads for templates or provider specific objects. 229 UnstructuredCachingClient client.Client 230 } 231 232 func (r *ClusterClassReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error { 233 r.internalReconciler = &clusterclasscontroller.Reconciler{ 234 Client: r.Client, 235 RuntimeClient: r.RuntimeClient, 236 UnstructuredCachingClient: r.UnstructuredCachingClient, 237 WatchFilterValue: r.WatchFilterValue, 238 } 239 return r.internalReconciler.SetupWithManager(ctx, mgr, options) 240 } 241 242 // Reconcile can be used to reconcile a ClusterClass. 243 // Before it can be used, all fields of the ClusterClassReconciler have to be set 244 // and SetupWithManager has to be called. 245 // This method can be used when testing the behavior of the desired state computation of 246 // the Cluster topology controller (because that requires a reconciled ClusterClass). 247 func (r *ClusterClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 248 return r.internalReconciler.Reconcile(ctx, req) 249 }