This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
/
update_credentials.go
325 lines (279 loc) · 12.3 KB
/
update_credentials.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package client
import (
"github.com/pkg/errors"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"github.com/vmware-tanzu/tanzu-framework/tkg/clusterclient"
"github.com/vmware-tanzu/tanzu-framework/tkg/log"
"github.com/vmware-tanzu/tanzu-framework/tkg/region"
)
// UpdateCredentialsOptions update credential options
type UpdateCredentialsOptions struct {
ClusterName string
Namespace string
Kubeconfig string
VSphereUpdateClusterOptions *VSphereUpdateClusterOptions
AzureUpdateClusterOptions *AzureUpdateClusterOptions
IsRegionalCluster bool
IsCascading bool
}
// VSphereUpdateClusterOptions vsphere credential options
type VSphereUpdateClusterOptions struct {
Username string
Password string
}
// AzureUpdateClusterOptions azure credential options
type AzureUpdateClusterOptions struct {
AzureTenantID string
AzureClientID string
AzureClientSecret string
}
// UpdateCredentialsRegion update management cluster credentials
func (c *TkgClient) UpdateCredentialsRegion(options *UpdateCredentialsOptions) error {
if options == nil {
return errors.New("invalid update cluster options")
}
contexts, err := c.GetRegionContexts(options.ClusterName)
if err != nil || len(contexts) == 0 {
return errors.Errorf("management cluster %s not found", options.ClusterName)
}
currentRegion := contexts[0]
options.Kubeconfig = currentRegion.SourceFilePath
if currentRegion.Status == region.Failed {
return errors.Errorf("cannot update since deployment failed for management cluster %s", currentRegion.ClusterName)
}
regionalClusterClient, err := clusterclient.NewClient(currentRegion.SourceFilePath, currentRegion.ContextName, clusterclient.Options{OperationTimeout: c.timeout})
if err != nil {
return errors.Wrap(err, "unable to get cluster client while upgrading management cluster")
}
isPacific, err := regionalClusterClient.IsPacificRegionalCluster()
if err != nil {
return errors.Wrap(err, "error determining 'Tanzu Kubernetes Cluster service for vSphere' management cluster")
}
if isPacific {
return errors.New("updating 'Tanzu Kubernetes Cluster service for vSphere' management cluster is not yet supported")
}
infraProvider, err := regionalClusterClient.GetRegionalClusterDefaultProviderName(clusterctlv1.InfrastructureProviderType)
if err != nil {
return errors.Wrap(err, "failed to get cluster provider information")
}
infraProviderName, _, err := ParseProviderName(infraProvider)
if err != nil {
return errors.Wrap(err, "failed to parse provider name")
}
log.Infof("Updating credentials for management cluster %q", options.ClusterName)
if infraProviderName == VSphereProviderName {
if err := c.UpdateVSphereClusterCredentials(regionalClusterClient, options); err != nil {
return err
}
} else if infraProviderName == AzureProviderName {
log.Infof("Updating credentials for azure provider")
if err := c.UpdateAzureClusterCredentials(regionalClusterClient, options); err != nil {
return err
}
} else {
return errors.New("Updating '" + infraProviderName + "' cluster is not yet supported")
}
log.Infof("Credentials for management cluster is being updated")
return nil
}
// UpdateCredentialsCluster update cluster credentials
func (c *TkgClient) UpdateCredentialsCluster(options *UpdateCredentialsOptions) error {
currentRegion, err := c.GetCurrentRegionContext()
if err != nil {
return errors.Wrap(err, "cannot get current management cluster context")
}
options.Kubeconfig = currentRegion.SourceFilePath
log.V(4).Info("Creating management cluster client...")
regionalClusterClient, err := clusterclient.NewClient(currentRegion.SourceFilePath, currentRegion.ContextName, clusterclient.Options{OperationTimeout: c.timeout})
if err != nil {
return errors.Wrap(err, "unable to get cluster client while upgrading cluster")
}
isPacific, err := regionalClusterClient.IsPacificRegionalCluster()
if err != nil {
return errors.Wrap(err, "error determining 'Tanzu Kubernetes Cluster service for vSphere' management cluster")
}
if isPacific {
return errors.New("update operation not supported for 'Tanzu Kubernetes Service' clusters")
}
infraProvider, err := regionalClusterClient.GetRegionalClusterDefaultProviderName(clusterctlv1.InfrastructureProviderType)
if err != nil {
return errors.Wrap(err, "failed to get cluster provider information")
}
infraProviderName, _, err := ParseProviderName(infraProvider)
if err != nil {
return errors.Wrap(err, "failed to parse provider name")
}
log.Infof("Updating credentials for workload cluster %q", options.ClusterName)
if infraProviderName == VSphereProviderName {
if err := c.UpdateVSphereClusterCredentials(regionalClusterClient, options); err != nil {
return err
}
} else if infraProviderName == AzureProviderName {
log.V(4).Infof("Updating credentials for azure provider")
if err := c.UpdateAzureClusterCredentials(regionalClusterClient, options); err != nil {
return err
}
} else {
// update operation is supported only on vsphere and azure clusters for now
return errors.New("Updating '" + infraProviderName + "' cluster is not yet supported")
}
log.Infof("Credentials for workload cluster is being updated")
return nil
}
// UpdateVSphereClusterCredentials update vsphere cluster credentials
func (c *TkgClient) UpdateVSphereClusterCredentials(clusterClient clusterclient.Client, options *UpdateCredentialsOptions) error {
if options.VSphereUpdateClusterOptions.Username == "" || options.VSphereUpdateClusterOptions.Password == "" {
return errors.New("either username or password should not be empty")
}
if err := c.updateVSphereCredentialsForCluster(clusterClient, options); err != nil {
return err
}
if options.IsRegionalCluster {
if options.IsCascading {
log.Infof("Updating credentials for all workload clusters under management cluster %q", options.ClusterName)
clusters, err := clusterClient.ListClusters("")
if err != nil {
return errors.Wrapf(err, "unable to update credentials on workload clusters")
}
for i := range clusters {
if clusters[i].Name == options.ClusterName {
continue
}
log.Infof("Updating credentials for workload cluster %q ...", clusters[i].Name)
err := c.updateVSphereCredentialsForCluster(clusterClient, &UpdateCredentialsOptions{
ClusterName: clusters[i].Name,
Namespace: clusters[i].Namespace,
IsRegionalCluster: false,
VSphereUpdateClusterOptions: options.VSphereUpdateClusterOptions,
})
if err != nil {
log.Error(err, "unable to update credentials for workload cluster")
continue
}
}
}
}
return nil
}
func (c *TkgClient) updateVSphereCredentialsForCluster(clusterClient clusterclient.Client, options *UpdateCredentialsOptions) error {
if options.IsRegionalCluster {
// update capv-manager-bootstrap-credentials
if err := clusterClient.UpdateCapvManagerBootstrapCredentialsSecret(options.VSphereUpdateClusterOptions.Username, options.VSphereUpdateClusterOptions.Password); err != nil {
return err
}
}
// update cluster identityRef secret if present
if err := clusterClient.UpdateVsphereIdentityRefSecret(options.ClusterName, options.Namespace, options.VSphereUpdateClusterOptions.Username, options.VSphereUpdateClusterOptions.Password); err != nil {
return err
}
// update cloud-provider-vsphere-credentials
if err := clusterClient.UpdateVsphereCloudProviderCredentialsSecret(options.ClusterName, options.Namespace, options.VSphereUpdateClusterOptions.Username, options.VSphereUpdateClusterOptions.Password); err != nil {
return err
}
// update csi-vsphere-config
if err := clusterClient.UpdateVsphereCsiConfigSecret(options.ClusterName, options.Namespace, options.VSphereUpdateClusterOptions.Username, options.VSphereUpdateClusterOptions.Password); err != nil {
return err
}
return nil
}
// UpdateAzureClusterCredentials update azure cluster credentials
func (c *TkgClient) UpdateAzureClusterCredentials(clusterClient clusterclient.Client, options *UpdateCredentialsOptions) error {
if options.AzureUpdateClusterOptions.AzureTenantID == "" || options.AzureUpdateClusterOptions.AzureClientID == "" || options.AzureUpdateClusterOptions.AzureClientSecret == "" {
return errors.New("either tenantId, clientId or clientSecret should not be empty")
}
if !options.IsRegionalCluster {
unifiedIdentity, err := clusterClient.CheckUnifiedAzureClusterIdentity(options.ClusterName, options.Namespace)
if err != nil {
return err
}
if unifiedIdentity {
log.Warningf("Cluster %s use the same AzureClusterIdentity from its management cluster. It cannot be updated separately.", options.ClusterName)
return nil
}
}
if err := c.UpdateAzureCredentialsForCluster(clusterClient, options, false); err != nil {
return err
}
if options.IsRegionalCluster {
clusters, err := clusterClient.ListClusters("")
if err != nil {
return errors.Wrapf(err, "unable to list workload clusters")
}
for i := range clusters {
if clusters[i].Name == options.ClusterName {
continue
}
unifiedIdentity, err := clusterClient.CheckUnifiedAzureClusterIdentity(clusters[i].Name, clusters[i].Namespace)
if err != nil {
return err
}
if options.IsCascading || unifiedIdentity {
log.V(4).Infof("Updating credentials for workload cluster %q ...", clusters[i].Name)
err := c.UpdateAzureCredentialsForCluster(clusterClient, &UpdateCredentialsOptions{
ClusterName: clusters[i].Name,
Namespace: clusters[i].Namespace,
IsRegionalCluster: false,
AzureUpdateClusterOptions: options.AzureUpdateClusterOptions,
}, unifiedIdentity)
if err != nil {
return err
}
}
}
}
return nil
}
func (c *TkgClient) UpdateAzureCredentialsForCluster(clusterClient clusterclient.Client, options *UpdateCredentialsOptions, unifiedIdentity bool) error {
if options.IsRegionalCluster {
// update capz-manager-bootstrap-credentials
log.Infof("Updating secret capz-manager-bootstrap-credentials for management cluster %q", options.ClusterName)
if err := clusterClient.UpdateCapzManagerBootstrapCredentialsSecret(options.AzureUpdateClusterOptions.AzureTenantID, options.AzureUpdateClusterOptions.AzureClientID, options.AzureUpdateClusterOptions.AzureClientSecret); err != nil {
return err
}
// restart capz-controller-manager pod
log.Infof("Restarting capz-controller-manager pod")
if err := c.RestartCAPZControllerManagerPod(clusterClient); err != nil {
return err
}
// UpdateAzureClusterIdentity
log.Infof("Updating cluster %q AzureClusterIdentity", options.ClusterName)
if err := clusterClient.UpdateAzureClusterIdentity(options.ClusterName, options.Namespace, options.AzureUpdateClusterOptions.AzureTenantID, options.AzureUpdateClusterOptions.AzureClientID, options.AzureUpdateClusterOptions.AzureClientSecret); err != nil {
return err
}
} else {
if !unifiedIdentity {
// UpdateAzureClusterIdentity
log.Infof("Updating cluster %q AzureClusterIdentity", options.ClusterName)
if err := clusterClient.UpdateAzureClusterIdentity(options.ClusterName, options.Namespace, options.AzureUpdateClusterOptions.AzureTenantID, options.AzureUpdateClusterOptions.AzureClientID, options.AzureUpdateClusterOptions.AzureClientSecret); err != nil {
return err
}
} else {
log.Warningf("Cluster %s use the same AzureClusterIdentity from its management cluster. It must be updated together.", options.ClusterName)
}
}
// Recycle all KCP in the cluster
log.V(4).Infof("Updating KCP rolloutAfter for cluster %q", options.ClusterName)
if err := clusterClient.UpdateAzureKCP(options.ClusterName, options.Namespace); err != nil {
return err
}
return nil
}
func (c *TkgClient) RestartCAPZControllerManagerPod(clusterClient clusterclient.Client) error {
replicas, err := clusterClient.GetCAPZControllerManagerDeploymentsReplicas()
if err != nil {
return err
}
if replicas != 0 {
log.Infof("Set capz-controller-manager deployment replicas to 0")
if err := clusterClient.UpdateCAPZControllerManagerDeploymentReplicas(int32(0)); err != nil {
return err
}
log.Infof("Reset capz-controller-manager deployment replicas")
if err := clusterClient.UpdateCAPZControllerManagerDeploymentReplicas(replicas); err != nil {
return err
}
}
return nil
}