/
openshiftcluster_putorpatch.go
360 lines (305 loc) · 13.3 KB
/
openshiftcluster_putorpatch.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
package frontend
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/go-chi/chi/v5"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/api/admin"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
"github.com/Azure/ARO-RP/pkg/operator"
"github.com/Azure/ARO-RP/pkg/util/version"
)
func (f *frontend) putOrPatchOpenShiftCluster(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
var header http.Header
var b []byte
body := r.Context().Value(middleware.ContextKeyBody).([]byte)
correlationData := r.Context().Value(middleware.ContextKeyCorrelationData).(*api.CorrelationData)
systemData, _ := r.Context().Value(middleware.ContextKeySystemData).(*api.SystemData) // don't panic
originalPath := r.Context().Value(middleware.ContextKeyOriginalPath).(string)
referer := r.Header.Get("Referer")
subId := chi.URLParam(r, "subscriptionId")
resourceProviderNamespace := chi.URLParam(r, "resourceProviderNamespace")
apiVersion := r.URL.Query().Get(api.APIVersionKey)
err := cosmosdb.RetryOnPreconditionFailed(func() error {
var err error
b, err = f._putOrPatchOpenShiftCluster(ctx, log, body, correlationData, systemData, r.URL.Path, originalPath, r.Method, referer, &header, f.apis[apiVersion].OpenShiftClusterConverter, f.apis[apiVersion].OpenShiftClusterStaticValidator, subId, resourceProviderNamespace, apiVersion)
return err
})
frontendOperationResultLog(log, r.Method, err)
reply(log, w, header, b, err)
}
func (f *frontend) _putOrPatchOpenShiftCluster(ctx context.Context, log *logrus.Entry, body []byte, correlationData *api.CorrelationData, systemData *api.SystemData, path, originalPath, method, referer string, header *http.Header, converter api.OpenShiftClusterConverter, staticValidator api.OpenShiftClusterStaticValidator, subId, resourceProviderNamespace string, apiVersion string) ([]byte, error) {
subscription, err := f.validateSubscriptionState(ctx, path, api.SubscriptionStateRegistered)
if err != nil {
return nil, err
}
doc, err := f.dbOpenShiftClusters.Get(ctx, path)
if err != nil && !cosmosdb.IsErrorStatusCode(err, http.StatusNotFound) {
return nil, err
}
isCreate := doc == nil
if isCreate {
originalR, err := azure.ParseResourceID(originalPath)
if err != nil {
return nil, err
}
doc = &api.OpenShiftClusterDocument{
ID: f.dbOpenShiftClusters.NewUUID(),
Key: path,
OpenShiftCluster: &api.OpenShiftCluster{
ID: originalPath,
Name: originalR.ResourceName,
Type: originalR.Provider + "/" + originalR.ResourceType,
Properties: api.OpenShiftClusterProperties{
ArchitectureVersion: version.InstallArchitectureVersion,
ProvisioningState: api.ProvisioningStateSucceeded,
CreatedAt: f.now().UTC(),
CreatedBy: version.GitCommit,
ProvisionedBy: version.GitCommit,
},
},
}
if !f.env.IsLocalDevelopmentMode() /* not local dev or CI */ {
doc.OpenShiftCluster.Properties.FeatureProfile.GatewayEnabled = true
}
}
doc.CorrelationData = correlationData
err = validateTerminalProvisioningState(doc.OpenShiftCluster.Properties.ProvisioningState)
if err != nil {
return nil, err
}
if doc.OpenShiftCluster.Properties.ProvisioningState == api.ProvisioningStateFailed {
switch doc.OpenShiftCluster.Properties.FailedProvisioningState {
case api.ProvisioningStateCreating:
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeRequestNotAllowed, "", "Request is not allowed on cluster whose creation failed. Delete the cluster.")
case api.ProvisioningStateUpdating:
// allow: a previous failure to update should not prevent a new
// operation.
case api.ProvisioningStateDeleting:
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeRequestNotAllowed, "", "Request is not allowed on cluster whose deletion failed. Delete the cluster.")
default:
return nil, fmt.Errorf("unexpected failedProvisioningState %q", doc.OpenShiftCluster.Properties.FailedProvisioningState)
}
}
// If Put or Patch is executed we will enrich document with cluster data.
if !isCreate {
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
f.clusterEnricher.Enrich(timeoutCtx, log, doc.OpenShiftCluster)
}
var ext interface{}
switch method {
// In case of PUT we will take customer request payload and store into database
// Our base structure for unmarshal is skeleton document with values we
// think is required. We expect payload to have everything else required.
case http.MethodPut:
document := &api.OpenShiftCluster{
ID: doc.OpenShiftCluster.ID,
Name: doc.OpenShiftCluster.Name,
Type: doc.OpenShiftCluster.Type,
Properties: api.OpenShiftClusterProperties{
ProvisioningState: doc.OpenShiftCluster.Properties.ProvisioningState,
ClusterProfile: api.ClusterProfile{
PullSecret: doc.OpenShiftCluster.Properties.ClusterProfile.PullSecret,
Version: doc.OpenShiftCluster.Properties.ClusterProfile.Version,
},
},
SystemData: doc.OpenShiftCluster.SystemData,
}
if doc.OpenShiftCluster.Properties.ServicePrincipalProfile != nil {
document.Properties.ServicePrincipalProfile = &api.ServicePrincipalProfile{}
document.Properties.ServicePrincipalProfile.ClientSecret = doc.OpenShiftCluster.Properties.ServicePrincipalProfile.ClientSecret
}
ext = converter.ToExternal(document)
// In case of PATCH we take current cluster document, which is enriched
// from the cluster and use it as base for unmarshal. So customer can
// provide single field json to be updated in the database.
// Patch should be used for updating individual fields of the document.
case http.MethodPatch:
ext = converter.ToExternal(doc.OpenShiftCluster)
}
converter.ExternalNoReadOnly(ext)
err = json.Unmarshal(body, &ext)
if err != nil {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidRequestContent, "", "The request content was invalid and could not be deserialized: %q.", err)
}
if isCreate {
converter.ToInternal(ext, doc.OpenShiftCluster)
err = f.ValidateNewCluster(ctx, subscription, doc.OpenShiftCluster, staticValidator, ext, path)
if err != nil {
return nil, err
}
} else {
err = staticValidator.Static(ext, doc.OpenShiftCluster, f.env.Location(), f.env.Domain(), f.env.FeatureIsSet(env.FeatureRequireD2sV3Workers), path)
if err != nil {
return nil, err
}
}
oldID, oldName, oldType, oldSystemData := doc.OpenShiftCluster.ID, doc.OpenShiftCluster.Name, doc.OpenShiftCluster.Type, doc.OpenShiftCluster.SystemData
converter.ToInternal(ext, doc.OpenShiftCluster)
doc.OpenShiftCluster.ID, doc.OpenShiftCluster.Name, doc.OpenShiftCluster.Type, doc.OpenShiftCluster.SystemData = oldID, oldName, oldType, oldSystemData
// This will update systemData from the values in the header. Old values, which
// is not provided in the header must be preserved
f.systemDataClusterDocEnricher(doc, systemData)
if isCreate {
err = f.validateInstallVersion(ctx, doc.OpenShiftCluster)
if err != nil {
return nil, err
}
// on create, make the cluster resourcegroup ID lower case to work
// around LB/PLS bug
doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID = strings.ToLower(doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID)
doc.ClusterResourceGroupIDKey = strings.ToLower(doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID)
if doc.OpenShiftCluster.Properties.ServicePrincipalProfile != nil {
doc.ClientIDKey = strings.ToLower(doc.OpenShiftCluster.Properties.ServicePrincipalProfile.ClientID)
}
doc.OpenShiftCluster.Properties.ProvisioningState = api.ProvisioningStateCreating
doc.Bucket, err = f.bucketAllocator.Allocate()
if err != nil {
return nil, err
}
} else {
setUpdateProvisioningState(doc, apiVersion)
}
// SetDefaults will set defaults on cluster document
api.SetDefaults(doc, operator.DefaultOperatorFlags)
doc.AsyncOperationID, err = f.newAsyncOperation(ctx, subId, resourceProviderNamespace, doc)
if err != nil {
return nil, err
}
u, err := url.Parse(referer)
if err != nil {
return nil, err
}
u.Path = f.operationsPath(subId, resourceProviderNamespace, doc.AsyncOperationID)
*header = http.Header{
"Azure-AsyncOperation": []string{u.String()},
}
if isCreate {
newdoc, err := f.dbOpenShiftClusters.Create(ctx, doc)
if cosmosdb.IsErrorStatusCode(err, http.StatusPreconditionFailed) {
return nil, f.validateOpenShiftUniqueKey(ctx, doc)
}
doc = newdoc
} else {
doc, err = f.dbOpenShiftClusters.Update(ctx, doc)
}
if err != nil {
return nil, err
}
// We remove sensitive data from document to prevent sensitive data being
// returned to the customer.
doc.OpenShiftCluster.Properties.ClusterProfile.PullSecret = ""
if doc.OpenShiftCluster.Properties.ServicePrincipalProfile != nil {
doc.OpenShiftCluster.Properties.ServicePrincipalProfile.ClientSecret = ""
}
// We don't return enriched worker profile data on PUT/PATCH operations
doc.OpenShiftCluster.Properties.WorkerProfilesStatus = nil
b, err := json.MarshalIndent(converter.ToExternal(doc.OpenShiftCluster), "", " ")
if err != nil {
return nil, err
}
if isCreate {
err = statusCodeError(http.StatusCreated)
}
return b, err
}
// enrichClusterSystemData will selectively overwrite systemData fields based on
// arm inputs
func enrichClusterSystemData(doc *api.OpenShiftClusterDocument, systemData *api.SystemData) {
if systemData == nil {
return
}
if systemData.CreatedAt != nil {
doc.OpenShiftCluster.SystemData.CreatedAt = systemData.CreatedAt
}
if systemData.CreatedBy != "" {
doc.OpenShiftCluster.SystemData.CreatedBy = systemData.CreatedBy
}
if systemData.CreatedByType != "" {
doc.OpenShiftCluster.SystemData.CreatedByType = systemData.CreatedByType
}
if systemData.LastModifiedAt != nil {
doc.OpenShiftCluster.SystemData.LastModifiedAt = systemData.LastModifiedAt
}
if systemData.LastModifiedBy != "" {
doc.OpenShiftCluster.SystemData.LastModifiedBy = systemData.LastModifiedBy
}
if systemData.LastModifiedByType != "" {
doc.OpenShiftCluster.SystemData.LastModifiedByType = systemData.LastModifiedByType
}
}
func (f *frontend) ValidateNewCluster(ctx context.Context, subscription *api.SubscriptionDocument, cluster *api.OpenShiftCluster, staticValidator api.OpenShiftClusterStaticValidator, ext interface{}, path string) error {
err := staticValidator.Static(ext, nil, f.env.Location(), f.env.Domain(), f.env.FeatureIsSet(env.FeatureRequireD2sV3Workers), path)
if err != nil {
return err
}
err = f.skuValidator.ValidateVMSku(ctx, f.env.Environment(), f.env, subscription.ID, subscription.Subscription.Properties.TenantID, cluster)
if err != nil {
return err
}
err = f.quotaValidator.ValidateQuota(ctx, f.env.Environment(), f.env, subscription.ID, subscription.Subscription.Properties.TenantID, cluster)
if err != nil {
return err
}
err = f.providersValidator.ValidateProviders(ctx, f.env.Environment(), f.env, subscription.ID, subscription.Subscription.Properties.TenantID)
if err != nil {
return err
}
return nil
}
// setUpdateProvisioningState Sets either the admin update or update provisioning state
func setUpdateProvisioningState(doc *api.OpenShiftClusterDocument, apiVersion string) {
switch apiVersion {
case admin.APIVersion:
adminUpdateProvisioningState(doc)
default:
updateProvisioningState(doc)
}
}
// Non-admin update (ex: customer cluster update)
func updateProvisioningState(doc *api.OpenShiftClusterDocument) {
doc.OpenShiftCluster.Properties.LastProvisioningState = doc.OpenShiftCluster.Properties.ProvisioningState
doc.OpenShiftCluster.Properties.ProvisioningState = api.ProvisioningStateUpdating
doc.Dequeues = 0
}
// Admin update (ex: cluster maintenance)
func adminUpdateProvisioningState(doc *api.OpenShiftClusterDocument) {
if doc.OpenShiftCluster.Properties.MaintenanceTask.IsMaintenanceOngoingTask() {
doc.OpenShiftCluster.Properties.LastProvisioningState = doc.OpenShiftCluster.Properties.ProvisioningState
doc.OpenShiftCluster.Properties.ProvisioningState = api.ProvisioningStateAdminUpdating
doc.OpenShiftCluster.Properties.LastAdminUpdateError = ""
doc.Dequeues = 0
// Set the maintenance to ongoing so we emit the appropriate signal to customerss
if doc.OpenShiftCluster.Properties.MaintenanceState == api.MaintenanceStatePending {
doc.OpenShiftCluster.Properties.MaintenanceState = api.MaintenanceStatePlanned
} else {
doc.OpenShiftCluster.Properties.MaintenanceState = api.MaintenanceStateUnplanned
}
} else {
// No default needed since we're using an enum
switch doc.OpenShiftCluster.Properties.MaintenanceTask {
case api.MaintenanceTaskPending:
doc.OpenShiftCluster.Properties.MaintenanceState = api.MaintenanceStatePending
case api.MaintenanceTaskNone:
doc.OpenShiftCluster.Properties.MaintenanceState = api.MaintenanceStateNone
case api.MaintenanceTaskCustomerActionNeeded:
doc.OpenShiftCluster.Properties.MaintenanceState = api.MaintenanceStateCustomerActionNeeded
}
// This enables future admin update actions with body `{}` to succeed
doc.OpenShiftCluster.Properties.MaintenanceTask = ""
}
}