k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/util/arguments.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 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 util 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 24 "github.com/pkg/errors" 25 26 "k8s.io/apimachinery/pkg/util/sets" 27 "k8s.io/klog/v2" 28 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" 29 ) 30 31 // ArgumentsToCommand takes two Arg slices, one with the base arguments and one 32 // with optional override arguments. In the return list override arguments will precede base 33 // arguments. If an argument is present in the overrides, it will cause 34 // all instances of the same argument in the base list to be discarded, leaving 35 // only the instances of this argument in the overrides to be applied. 36 func ArgumentsToCommand(base []kubeadmapi.Arg, overrides []kubeadmapi.Arg) []string { 37 var command []string 38 // Copy the overrides arguments into a new slice. 39 args := make([]kubeadmapi.Arg, len(overrides)) 40 copy(args, overrides) 41 42 // overrideArgs is a set of args which will replace the args defined in the base 43 overrideArgs := sets.New[string]() 44 for _, arg := range overrides { 45 overrideArgs.Insert(arg.Name) 46 } 47 48 for _, arg := range base { 49 if !overrideArgs.Has(arg.Name) { 50 args = append(args, arg) 51 } 52 } 53 54 sort.Slice(args, func(i, j int) bool { 55 if args[i].Name == args[j].Name { 56 return args[i].Value < args[j].Value 57 } 58 return args[i].Name < args[j].Name 59 }) 60 61 for _, arg := range args { 62 command = append(command, fmt.Sprintf("--%s=%s", arg.Name, arg.Value)) 63 } 64 65 return command 66 } 67 68 // ArgumentsFromCommand parses a CLI command in the form "--foo=bar" to an Arg slice 69 func ArgumentsFromCommand(command []string) []kubeadmapi.Arg { 70 args := []kubeadmapi.Arg{} 71 for i, arg := range command { 72 key, val, err := parseArgument(arg) 73 74 // Ignore if the first argument doesn't satisfy the criteria, it's most often the binary name 75 // Warn in all other cases, but don't error out. This can happen only if the user has edited the argument list by hand, so they might know what they are doing 76 if err != nil { 77 if i != 0 { 78 klog.Warningf("[kubeadm] WARNING: The component argument %q could not be parsed correctly. The argument must be of the form %q. Skipping...\n", arg, "--") 79 } 80 continue 81 } 82 83 args = append(args, kubeadmapi.Arg{Name: key, Value: val}) 84 } 85 86 sort.Slice(args, func(i, j int) bool { 87 if args[i].Name == args[j].Name { 88 return args[i].Value < args[j].Value 89 } 90 return args[i].Name < args[j].Name 91 }) 92 return args 93 } 94 95 // parseArgument parses the argument "--foo=bar" to "foo" and "bar" 96 func parseArgument(arg string) (string, string, error) { 97 if !strings.HasPrefix(arg, "--") { 98 return "", "", errors.New("the argument should start with '--'") 99 } 100 if !strings.Contains(arg, "=") { 101 return "", "", errors.New("the argument should have a '=' between the flag and the value") 102 } 103 // Remove the starting -- 104 arg = strings.TrimPrefix(arg, "--") 105 // Split the string on =. Return only two substrings, since we want only key/value, but the value can include '=' as well 106 keyvalSlice := strings.SplitN(arg, "=", 2) 107 108 // Make sure both a key and value is present 109 if len(keyvalSlice) != 2 { 110 return "", "", errors.New("the argument must have both a key and a value") 111 } 112 if len(keyvalSlice[0]) == 0 { 113 return "", "", errors.New("the argument must have a key") 114 } 115 116 return keyvalSlice[0], keyvalSlice[1], nil 117 }