Skip to content

Commit

Permalink
Merge pull request #4225 from DataDog/gce-createinstances-basename
Browse files Browse the repository at this point in the history
GCE: CreateInstances() should use BaseInstanceName
  • Loading branch information
k8s-ci-robot authored Jul 29, 2021
2 parents b6a5cdc + 655bc6f commit 1ecc8b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
13 changes: 6 additions & 7 deletions cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (
defaultOperationWaitTimeout = 20 * time.Second
defaultOperationPollInterval = 100 * time.Millisecond
defaultOperationDeletionPollInterval = 1 * time.Second
instanceGroupNameSuffix = "-grp"
// ErrorCodeQuotaExceeded is an error code used in InstanceErrorInfo if quota exceeded error occurs.
ErrorCodeQuotaExceeded = "QUOTA_EXCEEDED"

Expand Down Expand Up @@ -77,7 +76,7 @@ type AutoscalingGceClient interface {
// modifying resources
ResizeMig(GceRef, int64) error
DeleteInstances(migRef GceRef, instances []GceRef) error
CreateInstances(GceRef, int64, []string) error
CreateInstances(GceRef, string, int64, []string) error
}

type autoscalingGceClientV1 struct {
Expand Down Expand Up @@ -198,7 +197,7 @@ func (client *autoscalingGceClientV1) ResizeMig(migRef GceRef, size int64) error
return client.waitForOp(op, migRef.Project, migRef.Zone, false)
}

func (client *autoscalingGceClientV1) CreateInstances(migRef GceRef, delta int64, existingInstances []string) error {
func (client *autoscalingGceClientV1) CreateInstances(migRef GceRef, baseName string, delta int64, existingInstances []string) error {
registerRequest("instance_group_managers", "create_instances")
req := gce.InstanceGroupManagersCreateInstancesRequest{}
instanceNames := map[string]bool{}
Expand All @@ -207,7 +206,7 @@ func (client *autoscalingGceClientV1) CreateInstances(migRef GceRef, delta int64
}
req.Instances = make([]*gce.PerInstanceConfig, 0, delta)
for i := int64(0); i < delta; i++ {
newInstanceName := generateInstanceName(migRef, instanceNames)
newInstanceName := generateInstanceName(baseName, instanceNames)
instanceNames[newInstanceName] = true
req.Instances = append(req.Instances, &gce.PerInstanceConfig{Name: newInstanceName})
}
Expand Down Expand Up @@ -369,15 +368,15 @@ func isInstanceNotRunningYet(gceInstance *gce.ManagedInstance) bool {
return gceInstance.InstanceStatus == "" || gceInstance.InstanceStatus == "PROVISIONING" || gceInstance.InstanceStatus == "STAGING"
}

func generateInstanceName(migRef GceRef, existingNames map[string]bool) string {
func generateInstanceName(baseName string, existingNames map[string]bool) string {
for i := 0; i < 100; i++ {
name := fmt.Sprintf("%v-%v", strings.TrimSuffix(migRef.Name, instanceGroupNameSuffix), rand.String(4))
name := fmt.Sprintf("%v-%v", baseName, rand.String(4))
if ok, _ := existingNames[name]; !ok {
return name
}
}
klog.Warning("Unable to create unique name for a new instance, duplicate name might occur")
name := fmt.Sprintf("%v-%v", strings.TrimSuffix(migRef.Name, instanceGroupNameSuffix), rand.String(4))
name := fmt.Sprintf("%v-%v", baseName, rand.String(4))
return name
}

Expand Down
10 changes: 9 additions & 1 deletion cluster-autoscaler/cloudprovider/gce/gce_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,16 @@ func (m *gceManagerImpl) CreateInstances(mig Mig, delta int64) error {
for _, ins := range instances {
instancesNames = append(instancesNames, ins.Id)
}
baseName, found := m.cache.GetMigBasename(mig.GceRef())
if !found {
baseName, err = m.GceService.FetchMigBasename(mig.GceRef())
if err != nil {
return fmt.Errorf("can't upscale %s: failed to collect BaseInstanceName: %w", mig.GceRef(), err)
}
m.cache.SetMigBasename(mig.GceRef(), baseName)
}
m.cache.InvalidateMigTargetSize(mig.GceRef())
return m.GceService.CreateInstances(mig.GceRef(), delta, instancesNames)
return m.GceService.CreateInstances(mig.GceRef(), baseName, delta, instancesNames)
}

func (m *gceManagerImpl) forceRefresh() error {
Expand Down

0 comments on commit 1ecc8b4

Please sign in to comment.