Skip to content

Commit

Permalink
add parameter webhook for yurt-manager
Browse files Browse the repository at this point in the history
Signed-off-by: hxcGit <houxc_mail@163.com>
  • Loading branch information
xavier-hou committed Apr 20, 2023
1 parent fe6b1b5 commit ce45643
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 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 @@ -61,6 +61,9 @@ spec:
{{- if .Values.yurtManager.controllers }}
- --controllers={{ .Values.yurtManager.controllers }}
{{- end }}
- {{- if .Values.yurtManager.webhooks }}
- --controllers={{ .Values.yurtManager.webhooks }}
{{- end }}
command:
- /usr/local/bin/yurt-manager
image: {{ .Values.yurtManager.image.repository }}:{{ .Values.yurtManager.image.tag }}
Expand Down
1 change: 1 addition & 0 deletions charts/openyurt/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ yurtManager:
port: 10271
# format should be "foo,-bar,*"
controllers: ""
webhooks: ""
healthProbe:
port: 10272
# resources of yurt-manager container
Expand Down
8 changes: 6 additions & 2 deletions cmd/yurt-manager/app/options/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/openyurtio/openyurt/pkg/features"
)

const enableAllController = "*"
const enableAll = "*"

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

return nil
}
Expand All @@ -91,6 +93,8 @@ func (o *GenericOptions) AddFlags(fs *pflag.FlagSet) {
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'.")
fs.StringSliceVar(&o.Webhooks, "webhooks", o.Webhooks, "A list of webhooks to enable. "+
"'*' enables all on-by-default webhooks, 'foo' enables the webhook named 'foo', '-foo' disables the webhook named 'foo'.")

features.DefaultMutableFeatureGate.AddFlag(fs)
}
2 changes: 2 additions & 0 deletions pkg/controller/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ type GenericConfiguration struct {
// '-foo' means "disable 'foo'"
// first item for a particular name wins
Controllers []string
// Same as Controllers
Webhooks []string
}
25 changes: 0 additions & 25 deletions pkg/webhook/add_v1alpha1_yurtappdaemon.go

This file was deleted.

20 changes: 13 additions & 7 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ import (
"net/http"
"time"

"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
"github.com/openyurtio/openyurt/pkg/controller/util"
ctrlutil "github.com/openyurtio/openyurt/pkg/controller/util"
v1alpha1gateway "github.com/openyurtio/openyurt/pkg/webhook/gateway/v1alpha1"
v1alpha1nodepool "github.com/openyurtio/openyurt/pkg/webhook/nodepool/v1alpha1"
v1beta1nodepool "github.com/openyurtio/openyurt/pkg/webhook/nodepool/v1beta1"
v1pod "github.com/openyurtio/openyurt/pkg/webhook/pod/v1"
v1alpha1staticpod "github.com/openyurtio/openyurt/pkg/webhook/staticpod/v1alpha1"
"github.com/openyurtio/openyurt/pkg/webhook/util"
webhookcontroller "github.com/openyurtio/openyurt/pkg/webhook/util/controller"
"github.com/openyurtio/openyurt/pkg/webhook/util/health"
v1alpha1yurtappdaemon "github.com/openyurtio/openyurt/pkg/webhook/yurtappdaemon/v1alpha1"
v1alpha1yurtappset "github.com/openyurtio/openyurt/pkg/webhook/yurtappset/v1alpha1"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

type SetupWebhookWithManager interface {
Expand Down Expand Up @@ -97,13 +99,17 @@ func SetupWithManager(c *config.CompletedConfig, mgr manager.Manager) error {
}

// set up independent webhooks
for _, s := range webhooks {
for name, s := range webhooks {
if !util.IsWebhookEnabled(name, c.ComponentConfig.Generic.Webhooks) {
klog.Warningf("Webhook %v is disabled", name)
continue
}
return setup(s)
}

// set up controller webhooks
for controllerName, list := range controllerWebhooks {
if !util.IsControllerEnabled(controllerName, c.ComponentConfig.Generic.Controllers) {
if !ctrlutil.IsControllerEnabled(controllerName, c.ComponentConfig.Generic.Controllers) {
klog.Warningf("Webhook for %v is disabled", controllerName)
continue
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/webhook/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ func GenerateValidatePath(gvk schema.GroupVersionKind) string {
return "/validate-" + strings.ReplaceAll(gvk.Group, ".", "-") + "-" +
gvk.Version + "-" + strings.ToLower(gvk.Kind)
}

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

return false
}

0 comments on commit ce45643

Please sign in to comment.