From 40ee4ce2778de9483b322756bd6d0dd74a78a4bc Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Sat, 7 Oct 2017 00:28:16 +0700 Subject: [PATCH] Added error when trying to use kube-scheduler policyConfigMap before Kube 1.7.x --- pkg/model/components/kubescheduler.go | 6 + pkg/model/components/kubescheduler_test.go | 135 +++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 pkg/model/components/kubescheduler_test.go diff --git a/pkg/model/components/kubescheduler.go b/pkg/model/components/kubescheduler.go index 09b22bda1270f..85de3667cbe16 100644 --- a/pkg/model/components/kubescheduler.go +++ b/pkg/model/components/kubescheduler.go @@ -17,6 +17,8 @@ limitations under the License. package components import ( + "fmt" + "k8s.io/kops/pkg/apis/kops" "k8s.io/kops/upup/pkg/fi" "k8s.io/kops/upup/pkg/fi/loader" @@ -37,6 +39,10 @@ func (b *KubeSchedulerOptionsBuilder) BuildOptions(o interface{}) error { config := clusterSpec.KubeScheduler + if config.PolicyConfigMap != "" && b.IsKubernetesLT("v1.7.0") { + return fmt.Errorf("policyConfigMap is only supported in Kubernetes 1.7.0 or later") + } + if config.LogLevel == 0 { // TODO: No way to set to 0? config.LogLevel = 2 diff --git a/pkg/model/components/kubescheduler_test.go b/pkg/model/components/kubescheduler_test.go new file mode 100644 index 0000000000000..be950af64cab5 --- /dev/null +++ b/pkg/model/components/kubescheduler_test.go @@ -0,0 +1,135 @@ +/* +Copyright 2017 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 components + +import ( + "testing" + + api "k8s.io/kops/pkg/apis/kops" + "k8s.io/kops/pkg/apis/kops/util" + "k8s.io/kops/pkg/assets" +) + +func buildSchedulerConfigMapCluster() *api.Cluster { + return &api.Cluster{ + Spec: api.ClusterSpec{ + CloudProvider: "aws", + KubernetesVersion: "v1.4.0", + KubeScheduler: &api.KubeSchedulerConfig{ + PolicyConfigMap: "scheduler-config", + }, + }, + } +} + +func Test_Build_Scheduler_Without_PolicyConfigMap(t *testing.T) { + versions := []string{"v1.6.0", "v1.6.4", "v1.7.0", "v1.7.4"} + b := assets.NewAssetBuilder(nil) + + for _, v := range versions { + + c := buildCluster() + + version, err := util.ParseKubernetesVersion(v) + + if err != nil { + t.Fatalf("unexpected error from ParseKubernetesVersion %s: %v", v, err) + } + + ks := &KubeSchedulerOptionsBuilder{ + &OptionsContext{ + AssetBuilder: b, + KubernetesVersion: *version, + }, + } + + spec := c.Spec + + spec.KubernetesVersion = v + err = ks.BuildOptions(&spec) + + if err != nil { + t.Fatalf("unexpected error from BuildOptions: %v", err) + } + } + +} +func Test_Build_Scheduler_PolicyConfigMap_Unsupported_Version(t *testing.T) { + versions := []string{"v1.6.0", "v1.6.4"} + b := assets.NewAssetBuilder(nil) + + for _, v := range versions { + + c := buildSchedulerConfigMapCluster() + + version, err := util.ParseKubernetesVersion(v) + + if err != nil { + t.Fatalf("unexpected error from ParseKubernetesVersion %s: %v", v, err) + } + + ks := &KubeSchedulerOptionsBuilder{ + &OptionsContext{ + AssetBuilder: b, + KubernetesVersion: *version, + }, + } + + spec := c.Spec + + spec.KubernetesVersion = v + err = ks.BuildOptions(&spec) + + if err == nil { + t.Fatalf("error is expected, but none are returned") + } + } + +} + +func Test_Build_Scheduler_PolicyConfigMap_Supported_Version(t *testing.T) { + versions := []string{"v1.7.0", "v1.7.4", "v1.8.0"} + b := assets.NewAssetBuilder(nil) + + for _, v := range versions { + + c := buildSchedulerConfigMapCluster() + + version, err := util.ParseKubernetesVersion(v) + + if err != nil { + t.Fatalf("unexpected error from ParseKubernetesVersion %s: %v", v, err) + } + + ks := &KubeSchedulerOptionsBuilder{ + &OptionsContext{ + AssetBuilder: b, + KubernetesVersion: *version, + }, + } + + spec := c.Spec + + spec.KubernetesVersion = v + err = ks.BuildOptions(&spec) + + if err != nil { + t.Fatalf("unexpected error from BuildOptions %s: %v", v, err) + } + } + +}