/
fakerp.go
361 lines (314 loc) · 11.6 KB
/
fakerp.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package fakerp
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
azresources "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/sirupsen/logrus"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/openshift/openshift-azure/pkg/api"
"github.com/openshift/openshift-azure/pkg/cluster/names"
"github.com/openshift/openshift-azure/pkg/fakerp/arm"
armconst "github.com/openshift/openshift-azure/pkg/fakerp/arm/constants"
"github.com/openshift/openshift-azure/pkg/fakerp/client"
"github.com/openshift/openshift-azure/pkg/util/azureclient"
"github.com/openshift/openshift-azure/pkg/util/azureclient/compute"
"github.com/openshift/openshift-azure/pkg/util/azureclient/resources"
"github.com/openshift/openshift-azure/pkg/util/enrich"
"github.com/openshift/openshift-azure/pkg/util/random"
"github.com/openshift/openshift-azure/pkg/util/resourceid"
"github.com/openshift/openshift-azure/pkg/util/vault"
"github.com/openshift/openshift-azure/test/util/diagnostics"
)
func debugDeployerError(ctx context.Context, log *logrus.Entry, cs *api.OpenShiftManagedCluster, testConfig api.TestConfig) error {
if testConfig.ArtifactDir == "" {
log.Debugf("skipping debug deployer as ArtifactDir is empty")
return nil
}
authorizer, err := azureclient.GetAuthorizerFromContext(ctx, api.ContextKeyClientAuthorizer)
if err != nil {
return err
}
deploymentOperations := resources.NewDeploymentOperationsClient(ctx, log, cs.Properties.AzProfile.SubscriptionID, authorizer)
operations, err := deploymentOperations.List(ctx, cs.Properties.AzProfile.ResourceGroup, "azuredeploy", nil)
if err != nil {
log.Warnf("failed to get deployment operations: %v", err)
return err
}
ssc := compute.NewVirtualMachineScaleSetsClient(ctx, log, cs.Properties.AzProfile.SubscriptionID, authorizer)
ssd := diagnostics.NewScalesetDebugger(log, cs.Properties.AzProfile.SubscriptionID, cs.Properties.AzProfile.ResourceGroup, testConfig, ssc)
for _, op := range operations {
if *op.Properties.ProvisioningState == "Succeeded" {
continue
}
b, _ := json.MarshalIndent(op, "", " ")
log.Debug(string(b))
if op.Properties.TargetResource != nil &&
*op.Properties.TargetResource.ResourceType == "Microsoft.Compute/virtualMachineScaleSets" {
ssd.GatherActivityLogs(ctx)
for _, app := range cs.Properties.AgentPoolProfiles {
prefix := names.GetScalesetName(&app, "")
ssName := *op.Properties.TargetResource.ResourceName
if strings.HasPrefix(ssName, prefix) {
ssd.GatherStatuses(ctx, ssName)
ssd.GatherHostLogs(ctx, ssName, app.Count, cs.Config.SSHKey)
}
}
}
}
return nil
}
// GetDeployer return deplyer function for ARM operations
func GetDeployer(log *logrus.Entry, cs *api.OpenShiftManagedCluster, conf *client.Config, testConfig api.TestConfig) api.DeployFn {
return func(ctx context.Context, azuretemplate map[string]interface{}) (*string, error) {
log.Info("applying arm template deployment")
authorizer, err := azureclient.GetAuthorizerFromContext(ctx, api.ContextKeyClientAuthorizer)
if err != nil {
return nil, err
}
deployments := resources.NewDeploymentsClient(ctx, log, cs.Properties.AzProfile.SubscriptionID, authorizer)
future, err := deployments.CreateOrUpdate(ctx, cs.Properties.AzProfile.ResourceGroup, "azuredeploy", azresources.Deployment{
Properties: &azresources.DeploymentProperties{
Template: azuretemplate,
Mode: azresources.Incremental,
},
})
if err != nil {
return nil, err
}
// initiate NetworkManager client
nm, err := newNetworkManager(ctx, log, cs.Properties.AzProfile.SubscriptionID, conf.ManagementResourceGroup)
if err != nil {
return nil, err
}
log.Info("waiting for arm template deployment to complete")
err = future.WaitForCompletionRef(ctx, deployments.Client())
if err != nil {
log.Warnf("deployment failed: %#v", err)
debugDeployerError(ctx, log, cs, testConfig)
}
// we check if PE exists and get IP
// else, deploy PE and return its IP
log.Info("check PE existence")
exist := nm.privateEndpointExists(ctx, fmt.Sprintf("%s-%s", armconst.PrivateEndpointNamePrefix, cs.Name))
// If we dont check plugin version prefix, code gets triggered on v9 single image updates
// TODO remove when v9 goes away
if strings.HasPrefix(cs.Config.PluginVersion, "v1") {
if !exist && cs.Properties.PrivateAPIServer {
log.Info("applying PLS deployment")
plsTemplate, err := arm.GenerateClusterSide(ctx, cs)
if err != nil {
return nil, err
}
future, err = deployments.CreateOrUpdate(ctx, cs.Properties.AzProfile.ResourceGroup, "plsdeploy", azresources.Deployment{
Properties: &azresources.DeploymentProperties{
Template: plsTemplate,
Mode: azresources.Incremental,
},
})
if err != nil {
return nil, err
}
log.Info("waiting for arm template deployment to complete")
err = future.WaitForCompletionRef(ctx, deployments.Client())
if err != nil {
log.Warnf("deployment failed: %#v", err)
debugDeployerError(ctx, log, cs, testConfig)
}
log.Info("applying PE deployment")
peTemplate, err := arm.GenerateRPSide(ctx, cs, conf)
if err != nil {
return nil, err
}
future, err = deployments.CreateOrUpdate(ctx, conf.ManagementResourceGroup, fmt.Sprintf("pedeploy-%d", time.Now().Unix()), azresources.Deployment{
Properties: &azresources.DeploymentProperties{
Template: peTemplate,
Mode: azresources.Incremental,
},
})
if err != nil {
return nil, err
}
log.Info("waiting for arm template deployment to complete")
err = future.WaitForCompletionRef(ctx, deployments.Client())
if err != nil {
log.Warnf("deployment failed: %#v", err)
debugDeployerError(ctx, log, cs, testConfig)
}
}
if cs.Properties.PrivateAPIServer {
log.Info("get PE IP address")
peIP, err := nm.getPrivateEndpointIP(ctx, fmt.Sprintf("%s-%s", armconst.PrivateEndpointNamePrefix, cs.Name))
if err != nil {
return nil, err
}
log.Debugf("PE IP Address %s ", peIP)
return &peIP, nil
}
}
return nil, nil
}
}
func parsePluginVersion(pluginVersion string) (major, minor int, err error) {
_, err = fmt.Sscanf(pluginVersion, "v%d.%d", &major, &minor)
return
}
func createOrUpdateWrapper(ctx context.Context, p api.Plugin, log *logrus.Entry, cs, oldCs *api.OpenShiftManagedCluster, isAdmin bool, conf *client.Config, testConfig api.TestConfig) (*api.OpenShiftManagedCluster, error) {
isUpdate := (oldCs != nil) // this is until we have called writeHelpers()
log.Info("enrich")
err := enrichCs(cs, conf)
if err != nil {
return nil, err
}
clients, err := newClients(ctx, log, cs, testConfig, conf)
if err != nil {
return nil, err
}
log.Info("setting up service principals")
err = clients.aadMgr.ensureApps(ctx)
if err != nil {
return nil, err
}
var errs []error
if isAdmin {
errs = p.ValidateAdmin(ctx, cs, oldCs)
} else {
errs = p.Validate(ctx, cs, oldCs, true)
}
if len(errs) > 0 {
return nil, kerrors.NewAggregate(errs)
}
// the real RP is responsible for validating ClusterVersion and twiddling
// PluginVersion; this is our fake equivalent
switch {
case cs.Properties.ClusterVersion == "":
case isAdmin && cs.Properties.ClusterVersion == "latest":
oldMajor, _, err := parsePluginVersion(cs.Config.PluginVersion)
if err != nil {
return nil, err
}
if oldMajor < 3 {
return nil, fmt.Errorf("tried to upgrade a cluster that is too old")
}
cs.Properties.ClusterVersion = ""
cs.Config.PluginVersion = "latest"
}
log.Info("setting up DNS")
err = clients.dnsMgr.createOrUpdateDns(ctx, cs)
if err != nil {
return nil, err
}
vaultURL, _, err := vault.SplitSecretURL(cs.Properties.APICertProfile.KeyVaultSecretURL)
if err != nil {
return nil, err
}
log.Info("setting up key vault")
err = clients.vaultMgr.createOrUpdateVault(ctx, log, conf.ClientID, cs.Properties.MasterServicePrincipalProfile.ClientID, conf.TenantID, conf.ResourceGroup, cs.Location, vaultURL)
if err != nil {
return nil, err
}
err = clients.vaultMgr.writeTLSCertsToVault(ctx, cs, vaultURL)
if err != nil {
return nil, err
}
if cs.Properties.MonitorProfile.WorkspaceResourceID != "" {
log.Info("enabling ContainerInsights solution on the workspace")
err = createOrUpdateContainerInsights(ctx, log, cs)
if err != nil {
return nil, err
}
err = enrich.MonitorIDAndKey(ctx, clients.workspacesClient, cs)
if err != nil {
return nil, err
}
}
if isAdmin {
// This usage is incorrect and is effectively almost but not quite a
// no-op, but it mirrors what the real RP actually does. In reality we
// would expect the real RP not to call ValidateAdmin() again, or to set
// the third argument here (oldCs) to nil (since it doesn't have access
// to oldCs any more), but as our admin validation code doesn't support
// a nil oldCs on ValidateAdmin, the RP sets it to cs.
errs = p.ValidateAdmin(ctx, cs, cs)
} else {
// This call is not a no-op: it additionally validates all the fields
// dynamically created by the RP (azmosa.io dns, etc.). To be true to
// the real RP, we pass nil here instead of oldCs, because the real RP
// doesn't have access to oldCs on the backend.
errs = p.Validate(ctx, cs, nil, false)
}
if len(errs) > 0 {
return nil, kerrors.NewAggregate(errs)
}
// generate or update the OpenShift config blob
err = p.GenerateConfig(ctx, cs, isUpdate)
if err != nil {
return nil, err
}
// write out development files
log.Info("write helpers")
err = os.MkdirAll("_data/_out", 0750)
if err != nil {
return nil, err
}
// persist the OpenShift container service
log.Info("persist config")
err = writeHelpers(log, cs)
if err != nil {
return nil, err
}
log.Info("plugin createorupdate")
deployer := GetDeployer(log, cs, conf, testConfig)
if err := p.CreateOrUpdate(ctx, cs, isUpdate, deployer); err != nil {
return nil, err
}
// persist the OpenShift container service with final fields
log.Info("persist final config")
err = writeHelpers(log, cs)
if err != nil {
return nil, err
}
return cs, nil
}
func enrichCs(cs *api.OpenShiftManagedCluster, conf *client.Config) error {
cs.Properties.AzProfile = api.AzProfile{
TenantID: conf.TenantID,
SubscriptionID: conf.SubscriptionID,
ResourceGroup: conf.ResourceGroup,
}
// /subscriptions/{subscription}/resourcegroups/{resource_group}/providers/Microsoft.ContainerService/openshiftmanagedClusters/{cluster_name}
cs.ID = resourceid.ResourceID(cs.Properties.AzProfile.SubscriptionID, cs.Properties.AzProfile.ResourceGroup, "Microsoft.ContainerService/openshiftmanagedClusters", cs.Name)
var vaultURL string
var err error
if cs.Properties.APICertProfile.KeyVaultSecretURL != "" {
vaultURL, _, err = vault.SplitSecretURL(cs.Properties.APICertProfile.KeyVaultSecretURL)
if err != nil {
return err
}
} else {
vaultURL, err = random.VaultURL("kv-")
if err != nil {
return err
}
}
cs.Properties.APICertProfile.KeyVaultSecretURL = vaultURL + "/secrets/" + vaultKeyNamePublicHostname
cs.Properties.RouterProfiles[0].RouterCertProfile.KeyVaultSecretURL = vaultURL + "/secrets/" + vaultKeyNameRouter
cs.Properties.PublicHostname = "openshift." + conf.ResourceGroup + "." + conf.DNSDomain
cs.Properties.RouterProfiles[0].PublicSubdomain = "apps." + conf.ResourceGroup + "." + conf.DNSDomain
if cs.Properties.FQDN == "" {
cs.Properties.FQDN, err = random.FQDN(cs.Location+".cloudapp.azure.com", 20)
if err != nil {
return err
}
}
if cs.Properties.RouterProfiles[0].FQDN == "" {
cs.Properties.RouterProfiles[0].FQDN, err = random.FQDN(cs.Location+".cloudapp.azure.com", 20)
if err != nil {
return err
}
}
return nil
}