Move creating cloud provider out of context

This commit is contained in:
Aleksandra Malinowska 2018-07-19 16:36:09 +02:00
parent 947822fdf2
commit 07e52e6c79
5 changed files with 23 additions and 16 deletions

View File

@ -18,7 +18,6 @@ package context
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/builder"
"k8s.io/autoscaler/cluster-autoscaler/clusterstate/utils"
"k8s.io/autoscaler/cluster-autoscaler/config"
"k8s.io/autoscaler/cluster-autoscaler/expander"
@ -72,15 +71,8 @@ func NewResourceLimiterFromAutoscalingOptions(options config.AutoscalingOptions)
// NewAutoscalingContext returns an autoscaling context from all the necessary parameters passed via arguments
func NewAutoscalingContext(options config.AutoscalingOptions, predicateChecker *simulator.PredicateChecker,
kubeClient kube_client.Interface, kubeEventRecorder kube_record.EventRecorder,
logEventRecorder *utils.LogEventRecorder, listerRegistry kube_util.ListerRegistry) (*AutoscalingContext, errors.AutoscalerError) {
cloudProvider := builder.NewCloudProvider(
options,
cloudprovider.NodeGroupDiscoveryOptions{
NodeGroupSpecs: options.NodeGroups,
NodeGroupAutoDiscoverySpecs: options.NodeGroupAutoDiscovery,
},
NewResourceLimiterFromAutoscalingOptions(options))
logEventRecorder *utils.LogEventRecorder, listerRegistry kube_util.ListerRegistry,
cloudProvider cloudprovider.CloudProvider) (*AutoscalingContext, errors.AutoscalerError) {
expanderStrategy, err := factory.ExpanderStrategyFromString(options.ExpanderName,
cloudProvider, listerRegistry.AllNodeLister())
if err != nil {

View File

@ -45,7 +45,9 @@ func TestNewAutoscalingContext(t *testing.T) {
},
simulator.NewTestPredicateChecker(),
fakeClient, fakeRecorder,
fakeLogRecorder, kube_util.NewListerRegistry(nil, nil, nil, nil, nil, nil))
fakeLogRecorder, kube_util.NewListerRegistry(nil, nil, nil, nil, nil, nil),
nil, // fake CloudProvider
)
assert.NoError(t, err)
assert.NotNil(t, autoscalingContext)
}

View File

@ -19,6 +19,7 @@ package core
import (
"time"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/config"
ca_processors "k8s.io/autoscaler/cluster-autoscaler/processors"
"k8s.io/autoscaler/cluster-autoscaler/simulator"
@ -55,10 +56,10 @@ func initializeDefaultOptions(opts *AutoscalerOptions) error {
}
// NewAutoscaler creates an autoscaler of an appropriate type according to the parameters
func NewAutoscaler(opts AutoscalerOptions) (Autoscaler, errors.AutoscalerError) {
func NewAutoscaler(opts AutoscalerOptions, cloudProvider cloudprovider.CloudProvider) (Autoscaler, errors.AutoscalerError) {
err := initializeDefaultOptions(&opts)
if err != nil {
return nil, errors.ToAutoscalerError(errors.InternalError, err)
}
return NewStaticAutoscaler(opts.AutoscalingOptions, opts.PredicateChecker, opts.KubeClient, opts.KubeEventRecorder, opts.ListerRegistry, opts.Processors)
return NewStaticAutoscaler(opts.AutoscalingOptions, opts.PredicateChecker, opts.KubeClient, opts.KubeEventRecorder, opts.ListerRegistry, opts.Processors, cloudProvider)
}

View File

@ -19,6 +19,7 @@ package core
import (
"time"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/clusterstate"
"k8s.io/autoscaler/cluster-autoscaler/clusterstate/utils"
"k8s.io/autoscaler/cluster-autoscaler/config"
@ -68,7 +69,7 @@ type StaticAutoscaler struct {
// NewStaticAutoscaler creates an instance of Autoscaler filled with provided parameters
func NewStaticAutoscaler(opts config.AutoscalingOptions, predicateChecker *simulator.PredicateChecker,
kubeClient kube_client.Interface, kubeEventRecorder kube_record.EventRecorder, listerRegistry kube_util.ListerRegistry,
processors *ca_processors.AutoscalingProcessors) (*StaticAutoscaler, errors.AutoscalerError) {
processors *ca_processors.AutoscalingProcessors, cloudProvider cloudprovider.CloudProvider) (*StaticAutoscaler, errors.AutoscalerError) {
logRecorder, err := utils.NewStatusMapRecorder(kubeClient, opts.ConfigNamespace, kubeEventRecorder, opts.WriteStatusConfigMap)
if err != nil {
glog.Error("Failed to initialize status configmap, unable to write status events")
@ -76,7 +77,7 @@ func NewStaticAutoscaler(opts config.AutoscalingOptions, predicateChecker *simul
// TODO(maciekpytel): recover from this after successful status configmap update?
logRecorder, _ = utils.NewStatusMapRecorder(kubeClient, opts.ConfigNamespace, kubeEventRecorder, false)
}
autoscalingContext, errctx := context.NewAutoscalingContext(opts, predicateChecker, kubeClient, kubeEventRecorder, logRecorder, listerRegistry)
autoscalingContext, errctx := context.NewAutoscalingContext(opts, predicateChecker, kubeClient, kubeEventRecorder, logRecorder, listerRegistry, cloudProvider)
if errctx != nil {
return nil, errctx
}

View File

@ -31,8 +31,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kube_flag "k8s.io/apiserver/pkg/util/flag"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
cloudBuilder "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/builder"
"k8s.io/autoscaler/cluster-autoscaler/config"
"k8s.io/autoscaler/cluster-autoscaler/context"
"k8s.io/autoscaler/cluster-autoscaler/core"
"k8s.io/autoscaler/cluster-autoscaler/estimator"
"k8s.io/autoscaler/cluster-autoscaler/expander"
@ -272,7 +274,16 @@ func run(healthCheck *metrics.HealthCheck) {
KubeEventRecorder: kubeEventRecorder,
ListerRegistry: listerRegistry,
}
autoscaler, err := core.NewAutoscaler(opts)
cloudProvider := cloudBuilder.NewCloudProvider(
autoscalingOptions,
cloudprovider.NodeGroupDiscoveryOptions{
NodeGroupSpecs: autoscalingOptions.NodeGroups,
NodeGroupAutoDiscoverySpecs: autoscalingOptions.NodeGroupAutoDiscovery,
},
context.NewResourceLimiterFromAutoscalingOptions(autoscalingOptions))
autoscaler, err := core.NewAutoscaler(opts, cloudProvider)
if err != nil {
glog.Fatalf("Failed to create autoscaler: %v", err)
}