-
Notifications
You must be signed in to change notification settings - Fork 431
/
azuremanagedcluster_controller.go
180 lines (153 loc) · 6.6 KB
/
azuremanagedcluster_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
/*
Copyright 2020 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"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"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"
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/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/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
// AzureManagedClusterReconciler reconciles an AzureManagedCluster object.
type AzureManagedClusterReconciler struct {
client.Client
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}
// SetupWithManager initializes this controller with a manager.
func (amcr *AzureManagedClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options Options) error {
ctx, log, done := tele.StartSpanWithLogger(ctx,
"controllers.AzureManagedClusterReconciler.SetupWithManager",
tele.KVP("controller", "AzureManagedCluster"),
)
defer done()
var r reconcile.Reconciler = amcr
if options.Cache != nil {
r = coalescing.NewReconciler(amcr, options.Cache, log)
}
azManagedCluster := &infrav1.AzureManagedCluster{}
// create mapper to transform incoming AzureManagedControlPlanes into AzureManagedCluster requests
azureManagedControlPlaneMapper, err := AzureManagedControlPlaneToAzureManagedClusterMapper(ctx, amcr.Client, log)
if err != nil {
return errors.Wrap(err, "failed to create AzureManagedControlPlane to AzureManagedClusters mapper")
}
c, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(options.Options).
For(azManagedCluster).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, amcr.WatchFilterValue)).
// watch AzureManagedControlPlane resources
Watches(
&source.Kind{Type: &infrav1.AzureManagedControlPlane{}},
handler.EnqueueRequestsFromMapFunc(azureManagedControlPlaneMapper),
).
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(ctx, infrav1.GroupVersion.WithKind("AzureManagedCluster"), mgr.GetClient(), &infrav1.AzureManagedCluster{})),
predicates.ClusterUnpaused(log),
predicates.ResourceNotPausedAndHasFilterLabel(log, amcr.WatchFilterValue),
); err != nil {
return errors.Wrap(err, "failed adding a watch for ready clusters")
}
return nil
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
// Reconcile idempotently gets, creates, and updates a managed cluster.
func (amcr *AzureManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(amcr.ReconcileTimeout))
defer cancel()
ctx, log, done := tele.StartSpanWithLogger(
ctx,
"controllers.AzureManagedClusterReconciler.Reconcile",
tele.KVP("namespace", req.Namespace),
tele.KVP("name", req.Name),
tele.KVP("kind", "AzureManagedCluster"),
)
defer done()
// Fetch the AzureManagedCluster instance
aksCluster := &infrav1.AzureManagedCluster{}
err := amcr.Get(ctx, req.NamespacedName, aksCluster)
if err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, amcr.Client, aksCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
log.Info("Cluster Controller has not yet set OwnerRef")
return reconcile.Result{}, nil
}
controlPlane := &infrav1.AzureManagedControlPlane{}
controlPlaneRef := types.NamespacedName{
Name: cluster.Spec.ControlPlaneRef.Name,
Namespace: cluster.Namespace,
}
log = log.WithValues("cluster", cluster.Name)
// Return early if the object or Cluster is paused.
if annotations.IsPaused(cluster, aksCluster) {
log.Info("AzureManagedCluster or linked Cluster is marked as paused. Won't reconcile")
return ctrl.Result{}, nil
}
if err := amcr.Get(ctx, controlPlaneRef, controlPlane); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to get control plane ref")
}
log = log.WithValues("controlPlane", controlPlaneRef.Name)
patchhelper, err := patch.NewHelper(aksCluster, amcr.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to init patch helper")
}
// Infrastructure must be ready before control plane. We should also enqueue
// requests from control plane to infra cluster to keep control plane endpoint accurate.
aksCluster.Status.Ready = true
// We only expect to set the apiserver endpoint values once;
// if we attempted to update existing ControlPlaneEndpoint values, they would be rejected via webhook enforcement.
if aksCluster.Spec.ControlPlaneEndpoint.Host == "" {
aksCluster.Spec.ControlPlaneEndpoint.Host = controlPlane.Spec.ControlPlaneEndpoint.Host
}
if aksCluster.Spec.ControlPlaneEndpoint.Port == 0 {
aksCluster.Spec.ControlPlaneEndpoint.Port = controlPlane.Spec.ControlPlaneEndpoint.Port
}
if err := patchhelper.Patch(ctx, aksCluster); err != nil {
return reconcile.Result{}, err
}
log.Info("Successfully reconciled")
return reconcile.Result{}, nil
}