Skip to content

Commit

Permalink
kube-controller-manager/options: unit test WatchListClient feature ga…
Browse files Browse the repository at this point in the history
…te for command line options
  • Loading branch information
p0lyn0mial committed Jan 5, 2024
1 parent a83178c commit a034fd2
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/kube-controller-manager/app/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package options

import (
"fmt"
"reflect"
"sort"
"strings"
Expand All @@ -41,6 +42,7 @@ import (
migration "k8s.io/controller-manager/pkg/leadermigration/options"
netutils "k8s.io/utils/net"

clientgofeaturegate "k8s.io/client-go/features"
kubecontrollerconfig "k8s.io/kubernetes/cmd/kube-controller-manager/app/config"
kubectrlmgrconfig "k8s.io/kubernetes/pkg/controller/apis/config"
csrsigningconfig "k8s.io/kubernetes/pkg/controller/certificates/signer/config"
Expand Down Expand Up @@ -1331,6 +1333,54 @@ func TestControllerManagerAliases(t *testing.T) {
}
}

func TestWatchListClientFlagUsage(t *testing.T) {
assertWatchListClientFeatureDefaultValue(t)

fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
s, _ := NewKubeControllerManagerOptions()
for _, f := range s.Flags([]string{""}, []string{""}, nil).FlagSets {
fs.AddFlagSet(f)
}

fgFlagName := "feature-gates"
fg := fs.Lookup(fgFlagName)
if fg == nil {
t.Fatalf("didn't find %q flag", fgFlagName)
}

expectedWatchListClientString := "WatchListClient=true|false (BETA - default=true)"
if !strings.Contains(fg.Usage, expectedWatchListClientString) {
t.Fatalf("%q flag doesn't contain the expected usage for %v feature gate.\nExpected = %v\nUsage = %v", fgFlagName, clientgofeaturegate.WatchList, expectedWatchListClientString, fg.Usage)
}
}

func TestWatchListClientFlagChange(t *testing.T) {
assertWatchListClientFeatureDefaultValue(t)

fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
s, _ := NewKubeControllerManagerOptions()
for _, f := range s.Flags([]string{""}, []string{""}, nil).FlagSets {
fs.AddFlagSet(f)
}

args := []string{fmt.Sprintf("--feature-gates=%v=false", clientgofeaturegate.WatchList)}
if err := fs.Parse(args); err != nil {
t.Fatal(err)
}

watchListClientValue := clientgofeaturegate.FeatureGates().Enabled(clientgofeaturegate.WatchList)
if watchListClientValue {
t.Fatalf("expected %q feature gate to be disabled after setting the command line flag", clientgofeaturegate.WatchList)
}
}

func assertWatchListClientFeatureDefaultValue(t *testing.T) {
watchListClientDefaultValue := clientgofeaturegate.FeatureGates().Enabled(clientgofeaturegate.WatchList)
if !watchListClientDefaultValue {
t.Fatalf("expected %q feature gate to be enabled for KCM", clientgofeaturegate.WatchList)
}
}

type sortedGCIgnoredResources []garbagecollectorconfig.GroupResource

func (r sortedGCIgnoredResources) Len() int {
Expand Down

0 comments on commit a034fd2

Please sign in to comment.