Skip to content

Commit

Permalink
refactor: move scheduler config to a utils/scheduler package`
Browse files Browse the repository at this point in the history
- use default config as a fallback
Signed-off-by: vadasambar <surajrbanakar@gmail.com>
  • Loading branch information
vadasambar committed May 10, 2023
1 parent fade953 commit d58399d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
39 changes: 5 additions & 34 deletions cluster-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/simulator/clustersnapshot"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
scheduler_util "k8s.io/autoscaler/cluster-autoscaler/utils/scheduler"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
"k8s.io/autoscaler/cluster-autoscaler/version"
kube_client "k8s.io/client-go/kubernetes"
Expand All @@ -67,9 +68,6 @@ import (
"k8s.io/component-base/config/options"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/klog/v2"
schedconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
schedscheme "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
schedvalidation "k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
)

// MultiStringFlag is a flag for passing multiple parameters using same flag
Expand Down Expand Up @@ -256,7 +254,10 @@ func createAutoscalingOptions() config.AutoscalingOptions {
klog.Fatalf("Invalid configuration, could not use --max-drain-parallelism > 1 if --parallel-drain is false")
}

parsedSchedConfig := getSchedulerConfig(*schedulerConfig)
parsedSchedConfig, err := scheduler_util.GetSchedulerConfig(*schedulerConfig)
if err != nil {
klog.Fatalf("Failed to get scheduler config: %v", err)
}

return config.AutoscalingOptions{
NodeGroupDefaults: config.NodeGroupAutoscalingOptions{
Expand Down Expand Up @@ -376,36 +377,6 @@ func getKubeConfig() *rest.Config {
return kubeConfig
}

func getSchedulerConfig(path string) *schedconfig.KubeSchedulerConfiguration {
data, err := os.ReadFile(path)

defaultFallback := "(falling back to default scheduler config)"

if err != nil {
klog.Errorf("couldn't load scheduler config %s: %v", defaultFallback, err)
return &schedconfig.KubeSchedulerConfiguration{}
}

obj, gvk, err := schedscheme.Codecs.UniversalDecoder().Decode(data, nil, nil)
if err != nil {
klog.Errorf("couldn't decode scheduler config %s: %v", defaultFallback, err)
return &schedconfig.KubeSchedulerConfiguration{}
}

cfgObj, ok := obj.(*schedconfig.KubeSchedulerConfiguration)
if !ok {
klog.Errorf("couldn't decode as KubeSchedulerConfiguration, got %s %s", defaultFallback, gvk)
return &schedconfig.KubeSchedulerConfiguration{}
}

if err := schedvalidation.ValidateKubeSchedulerConfiguration(cfgObj); err != nil {
klog.Errorf("invalid KubeSchedulerConfiguration %s: %v", defaultFallback, err)
return &schedconfig.KubeSchedulerConfiguration{}
}

return cfgObj
}

func createKubeClient(kubeConfig *rest.Config) kube_client.Interface {
return kube_client.NewForConfigOrDie(kubeConfig)
}
Expand Down
41 changes: 41 additions & 0 deletions cluster-autoscaler/utils/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ package scheduler

import (
"fmt"
"os"
"strings"

apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/klog/v2"
schedconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
schedconfiglatest "k8s.io/kubernetes/pkg/scheduler/apis/config/latest"
schedscheme "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
schedvalidation "k8s.io/kubernetes/pkg/scheduler/apis/config/validation"
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework"
)

Expand Down Expand Up @@ -106,3 +112,38 @@ func ResourceToResourceList(r *schedulerframework.Resource) apiv1.ResourceList {
}
return result
}

// GetSchedulerConfig loads scheduler config from a path
// (falls back to default config in case of error)
func GetSchedulerConfig(path string) (*schedconfig.KubeSchedulerConfiguration, error) {
defaultFallbackMsg := "(falling back to default scheduler config)"
defaultFallback, err := schedconfiglatest.Default()
if err != nil {
return nil, fmt.Errorf("couldn't create scheduler config: %v", err)
}

data, err := os.ReadFile(path)
if err != nil {
klog.Errorf("couldn't load scheduler config %s: %v", defaultFallbackMsg, err)
return defaultFallback, nil
}

obj, gvk, err := schedscheme.Codecs.UniversalDecoder().Decode(data, nil, nil)
if err != nil {
klog.Errorf("couldn't decode scheduler config %s: %v", defaultFallbackMsg, err)
return defaultFallback, nil
}

cfgObj, ok := obj.(*schedconfig.KubeSchedulerConfiguration)
if !ok {
klog.Errorf("couldn't decode as KubeSchedulerConfiguration, got %s %s", defaultFallbackMsg, gvk)
return defaultFallback, nil
}

if err := schedvalidation.ValidateKubeSchedulerConfiguration(cfgObj); err != nil {
klog.Errorf("invalid KubeSchedulerConfiguration %s: %v", defaultFallbackMsg, err)
return defaultFallback, nil
}

return cfgObj, nil
}

0 comments on commit d58399d

Please sign in to comment.