-
Notifications
You must be signed in to change notification settings - Fork 431
/
azuremanagedcluster_controller.go
156 lines (131 loc) · 5.58 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
/*
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/go-logr/logr"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/label"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"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/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/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha4"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
// AzureManagedClusterReconciler reconciles a AzureManagedCluster object
type AzureManagedClusterReconciler struct {
client.Client
Log logr.Logger
Recorder record.EventRecorder
ReconcileTimeout time.Duration
WatchFilterValue string
}
// SetupWithManager initializes this controller with a manager.
func (r *AzureManagedClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
log := r.Log.WithValues("controller", "AzureManagedCluster")
azManagedCluster := &infrav1exp.AzureManagedCluster{}
c, err := ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(azManagedCluster).
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(infrav1exp.GroupVersion.WithKind("AzureManagedCluster"))),
predicates.ClusterUnpaused(log),
); err != nil {
return errors.Wrap(err, "failed adding a watch for ready clusters")
}
return nil
}
// +kubebuilder:rbac:groups=exp.infrastructure.cluster.x-k8s.io,resources=azuremanagedclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=exp.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 (r *AzureManagedClusterReconciler) 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, "azureManagedCluster", req.Name)
ctx, span := tele.Tracer().Start(ctx, "controllers.AzureManagedClusterReconciler.Reconcile",
trace.WithAttributes(
label.String("namespace", req.Namespace),
label.String("name", req.Name),
label.String("kind", "AzureManagedCluster"),
))
defer span.End()
// Fetch the AzureManagedCluster instance
aksCluster := &infrav1exp.AzureManagedCluster{}
err := r.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, r.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 := &infrav1exp.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 := r.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, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to init patch helper")
}
// Match whatever the control plane says. We should also enqueue
// requests from control plane to infra cluster to keep this accurate
aksCluster.Status.Ready = controlPlane.Status.Ready
aksCluster.Spec.ControlPlaneEndpoint = controlPlane.Spec.ControlPlaneEndpoint
if err := patchhelper.Patch(ctx, aksCluster); err != nil {
return reconcile.Result{}, err
}
log.Info("Successfully reconciled")
return reconcile.Result{}, nil
}