Skip to content

Commit 6c4beec

Browse files
authored
Populate default tag in cluster configure (#1038)
1 parent d79cbaa commit 6c4beec

File tree

3 files changed

+112
-98
lines changed

3 files changed

+112
-98
lines changed

cli/cmd/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ var _configureCmd = &cobra.Command{
217217

218218
cachedClusterConfig := refreshCachedClusterConfig(awsCreds, _flagClusterDisallowPrompt)
219219

220-
clusterConfig, err := getClusterConfigureConfig(cachedClusterConfig, awsCreds, _flagClusterDisallowPrompt)
220+
clusterConfig, err := getConfigureClusterConfig(cachedClusterConfig, awsCreds, _flagClusterDisallowPrompt)
221221
if err != nil {
222222
exit.Error(err)
223223
}

cli/cmd/lib_cluster_config.go

Lines changed: 107 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func getInstallClusterConfig(awsCreds AWSCredentials, envName string, disallowPr
180180
return clusterConfig, nil
181181
}
182182

183-
func getClusterConfigureConfig(cachedClusterConfig clusterconfig.Config, awsCreds AWSCredentials, disallowPrompt bool) (*clusterconfig.Config, error) {
183+
func getConfigureClusterConfig(cachedClusterConfig clusterconfig.Config, awsCreds AWSCredentials, disallowPrompt bool) (*clusterconfig.Config, error) {
184184
userClusterConfig := &clusterconfig.Config{}
185185
var awsClient *aws.Client
186186

@@ -215,131 +215,144 @@ func getClusterConfigureConfig(cachedClusterConfig clusterconfig.Config, awsCred
215215
}
216216
promptIfNotAdmin(awsClient, disallowPrompt)
217217

218-
if userClusterConfig.Bucket != "" && userClusterConfig.Bucket != cachedClusterConfig.Bucket {
219-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.BucketKey, cachedClusterConfig.Bucket)
218+
err = setConfigFieldsFromCached(userClusterConfig, &cachedClusterConfig, awsClient)
219+
if err != nil {
220+
return nil, err
220221
}
221-
userClusterConfig.Bucket = cachedClusterConfig.Bucket
222222

223-
if userClusterConfig.LogGroup != "" && userClusterConfig.LogGroup != cachedClusterConfig.LogGroup {
224-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.LogGroupKey, cachedClusterConfig.LogGroup)
223+
err = clusterconfig.ConfigurePrompt(userClusterConfig, &cachedClusterConfig, true, disallowPrompt)
224+
if err != nil {
225+
return nil, err
225226
}
226-
userClusterConfig.LogGroup = cachedClusterConfig.LogGroup
227+
}
227228

228-
if userClusterConfig.InstanceType != nil && *userClusterConfig.InstanceType != *cachedClusterConfig.InstanceType {
229-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceTypeKey, *cachedClusterConfig.InstanceType)
230-
}
231-
userClusterConfig.InstanceType = cachedClusterConfig.InstanceType
229+
var err error
230+
userClusterConfig.Telemetry, err = readTelemetryConfig()
231+
if err != nil {
232+
return nil, err
233+
}
232234

233-
if !reflect.DeepEqual(userClusterConfig.Tags, cachedClusterConfig.Tags) {
234-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.TagsKey, s.ObjFlat(*&cachedClusterConfig.Tags))
235+
err = userClusterConfig.Validate(awsClient)
236+
if err != nil {
237+
if _flagClusterConfig != "" {
238+
err = errors.Wrap(err, _flagClusterConfig)
235239
}
240+
return nil, err
241+
}
236242

237-
if len(userClusterConfig.AvailabilityZones) > 0 && !strset.New(userClusterConfig.AvailabilityZones...).IsEqual(strset.New(cachedClusterConfig.AvailabilityZones...)) {
238-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.AvailabilityZonesKey, cachedClusterConfig.AvailabilityZones)
239-
}
240-
userClusterConfig.AvailabilityZones = cachedClusterConfig.AvailabilityZones
243+
confirmConfigureClusterConfig(*userClusterConfig, awsCreds, awsClient, disallowPrompt)
241244

242-
if userClusterConfig.InstanceVolumeSize != cachedClusterConfig.InstanceVolumeSize {
243-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceVolumeSizeKey, cachedClusterConfig.InstanceVolumeSize)
244-
}
245-
userClusterConfig.InstanceVolumeSize = cachedClusterConfig.InstanceVolumeSize
245+
return userClusterConfig, nil
246+
}
246247

247-
if userClusterConfig.InstanceVolumeType != cachedClusterConfig.InstanceVolumeType {
248-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceVolumeTypeKey, cachedClusterConfig.InstanceVolumeType)
249-
}
250-
userClusterConfig.InstanceVolumeType = cachedClusterConfig.InstanceVolumeType
248+
func setConfigFieldsFromCached(userClusterConfig *clusterconfig.Config, cachedClusterConfig *clusterconfig.Config, awsClient *aws.Client) error {
249+
if userClusterConfig.Bucket != "" && userClusterConfig.Bucket != cachedClusterConfig.Bucket {
250+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.BucketKey, cachedClusterConfig.Bucket)
251+
}
252+
userClusterConfig.Bucket = cachedClusterConfig.Bucket
251253

252-
if userClusterConfig.InstanceVolumeIOPS != cachedClusterConfig.InstanceVolumeIOPS {
253-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceVolumeIOPSKey, cachedClusterConfig.InstanceVolumeIOPS)
254-
}
255-
userClusterConfig.InstanceVolumeIOPS = cachedClusterConfig.InstanceVolumeIOPS
254+
if userClusterConfig.LogGroup != "" && userClusterConfig.LogGroup != cachedClusterConfig.LogGroup {
255+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.LogGroupKey, cachedClusterConfig.LogGroup)
256+
}
257+
userClusterConfig.LogGroup = cachedClusterConfig.LogGroup
256258

257-
if userClusterConfig.SubnetVisibility != cachedClusterConfig.SubnetVisibility {
258-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.SubnetVisibilityKey, cachedClusterConfig.SubnetVisibility)
259-
}
260-
userClusterConfig.SubnetVisibility = cachedClusterConfig.SubnetVisibility
259+
if userClusterConfig.InstanceType != nil && *userClusterConfig.InstanceType != *cachedClusterConfig.InstanceType {
260+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceTypeKey, *cachedClusterConfig.InstanceType)
261+
}
262+
userClusterConfig.InstanceType = cachedClusterConfig.InstanceType
261263

262-
if userClusterConfig.NATGateway != cachedClusterConfig.NATGateway {
263-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.NATGatewayKey, cachedClusterConfig.NATGateway)
264-
}
265-
userClusterConfig.NATGateway = cachedClusterConfig.NATGateway
264+
if _, ok := userClusterConfig.Tags[clusterconfig.ClusterNameTag]; !ok {
265+
userClusterConfig.Tags[clusterconfig.ClusterNameTag] = userClusterConfig.ClusterName
266+
}
267+
if !reflect.DeepEqual(userClusterConfig.Tags, cachedClusterConfig.Tags) {
268+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.TagsKey, s.ObjFlat(cachedClusterConfig.Tags))
269+
}
266270

267-
if userClusterConfig.APILoadBalancerScheme != cachedClusterConfig.APILoadBalancerScheme {
268-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.APILoadBalancerSchemeKey, cachedClusterConfig.APILoadBalancerScheme)
269-
}
270-
userClusterConfig.APILoadBalancerScheme = cachedClusterConfig.APILoadBalancerScheme
271+
if len(userClusterConfig.AvailabilityZones) > 0 && !strset.New(userClusterConfig.AvailabilityZones...).IsEqual(strset.New(cachedClusterConfig.AvailabilityZones...)) {
272+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.AvailabilityZonesKey, cachedClusterConfig.AvailabilityZones)
273+
}
274+
userClusterConfig.AvailabilityZones = cachedClusterConfig.AvailabilityZones
271275

272-
if userClusterConfig.OperatorLoadBalancerScheme != cachedClusterConfig.OperatorLoadBalancerScheme {
273-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OperatorLoadBalancerSchemeKey, cachedClusterConfig.OperatorLoadBalancerScheme)
274-
}
275-
userClusterConfig.OperatorLoadBalancerScheme = cachedClusterConfig.OperatorLoadBalancerScheme
276+
if userClusterConfig.InstanceVolumeSize != cachedClusterConfig.InstanceVolumeSize {
277+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceVolumeSizeKey, cachedClusterConfig.InstanceVolumeSize)
278+
}
279+
userClusterConfig.InstanceVolumeSize = cachedClusterConfig.InstanceVolumeSize
276280

277-
if userClusterConfig.Spot != nil && *userClusterConfig.Spot != *cachedClusterConfig.Spot {
278-
return nil, clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.SpotKey, *cachedClusterConfig.Spot)
279-
}
280-
userClusterConfig.Spot = cachedClusterConfig.Spot
281+
if userClusterConfig.InstanceVolumeType != cachedClusterConfig.InstanceVolumeType {
282+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceVolumeTypeKey, cachedClusterConfig.InstanceVolumeType)
283+
}
284+
userClusterConfig.InstanceVolumeType = cachedClusterConfig.InstanceVolumeType
281285

282-
if userClusterConfig.Spot != nil && *userClusterConfig.Spot {
283-
err = userClusterConfig.FillEmptySpotFields(awsClient)
284-
if err != nil {
285-
return nil, err
286-
}
287-
}
286+
if userClusterConfig.InstanceVolumeIOPS != cachedClusterConfig.InstanceVolumeIOPS {
287+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceVolumeIOPSKey, cachedClusterConfig.InstanceVolumeIOPS)
288+
}
289+
userClusterConfig.InstanceVolumeIOPS = cachedClusterConfig.InstanceVolumeIOPS
288290

289-
if userClusterConfig.SpotConfig != nil && s.Obj(userClusterConfig.SpotConfig) != s.Obj(cachedClusterConfig.SpotConfig) {
290-
if cachedClusterConfig.SpotConfig == nil {
291-
return nil, clusterconfig.ErrorConfiguredWhenSpotIsNotEnabled(clusterconfig.SpotConfigKey)
292-
}
291+
if userClusterConfig.SubnetVisibility != cachedClusterConfig.SubnetVisibility {
292+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.SubnetVisibilityKey, cachedClusterConfig.SubnetVisibility)
293+
}
294+
userClusterConfig.SubnetVisibility = cachedClusterConfig.SubnetVisibility
293295

294-
if !strset.New(userClusterConfig.SpotConfig.InstanceDistribution...).IsEqual(strset.New(cachedClusterConfig.SpotConfig.InstanceDistribution...)) {
295-
return nil, errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceDistributionKey, cachedClusterConfig.SpotConfig.InstanceDistribution), clusterconfig.SpotConfigKey)
296-
}
296+
if userClusterConfig.NATGateway != cachedClusterConfig.NATGateway {
297+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.NATGatewayKey, cachedClusterConfig.NATGateway)
298+
}
299+
userClusterConfig.NATGateway = cachedClusterConfig.NATGateway
297300

298-
if userClusterConfig.SpotConfig.OnDemandBaseCapacity != nil && *userClusterConfig.SpotConfig.OnDemandBaseCapacity != *cachedClusterConfig.SpotConfig.OnDemandBaseCapacity {
299-
return nil, errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OnDemandBaseCapacityKey, *cachedClusterConfig.SpotConfig.OnDemandBaseCapacity), clusterconfig.SpotConfigKey)
300-
}
301+
if userClusterConfig.APILoadBalancerScheme != cachedClusterConfig.APILoadBalancerScheme {
302+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.APILoadBalancerSchemeKey, cachedClusterConfig.APILoadBalancerScheme)
303+
}
304+
userClusterConfig.APILoadBalancerScheme = cachedClusterConfig.APILoadBalancerScheme
301305

302-
if userClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity != nil && *userClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity != *cachedClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity {
303-
return nil, errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OnDemandPercentageAboveBaseCapacityKey, *cachedClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity), clusterconfig.SpotConfigKey)
304-
}
306+
if userClusterConfig.OperatorLoadBalancerScheme != cachedClusterConfig.OperatorLoadBalancerScheme {
307+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OperatorLoadBalancerSchemeKey, cachedClusterConfig.OperatorLoadBalancerScheme)
308+
}
309+
userClusterConfig.OperatorLoadBalancerScheme = cachedClusterConfig.OperatorLoadBalancerScheme
305310

306-
if userClusterConfig.SpotConfig.MaxPrice != nil && *userClusterConfig.SpotConfig.MaxPrice != *cachedClusterConfig.SpotConfig.MaxPrice {
307-
return nil, errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.MaxPriceKey, *cachedClusterConfig.SpotConfig.MaxPrice), clusterconfig.SpotConfigKey)
308-
}
311+
if userClusterConfig.Spot != nil && *userClusterConfig.Spot != *cachedClusterConfig.Spot {
312+
return clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.SpotKey, *cachedClusterConfig.Spot)
313+
}
314+
userClusterConfig.Spot = cachedClusterConfig.Spot
309315

310-
if userClusterConfig.SpotConfig.InstancePools != nil && *userClusterConfig.SpotConfig.InstancePools != *cachedClusterConfig.SpotConfig.InstancePools {
311-
return nil, errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstancePoolsKey, *cachedClusterConfig.SpotConfig.InstancePools), clusterconfig.SpotConfigKey)
312-
}
316+
if userClusterConfig.Spot != nil && *userClusterConfig.Spot {
317+
err := userClusterConfig.FillEmptySpotFields(awsClient)
318+
if err != nil {
319+
return err
320+
}
321+
}
313322

314-
if userClusterConfig.SpotConfig.OnDemandBackup != cachedClusterConfig.SpotConfig.OnDemandBackup {
315-
return nil, errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OnDemandBackupKey, cachedClusterConfig.SpotConfig.OnDemandBackup), clusterconfig.SpotConfigKey)
316-
}
323+
if userClusterConfig.SpotConfig != nil && s.Obj(userClusterConfig.SpotConfig) != s.Obj(cachedClusterConfig.SpotConfig) {
324+
if cachedClusterConfig.SpotConfig == nil {
325+
return clusterconfig.ErrorConfiguredWhenSpotIsNotEnabled(clusterconfig.SpotConfigKey)
317326
}
318-
userClusterConfig.SpotConfig = cachedClusterConfig.SpotConfig
319327

320-
err = clusterconfig.ConfigurePrompt(userClusterConfig, &cachedClusterConfig, true, disallowPrompt)
321-
if err != nil {
322-
return nil, err
328+
if !strset.New(userClusterConfig.SpotConfig.InstanceDistribution...).IsEqual(strset.New(cachedClusterConfig.SpotConfig.InstanceDistribution...)) {
329+
return errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstanceDistributionKey, cachedClusterConfig.SpotConfig.InstanceDistribution), clusterconfig.SpotConfigKey)
323330
}
324-
}
325331

326-
var err error
327-
userClusterConfig.Telemetry, err = readTelemetryConfig()
328-
if err != nil {
329-
return nil, err
330-
}
332+
if userClusterConfig.SpotConfig.OnDemandBaseCapacity != nil && *userClusterConfig.SpotConfig.OnDemandBaseCapacity != *cachedClusterConfig.SpotConfig.OnDemandBaseCapacity {
333+
return errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OnDemandBaseCapacityKey, *cachedClusterConfig.SpotConfig.OnDemandBaseCapacity), clusterconfig.SpotConfigKey)
334+
}
331335

332-
err = userClusterConfig.Validate(awsClient)
333-
if err != nil {
334-
if _flagClusterConfig != "" {
335-
err = errors.Wrap(err, _flagClusterConfig)
336+
if userClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity != nil && *userClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity != *cachedClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity {
337+
return errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OnDemandPercentageAboveBaseCapacityKey, *cachedClusterConfig.SpotConfig.OnDemandPercentageAboveBaseCapacity), clusterconfig.SpotConfigKey)
338+
}
339+
340+
if userClusterConfig.SpotConfig.MaxPrice != nil && *userClusterConfig.SpotConfig.MaxPrice != *cachedClusterConfig.SpotConfig.MaxPrice {
341+
return errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.MaxPriceKey, *cachedClusterConfig.SpotConfig.MaxPrice), clusterconfig.SpotConfigKey)
342+
}
343+
344+
if userClusterConfig.SpotConfig.InstancePools != nil && *userClusterConfig.SpotConfig.InstancePools != *cachedClusterConfig.SpotConfig.InstancePools {
345+
return errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.InstancePoolsKey, *cachedClusterConfig.SpotConfig.InstancePools), clusterconfig.SpotConfigKey)
346+
}
347+
348+
if userClusterConfig.SpotConfig.OnDemandBackup != cachedClusterConfig.SpotConfig.OnDemandBackup {
349+
return errors.Wrap(clusterconfig.ErrorConfigCannotBeChangedOnUpdate(clusterconfig.OnDemandBackupKey, cachedClusterConfig.SpotConfig.OnDemandBackup), clusterconfig.SpotConfigKey)
336350
}
337-
return nil, err
338351
}
339352

340-
confirmConfigureClusterConfig(*userClusterConfig, awsCreds, awsClient, disallowPrompt)
353+
userClusterConfig.SpotConfig = cachedClusterConfig.SpotConfig
341354

342-
return userClusterConfig, nil
355+
return nil
343356
}
344357

345358
func confirmInstallClusterConfig(clusterConfig *clusterconfig.Config, awsCreds AWSCredentials, awsClient *aws.Client, envName string, disallowPrompt bool) {

pkg/types/clusterconfig/clusterconfig.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ import (
3535
"github.com/cortexlabs/cortex/pkg/lib/table"
3636
)
3737

38+
const ClusterNameTag = "cortex.dev/cluster-name"
39+
3840
var (
3941
_spotInstanceDistributionLength = 2
4042
_maxInstancePools = 20
41-
_tagName = "cortex.dev/cluster-name"
4243
// This regex is stricter than the actual S3 rules
4344
_strictS3BucketRegex = regexp.MustCompile(`^([a-z0-9])+(-[a-z0-9]+)*$`)
4445
)
@@ -522,8 +523,8 @@ func (cc *Config) Validate(awsClient *aws.Client) error {
522523
}
523524
}
524525

525-
if _, ok := cc.Tags[_tagName]; !ok {
526-
cc.Tags[_tagName] = cc.ClusterName
526+
if _, ok := cc.Tags[ClusterNameTag]; !ok {
527+
cc.Tags[ClusterNameTag] = cc.ClusterName
527528
}
528529

529530
if err := cc.validateAvailabilityZones(awsClient); err != nil {

0 commit comments

Comments
 (0)