Skip to content

Commit

Permalink
Add kube-env to MigInfoProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
BigDarkClown committed Feb 15, 2024
1 parent a2f8902 commit e0f30b4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cluster-autoscaler/cloudprovider/gce/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type GceCache struct {
migBaseNameCache map[GceRef]string
instanceTemplateNameCache map[GceRef]string
instanceTemplatesCache map[GceRef]*gce.InstanceTemplate
kubeEnvCache map[GceRef]KubeEnv
}

// NewGceCache creates empty GceCache.
Expand All @@ -84,6 +85,7 @@ func NewGceCache() *GceCache {
migBaseNameCache: map[GceRef]string{},
instanceTemplateNameCache: map[GceRef]string{},
instanceTemplatesCache: map[GceRef]*gce.InstanceTemplate{},
kubeEnvCache: map[GceRef]KubeEnv{},
}
}

Expand Down Expand Up @@ -409,6 +411,46 @@ func (gc *GceCache) InvalidateAllMigInstanceTemplates() {
gc.instanceTemplatesCache = map[GceRef]*gce.InstanceTemplate{}
}

// GetMigKubeEnv returns the cached KubeEnv for a mig GceRef
func (gc *GceCache) GetMigKubeEnv(ref GceRef) (KubeEnv, bool) {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()

kubeEnv, found := gc.kubeEnvCache[ref]
if found {
klog.V(5).Infof("Kube-env cache hit for %s", ref)
}
return kubeEnv, found
}

// SetMigKubeEnv sets KubeEnv for a mig GceRef
func (gc *GceCache) SetMigKubeEnv(ref GceRef, kubeEnv KubeEnv) {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()

gc.kubeEnvCache[ref] = kubeEnv
}

// InvalidateMigKubeEnv clears the kube-env cache for a mig GceRef
func (gc *GceCache) InvalidateMigKubeEnv(ref GceRef) {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()

if _, found := gc.kubeEnvCache[ref]; found {
klog.V(5).Infof("Kube-env cache invalidated for %s", ref)
delete(gc.kubeEnvCache, ref)
}
}

// InvalidateAllMigKubeEnvs clears the kube-env cache
func (gc *GceCache) InvalidateAllMigKubeEnvs() {
gc.cacheMutex.Lock()
defer gc.cacheMutex.Unlock()

klog.V(5).Infof("Kube-env cache invalidated")
gc.kubeEnvCache = map[GceRef]KubeEnv{}
}

// GetMachine retrieves machine type from cache under lock.
func (gc *GceCache) GetMachine(machineTypeName string, zone string) (MachineType, bool) {
gc.cacheMutex.Lock()
Expand Down
34 changes: 34 additions & 0 deletions cluster-autoscaler/cloudprovider/gce/kube_env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gce

import (
"fmt"

gce "google.golang.org/api/compute/v1"
"sigs.k8s.io/yaml"
)

const autoscalerVars = "AUTOSCALER_ENV_VARS"

// KubeEnv stores kube-env information from InstanceTemplate
type KubeEnv map[string]string

// ExtractKubeEnv extracts kube-env from InstanceTemplate
func ExtractKubeEnv(template *gce.InstanceTemplate) (KubeEnv, error) {
if template.Properties.Metadata == nil {
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.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 nil, nil
}
26 changes: 26 additions & 0 deletions cluster-autoscaler/cloudprovider/gce/mig_info_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type MigInfoProvider interface {
GetMigInstanceTemplateName(migRef GceRef) (string, error)
// GetMigInstanceTemplate returns instance template for given MIG ref
GetMigInstanceTemplate(migRef GceRef) (*gce.InstanceTemplate, error)
// GetMigKubeEnv returns kube-env for given MIG ref
GetMigKubeEnv(migRef GceRef) (KubeEnv, error)
// GetMigMachineType returns machine type used by a MIG.
// For custom machines cpu and memory information is based on parsing
// machine name. For standard types it's retrieved from GCE API.
Expand Down Expand Up @@ -285,6 +287,30 @@ func (c *cachingMigInfoProvider) GetMigInstanceTemplate(migRef GceRef) (*gce.Ins
return template, nil
}

func (c *cachingMigInfoProvider) GetMigKubeEnv(migRef GceRef) (KubeEnv, error) {
templateName, err := c.GetMigInstanceTemplateName(migRef)
if err != nil {
return nil, err
}

template, templateFound := c.cache.GetMigInstanceTemplate(migRef)
kubeEnv, kubeEnvFound := c.cache.GetMigKubeEnv(migRef)
if templateFound && template.Name == templateName && kubeEnvFound {
return kubeEnv, nil
}

template, err = c.GetMigInstanceTemplate(migRef)
if err != nil {
return nil, err
}
kubeEnv, err = ExtractKubeEnv(template)
if err != nil {
return nil, err
}
c.cache.SetMigKubeEnv(migRef, kubeEnv)
return kubeEnv, nil
}

// filMigInfoCache needs to be called with migInfoMutex locked
func (c *cachingMigInfoProvider) fillMigInfoCache() error {
var zones []string
Expand Down

0 comments on commit e0f30b4

Please sign in to comment.