Skip to content

Commit

Permalink
support to specify enabled controllers (#1388)
Browse files Browse the repository at this point in the history
* support to specify enabled controllers

Signed-off-by: hxcGit <houxc_mail@163.com>

* fix SetupWebhookWithManager slice len

Signed-off-by: hxcGit <houxc_mail@163.com>

* fix controllers in chart template

Signed-off-by: hxcGit <houxc_mail@163.com>

---------

Signed-off-by: hxcGit <houxc_mail@163.com>
  • Loading branch information
xavier-hou authored Apr 20, 2023
1 parent 1ba0e7b commit 260a5b3
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 39 deletions.
3 changes: 3 additions & 0 deletions charts/openyurt/templates/yurt-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ spec:
- --logtostderr=true
- --v={{ .Values.yurtManager.log.level }}
- --working-namespace={{ include "openyurt.namespace" . }}
{{- if .Values.yurtManager.controllers }}
- --controllers={{ .Values.yurtManager.controllers }}
{{- end }}
command:
- /usr/local/bin/yurt-manager
image: {{ .Values.yurtManager.image.repository }}:{{ .Values.yurtManager.image.tag }}
Expand Down
2 changes: 2 additions & 0 deletions charts/openyurt/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ yurtManager:
port: 10270
metrics:
port: 10271
# format should be "foo,-bar,*"
controllers: ""
healthProbe:
port: 10272
# resources of yurt-manager container
Expand Down
2 changes: 1 addition & 1 deletion cmd/yurt-manager/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {
}

setupLog.Info("setup webhook")
if err = webhook.SetupWithManager(mgr); err != nil {
if err = webhook.SetupWithManager(c, mgr); err != nil {
setupLog.Error(err, "unable to setup webhook")
os.Exit(1)
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/yurt-manager/app/options/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/openyurtio/openyurt/pkg/features"
)

const enableAllController = "*"

type GenericOptions struct {
*config.GenericConfiguration
}
Expand All @@ -38,6 +40,7 @@ func NewGenericOptions() *GenericOptions {
RestConfigQPS: 30,
RestConfigBurst: 50,
WorkingNamespace: "kube-system",
Controllers: []string{enableAllController},
},
}
}
Expand Down Expand Up @@ -67,6 +70,7 @@ func (o *GenericOptions) ApplyTo(cfg *config.GenericConfiguration) error {
cfg.RestConfigQPS = o.RestConfigQPS
cfg.RestConfigBurst = o.RestConfigBurst
cfg.WorkingNamespace = o.WorkingNamespace
cfg.Controllers = o.Controllers

return nil
}
Expand All @@ -85,6 +89,8 @@ func (o *GenericOptions) AddFlags(fs *pflag.FlagSet) {
fs.IntVar(&o.RestConfigQPS, "rest-config-qps", o.RestConfigQPS, "rest-config-qps.")
fs.IntVar(&o.RestConfigBurst, "rest-config-burst", o.RestConfigBurst, "rest-config-burst.")
fs.StringVar(&o.WorkingNamespace, "working-namespace", o.WorkingNamespace, "The namespace where the yurt-manager is working.")
fs.StringSliceVar(&o.Controllers, "controllers", o.Controllers, "A list of controllers to enable. "+
"'*' enables all on-by-default controllers, 'foo' enables the controller named 'foo', '-foo' disables the controller named 'foo'.")

features.DefaultMutableFeatureGate.AddFlag(fs)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_csrapprover.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, csrapprover.Add)
addController("csrapprover", csrapprover.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_daemonpodupdater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, daemonpodupdater.Add)
addController("daemonpodupdater", daemonpodupdater.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_delegatelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
controllerAddFuncs = append(controllerAddFuncs, delegatelease.Add)
addController("delegatelease", delegatelease.Add)
}
3 changes: 2 additions & 1 deletion pkg/controller/add_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, gateway.Add, service.Add)
addController("gateway", gateway.Add)
addController("service", service.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, nodepool.Add)
addController("nodepool", nodepool.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_podbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
controllerAddFuncs = append(controllerAddFuncs, podbinding.Add)
addController("podbinding", podbinding.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_poolcoordinatorcert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, poolcoordinatorcert.Add)
addController("poolcoordinatorcert", poolcoordinatorcert.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_servicetopology_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, servicetopologyEndpoints.Add)
addController("servicetopologyEndpoints", servicetopologyEndpoints.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_servicetopology_endpointslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, servicetopologyEndpointslice.Add)
addController("servicetopologyEndpointslice", servicetopologyEndpointslice.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_staticpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, staticpod.Add)
addController("staticpod", staticpod.Add)
}
2 changes: 1 addition & 1 deletion pkg/controller/add_yurtappset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ import (
// Note !!!

func init() {
controllerAddFuncs = append(controllerAddFuncs, yurtappset.Add)
addController("yurtappset", yurtappset.Add)
}
6 changes: 6 additions & 0 deletions pkg/controller/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ type GenericConfiguration struct {
RestConfigQPS int
RestConfigBurst int
WorkingNamespace string
// Controllers is the list of controllers to enable or disable
// '*' means "all enabled by default controllers"
// 'foo' means "enable 'foo'"
// '-foo' means "disable 'foo'"
// first item for a particular name wins
Controllers []string
}
16 changes: 13 additions & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
"github.com/openyurtio/openyurt/pkg/controller/util"
)

// Note !!! @kadisi
Expand All @@ -30,9 +31,13 @@ import (

// Don`t Change this Name !!!! @kadisi
// TODO support feature gate @kadisi
var controllerAddFuncs []func(*config.CompletedConfig, manager.Manager) error
var controllerAddFuncs map[string]func(*config.CompletedConfig, manager.Manager) error

func init() {
func addController(name string, fn func(*config.CompletedConfig, manager.Manager) error) {
if controllerAddFuncs == nil {
controllerAddFuncs = make(map[string]func(*config.CompletedConfig, manager.Manager) error)
}
controllerAddFuncs[name] = fn
}

// If you want to add additional RBAC, enter it here !!! @kadisi
Expand All @@ -42,7 +47,12 @@ func init() {

func SetupWithManager(c *config.CompletedConfig, m manager.Manager) error {
klog.InfoS("SetupWithManager", "len", len(controllerAddFuncs))
for _, f := range controllerAddFuncs {
for controllerName, f := range controllerAddFuncs {
if !util.IsControllerEnabled(controllerName, c.ComponentConfig.Generic.Controllers) {
klog.Warningf("Controller %v is disabled", controllerName)
continue
}

if err := f(c, m); err != nil {
if kindMatchErr, ok := err.(*meta.NoKindMatchError); ok {
klog.Infof("CRD %v is not installed, its controller will perform noops!", kindMatchErr.GroupKind)
Expand Down
34 changes: 34 additions & 0 deletions pkg/controller/util/controller_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

// IsControllerEnabled check if a specified controller enabled or not.
func IsControllerEnabled(name string, controllers []string) bool {
for _, ctrl := range controllers {
if ctrl == name {
return true
}
if ctrl == "-"+name {
return false
}
if ctrl == "*" {
return true
}
}

return false
}
2 changes: 1 addition & 1 deletion pkg/webhook/add_v1alpha1_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
addWebhook(&v1alpha1.GatewayHandler{})
addWebhook("gateway", &v1alpha1.GatewayHandler{})
}
2 changes: 1 addition & 1 deletion pkg/webhook/add_v1alpha1_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
addWebhook(&v1alpha1.NodePoolHandler{})
addWebhook("nodepool", &v1alpha1.NodePoolHandler{})
}
2 changes: 1 addition & 1 deletion pkg/webhook/add_v1alpha1_staticpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
addWebhook(&v1alpha1.StaticPodHandler{})
addWebhook("staticpod", &v1alpha1.StaticPodHandler{})
}
2 changes: 1 addition & 1 deletion pkg/webhook/add_v1alpha1_yurtappset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
addWebhook(&v1alpha1.YurtAppSetHandler{})
addWebhook("yurtappset", &v1alpha1.YurtAppSetHandler{})
}
2 changes: 1 addition & 1 deletion pkg/webhook/add_v1beta1_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
)

func init() {
addWebhook(&v1beta1.NodePoolHandler{})
addWebhook("nodepool", &v1beta1.NodePoolHandler{})
}
53 changes: 34 additions & 19 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
"github.com/openyurtio/openyurt/pkg/controller/util"
webhookcontroller "github.com/openyurtio/openyurt/pkg/webhook/util/controller"
"github.com/openyurtio/openyurt/pkg/webhook/util/health"
)
Expand All @@ -40,33 +41,47 @@ type SetupWebhookWithManager interface {
SetupWebhookWithManager(mgr ctrl.Manager) (string, string, error)
}

var WebhookLists []SetupWebhookWithManager = make([]SetupWebhookWithManager, 0, 5)
var controllerWebhook map[string][]SetupWebhookWithManager
var WebhookHandlerPath = make(map[string]struct{})

func addWebhook(name string, handler SetupWebhookWithManager) {
if controllerWebhook == nil {
controllerWebhook = make(map[string][]SetupWebhookWithManager)
}

if controllerWebhook[name] == nil {
controllerWebhook[name] = make([]SetupWebhookWithManager, 0)
}

controllerWebhook[name] = append(controllerWebhook[name], handler)
}

// Note !!! @kadisi
// Do not change the name of the file or the contents of the file !!!!!!!!!!
// Note !!!

func addWebhook(w SetupWebhookWithManager) {
WebhookLists = append(WebhookLists, w)
}

func SetupWithManager(mgr manager.Manager) error {
for _, s := range WebhookLists {
m, v, err := s.SetupWebhookWithManager(mgr)
if err != nil {
return fmt.Errorf("unable to create webhook %v", err)
}
if _, ok := WebhookHandlerPath[m]; ok {
panic(fmt.Errorf("webhook handler path %s duplicated", m))
func SetupWithManager(c *config.CompletedConfig, mgr manager.Manager) error {
for controllerName, list := range controllerWebhook {
if !util.IsControllerEnabled(controllerName, c.ComponentConfig.Generic.Controllers) {
klog.Warningf("Webhook for %v is disabled", controllerName)
continue
}
WebhookHandlerPath[m] = struct{}{}
klog.Infof("Add webhook mutate path %s", m)
if _, ok := WebhookHandlerPath[v]; ok {
panic(fmt.Errorf("webhook handler path %s duplicated", v))
for _, s := range list {
m, v, err := s.SetupWebhookWithManager(mgr)
if err != nil {
return fmt.Errorf("unable to create webhook %v", err)
}
if _, ok := WebhookHandlerPath[m]; ok {
panic(fmt.Errorf("webhook handler path %s duplicated", m))
}
WebhookHandlerPath[m] = struct{}{}
klog.Infof("Add webhook mutate path %s", m)
if _, ok := WebhookHandlerPath[v]; ok {
panic(fmt.Errorf("webhook handler path %s duplicated", v))
}
WebhookHandlerPath[v] = struct{}{}
klog.Infof("Add webhook validate path %s", v)
}
WebhookHandlerPath[v] = struct{}{}
klog.Infof("Add webhook validate path %s", v)
}
return nil
}
Expand Down

0 comments on commit 260a5b3

Please sign in to comment.