Exporting the AddFeatureGate function and adding a unit test for it. (#3661)

pull/3732/head
Jamie Phillips 2021-07-28 16:04:42 -04:00 committed by GitHub
parent fc19b805d5
commit 7704fb6ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 11 deletions

View File

@ -57,13 +57,6 @@ func startKubelet(cfg *daemonconfig.Agent) error {
return executor.Kubelet(args)
}
func addFeatureGate(current, new string) string {
if current == "" {
return new
}
return current + "," + new
}
// ImageCredProvAvailable checks to see if the kubelet image credential provider bin dir and config
// files exist and are of the correct types. This is exported so that it may be used by downstream projects.
func ImageCredProvAvailable(cfg *daemonconfig.Agent) bool {

View File

@ -134,7 +134,7 @@ func kubeletArgs(cfg *config.Agent) map[string]string {
logrus.Warn("Disabling pod PIDs limit feature due to missing cgroup pids support")
argsMap["cgroups-per-qos"] = "false"
argsMap["enforce-node-allocatable"] = ""
argsMap["feature-gates"] = addFeatureGate(argsMap["feature-gates"], "SupportPodPidsLimit=false")
argsMap["feature-gates"] = util.AddFeatureGate(argsMap["feature-gates"], "SupportPodPidsLimit=false")
}
if kubeletRoot != "" {
argsMap["kubelet-cgroups"] = kubeletRoot
@ -143,7 +143,7 @@ func kubeletArgs(cfg *config.Agent) map[string]string {
argsMap["runtime-cgroups"] = runtimeRoot
}
if system.RunningInUserNS() {
argsMap["feature-gates"] = addFeatureGate(argsMap["feature-gates"], "DevicePlugins=false")
argsMap["feature-gates"] = util.AddFeatureGate(argsMap["feature-gates"], "DevicePlugins=false")
}
argsMap["node-labels"] = strings.Join(cfg.NodeLabels, ",")
@ -156,7 +156,7 @@ func kubeletArgs(cfg *config.Agent) map[string]string {
if ImageCredProvAvailable(cfg) {
logrus.Infof("Kubelet image credential provider bin dir and configuration file found.")
argsMap["feature-gates"] = addFeatureGate(argsMap["feature-gates"], "KubeletCredentialProviders=true")
argsMap["feature-gates"] = util.AddFeatureGate(argsMap["feature-gates"], "KubeletCredentialProviders=true")
argsMap["image-credential-provider-bin-dir"] = cfg.ImageCredProvBinDir
argsMap["image-credential-provider-config"] = cfg.ImageCredProvConfig
}

View File

@ -120,7 +120,7 @@ func kubeletArgs(cfg *config.Agent) map[string]string {
if ImageCredProvAvailable(cfg) {
logrus.Infof("Kubelet image credential provider bin dir and configuration file found.")
argsMap["feature-gates"] = addFeatureGate(argsMap["feature-gates"], "KubeletCredentialProviders=true")
argsMap["feature-gates"] = util.AddFeatureGate(argsMap["feature-gates"], "KubeletCredentialProviders=true")
argsMap["image-credential-provider-bin-dir"] = cfg.ImageCredProvBinDir
argsMap["image-credential-provider-config"] = cfg.ImageCredProvConfig
}

9
pkg/util/gates.go Normal file
View File

@ -0,0 +1,9 @@
package util
// AddFeatureGate correctly appends a feature gate key pair to the feature gates CLI switch.
func AddFeatureGate(current, new string) string {
if current == "" {
return new
}
return current + "," + new
}

43
pkg/util/gates_test.go Normal file
View File

@ -0,0 +1,43 @@
package util
import (
"testing"
)
func TestAddFeatureGate(t *testing.T) {
type args struct {
currentArg string
featureGate string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Feature gate added to empty arg",
args: args{
currentArg: "",
featureGate: "SupportPodPidsLimit=false",
},
want: "SupportPodPidsLimit=false",
},
{
name: "Feature gate added to existing arg",
args: args{
currentArg: "SupportPodPidsLimit=false",
featureGate: "DevicePlugins=false",
},
want: "SupportPodPidsLimit=false,DevicePlugins=false",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AddFeatureGate(tt.args.currentArg, tt.args.featureGate)
if got != tt.want {
t.Errorf("error, should be " + tt.want + ", but got " + got)
}
})
}
}