Skip to content

Commit 6b48953

Browse files
committed
bsutil: extra-opts validation
1 parent 0f2b4e9 commit 6b48953

File tree

4 files changed

+105
-15
lines changed

4 files changed

+105
-15
lines changed

Diff for: cmd/minikube/cmd/start.go

+14
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,20 @@ func validateFlags(cmd *cobra.Command, drvName string) {
848848
}
849849
}
850850

851+
// validate kubeadm extra args
852+
if invalidOpts := bsutil.FindInvalidExtraConfigFlags(config.ExtraOptions); invalidOpts != nil {
853+
out.ErrT(
854+
out.Warning,
855+
"These --extra-config parameters are invalid: {{.invalid_extra_opts}}",
856+
out.V{"invalid_extra_opts": invalidOpts},
857+
)
858+
exit.WithCodeT(
859+
exit.Config,
860+
"Valid components are: {{.valid_extra_opts}}",
861+
out.V{"valid_extra_opts": bsutil.KubeadmExtraConfigOpts},
862+
)
863+
}
864+
851865
// check that kubeadm extra args contain only whitelisted parameters
852866
for param := range config.ExtraOptions.AsMap().Get(bsutil.Kubeadm) {
853867
if !config.ContainsParam(bsutil.KubeadmExtraArgsWhitelist[bsutil.KubeadmCmdParam], param) &&

Diff for: pkg/minikube/bootstrapper/bsutil/extraconfig.go

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

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

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

148-
for _, component := range keys {
154+
var kubeadmExtraArgs []componentOptions
155+
for _, component := range KubeadmExtraConfigOpts {
149156
kubeadmComponentKey := componentToKubeadmConfigKey[component]
150157
if kubeadmComponentKey == "" {
151158
continue

Diff for: pkg/minikube/bootstrapper/bsutil/extraconfig_test.go

+59
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+
}

Diff for: pkg/minikube/bootstrapper/bsutil/kubeadm.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,24 @@ func GenerateKubeadmYAML(cc config.ClusterConfig, n config.Node, r cruntime.Mana
132132
// These are the components that can be configured
133133
// through the "extra-config"
134134
const (
135-
Kubelet = "kubelet"
136-
Kubeadm = "kubeadm"
137135
Apiserver = "apiserver"
138-
Scheduler = "scheduler"
139136
ControllerManager = "controller-manager"
137+
Kubeadm = "kubeadm"
138+
Kubelet = "kubelet"
140139
Kubeproxy = "kube-proxy"
140+
Scheduler = "scheduler"
141141
)
142142

143+
// KubeadmExtraConfigOpts is a list of allowed "extra-config" components
144+
var KubeadmExtraConfigOpts = []string{
145+
Apiserver,
146+
ControllerManager,
147+
Kubeadm,
148+
Kubelet,
149+
Kubeproxy,
150+
Scheduler,
151+
}
152+
143153
// InvokeKubeadm returns the invocation command for Kubeadm
144154
func InvokeKubeadm(version string) string {
145155
return fmt.Sprintf("sudo env PATH=%s:$PATH kubeadm", binRoot(version))

0 commit comments

Comments
 (0)