This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
create.go
285 lines (231 loc) · 9.66 KB
/
create.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
package azureclusteridentity
import (
"context"
"reflect"
apiextensionslabels "github.com/giantswarm/apiextensions/v6/pkg/label"
"github.com/giantswarm/microerror"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v8/pkg/project"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
const (
secretDataFieldName = "clientSecret"
legacySecretClientIDFieldName = "azure.azureoperator.clientid"
legacySecretClientSecretFieldName = "azure.azureoperator.clientsecret"
legacySecretSubscriptionIDFieldName = "azure.azureoperator.subscriptionid"
legacySecretTenantIDFieldName = "azure.azureoperator.tenantid"
)
// EnsureCreated ensures there is an AzureClusterIdentity CR and a related Secret
// with the same contents as the Giant Swarm credential secret.
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
var err error
azureCluster, err := key.ToAzureCluster(obj)
if err != nil {
return microerror.Mask(err)
}
// Retrieve the legacy secret related to the organization this AzureCluster belongs to.
var legacySecret corev1.Secret
{
credentialSecret, err := r.azureClientsFactory.GetCredentialSecret(ctx, azureCluster.ObjectMeta)
if err != nil {
return microerror.Mask(err)
}
err = r.ctrlClient.Get(ctx, client.ObjectKey{Name: credentialSecret.Name, Namespace: credentialSecret.Namespace}, &legacySecret)
if apierrors.IsNotFound(err) {
// Legacy secret does not exist, we can do nothing but hope the IdentityRef is set.
return nil
} else if err != nil {
return microerror.Mask(err)
}
}
if azureCluster.Spec.IdentityRef == nil {
r.logger.Debugf(ctx, "AzureCluster %q has no IdentityRef set, setting it from Secret %q in namespace %q", azureCluster.Name, legacySecret.Name, legacySecret.Namespace)
err = r.ensureNewSecret(ctx, &azureCluster, legacySecret)
if err != nil {
return microerror.Mask(err)
}
identity, err := r.ensureAzureClusterIdentity(ctx, &azureCluster, legacySecret)
if err != nil {
return microerror.Mask(err)
}
azureCluster.Spec.IdentityRef = &corev1.ObjectReference{
Kind: identity.Kind,
Name: identity.Name,
Namespace: identity.Namespace,
}
err = r.ctrlClient.Update(ctx, &azureCluster)
if apierrors.IsConflict(err) {
r.logger.Debugf(ctx, "conflict trying to save object in k8s API concurrently")
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Set IdentityRef for AzureCluster %q", azureCluster.Name)
} else {
// Ensure AzureClusterIdentity is up to date.
azureClusterIdentity := capz.AzureClusterIdentity{}
err := r.ctrlClient.Get(ctx, client.ObjectKey{Name: azureCluster.Spec.IdentityRef.Name, Namespace: azureCluster.Spec.IdentityRef.Namespace}, &azureClusterIdentity)
if apierrors.IsNotFound(err) {
// identityRef is set to a non-existing object. Empty the reference.
r.logger.Debugf(ctx, "AzureClusterIdentity %s/%s was not found for AzureCluster %q", azureCluster.Spec.IdentityRef.Namespace, azureCluster.Spec.IdentityRef.Name, azureCluster.Name)
azureCluster.Spec.IdentityRef = nil
err = r.ctrlClient.Update(ctx, &azureCluster)
if err != nil {
return microerror.Mask(err)
}
return nil
} else if err != nil {
return microerror.Mask(err)
}
// If AzureClusterIdentity was not created by azure-operator, ignore it
managedBy := azureClusterIdentity.Labels[apiextensionslabels.ManagedBy]
if managedBy != project.Name() {
return nil
}
newSecret := corev1.Secret{}
err = r.ctrlClient.Get(ctx, client.ObjectKey{Name: azureClusterIdentity.Spec.ClientSecret.Name, Namespace: azureClusterIdentity.Spec.ClientSecret.Namespace}, &newSecret)
if err != nil {
return microerror.Mask(err)
}
err = r.ensureNewSecretUpdated(ctx, legacySecret, newSecret)
if err != nil {
return microerror.Mask(err)
}
err = r.ensureAzureClusterIdentityUpdated(ctx, &azureCluster, legacySecret, azureClusterIdentity)
if err != nil {
return microerror.Mask(err)
}
}
// Check if the AzureCluster CR has a SubscriptionID set.
if azureCluster.Spec.SubscriptionID == "" {
r.logger.Debugf(ctx, "AzureCluster doesn't have a Subscription ID set.")
azureCluster.Spec.SubscriptionID = string(legacySecret.Data[legacySecretSubscriptionIDFieldName])
err = r.ctrlClient.Update(ctx, &azureCluster)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Set Subscription ID %q in AzureCluster", string(legacySecret.Data[legacySecretSubscriptionIDFieldName]))
}
return nil
}
func (r *Resource) ensureNewSecret(ctx context.Context, azureCluster *capz.AzureCluster, legacySecret corev1.Secret) error {
newName := newSecretName(legacySecret)
r.logger.Debugf(ctx, "Looking for Secret %q in namespace %q", newName, azureCluster.Namespace)
clientSecret := string(legacySecret.Data[legacySecretClientSecretFieldName])
existing := &corev1.Secret{}
err := r.ctrlClient.Get(ctx, client.ObjectKey{Namespace: azureCluster.Namespace, Name: newName}, existing)
if apierrors.IsNotFound(err) {
r.logger.Debugf(ctx, "Secret %q wasn't found in namespace %q, creating it", newName, azureCluster.Namespace)
// We need to create the secret.
newSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: newName,
Namespace: azureCluster.Namespace,
Labels: map[string]string{
apiextensionslabels.ManagedBy: project.Name(),
apiextensionslabels.Organization: legacySecret.GetLabels()[apiextensionslabels.Organization],
},
},
StringData: map[string]string{
secretDataFieldName: clientSecret,
},
}
err := r.ctrlClient.Create(ctx, newSecret)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Secret %q created in namespace %q", newName, azureCluster.Namespace)
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Secret %q found in namespace %q", newName, azureCluster.Namespace)
return nil
}
func (r *Resource) ensureNewSecretUpdated(ctx context.Context, legacySecret corev1.Secret, newSecret corev1.Secret) error {
clientSecret := string(legacySecret.Data[legacySecretClientSecretFieldName])
currentClientSecret := string(newSecret.Data[secretDataFieldName])
if currentClientSecret != clientSecret {
r.logger.Debugf(ctx, "Secret %q is outdated, updating", newSecret.Name)
newSecret.StringData = map[string]string{
secretDataFieldName: clientSecret,
}
err := r.ctrlClient.Update(ctx, &newSecret)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "Secret %q updated successfully", newSecret.Name)
return nil
}
r.logger.Debugf(ctx, "Secret %q is up to date", newSecret.Name)
return nil
}
func (r *Resource) ensureAzureClusterIdentity(ctx context.Context, azureCluster *capz.AzureCluster, legacySecret corev1.Secret) (*capz.AzureClusterIdentity, error) {
newName := newSecretName(legacySecret)
desiredSpec := getAzureClusterIdentitySpec(azureCluster, legacySecret)
r.logger.Debugf(ctx, "Looking for AzureClusterIdentity %q in namespace %q", newName, azureCluster.Namespace)
existing := &capz.AzureClusterIdentity{}
err := r.ctrlClient.Get(ctx, client.ObjectKey{Namespace: azureCluster.Namespace, Name: newName}, existing)
if apierrors.IsNotFound(err) {
r.logger.Debugf(ctx, "AzureClusterIdentity %q wasn't found in namespace %q, creating it", newName, azureCluster.Namespace)
// We need to create the AzureClusterIdentity.
aci := &capz.AzureClusterIdentity{
ObjectMeta: metav1.ObjectMeta{
Name: newName,
Namespace: azureCluster.Namespace,
Labels: map[string]string{
apiextensionslabels.ManagedBy: project.Name(),
apiextensionslabels.Organization: legacySecret.GetLabels()[apiextensionslabels.Organization],
},
},
Spec: desiredSpec,
}
err := r.ctrlClient.Create(ctx, aci)
if err != nil {
return nil, microerror.Mask(err)
}
r.logger.Debugf(ctx, "AzureClusterIdentity %q created in namespace %q", newName, azureCluster.Namespace)
return aci, nil
} else if err != nil {
return nil, microerror.Mask(err)
}
r.logger.Debugf(ctx, "AzureClusterIdentity %q found in namespace %q", newName, azureCluster.Namespace)
return existing, nil
}
func (r *Resource) ensureAzureClusterIdentityUpdated(ctx context.Context, azureCluster *capz.AzureCluster, legacySecret corev1.Secret, existing capz.AzureClusterIdentity) error {
newName := newSecretName(legacySecret)
desiredSpec := getAzureClusterIdentitySpec(azureCluster, legacySecret)
if !reflect.DeepEqual(existing.Spec, desiredSpec) {
r.logger.Debugf(ctx, "AzureClusterIdentity %q is outdated, updating", newName)
existing.Spec = desiredSpec
err := r.ctrlClient.Update(ctx, &existing)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "AzureClusterIdentity %q updated successfully", newName)
return nil
}
r.logger.Debugf(ctx, "AzureClusterIdentity %q is up to date", newName)
return nil
}
func getAzureClusterIdentitySpec(azureCluster *capz.AzureCluster, legacySecret corev1.Secret) capz.AzureClusterIdentitySpec {
newName := newSecretName(legacySecret)
newNamespace := azureCluster.Namespace
clientID := string(legacySecret.Data[legacySecretClientIDFieldName])
tenantID := string(legacySecret.Data[legacySecretTenantIDFieldName])
return capz.AzureClusterIdentitySpec{
Type: capz.ServicePrincipal,
ClientID: clientID,
ClientSecret: corev1.SecretReference{
Name: newName,
Namespace: newNamespace,
},
TenantID: tenantID,
AllowedNamespaces: nil,
}
}