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 df02299 commit e81c27b
Show file tree
Hide file tree
Showing 5 changed files with 246 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
47 changes: 39 additions & 8 deletions cluster-autoscaler/cloudprovider/gce/kube_env.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gce

import (
Expand All @@ -7,7 +23,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 +36,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 e81c27b

Please sign in to comment.