diff --git a/docs/userguide/configure-controllers.md b/docs/userguide/configure-controllers.md index ec0d76560..74b98dad5 100644 --- a/docs/userguide/configure-controllers.md +++ b/docs/userguide/configure-controllers.md @@ -48,9 +48,9 @@ E.g. Specify a controller list: --controllers=cluster,clusterStatus,binding,xxx ``` -E.g. Disable some controllers: +E.g. Disable some controllers(remember to keep `*` if you want to keep the rest controllers in the default list): ```bash ---controllers=-hpa,-unifiedAuth +--controllers=-hpa,-unifiedAuth,* ``` Use `-foo` to disable the controller named `foo`. diff --git a/pkg/controllers/context/context_test.go b/pkg/controllers/context/context_test.go new file mode 100644 index 000000000..bbbb4c04a --- /dev/null +++ b/pkg/controllers/context/context_test.go @@ -0,0 +1,109 @@ +package context + +import ( + "testing" + + "k8s.io/apimachinery/pkg/util/sets" +) + +type args struct { + controllerName string + disabledByDefaultControllers []string +} + +func TestContext_IsControllerEnabled(t *testing.T) { + tests := []struct { + name string + opts Options + args args + expected bool + }{ + { + name: "on by name", + args: args{ + controllerName: "bravo", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"alpha", "bravo", "-charlie"}, + }, + expected: true, + }, + { + name: "off by name", + args: args{ + controllerName: "charlie", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"alpha", "bravo", "-charlie"}, + }, + expected: false, + }, + { + name: "on by default", + args: args{ + controllerName: "alpha", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"*"}, + }, + expected: true, + }, + { + name: "off by default", + args: args{ + controllerName: "delta", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"*"}, + }, + expected: false, + }, + { + name: "on by star, not off by name", + args: args{ + controllerName: "alpha", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"*", "-charlie"}, + }, + expected: true, + }, + { + name: "off by name with star", + args: args{ + controllerName: "charlie", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"*", "-charlie"}, + }, + expected: false, + }, + { + name: "off by default implicit, no star", + args: args{ + controllerName: "foxtrot", + disabledByDefaultControllers: []string{"delta", "echo"}, + }, + opts: Options{ + Controllers: []string{"alpha", "bravo", "-charlie"}, + }, + expected: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := Context{ + Opts: tt.opts, + } + if got := c.IsControllerEnabled(tt.args.controllerName, sets.NewString(tt.args.disabledByDefaultControllers...)); got != tt.expected { + t.Errorf("IsControllerEnabled() = %v, want %v", got, tt.expected) + } + }) + } +}