-
Notifications
You must be signed in to change notification settings - Fork 431
/
azurecluster_controller.go
338 lines (288 loc) · 14 KB
/
azurecluster_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright 2019 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 controllers
import (
"context"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/pkg/coalescing"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
// AzureClusterReconciler reconciles an AzureCluster object.
type AzureClusterReconciler struct {
client.Client
Recorder record.EventRecorder
Timeouts reconciler.Timeouts
WatchFilterValue string
CredentialCache azure.CredentialCache
createAzureClusterService azureClusterServiceCreator
}
type azureClusterServiceCreator func(clusterScope *scope.ClusterScope) (*azureClusterService, error)
// NewAzureClusterReconciler returns a new AzureClusterReconciler instance.
func NewAzureClusterReconciler(client client.Client, recorder record.EventRecorder, timeouts reconciler.Timeouts, watchFilterValue string, credCache azure.CredentialCache) *AzureClusterReconciler {
acr := &AzureClusterReconciler{
Client: client,
Recorder: recorder,
Timeouts: timeouts,
WatchFilterValue: watchFilterValue,
CredentialCache: credCache,
}
acr.createAzureClusterService = newAzureClusterService
return acr
}
// SetupWithManager initializes this controller with a manager.
func (acr *AzureClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options Options) error {
ctx, log, done := tele.StartSpanWithLogger(ctx,
"controllers.AzureClusterReconciler.SetupWithManager",
tele.KVP("controller", "AzureCluster"),
)
defer done()
var r reconcile.Reconciler = acr
if options.Cache != nil {
r = coalescing.NewReconciler(acr, options.Cache, log)
}
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options.Options).
For(&infrav1.AzureCluster{}).
WithEventFilter(predicates.ResourceHasFilterLabel(log, acr.WatchFilterValue)).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(log)).
// Add a watch on clusterv1.Cluster object for pause/unpause notifications.
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind(infrav1.AzureClusterKind), mgr.GetClient(), &infrav1.AzureCluster{})),
builder.WithPredicates(
ClusterUpdatePauseChange(log),
predicates.ResourceHasFilterLabel(log, acr.WatchFilterValue),
),
).
Complete(r)
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azureclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azureclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinetemplates;azuremachinetemplates/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azureclusteridentities;azureclusteridentities/status,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=list;
// +kubebuilder:rbac:groups=resources.azure.com,resources=resourcegroups,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=resources.azure.com,resources=resourcegroups/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=network.azure.com,resources=natgateways;bastionhosts;privateendpoints;virtualnetworks;virtualnetworkssubnets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=network.azure.com,resources=natgateways/status;bastionhosts/status;privateendpoints/status;virtualnetworks/status;virtualnetworkssubnets/status,verbs=get;list;watch
// Reconcile idempotently gets, creates, and updates a cluster.
func (acr *AzureClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
ctx, cancel := context.WithTimeout(ctx, acr.Timeouts.DefaultedLoopTimeout())
defer cancel()
ctx, log, done := tele.StartSpanWithLogger(
ctx,
"controllers.AzureClusterReconciler.Reconcile",
tele.KVP("namespace", req.Namespace),
tele.KVP("name", req.Name),
tele.KVP("kind", infrav1.AzureClusterKind),
)
defer done()
// Fetch the AzureCluster instance
azureCluster := &infrav1.AzureCluster{}
err := acr.Get(ctx, req.NamespacedName, azureCluster)
if err != nil {
if apierrors.IsNotFound(err) {
acr.Recorder.Eventf(azureCluster, corev1.EventTypeNormal, "AzureClusterObjectNotFound", err.Error())
log.Info("object was not found")
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, acr.Client, azureCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
acr.Recorder.Eventf(azureCluster, corev1.EventTypeNormal, "OwnerRefNotSet", "Cluster Controller has not yet set OwnerRef")
log.Info("Cluster Controller has not yet set OwnerRef")
return reconcile.Result{}, nil
}
log = log.WithValues("cluster", cluster.Name)
// Create the scope.
clusterScope, err := scope.NewClusterScope(ctx, scope.ClusterScopeParams{
Client: acr.Client,
Cluster: cluster,
AzureCluster: azureCluster,
Timeouts: acr.Timeouts,
CredentialCache: acr.CredentialCache,
})
if err != nil {
err = errors.Wrap(err, "failed to create scope")
acr.Recorder.Eventf(azureCluster, corev1.EventTypeWarning, "CreateClusterScopeFailed", err.Error())
return reconcile.Result{}, err
}
// Always close the scope when exiting this function so we can persist any AzureMachine changes.
defer func() {
if err := clusterScope.Close(ctx); err != nil && reterr == nil {
reterr = err
}
}()
// Return early if the object or Cluster is paused.
if annotations.IsPaused(cluster, azureCluster) {
acr.Recorder.Eventf(azureCluster, corev1.EventTypeNormal, "ClusterPaused", "AzureCluster or linked Cluster is marked as paused. Won't reconcile normally")
log.Info("AzureCluster or linked Cluster is marked as paused. Won't reconcile normally")
return acr.reconcilePause(ctx, clusterScope)
}
if azureCluster.Spec.IdentityRef != nil {
err := EnsureClusterIdentity(ctx, acr.Client, azureCluster, azureCluster.Spec.IdentityRef, infrav1.ClusterFinalizer)
if err != nil {
return reconcile.Result{}, err
}
} else {
log.Info(fmt.Sprintf("WARNING, %s", deprecatedManagerCredsWarning))
acr.Recorder.Eventf(azureCluster, corev1.EventTypeWarning, "AzureClusterIdentity", deprecatedManagerCredsWarning)
}
// Handle deleted clusters
if !azureCluster.DeletionTimestamp.IsZero() {
return acr.reconcileDelete(ctx, clusterScope)
}
// Handle non-deleted clusters
return acr.reconcileNormal(ctx, clusterScope)
}
func (acr *AzureClusterReconciler) reconcileNormal(ctx context.Context, clusterScope *scope.ClusterScope) (reconcile.Result, error) {
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.AzureClusterReconciler.reconcileNormal")
defer done()
log.Info("Reconciling AzureCluster")
azureCluster := clusterScope.AzureCluster
// Register our finalizer immediately to avoid orphaning Azure resources on delete
needsPatch := controllerutil.AddFinalizer(azureCluster, infrav1.ClusterFinalizer)
// Register the block-move annotation immediately to avoid moving un-paused ASO resources
needsPatch = AddBlockMoveAnnotation(azureCluster) || needsPatch
if needsPatch {
if err := clusterScope.PatchObject(ctx); err != nil {
return reconcile.Result{}, err
}
}
acs, err := acr.createAzureClusterService(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create a new AzureClusterReconciler")
}
if err := acs.Reconcile(ctx); err != nil {
// Handle terminal & transient errors
var reconcileError azure.ReconcileError
if errors.As(err, &reconcileError) {
if reconcileError.IsTerminal() {
acr.Recorder.Eventf(clusterScope.AzureCluster, corev1.EventTypeWarning, "ReconcileError", errors.Wrapf(err, "failed to reconcile AzureCluster").Error())
log.Error(err, "failed to reconcile AzureCluster", "name", clusterScope.ClusterName())
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.FailedReason, clusterv1.ConditionSeverityError, "")
return reconcile.Result{}, nil
}
if reconcileError.IsTransient() {
if azure.IsOperationNotDoneError(reconcileError) {
log.V(2).Info(fmt.Sprintf("AzureCluster reconcile not done: %s", reconcileError.Error()))
} else {
log.V(2).Info(fmt.Sprintf("transient failure to reconcile AzureCluster, retrying: %s", reconcileError.Error()))
}
return reconcile.Result{RequeueAfter: reconcileError.RequeueAfter()}, nil
}
}
wrappedErr := errors.Wrap(err, "failed to reconcile cluster services")
acr.Recorder.Eventf(azureCluster, corev1.EventTypeWarning, "ClusterReconcilerNormalFailed", wrappedErr.Error())
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.FailedReason, clusterv1.ConditionSeverityError, wrappedErr.Error())
return reconcile.Result{}, wrappedErr
}
if azureCluster.Spec.ControlPlaneEnabled {
// Set APIEndpoints so the Cluster API Cluster Controller can pull them
if azureCluster.Spec.ControlPlaneEndpoint.Host == "" {
azureCluster.Spec.ControlPlaneEndpoint.Host = clusterScope.APIServerHost()
}
if azureCluster.Spec.ControlPlaneEndpoint.Port == 0 {
azureCluster.Spec.ControlPlaneEndpoint.Port = clusterScope.APIServerPort()
}
} else {
if azureCluster.Spec.ControlPlaneEndpoint.Host == "" {
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, "ExternallyManagedControlPlane", clusterv1.ConditionSeverityInfo, "Waiting for the Control Plane host")
return reconcile.Result{}, nil
} else if azureCluster.Spec.ControlPlaneEndpoint.Port == 0 {
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, "ExternallyManagedControlPlane", clusterv1.ConditionSeverityInfo, "Waiting for the Control Plane port")
return reconcile.Result{}, nil
}
}
// No errors, so mark us ready so the Cluster API Cluster Controller can pull it
azureCluster.Status.Ready = true
conditions.MarkTrue(azureCluster, infrav1.NetworkInfrastructureReadyCondition)
return reconcile.Result{}, nil
}
func (acr *AzureClusterReconciler) reconcilePause(ctx context.Context, clusterScope *scope.ClusterScope) (reconcile.Result, error) {
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.AzureClusterReconciler.reconcilePause")
defer done()
log.Info("Reconciling AzureCluster pause")
acs, err := acr.createAzureClusterService(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create a new azureClusterService")
}
if err := acs.Pause(ctx); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to pause cluster services")
}
RemoveBlockMoveAnnotation(clusterScope.AzureCluster)
return reconcile.Result{}, nil
}
func (acr *AzureClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) (reconcile.Result, error) {
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.AzureClusterReconciler.reconcileDelete")
defer done()
log.Info("Reconciling AzureCluster delete")
azureCluster := clusterScope.AzureCluster
acs, err := acr.createAzureClusterService(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create a new AzureClusterReconciler")
}
if err := acs.Delete(ctx); err != nil {
// Handle transient errors
var reconcileError azure.ReconcileError
if errors.As(err, &reconcileError) {
if reconcileError.IsTransient() {
if azure.IsOperationNotDoneError(reconcileError) {
log.V(2).Info(fmt.Sprintf("AzureCluster delete not done: %s", reconcileError.Error()))
} else {
log.V(2).Info("transient failure to delete AzureCluster, retrying")
}
return reconcile.Result{RequeueAfter: reconcileError.RequeueAfter()}, nil
}
}
wrappedErr := errors.Wrapf(err, "error deleting AzureCluster %s/%s", azureCluster.Namespace, azureCluster.Name)
acr.Recorder.Eventf(azureCluster, corev1.EventTypeWarning, "ClusterReconcilerDeleteFailed", wrappedErr.Error())
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, clusterv1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
return reconcile.Result{}, wrappedErr
}
// Cluster is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(azureCluster, infrav1.ClusterFinalizer)
if azureCluster.Spec.IdentityRef != nil {
// Cluster is deleted so remove the identity finalizer.
err := RemoveClusterIdentityFinalizer(ctx, acr.Client, azureCluster, azureCluster.Spec.IdentityRef, infrav1.ClusterFinalizer)
if err != nil {
return reconcile.Result{}, err
}
}
return reconcile.Result{}, nil
}