Skip to content

Commit

Permalink
feat: support yurt-manager work in specified namespace
Browse files Browse the repository at this point in the history
Signed-off-by: ricky <yricky509@gmail.com>
  • Loading branch information
y-ykcir committed Apr 13, 2023
1 parent 4f62983 commit 632e001
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
10 changes: 5 additions & 5 deletions charts/openyurt/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ webhooks:
clientConfig:
service:
name: webhook-service
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
path: /mutate-raven-openyurt-io-v1alpha1-gateway
failurePolicy: Fail
name: mutate.raven.v1alpha1.gateway.openyurt.io
Expand All @@ -749,7 +749,7 @@ webhooks:
clientConfig:
service:
name: webhook-service
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
path: /mutate-apps-openyurt-io-v1beta1-nodepool
failurePolicy: Fail
name: m.v1beta1.nodepool.kb.io
Expand Down Expand Up @@ -777,7 +777,7 @@ webhooks:
clientConfig:
service:
name: webhook-service
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
path: /validate-raven-openyurt-io-v1alpha1-gateway
failurePolicy: Fail
name: validate.raven.v1alpha1.gateway.openyurt.io
Expand All @@ -797,7 +797,7 @@ webhooks:
clientConfig:
service:
name: webhook-service
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
path: /validate-apps-openyurt-io-v1beta1-nodepool
failurePolicy: Fail
name: v.v1beta1.nodepool.kb.io
Expand All @@ -818,7 +818,7 @@ webhooks:
clientConfig:
service:
name: webhook-service
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
path: /validate-core-openyurt-io-v1-pod
failurePolicy: Fail
name: validate.core.v1.pod.openyurt.io
Expand Down
8 changes: 4 additions & 4 deletions charts/openyurt/templates/yurt-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: yurt-manager
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand All @@ -15,14 +15,14 @@ roleRef:
subjects:
- kind: ServiceAccount
name: yurt-manager
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
---

apiVersion: v1
kind: Service
metadata:
name: yurt-manager-webhook-service
namespace: kube-system
namespace: {{ .Release.Namespace | quote }}
spec:
ports:
- port: 443
Expand All @@ -37,7 +37,7 @@ metadata:
labels:
{{- include "yurt-manager.labels" . | nindent 4 }}
name: yurt-manager
namespace: "kube-system"
namespace: {{ .Release.Namespace | quote }}
spec:
replicas: {{ .Values.yurtManager.replicas }}
selector:
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 @@ -193,7 +193,7 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error {

// +kubebuilder:scaffold:builder
setupLog.Info("initialize webhook")
if err := webhook.Initialize(ctx, cfg); err != nil {
if err := webhook.Initialize(ctx, cfg, c); err != nil {
setupLog.Error(err, "unable to initialize webhook")
os.Exit(1)
}
Expand Down
2 changes: 2 additions & 0 deletions hack/make-rules/kustomize_to_chart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ EOF
append_note $yurt_manager_templatefile

kubectl kustomize ${output_default_dir} >> $yurt_manager_templatefile

sed "s/namespace: kube-system/namespace: {{ .Release.Namespace | quote }}/g" $yurt_manager_templatefile > tmpfile && mv tmpfile $yurt_manager_templatefile
}


Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/gateway/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func Add(c *appconfig.CompletedConfig, mgr manager.Manager) error {
if !utildiscovery.DiscoverGVK(controllerKind) {
return nil
}
// init global variables
cfg := c.ComponentConfig.Generic
ravenv1alpha1.ServiceNamespacedName.Namespace = cfg.WorkingNamespace

return add(mgr, newReconciler(c, mgr))
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
webhookcontroller "github.com/openyurtio/openyurt/pkg/webhook/util/controller"
"github.com/openyurtio/openyurt/pkg/webhook/util/health"
)
Expand Down Expand Up @@ -77,8 +78,8 @@ type GateFunc func() (enabled bool)
// +kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=validatingwebhookconfigurations,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch;update;patch

func Initialize(ctx context.Context, cfg *rest.Config) error {
c, err := webhookcontroller.New(cfg, WebhookHandlerPath)
func Initialize(ctx context.Context, cfg *rest.Config, cc *config.CompletedConfig) error {
c, err := webhookcontroller.New(cfg, WebhookHandlerPath, cc)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/webhook/util/controller/webhook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"

"github.com/openyurtio/openyurt/cmd/yurt-manager/app/config"
extclient "github.com/openyurtio/openyurt/pkg/client"
webhookutil "github.com/openyurtio/openyurt/pkg/webhook/util"
"github.com/openyurtio/openyurt/pkg/webhook/util/configuration"
Expand All @@ -48,7 +49,6 @@ const (
)

var (
namespace = webhookutil.GetNamespace()
secretName = webhookutil.GetSecretName()

uninit = make(chan struct{})
Expand All @@ -69,7 +69,9 @@ type Controller struct {
queue workqueue.RateLimitingInterface
}

func New(cfg *rest.Config, handlers map[string]struct{}) (*Controller, error) {
func New(cfg *rest.Config, handlers map[string]struct{}, cc *config.CompletedConfig) (*Controller, error) {
webhookutil.SetNamespace(cc.ComponentConfig.Generic.WorkingNamespace)

c := &Controller{
kubeClient: extclient.GetGenericClientWithName("webhook-controller").KubeClient,
handlers: handlers,
Expand All @@ -78,7 +80,7 @@ func New(cfg *rest.Config, handlers map[string]struct{}) (*Controller, error) {

c.informerFactory = informers.NewSharedInformerFactory(c.kubeClient, 0)

secretInformer := coreinformers.New(c.informerFactory, namespace, nil).Secrets()
secretInformer := coreinformers.New(c.informerFactory, webhookutil.GetNamespace(), nil).Secrets()
admissionRegistrationInformer := admissionregistrationinformers.New(c.informerFactory, v1.NamespaceAll, nil)

secretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
Expand Down
8 changes: 7 additions & 1 deletion pkg/webhook/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ const (
ValidatingWebhookConfigurationName = "yurt-manager-validating-webhook-configuration"
)

var namespace = "kube-system"

func GetHost() string {
return os.Getenv("WEBHOOK_HOST")
}

func GetNamespace() string {
return "kube-system"
return namespace
}

func SetNamespace(ns string) {
namespace = ns
}

func GetSecretName() string {
Expand Down

0 comments on commit 632e001

Please sign in to comment.