mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Merge pull request #121020 from chendave/set_opt
kubeadm: Optimize the logic to override the arguments
This commit is contained in:
commit
0554675d78
@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
)
|
)
|
||||||
@ -34,26 +35,22 @@ import (
|
|||||||
// only the instances of this argument in the overrides to be applied.
|
// only the instances of this argument in the overrides to be applied.
|
||||||
func ArgumentsToCommand(base []kubeadmapi.Arg, overrides []kubeadmapi.Arg) []string {
|
func ArgumentsToCommand(base []kubeadmapi.Arg, overrides []kubeadmapi.Arg) []string {
|
||||||
var command []string
|
var command []string
|
||||||
// Copy the base arguments into a new slice.
|
// Copy the overrides arguments into a new slice.
|
||||||
args := make([]kubeadmapi.Arg, len(base))
|
args := make([]kubeadmapi.Arg, len(overrides))
|
||||||
copy(args, base)
|
copy(args, overrides)
|
||||||
|
|
||||||
// Go trough the override arguments and delete all instances of arguments with the same name
|
// overrideArgs is a set of args which will replace the args defined in the base
|
||||||
// in the base list of arguments.
|
overrideArgs := sets.New[string]()
|
||||||
for i := 0; i < len(overrides); i++ {
|
for _, arg := range overrides {
|
||||||
repeat:
|
overrideArgs.Insert(arg.Name)
|
||||||
for j := 0; j < len(args); j++ {
|
|
||||||
if overrides[i].Name == args[j].Name {
|
|
||||||
// Remove this existing argument and search for another argument
|
|
||||||
// with the same name in base.
|
|
||||||
args = append(args[:j], args[j+1:]...)
|
|
||||||
goto repeat
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, arg := range base {
|
||||||
|
if !overrideArgs.Has(arg.Name) {
|
||||||
|
args = append(args, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concatenate the overrides and the base arguments and then sort them.
|
|
||||||
args = append(args, overrides...)
|
|
||||||
sort.Slice(args, func(i, j int) bool {
|
sort.Slice(args, func(i, j int) bool {
|
||||||
if args[i].Name == args[j].Name {
|
if args[i].Name == args[j].Name {
|
||||||
return args[i].Value < args[j].Value
|
return args[i].Value < args[j].Value
|
||||||
|
Loading…
Reference in New Issue
Block a user