diff --git a/cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go b/cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go index 12d88fb183c5..cbd2eff6a492 100644 --- a/cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go +++ b/cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go @@ -103,8 +103,8 @@ type AutoscalingGceClient interface { FetchMigTargetSize(GceRef) (int64, error) FetchMigBasename(GceRef) (string, error) FetchMigInstances(GceRef) ([]GceInstance, error) - FetchMigTemplateName(migRef GceRef) (string, error) - FetchMigTemplate(migRef GceRef, templateName string) (*gce.InstanceTemplate, error) + FetchMigTemplateName(migRef GceRef) (InstanceTemplateName, error) + FetchMigTemplate(migRef GceRef, templateName string, regional bool) (*gce.InstanceTemplate, error) FetchMigsWithName(zone string, filter *regexp.Regexp) ([]string, error) FetchZones(region string) ([]string, error) FetchAvailableCpuPlatforms() (map[string][]string, error) @@ -580,26 +580,37 @@ func (client *autoscalingGceClientV1) FetchAvailableCpuPlatforms() (map[string][ return availableCpuPlatforms, nil } -func (client *autoscalingGceClientV1) FetchMigTemplateName(migRef GceRef) (string, error) { +func (client *autoscalingGceClientV1) FetchMigTemplateName(migRef GceRef) (InstanceTemplateName, error) { registerRequest("instance_group_managers", "get") igm, err := client.gceService.InstanceGroupManagers.Get(migRef.Project, migRef.Zone, migRef.Name).Do() if err != nil { if err, ok := err.(*googleapi.Error); ok { if err.Code == http.StatusNotFound { - return "", errors.NewAutoscalerError(errors.NodeGroupDoesNotExistError, "%s", err.Error()) + return InstanceTemplateName{}, errors.NewAutoscalerError(errors.NodeGroupDoesNotExistError, "%s", err.Error()) } } - return "", err + return InstanceTemplateName{}, err } templateUrl, err := url.Parse(igm.InstanceTemplate) if err != nil { - return "", err + return InstanceTemplateName{}, err } + regional, err := IsInstanceTemplateRegional(templateUrl.String()) + if err != nil { + return InstanceTemplateName{}, err + } + _, templateName := path.Split(templateUrl.EscapedPath()) - return templateName, nil + return InstanceTemplateName{templateName, regional}, nil } -func (client *autoscalingGceClientV1) FetchMigTemplate(migRef GceRef, templateName string) (*gce.InstanceTemplate, error) { +func (client *autoscalingGceClientV1) FetchMigTemplate(migRef GceRef, templateName string, regional bool) (*gce.InstanceTemplate, error) { + if regional { + zoneHyphenIndex := strings.LastIndex(migRef.Zone, "-") + region := migRef.Zone[:zoneHyphenIndex] + registerRequest("region_instance_templates", "get") + return client.gceService.RegionInstanceTemplates.Get(migRef.Project, region, templateName).Do() + } registerRequest("instance_templates", "get") return client.gceService.InstanceTemplates.Get(migRef.Project, templateName).Do() } diff --git a/cluster-autoscaler/cloudprovider/gce/cache.go b/cluster-autoscaler/cloudprovider/gce/cache.go index 1d46e4f353d5..d31f20dd37b9 100644 --- a/cluster-autoscaler/cloudprovider/gce/cache.go +++ b/cluster-autoscaler/cloudprovider/gce/cache.go @@ -33,6 +33,13 @@ type MachineTypeKey struct { MachineTypeName string } +// InstanceTemplateName is used to store the name, and +// whether or not the instance template is regional +type InstanceTemplateName struct { + Name string + Regional bool +} + // GceCache is used for caching cluster resources state. // // It is needed to: @@ -67,7 +74,7 @@ type GceCache struct { migTargetSizeCache map[GceRef]int64 migBaseNameCache map[GceRef]string listManagedInstancesResultsCache map[GceRef]string - instanceTemplateNameCache map[GceRef]string + instanceTemplateNameCache map[GceRef]InstanceTemplateName instanceTemplatesCache map[GceRef]*gce.InstanceTemplate kubeEnvCache map[GceRef]KubeEnv } @@ -85,7 +92,7 @@ func NewGceCache() *GceCache { migTargetSizeCache: map[GceRef]int64{}, migBaseNameCache: map[GceRef]string{}, listManagedInstancesResultsCache: map[GceRef]string{}, - instanceTemplateNameCache: map[GceRef]string{}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{}, kubeEnvCache: map[GceRef]KubeEnv{}, } @@ -334,23 +341,23 @@ func (gc *GceCache) InvalidateAllMigTargetSizes() { } // GetMigInstanceTemplateName returns the cached instance template ref for a mig GceRef -func (gc *GceCache) GetMigInstanceTemplateName(ref GceRef) (string, bool) { +func (gc *GceCache) GetMigInstanceTemplateName(ref GceRef) (InstanceTemplateName, bool) { gc.cacheMutex.Lock() defer gc.cacheMutex.Unlock() - templateName, found := gc.instanceTemplateNameCache[ref] + instanceTemplateName, found := gc.instanceTemplateNameCache[ref] if found { klog.V(5).Infof("Instance template names cache hit for %s", ref) } - return templateName, found + return instanceTemplateName, found } // SetMigInstanceTemplateName sets instance template ref for a mig GceRef -func (gc *GceCache) SetMigInstanceTemplateName(ref GceRef, templateName string) { +func (gc *GceCache) SetMigInstanceTemplateName(ref GceRef, instanceTemplateName InstanceTemplateName) { gc.cacheMutex.Lock() defer gc.cacheMutex.Unlock() - gc.instanceTemplateNameCache[ref] = templateName + gc.instanceTemplateNameCache[ref] = instanceTemplateName } // InvalidateMigInstanceTemplateName clears the instance template ref cache for a mig GceRef @@ -370,7 +377,7 @@ func (gc *GceCache) InvalidateAllMigInstanceTemplateNames() { defer gc.cacheMutex.Unlock() klog.V(5).Infof("Instance template names cache invalidated") - gc.instanceTemplateNameCache = map[GceRef]string{} + gc.instanceTemplateNameCache = map[GceRef]InstanceTemplateName{} } // GetMigInstanceTemplate returns the cached gce.InstanceTemplate for a mig GceRef diff --git a/cluster-autoscaler/cloudprovider/gce/gce_manager_test.go b/cluster-autoscaler/cloudprovider/gce/gce_manager_test.go index b4ddba0b4b87..207c04deb9b3 100644 --- a/cluster-autoscaler/cloudprovider/gce/gce_manager_test.go +++ b/cluster-autoscaler/cloudprovider/gce/gce_manager_test.go @@ -344,7 +344,7 @@ func newTestGceManager(t *testing.T, testServerURL string, regional bool) *gceMa {"us-central1-f", "n1-standard-1"}: {Name: "n1-standard-1", CPU: 1, Memory: 1}, }, migTargetSizeCache: map[GceRef]int64{}, - instanceTemplateNameCache: map[GceRef]string{}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{}, kubeEnvCache: map[GceRef]KubeEnv{}, migBaseNameCache: map[GceRef]string{}, diff --git a/cluster-autoscaler/cloudprovider/gce/gce_url.go b/cluster-autoscaler/cloudprovider/gce/gce_url.go index 7b0ab135a46c..80ef91207ff0 100644 --- a/cluster-autoscaler/cloudprovider/gce/gce_url.go +++ b/cluster-autoscaler/cloudprovider/gce/gce_url.go @@ -78,6 +78,11 @@ func GenerateMigUrl(domainUrl string, ref GceRef) string { return fmt.Sprintf(migUrlTemplate, ref.Project, ref.Zone, ref.Name) } +// IsInstanceTemplateRegional determines whether or not an instance template is regional based on the url +func IsInstanceTemplateRegional(templateUrl string) (bool, error) { + return regexp.MatchString("(/projects/.*[A-Za-z0-9]+.*/regions/)", templateUrl) +} + func parseGceUrl(url, expectedResource string) (project string, zone string, name string, err error) { reg := regexp.MustCompile(fmt.Sprintf("https://.*/projects/.*/zones/.*/%s/.*", expectedResource)) errMsg := fmt.Errorf("wrong url: expected format /projects//zones//%s/, got %s", expectedResource, url) diff --git a/cluster-autoscaler/cloudprovider/gce/gce_url_test.go b/cluster-autoscaler/cloudprovider/gce/gce_url_test.go index 1317ef76e605..feee3b2432ae 100644 --- a/cluster-autoscaler/cloudprovider/gce/gce_url_test.go +++ b/cluster-autoscaler/cloudprovider/gce/gce_url_test.go @@ -263,3 +263,34 @@ func TestParseMigUrl(t *testing.T) { }) } } + +func TestIsInstanceTemplateRegional(t *testing.T) { + tests := []struct { + name string + templateUrl string + expectRegional bool + wantErr error + }{ + { + name: "Has regional instance url", + templateUrl: "https://www.googleapis.com/compute/v1/projects/test-project/regions/us-central1/instanceTemplates/instance-template", + expectRegional: true, + }, + { + name: "Has global instance url", + templateUrl: "https://www.googleapis.com/compute/v1/projects/test-project/global/instanceTemplates/instance-template", + expectRegional: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + regional, err := IsInstanceTemplateRegional(tt.templateUrl) + assert.Equal(t, tt.wantErr, err) + if tt.wantErr != nil { + return + } + assert.Equal(t, tt.expectRegional, regional) + }) + } +} diff --git a/cluster-autoscaler/cloudprovider/gce/mig_info_provider.go b/cluster-autoscaler/cloudprovider/gce/mig_info_provider.go index bd7c61c2ef8e..ad407bec1b10 100644 --- a/cluster-autoscaler/cloudprovider/gce/mig_info_provider.go +++ b/cluster-autoscaler/cloudprovider/gce/mig_info_provider.go @@ -43,7 +43,7 @@ type MigInfoProvider interface { // GetMigBasename returns basename for given MIG ref GetMigBasename(migRef GceRef) (string, error) // GetMigInstanceTemplateName returns instance template name for given MIG ref - GetMigInstanceTemplateName(migRef GceRef) (string, error) + GetMigInstanceTemplateName(migRef GceRef) (InstanceTemplateName, error) // GetMigInstanceTemplate returns instance template for given MIG ref GetMigInstanceTemplate(migRef GceRef) (*gce.InstanceTemplate, error) // GetMigKubeEnv returns kube-env for given MIG ref @@ -243,44 +243,44 @@ func (c *cachingMigInfoProvider) GetMigBasename(migRef GceRef) (string, error) { return basename, nil } -func (c *cachingMigInfoProvider) GetMigInstanceTemplateName(migRef GceRef) (string, error) { +func (c *cachingMigInfoProvider) GetMigInstanceTemplateName(migRef GceRef) (InstanceTemplateName, error) { c.migInfoMutex.Lock() defer c.migInfoMutex.Unlock() - templateName, found := c.cache.GetMigInstanceTemplateName(migRef) + instanceTemplateName, found := c.cache.GetMigInstanceTemplateName(migRef) if found { - return templateName, nil + return instanceTemplateName, nil } err := c.fillMigInfoCache() - templateName, found = c.cache.GetMigInstanceTemplateName(migRef) + instanceTemplateName, found = c.cache.GetMigInstanceTemplateName(migRef) if err == nil && found { - return templateName, nil + return instanceTemplateName, nil } // fallback to querying for single mig - templateName, err = c.gceClient.FetchMigTemplateName(migRef) + instanceTemplateName, err = c.gceClient.FetchMigTemplateName(migRef) if err != nil { c.migLister.HandleMigIssue(migRef, err) - return "", err + return InstanceTemplateName{}, err } - c.cache.SetMigInstanceTemplateName(migRef, templateName) - return templateName, nil + c.cache.SetMigInstanceTemplateName(migRef, instanceTemplateName) + return instanceTemplateName, nil } func (c *cachingMigInfoProvider) GetMigInstanceTemplate(migRef GceRef) (*gce.InstanceTemplate, error) { - templateName, err := c.GetMigInstanceTemplateName(migRef) + instanceTemplateName, err := c.GetMigInstanceTemplateName(migRef) if err != nil { return nil, err } template, found := c.cache.GetMigInstanceTemplate(migRef) - if found && template.Name == templateName { + if found && template.Name == instanceTemplateName.Name { return template, nil } - klog.V(2).Infof("Instance template of mig %v changed to %v", migRef.Name, templateName) - template, err = c.gceClient.FetchMigTemplate(migRef, templateName) + klog.V(2).Infof("Instance template of mig %v changed to %v", migRef.Name, instanceTemplateName.Name) + template, err = c.gceClient.FetchMigTemplate(migRef, instanceTemplateName.Name, instanceTemplateName.Regional) if err != nil { return nil, err } @@ -289,13 +289,13 @@ func (c *cachingMigInfoProvider) GetMigInstanceTemplate(migRef GceRef) (*gce.Ins } func (c *cachingMigInfoProvider) GetMigKubeEnv(migRef GceRef) (KubeEnv, error) { - templateName, err := c.GetMigInstanceTemplateName(migRef) + instanceTemplateName, err := c.GetMigInstanceTemplateName(migRef) if err != nil { return KubeEnv{}, err } kubeEnv, kubeEnvFound := c.cache.GetMigKubeEnv(migRef) - if kubeEnvFound && kubeEnv.templateName == templateName { + if kubeEnvFound && kubeEnv.templateName == instanceTemplateName.Name { return kubeEnv, nil } @@ -363,7 +363,12 @@ func (c *cachingMigInfoProvider) fillMigInfoCache() error { templateUrl, err := url.Parse(zoneMig.InstanceTemplate) if err == nil { _, templateName := path.Split(templateUrl.EscapedPath()) - c.cache.SetMigInstanceTemplateName(zoneMigRef, templateName) + regional, err := IsInstanceTemplateRegional(templateUrl.String()) + if err != nil { + klog.Errorf("Error parsing instance template url: %v; err=%v ", templateUrl.String(), err) + } else { + c.cache.SetMigInstanceTemplateName(zoneMigRef, InstanceTemplateName{templateName, regional}) + } } } } diff --git a/cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go b/cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go index a7e9e311766f..838a36ba6134 100644 --- a/cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go +++ b/cluster-autoscaler/cloudprovider/gce/mig_info_provider_test.go @@ -53,8 +53,8 @@ type mockAutoscalingGceClient struct { fetchMigTargetSize func(GceRef) (int64, error) fetchMigBasename func(GceRef) (string, error) fetchMigInstances func(GceRef) ([]GceInstance, error) - fetchMigTemplateName func(GceRef) (string, error) - fetchMigTemplate func(GceRef, string) (*gce.InstanceTemplate, error) + fetchMigTemplateName func(GceRef) (InstanceTemplateName, error) + fetchMigTemplate func(GceRef, string, bool) (*gce.InstanceTemplate, error) fetchMachineType func(string, string) (*gce.MachineType, error) fetchListManagedInstancesResults func(GceRef) (string, error) } @@ -87,12 +87,12 @@ func (client *mockAutoscalingGceClient) FetchMigInstances(migRef GceRef) ([]GceI return client.fetchMigInstances(migRef) } -func (client *mockAutoscalingGceClient) FetchMigTemplateName(migRef GceRef) (string, error) { +func (client *mockAutoscalingGceClient) FetchMigTemplateName(migRef GceRef) (InstanceTemplateName, error) { return client.fetchMigTemplateName(migRef) } -func (client *mockAutoscalingGceClient) FetchMigTemplate(migRef GceRef, templateName string) (*gce.InstanceTemplate, error) { - return client.fetchMigTemplate(migRef, templateName) +func (client *mockAutoscalingGceClient) FetchMigTemplate(migRef GceRef, templateName string, regional bool) (*gce.InstanceTemplate, error) { + return client.fetchMigTemplate(migRef, templateName, regional) } func (client *mockAutoscalingGceClient) FetchMigsWithName(_ string, _ *regexp.Regexp) ([]string, error) { @@ -852,22 +852,29 @@ func TestGetMigInstanceTemplateName(t *testing.T) { instanceGroupManager := &gce.InstanceGroupManager{ Zone: mig.GceRef().Zone, Name: mig.GceRef().Name, - InstanceTemplate: templateName, + InstanceTemplate: "https://www.googleapis.com/compute/v1/projects/test-project/global/instanceTemplates/template-name", + } + + instanceGroupManagerRegional := &gce.InstanceGroupManager{ + Zone: mig.GceRef().Zone, + Name: mig.GceRef().Name, + InstanceTemplate: "https://www.googleapis.com/compute/v1/projects/test-project/regions/us-central1/instanceTemplates/template-name", } testCases := []struct { name string cache *GceCache fetchMigs func(string) ([]*gce.InstanceGroupManager, error) - fetchMigTemplateName func(GceRef) (string, error) + fetchMigTemplateName func(GceRef) (InstanceTemplateName, error) expectedTemplateName string + expectedRegion bool expectedErr error }{ { name: "template name in cache", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, }, expectedTemplateName: templateName, }, @@ -877,18 +884,24 @@ func TestGetMigInstanceTemplateName(t *testing.T) { fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{instanceGroupManager}), expectedTemplateName: templateName, }, + { + name: "target size from cache fill, regional", + cache: emptyCache(), + fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{instanceGroupManagerRegional}), + expectedTemplateName: templateName, + }, { name: "cache fill without mig, fallback success", cache: emptyCache(), fetchMigs: fetchMigsConst([]*gce.InstanceGroupManager{}), - fetchMigTemplateName: fetchMigTemplateNameConst(templateName), + fetchMigTemplateName: fetchMigTemplateNameConst(InstanceTemplateName{templateName, false}), expectedTemplateName: templateName, }, { name: "cache fill failure, fallback success", cache: emptyCache(), fetchMigs: fetchMigsFail, - fetchMigTemplateName: fetchMigTemplateNameConst(templateName), + fetchMigTemplateName: fetchMigTemplateNameConst(InstanceTemplateName{templateName, false}), expectedTemplateName: templateName, }, { @@ -916,14 +929,14 @@ func TestGetMigInstanceTemplateName(t *testing.T) { migLister := NewMigLister(tc.cache) provider := NewCachingMigInfoProvider(tc.cache, migLister, client, mig.GceRef().Project, 1, 0*time.Second) - templateName, err := provider.GetMigInstanceTemplateName(mig.GceRef()) - cachedTemplateName, found := tc.cache.GetMigInstanceTemplateName(mig.GceRef()) + instanceTemplateName, err := provider.GetMigInstanceTemplateName(mig.GceRef()) + cachedInstanceTemplateName, found := tc.cache.GetMigInstanceTemplateName(mig.GceRef()) assert.Equal(t, tc.expectedErr, err) assert.Equal(t, tc.expectedErr == nil, found) if tc.expectedErr == nil { - assert.Equal(t, tc.expectedTemplateName, templateName) - assert.Equal(t, tc.expectedTemplateName, cachedTemplateName) + assert.Equal(t, tc.expectedTemplateName, instanceTemplateName.Name) + assert.Equal(t, tc.expectedTemplateName, cachedInstanceTemplateName.Name) } }) } @@ -944,8 +957,8 @@ func TestGetMigInstanceTemplate(t *testing.T) { name string cache *GceCache fetchMigs func(string) ([]*gce.InstanceGroupManager, error) - fetchMigTemplateName func(GceRef) (string, error) - fetchMigTemplate func(GceRef, string) (*gce.InstanceTemplate, error) + fetchMigTemplateName func(GceRef) (InstanceTemplateName, error) + fetchMigTemplate func(GceRef, string, bool) (*gce.InstanceTemplate, error) expectedTemplate *gce.InstanceTemplate expectedCachedTemplate *gce.InstanceTemplate expectedErr error @@ -954,7 +967,7 @@ func TestGetMigInstanceTemplate(t *testing.T) { name: "template in cache", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): template}, }, expectedTemplate: template, @@ -964,7 +977,7 @@ func TestGetMigInstanceTemplate(t *testing.T) { name: "cache without template, fetch success", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate), }, fetchMigTemplate: fetchMigTemplateConst(template), @@ -975,7 +988,7 @@ func TestGetMigInstanceTemplate(t *testing.T) { name: "cache with old template, fetch success", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): oldTemplate}, }, fetchMigTemplate: fetchMigTemplateConst(template), @@ -986,7 +999,7 @@ func TestGetMigInstanceTemplate(t *testing.T) { name: "cache without template, fetch failure", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate), }, fetchMigTemplate: fetchMigTemplateFail, @@ -996,7 +1009,7 @@ func TestGetMigInstanceTemplate(t *testing.T) { name: "cache with old template, fetch failure", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): oldTemplate}, }, fetchMigTemplate: fetchMigTemplateFail, @@ -1075,8 +1088,8 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name string cache *GceCache fetchMigs func(string) ([]*gce.InstanceGroupManager, error) - fetchMigTemplateName func(GceRef) (string, error) - fetchMigTemplate func(GceRef, string) (*gce.InstanceTemplate, error) + fetchMigTemplateName func(GceRef) (InstanceTemplateName, error) + fetchMigTemplate func(GceRef, string, bool) (*gce.InstanceTemplate, error) expectedKubeEnv KubeEnv expectedCachedKubeEnv KubeEnv expectedErr error @@ -1085,7 +1098,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "kube-env in cache", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, kubeEnvCache: map[GceRef]KubeEnv{mig.GceRef(): kubeEnv}, }, expectedKubeEnv: kubeEnv, @@ -1095,7 +1108,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "cache without kube-env, template in cache", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): template}, kubeEnvCache: make(map[GceRef]KubeEnv), }, @@ -1106,7 +1119,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "cache without kube-env, fetch success", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate), kubeEnvCache: make(map[GceRef]KubeEnv), }, @@ -1118,7 +1131,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "cache with old kube-env, new template cached", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): template}, kubeEnvCache: map[GceRef]KubeEnv{mig.GceRef(): oldKubeEnv}, }, @@ -1129,7 +1142,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "cache with old kube-env, fetch success", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): oldTemplate}, kubeEnvCache: map[GceRef]KubeEnv{mig.GceRef(): oldKubeEnv}, }, @@ -1141,7 +1154,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "cache without kube-env, fetch failure", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate), kubeEnvCache: make(map[GceRef]KubeEnv), }, @@ -1152,7 +1165,7 @@ func TestGetMigInstanceKubeEnv(t *testing.T) { name: "cache with old kube-env, fetch failure", cache: &GceCache{ migs: map[GceRef]Mig{mig.GceRef(): mig}, - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): templateName}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {templateName, false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{mig.GceRef(): oldTemplate}, kubeEnvCache: map[GceRef]KubeEnv{mig.GceRef(): oldKubeEnv}, }, @@ -1251,7 +1264,7 @@ func TestGetMigMachineType(t *testing.T) { }, } cache := &GceCache{ - instanceTemplateNameCache: map[GceRef]string{mig.GceRef(): "template"}, + instanceTemplateNameCache: map[GceRef]InstanceTemplateName{mig.GceRef(): {"template", false}}, instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{ mig.GceRef(): { Name: "template", @@ -1377,7 +1390,7 @@ func emptyCache() *GceCache { migTargetSizeCache: make(map[GceRef]int64), migBaseNameCache: make(map[GceRef]string), listManagedInstancesResultsCache: make(map[GceRef]string), - instanceTemplateNameCache: make(map[GceRef]string), + instanceTemplateNameCache: make(map[GceRef]InstanceTemplateName), instanceTemplatesCache: make(map[GceRef]*gce.InstanceTemplate), instancesFromUnknownMig: make(map[GceRef]bool), } @@ -1436,22 +1449,22 @@ func fetchMigBasenameConst(basename string) func(GceRef) (string, error) { } } -func fetchMigTemplateNameFail(_ GceRef) (string, error) { - return "", errFetchMigTemplateName +func fetchMigTemplateNameFail(_ GceRef) (InstanceTemplateName, error) { + return InstanceTemplateName{}, errFetchMigTemplateName } -func fetchMigTemplateNameConst(templateName string) func(GceRef) (string, error) { - return func(GceRef) (string, error) { - return templateName, nil +func fetchMigTemplateNameConst(instanceTemplateName InstanceTemplateName) func(GceRef) (InstanceTemplateName, error) { + return func(GceRef) (InstanceTemplateName, error) { + return instanceTemplateName, nil } } -func fetchMigTemplateFail(_ GceRef, _ string) (*gce.InstanceTemplate, error) { +func fetchMigTemplateFail(_ GceRef, _ string, _ bool) (*gce.InstanceTemplate, error) { return nil, errFetchMigTemplate } -func fetchMigTemplateConst(template *gce.InstanceTemplate) func(GceRef, string) (*gce.InstanceTemplate, error) { - return func(GceRef, string) (*gce.InstanceTemplate, error) { +func fetchMigTemplateConst(template *gce.InstanceTemplate) func(GceRef, string, bool) (*gce.InstanceTemplate, error) { + return func(GceRef, string, bool) (*gce.InstanceTemplate, error) { return template, nil } }