-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathazurecluster_controller.go
270 lines (227 loc) · 10.6 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
/*
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"
"time"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/label"
corev1 "k8s.io/api/core/v1"
"github.com/go-logr/logr"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"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/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
// AzureClusterReconciler reconciles a AzureCluster object
type AzureClusterReconciler struct {
client.Client
Log logr.Logger
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
createAzureClusterService azureClusterServiceCreator
}
type azureClusterServiceCreator func(clusterScope *scope.ClusterScope) (*azureClusterService, error)
// NewAzureClusterReconciler returns a new AzureClusterReconciler instance
func NewAzureClusterReconciler(client client.Client, log logr.Logger, recorder record.EventRecorder, reconcileTimeout time.Duration, watchFilterValue string) *AzureClusterReconciler {
acr := &AzureClusterReconciler{
Client: client,
Log: log,
Recorder: recorder,
ReconcileTimeout: reconcileTimeout,
WatchFilterValue: watchFilterValue,
}
acr.createAzureClusterService = newAzureClusterService
return acr
}
// SetupWithManager initializes this controller with a manager.
func (r *AzureClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
log := r.Log.WithValues("controller", "AzureCluster")
c, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrav1.AzureCluster{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Build(r)
if err != nil {
return errors.Wrap(err, "error creating controller")
}
// Add a watch on clusterv1.Cluster object for unpause notifications.
if err = c.Watch(
&source.Kind{Type: &clusterv1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(util.ClusterToInfrastructureMapFunc(infrav1.GroupVersion.WithKind("AzureCluster"))),
predicates.ClusterUnpaused(log),
); err != nil {
return errors.Wrap(err, "failed adding a watch for ready clusters")
}
return nil
}
// +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
// Reconcile idempotently gets, creates, and updates a cluster.
func (r *AzureClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(r.ReconcileTimeout))
defer cancel()
log := r.Log.WithValues("namespace", req.Namespace, "azureCluster", req.Name)
ctx, span := tele.Tracer().Start(ctx, "controllers.AzureClusterReconciler.Reconcile",
trace.WithAttributes(
label.String("namespace", req.Namespace),
label.String("name", req.Name),
label.String("kind", "AzureCluster"),
))
defer span.End()
// Fetch the AzureCluster instance
azureCluster := &infrav1.AzureCluster{}
err := r.Get(ctx, req.NamespacedName, azureCluster)
if err != nil {
if apierrors.IsNotFound(err) {
r.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, r.Client, azureCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
r.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)
// Return early if the object or Cluster is paused.
if annotations.IsPaused(cluster, azureCluster) {
r.Recorder.Eventf(azureCluster, corev1.EventTypeNormal, "ClusterPaused", "AzureCluster or linked Cluster is marked as paused. Won't reconcile")
log.Info("AzureCluster or linked Cluster is marked as paused. Won't reconcile")
return ctrl.Result{}, nil
}
if azureCluster.Spec.IdentityRef != nil {
identity, err := GetClusterIdentityFromRef(ctx, r.Client, azureCluster.Namespace, azureCluster.Spec.IdentityRef)
if err != nil {
return reconcile.Result{}, err
}
if !scope.IsClusterNamespaceAllowed(ctx, r.Client, identity.Spec.AllowedNamespaces, azureCluster.Namespace) {
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, infrav1.NamespaceNotAllowedByIdentity, clusterv1.ConditionSeverityError, "")
return reconcile.Result{}, errors.New("AzureClusterIdentity list of allowed namespaces doesn't include current cluster namespace")
}
if identity.Namespace == azureCluster.Namespace {
patchhelper, err := patch.NewHelper(identity, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to init patch helper")
}
identity.ObjectMeta.OwnerReferences = azureCluster.GetOwnerReferences()
if err := patchhelper.Patch(ctx, identity); err != nil {
return reconcile.Result{}, err
}
}
}
// Create the scope.
clusterScope, err := scope.NewClusterScope(ctx, scope.ClusterScopeParams{
Client: r.Client,
Logger: log,
Cluster: cluster,
AzureCluster: azureCluster,
})
if err != nil {
err = errors.Errorf("failed to create scope: %+v", err)
r.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
}
}()
// Handle deleted clusters
if !azureCluster.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, clusterScope)
}
// Handle non-deleted clusters
return r.reconcileNormal(ctx, clusterScope)
}
func (r *AzureClusterReconciler) reconcileNormal(ctx context.Context, clusterScope *scope.ClusterScope) (reconcile.Result, error) {
ctx, span := tele.Tracer().Start(ctx, "controllers.AzureClusterReconciler.reconcileNormal")
defer span.End()
clusterScope.Info("Reconciling AzureCluster")
azureCluster := clusterScope.AzureCluster
// If the AzureCluster doesn't have our finalizer, add it.
controllerutil.AddFinalizer(azureCluster, infrav1.ClusterFinalizer)
// Register the finalizer immediately to avoid orphaning Azure resources on delete
if err := clusterScope.PatchObject(ctx); err != nil {
return reconcile.Result{}, err
}
acr, err := r.createAzureClusterService(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create a new AzureClusterReconciler")
}
if err := acr.Reconcile(ctx); err != nil {
wrappedErr := errors.Wrap(err, "failed to reconcile cluster services")
r.Recorder.Eventf(azureCluster, corev1.EventTypeWarning, "ClusterReconcilerNormalFailed", wrappedErr.Error())
return reconcile.Result{}, wrappedErr
}
// Set APIEndpoints so the Cluster API Cluster Controller can pull them
azureCluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
Host: clusterScope.APIServerHost(),
Port: clusterScope.APIServerPort(),
}
// 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 (r *AzureClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) (reconcile.Result, error) {
ctx, span := tele.Tracer().Start(ctx, "controllers.AzureClusterReconciler.reconcileDelete")
defer span.End()
clusterScope.Info("Reconciling AzureCluster delete")
azureCluster := clusterScope.AzureCluster
conditions.MarkFalse(azureCluster, infrav1.NetworkInfrastructureReadyCondition, clusterv1.DeletedReason, clusterv1.ConditionSeverityInfo, "")
if err := clusterScope.PatchObject(ctx); err != nil {
return reconcile.Result{}, err
}
acr, err := r.createAzureClusterService(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create a new AzureClusterReconciler")
}
if err := acr.Delete(ctx); err != nil {
wrappedErr := errors.Wrapf(err, "error deleting AzureCluster %s/%s", azureCluster.Namespace, azureCluster.Name)
r.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(clusterScope.AzureCluster, infrav1.ClusterFinalizer)
return reconcile.Result{}, nil
}