Extract selecting builder to separate files with build tags

This commit is contained in:
Aleksandra Malinowska 2018-10-15 16:20:14 +02:00
parent 59a47e95d7
commit c8b14e8cac
6 changed files with 237 additions and 29 deletions

View File

@ -0,0 +1,58 @@
// +build !gce,!aws,!azure,!kubemark
/*
Copyright 2018 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 builder
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/aws"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/azure"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/gce"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/gke"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/kubemark"
"k8s.io/autoscaler/cluster-autoscaler/config"
)
// AvailableCloudProviders supported by the cloud provider builder.
var AvailableCloudProviders = []string{
aws.ProviderName,
azure.ProviderName,
gce.ProviderNameGCE,
gke.ProviderNameGKE,
kubemark.ProviderName,
}
// DefaultCloudProvider is GCE.
const DefaultCloudProvider = gce.ProviderNameGCE
func buildCloudProvider(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscoveryOptions, rl *cloudprovider.ResourceLimiter) cloudprovider.CloudProvider {
switch opts.CloudProviderName {
case gce.ProviderNameGCE:
return gce.BuildGCE(opts, do, rl)
case gke.ProviderNameGKE:
return gke.BuildGKE(opts, do, rl)
case aws.ProviderName:
return aws.BuildAWS(opts, do, rl)
case azure.ProviderName:
return azure.BuildAzure(opts, do, rl)
case kubemark.ProviderName:
return kubemark.BuildKubemark(opts, do, rl)
}
return nil
}

View File

@ -0,0 +1,42 @@
// +build aws
/*
Copyright 2018 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 builder
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/aws"
"k8s.io/autoscaler/cluster-autoscaler/config"
)
// AvailableCloudProviders supported by the cloud provider builder.
var AvailableCloudProviders = []string{
aws.ProviderName,
}
// DefaultCloudProvider for AWS-only build is AWS.
const DefaultCloudProvider = aws.ProviderName
func buildCloudProvider(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscoveryOptions, rl *cloudprovider.ResourceLimiter) cloudprovider.CloudProvider {
switch opts.CloudProviderName {
case aws.ProviderName:
return aws.BuildAWS(opts, do, rl)
}
return nil
}

View File

@ -0,0 +1,42 @@
// +build azure
/*
Copyright 2018 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 builder
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/azure"
"k8s.io/autoscaler/cluster-autoscaler/config"
)
// AvailableCloudProviders supported by the cloud provider builder.
var AvailableCloudProviders = []string{
azure.ProviderName,
}
// DefaultCloudProvider on Azure-only build is Azure.
const DefaultCloudProvider = azure.ProviderName
func buildCloudProvider(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscoveryOptions, rl *cloudprovider.ResourceLimiter) cloudprovider.CloudProvider {
switch opts.CloudProviderName {
case azure.ProviderName:
return azure.BuildAzure(opts, do, rl)
}
return nil
}

View File

@ -0,0 +1,46 @@
// +build gce
/*
Copyright 2018 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 builder
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/gce"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/gke"
"k8s.io/autoscaler/cluster-autoscaler/config"
)
// AvailableCloudProviders supported by the cloud provider builder.
var AvailableCloudProviders = []string{
gce.ProviderNameGCE,
gke.ProviderNameGKE,
}
// DefaultCloudProvider is GCE.
const DefaultCloudProvider = gce.ProviderNameGCE
func buildCloudProvider(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscoveryOptions, rl *cloudprovider.ResourceLimiter) cloudprovider.CloudProvider {
switch opts.CloudProviderName {
case gce.ProviderNameGCE:
return gce.BuildGCE(opts, do, rl)
case gke.ProviderNameGKE:
return gke.BuildGKE(opts, do, rl)
}
return nil
}

View File

@ -0,0 +1,42 @@
// +build kubemark
/*
Copyright 2018 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 builder
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/kubemark"
"k8s.io/autoscaler/cluster-autoscaler/config"
)
// AvailableCloudProviders supported by the cloud provider builder.
var AvailableCloudProviders = []string{
kubemark.ProviderName,
}
// DefaultCloudProvider for Kubemark-only build is Kubemark.
const DefaultCloudProvider = kubemark.ProviderName
func buildCloudProvider(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscoveryOptions, rl *cloudprovider.ResourceLimiter) cloudprovider.CloudProvider {
switch opts.CloudProviderName {
case kubemark.ProviderName:
return kubemark.BuildKubemark(opts, do, rl)
}
return nil
}

View File

@ -18,29 +18,12 @@ package builder
import (
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/aws"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/azure"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/gce"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/gke"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/kubemark"
"k8s.io/autoscaler/cluster-autoscaler/config"
"k8s.io/autoscaler/cluster-autoscaler/context"
"github.com/golang/glog"
)
// AvailableCloudProviders supported by the cloud provider builder.
var AvailableCloudProviders = []string{
aws.ProviderName,
azure.ProviderName,
gce.ProviderNameGCE,
gke.ProviderNameGKE,
kubemark.ProviderName,
}
// DefaultCloudProvider is GCE.
const DefaultCloudProvider = gce.ProviderNameGCE
// NewCloudProvider builds a cloud provider from provided parameters.
func NewCloudProvider(opts config.AutoscalingOptions) cloudprovider.CloudProvider {
glog.V(1).Infof("Building %s cloud provider.", opts.CloudProviderName)
@ -49,26 +32,21 @@ func NewCloudProvider(opts config.AutoscalingOptions) cloudprovider.CloudProvide
NodeGroupSpecs: opts.NodeGroups,
NodeGroupAutoDiscoverySpecs: opts.NodeGroupAutoDiscovery,
}
rl := context.NewResourceLimiterFromAutoscalingOptions(opts)
switch opts.CloudProviderName {
case gce.ProviderNameGCE:
return gce.BuildGCE(opts, do, rl)
case gke.ProviderNameGKE:
return gke.BuildGKE(opts, do, rl)
case aws.ProviderName:
return aws.BuildAWS(opts, do, rl)
case azure.ProviderName:
return azure.BuildAzure(opts, do, rl)
case kubemark.ProviderName:
return kubemark.BuildKubemark(opts, do, rl)
case "":
if opts.CloudProviderName == "" {
// Ideally this would be an error, but several unit tests of the
// StaticAutoscaler depend on this behaviour.
glog.Warning("Returning a nil cloud provider")
return nil
}
provider := buildCloudProvider(opts, do, rl)
if provider != nil {
return provider
}
glog.Fatalf("Unknown cloud provider: %s", opts.CloudProviderName)
return nil // This will never happen because the Fatalf will os.Exit
}