Skip to content

Commit 1bcb375

Browse files
authored
Merge pull request #8147 from nezorflame/fix-error-msg
add new --extra-config option "scheduler"
2 parents d8106f7 + e1f443e commit 1bcb375

File tree

4 files changed

+107
-16
lines changed

4 files changed

+107
-16
lines changed

cmd/minikube/cmd/start.go

+14
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,20 @@ func validateFlags(cmd *cobra.Command, drvName string) {
898898
}
899899
}
900900

901+
// validate kubeadm extra args
902+
if invalidOpts := bsutil.FindInvalidExtraConfigFlags(config.ExtraOptions); len(invalidOpts) > 0 {
903+
out.ErrT(
904+
out.Warning,
905+
"These --extra-config parameters are invalid: {{.invalid_extra_opts}}",
906+
out.V{"invalid_extra_opts": invalidOpts},
907+
)
908+
exit.WithCodeT(
909+
exit.Config,
910+
"Valid components are: {{.valid_extra_opts}}",
911+
out.V{"valid_extra_opts": bsutil.KubeadmExtraConfigOpts},
912+
)
913+
}
914+
901915
// check that kubeadm extra args contain only allowed parameters
902916
for param := range config.ExtraOptions.AsMap().Get(bsutil.Kubeadm) {
903917
if !config.ContainsParam(bsutil.KubeadmExtraArgsAllowed[bsutil.KubeadmCmdParam], param) &&

pkg/minikube/bootstrapper/bsutil/extraconfig.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ func CreateFlagsFromExtraArgs(extraOptions config.ExtraOptionSlice) string {
9595
return convertToFlags(kubeadmExtraOpts)
9696
}
9797

98+
// FindInvalidExtraConfigFlags returns all invalid 'extra-config' options
99+
func FindInvalidExtraConfigFlags(opts config.ExtraOptionSlice) []string {
100+
invalidOptsMap := make(map[string]struct{})
101+
var invalidOpts []string
102+
for _, extraOpt := range opts {
103+
if _, ok := componentToKubeadmConfigKey[extraOpt.Component]; !ok {
104+
if _, ok := invalidOptsMap[extraOpt.Component]; !ok {
105+
invalidOpts = append(invalidOpts, extraOpt.Component)
106+
invalidOptsMap[extraOpt.Component] = struct{}{}
107+
}
108+
}
109+
}
110+
return invalidOpts
111+
}
112+
98113
// extraConfigForComponent generates a map of flagname-value pairs for a k8s
99114
// component.
100115
func extraConfigForComponent(component string, opts config.ExtraOptionSlice, version semver.Version) (map[string]string, error) {
@@ -133,20 +148,12 @@ func defaultOptionsForComponentAndVersion(component string, version semver.Versi
133148

134149
// newComponentOptions creates a new componentOptions
135150
func newComponentOptions(opts config.ExtraOptionSlice, version semver.Version, featureGates string, cp config.Node) ([]componentOptions, error) {
136-
var kubeadmExtraArgs []componentOptions
137-
for _, extraOpt := range opts {
138-
if _, ok := componentToKubeadmConfigKey[extraOpt.Component]; !ok {
139-
return nil, fmt.Errorf("unknown component %q. valid components are: %v", componentToKubeadmConfigKey, componentToKubeadmConfigKey)
140-
}
141-
}
142-
143-
keys := []string{}
144-
for k := range componentToKubeadmConfigKey {
145-
keys = append(keys, k)
151+
if invalidOpts := FindInvalidExtraConfigFlags(opts); len(invalidOpts) > 0 {
152+
return nil, fmt.Errorf("unknown components %v. valid components are: %v", invalidOpts, KubeadmExtraConfigOpts)
146153
}
147-
sort.Strings(keys)
148154

149-
for _, component := range keys {
155+
var kubeadmExtraArgs []componentOptions
156+
for _, component := range KubeadmExtraConfigOpts {
150157
kubeadmComponentKey := componentToKubeadmConfigKey[component]
151158
if kubeadmComponentKey == "" {
152159
continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package bsutil will eventually be renamed to kubeadm package after getting rid of older one
18+
package bsutil
19+
20+
import (
21+
"reflect"
22+
"testing"
23+
24+
"k8s.io/minikube/pkg/minikube/config"
25+
)
26+
27+
func TestFindInvalidExtraConfigFlags(t *testing.T) {
28+
defaultOpts := getExtraOpts()
29+
badOption1 := config.ExtraOption{Component: "bad_option_1"}
30+
badOption2 := config.ExtraOption{Component: "bad_option_2"}
31+
tests := []struct {
32+
name string
33+
opts config.ExtraOptionSlice
34+
want []string
35+
}{
36+
{
37+
name: "with valid options only",
38+
opts: defaultOpts,
39+
want: nil,
40+
},
41+
{
42+
name: "with invalid options",
43+
opts: append(defaultOpts, badOption1, badOption2),
44+
want: []string{"bad_option_1", "bad_option_2"},
45+
},
46+
{
47+
name: "with invalid options and duplicates",
48+
opts: append(defaultOpts, badOption2, badOption1, badOption1),
49+
want: []string{"bad_option_2", "bad_option_1"},
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
if got := FindInvalidExtraConfigFlags(tt.opts); !reflect.DeepEqual(got, tt.want) {
55+
t.Errorf("FindInvalidExtraConfigFlags() = %v, want %v", got, tt.want)
56+
}
57+
})
58+
}
59+
}

pkg/minikube/bootstrapper/bsutil/kubeadm.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,26 @@ func GenerateKubeadmYAML(cc config.ClusterConfig, n config.Node, r cruntime.Mana
147147
// These are the components that can be configured
148148
// through the "extra-config"
149149
const (
150-
Kubelet = "kubelet"
151-
Kubeadm = "kubeadm"
152150
Apiserver = "apiserver"
153-
Scheduler = "scheduler"
154151
ControllerManager = "controller-manager"
155-
Kubeproxy = "kube-proxy"
152+
Scheduler = "scheduler"
156153
Etcd = "etcd"
154+
Kubeadm = "kubeadm"
155+
Kubeproxy = "kube-proxy"
156+
Kubelet = "kubelet"
157157
)
158158

159+
// KubeadmExtraConfigOpts is a list of allowed "extra-config" components
160+
var KubeadmExtraConfigOpts = []string{
161+
Apiserver,
162+
ControllerManager,
163+
Scheduler,
164+
Etcd,
165+
Kubeadm,
166+
Kubelet,
167+
Kubeproxy,
168+
}
169+
159170
// InvokeKubeadm returns the invocation command for Kubeadm
160171
func InvokeKubeadm(version string) string {
161172
return fmt.Sprintf("sudo env PATH=%s:$PATH kubeadm", binRoot(version))

0 commit comments

Comments
 (0)