-
Notifications
You must be signed in to change notification settings - Fork 430
/
azurejson_machinetemplate_controller.go
222 lines (194 loc) · 8.02 KB
/
azurejson_machinetemplate_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
/*
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"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
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/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"
"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/azure/services/identities"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
// AzureJSONTemplateReconciler reconciles Azure json secrets for AzureMachineTemplate objects.
type AzureJSONTemplateReconciler struct {
client.Client
Recorder record.EventRecorder
Timeouts reconciler.Timeouts
WatchFilterValue string
CredentialCache azure.CredentialCache
}
// SetupWithManager initializes this controller with a manager.
func (r *AzureJSONTemplateReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
_, log, done := tele.StartSpanWithLogger(ctx,
"controllers.AzureJSONTemplateReconciler.SetupWithManager",
)
defer done()
azureMachineTemplateMapper, err := util.ClusterToTypedObjectsMapper(r.Client, &infrav1.AzureMachineTemplateList{}, mgr.GetScheme())
if err != nil {
return errors.Wrap(err, "failed to create mapper for Cluster to AzureMachineTemplates")
}
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrav1.AzureMachineTemplate{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
Owns(&corev1.Secret{}).
// Add a watch on Clusters to requeue when the infraRef is set. This is needed because the infraRef is not initially
// set in Clusters created from a ClusterClass.
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(azureMachineTemplateMapper),
builder.WithPredicates(
predicates.ClusterUnpausedAndInfrastructureReady(log),
predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue),
),
).
Complete(r)
}
// Reconcile reconciles Azure json secrets for Azure machine templates.
func (r *AzureJSONTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
ctx, cancel := context.WithTimeout(ctx, r.Timeouts.DefaultedLoopTimeout())
defer cancel()
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.AzureJSONTemplateReconciler.Reconcile",
tele.KVP("namespace", req.Namespace),
tele.KVP("name", req.Name),
tele.KVP("kind", "AzureMachineTemplate"),
)
defer done()
// Fetch the AzureMachineTemplate instance
azureMachineTemplate := &infrav1.AzureMachineTemplate{}
err := r.Get(ctx, req.NamespacedName, azureMachineTemplate)
if err != nil {
if apierrors.IsNotFound(err) {
log.Info("object was not found")
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, azureMachineTemplate.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
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, azureMachineTemplate) {
log.Info("AzureMachineTemplate or linked Cluster is marked as paused. Won't reconcile")
return ctrl.Result{}, nil
}
// only look at azure clusters
if cluster.Spec.InfrastructureRef == nil {
log.Info("infra ref is nil")
return ctrl.Result{}, nil
}
if cluster.Spec.InfrastructureRef.Kind != infrav1.AzureClusterKind {
log.WithValues("kind", cluster.Spec.InfrastructureRef.Kind).Info("infra ref was not an AzureCluster")
return ctrl.Result{}, nil
}
// fetch the corresponding azure cluster
azureCluster := &infrav1.AzureCluster{}
azureClusterName := types.NamespacedName{
Namespace: req.Namespace,
Name: cluster.Spec.InfrastructureRef.Name,
}
if err := r.Get(ctx, azureClusterName, azureCluster); err != nil {
log.Error(err, "failed to fetch AzureCluster")
return reconcile.Result{}, err
}
// Create the scope.
clusterScope, err := scope.NewClusterScope(ctx, scope.ClusterScopeParams{
Client: r.Client,
Cluster: cluster,
AzureCluster: azureCluster,
Timeouts: r.Timeouts,
CredentialCache: r.CredentialCache,
})
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create scope")
}
apiVersion, kind := infrav1.GroupVersion.WithKind("AzureMachineTemplate").ToAPIVersionAndKind()
owner := metav1.OwnerReference{
APIVersion: apiVersion,
Kind: kind,
Name: azureMachineTemplate.GetName(),
UID: azureMachineTemplate.GetUID(),
Controller: ptr.To(true),
}
// Construct secret for this machine template
userAssignedIdentityIfExists := ""
if len(azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities) > 0 {
var identitiesClient identities.Client
identitiesClient, err := identities.NewClient(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create identities client")
}
parsed, err := azureutil.ParseResourceID(azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to parse ProviderID %s", azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
}
if parsed.SubscriptionID != clusterScope.SubscriptionID() {
identitiesClient, err = identities.NewClientBySub(clusterScope, parsed.SubscriptionID)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to create identities client from subscription ID %s", parsed.SubscriptionID)
}
}
userAssignedIdentityIfExists, err = identitiesClient.GetClientID(
ctx, azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
}
}
if azureMachineTemplate.Spec.Template.Spec.Identity == infrav1.VMIdentityNone {
log.Info(fmt.Sprintf("WARNING, %s", spIdentityWarning))
r.Recorder.Eventf(azureMachineTemplate, corev1.EventTypeWarning, "VMIdentityNone", spIdentityWarning)
}
newSecret, err := GetCloudProviderSecret(
clusterScope,
azureMachineTemplate.Namespace,
azureMachineTemplate.Name,
owner,
azureMachineTemplate.Spec.Template.Spec.Identity,
userAssignedIdentityIfExists,
)
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to create cloud provider config")
}
if err := reconcileAzureSecret(ctx, r.Client, owner, newSecret, clusterScope.ClusterName()); err != nil {
r.Recorder.Eventf(azureMachineTemplate, corev1.EventTypeWarning, "Error reconciling cloud provider secret for AzureMachineTemplate", err.Error())
return ctrl.Result{}, errors.Wrap(err, "failed to reconcile azure secret")
}
return ctrl.Result{}, nil
}