Skip to content

Commit

Permalink
add controller gate flag
Browse files Browse the repository at this point in the history
Signed-off-by: googs1025 <[email protected]>
  • Loading branch information
googs1025 committed May 29, 2024
1 parent aa7c8b8 commit 937104b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/controller-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ type ServerOption struct {
// WorkerThreadsForGC is the number of threads for recycling jobs
// The larger the number, the faster the job recycling, but requires more CPU load.
WorkerThreadsForGC uint32
// Controllers Specify controllers.
// Case1: Use '*' for all controllers,
// Case2: "+gc-controller,+job-controller,+jobflow-controller,+jobtemplate-controller,+pg-controller,+queue-controller"
// to enable specific controllers,
// Case3: "-gc-controller,-job-controller,-jobflow-controller,-jobtemplate-controller,-pg-controller,-queue-controller"
// to disable specific controllers,
Controllers []string
}

type DecryptFunc func(c *ServerOption) error
Expand Down Expand Up @@ -106,6 +113,8 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&s.InheritOwnerAnnotations, "inherit-owner-annotations", true, "Enable inherit owner annotations for pods when create podgroup; it is enabled by default")
fs.Uint32Var(&s.WorkerThreadsForPG, "worker-threads-for-podgroup", defaultPodGroupWorkers, "The number of threads syncing podgroup operations. The larger the number, the faster the podgroup processing, but requires more CPU load.")
fs.Uint32Var(&s.WorkerThreadsForGC, "worker-threads-for-gc", defaultGCWorkers, "The number of threads for recycling jobs. The larger the number, the faster the job recycling, but requires more CPU load.")
fs.StringSliceVar(&s.Controllers, "controller-gates", []string{"*"}, "Specify controller gates. Use '*' for all controllers, \"+gc-controller,+job-controller,+jobflow-controller,+jobtemplate-controller,+pg-controller,+queue-controller\" to enable specific controllers, "+
"and \"-gc-controller,-job-controller,-jobflow-controller,-jobtemplate-controller,-pg-controller,-queue-controller\" to disable specific controllers.")
}

// CheckOptionOrDie check leader election flag when LeaderElection is enabled.
Expand Down
1 change: 1 addition & 0 deletions cmd/controller-manager/app/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestAddFlags(t *testing.T) {
LockObjectNamespace: defaultLockObjectNamespace,
WorkerThreadsForPG: 5,
WorkerThreadsForGC: 1,
Controllers: []string{"*"},
}

if !reflect.DeepEqual(expected, s) {
Expand Down
35 changes: 35 additions & 0 deletions cmd/controller-manager/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func startControllers(config *rest.Config, opt *options.ServerOption) func(ctx c

return func(ctx context.Context) {
framework.ForeachController(func(c framework.Controller) {
// if controller is not enabled, skip it
if !isControllerEnabled(c.Name(), opt.Controllers) {
klog.Infof("Controller <%s> is not enabled", c.Name())
return
}
if err := c.Initialize(controllerOpt); err != nil {
klog.Errorf("Failed to initialize controller <%s>: %v", c.Name(), err)
return
Expand All @@ -139,3 +144,33 @@ func startControllers(config *rest.Config, opt *options.ServerOption) func(ctx c
<-ctx.Done()
}
}

// isControllerEnabled check if a specified controller enabled or not.
// If the input controllers starts with a "+name" or "name", it is considered as an explicit inclusion.
// Otherwise, it is considered as an explicit exclusion.
func isControllerEnabled(name string, controllers []string) bool {
hasStar := false
// if no explicit inclusion or exclusion, enable all controllers by default
if len(controllers) == 0 {
return true
}
for _, ctrl := range controllers {
// if we get here, there was an explicit inclusion
if ctrl == name {
return true
}
// if we get here, there was an explicit inclusion
if ctrl == "+"+name {
return true
}
// if we get here, there was an explicit exclusion
if ctrl == "-"+name {
return false
}
if ctrl == "*" {
hasStar = true
}
}
// if we get here, there was no explicit inclusion or exclusion
return hasStar
}
44 changes: 44 additions & 0 deletions cmd/controller-manager/app/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package app

import "testing"

func TestIsControllerEnabled(t *testing.T) {
testCases := []struct {
name string
gotControllerName string
inputControllers []string
isEnable bool
}{
{
name: "all controller should be enable",
gotControllerName: "controller1",
inputControllers: []string{"*"},
isEnable: true,
},
{
name: "controller2 should be disable",
gotControllerName: "controller2",
inputControllers: []string{"+controller1", "-controller2", "controller3", "*"},
isEnable: false,
},
{
name: "controller3 should be enable",
gotControllerName: "controller3",
inputControllers: []string{"+controller1", "-controller2", "controller3"},
isEnable: true,
},
{
name: "controller4 is not in inputControllers, controller4 should be disable",
gotControllerName: "controller4",
inputControllers: []string{"+controller1", "-controller2", "controller3"},
isEnable: false,
},
}

for _, tc := range testCases {
result := isControllerEnabled(tc.gotControllerName, tc.inputControllers)
if result != tc.isEnable {
t.Errorf("Expected %s to be enabled: %v, but got: %v", tc.gotControllerName, tc.isEnable, result)
}
}
}

0 comments on commit 937104b

Please sign in to comment.