Skip to content

Commit

Permalink
Use KubeEnv in gce/templates.go
Browse files Browse the repository at this point in the history
  • Loading branch information
BigDarkClown committed Feb 15, 2024
1 parent e0f30b4 commit de085c9
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 216 deletions.
12 changes: 8 additions & 4 deletions cluster-autoscaler/cloudprovider/gce/gce_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ func (m *gceManagerImpl) refreshAutoscalingOptions() {
klog.Warningf("Failed to extract autoscaling options from %q metadata: instance template is incomplete", template.Name)
continue
}
kubeEnvValue, err := getKubeEnvValueFromTemplateMetadata(template)
kubeEnv, err := m.migInfoProvider.GetMigKubeEnv(mig.GceRef())
if err != nil {
klog.Warningf("Failed to extract autoscaling options from %q instance template's metadata: can't get KubeEnv: %v", template.Name, err)
continue
}
options, err := extractAutoscalingOptionsFromKubeEnv(kubeEnvValue)
options, err := extractAutoscalingOptionsFromKubeEnv(kubeEnv)
if err != nil {
klog.Warningf("Failed to extract autoscaling options from %q instance template's metadata: %v", template.Name, err)
continue
Expand Down Expand Up @@ -591,15 +591,19 @@ func (m *gceManagerImpl) GetMigTemplateNode(mig Mig) (*apiv1.Node, error) {
if err != nil {
return nil, err
}
kubeEnv, err := m.migInfoProvider.GetMigKubeEnv(mig.GceRef())
if err != nil {
return nil, err
}
machineType, err := m.migInfoProvider.GetMigMachineType(mig.GceRef())
if err != nil {
return nil, err
}
migOsInfo, err := m.templates.MigOsInfo(mig.Id(), template)
migOsInfo, err := m.templates.MigOsInfo(mig.Id(), kubeEnv)
if err != nil {
return nil, err
}
return m.templates.BuildNodeFromTemplate(mig, migOsInfo, template, machineType.CPU, machineType.Memory, nil, m.reserved)
return m.templates.BuildNodeFromTemplate(mig, migOsInfo, template, kubeEnv, machineType.CPU, machineType.Memory, nil, m.reserved)
}

// parseMIGAutoDiscoverySpecs returns any provided NodeGroupAutoDiscoverySpecs
Expand Down
1 change: 1 addition & 0 deletions cluster-autoscaler/cloudprovider/gce/gce_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func newTestGceManager(t *testing.T, testServerURL string, regional bool) *gceMa
migTargetSizeCache: map[GceRef]int64{},
instanceTemplateNameCache: map[GceRef]string{},
instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{},
kubeEnvCache: map[GceRef]KubeEnv{},
migBaseNameCache: map[GceRef]string{},
}
migLister := NewMigLister(cache)
Expand Down
31 changes: 23 additions & 8 deletions cluster-autoscaler/cloudprovider/gce/kube_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"sigs.k8s.io/yaml"
)

const autoscalerVars = "AUTOSCALER_ENV_VARS"
const (
kubeEnvKey = "kube-env"
)

// KubeEnv stores kube-env information from InstanceTemplate
type KubeEnv map[string]string
Expand All @@ -18,17 +20,30 @@ func ExtractKubeEnv(template *gce.InstanceTemplate) (KubeEnv, error) {
return nil, fmt.Errorf("instance template %s has no metadata", template.Name)
}
for _, item := range template.Properties.Metadata.Items {
if item.Key == "kube-env" {
if item.Key == kubeEnvKey {
if item.Value == nil {
return nil, fmt.Errorf("no kube-env content in metadata")
}
kubeEnv := make(KubeEnv)
err := yaml.Unmarshal([]byte(*item.Value), &kubeEnv)
if err != nil {
return nil, fmt.Errorf("error unmarshalling kubeEnv: %v", err)
}
return kubeEnv, nil
return parseKubeEnv(*item.Value)
}
}
return nil, nil
}

func parseKubeEnv(kubeEnvValue string) (KubeEnv, error) {
kubeEnv := make(map[string]string)
err := yaml.Unmarshal([]byte(kubeEnvValue), &kubeEnv)
if err != nil {
return nil, fmt.Errorf("error unmarshalling kubeEnv: %v", err)
}
return kubeEnv, nil
}

// Var extracts variable from KubeEnv
func (ke KubeEnv) Var(name string) (string, bool) {
if ke == nil {
return "", false
}
val, found := ke[name]
return val, found
}
Loading

0 comments on commit de085c9

Please sign in to comment.