Commit e6009b1e authored by Darren Shepherd's avatar Darren Shepherd Committed by Brad Davidson

Introduce servicelb-namespace parameter

This parameter controls which namespace the klipper-lb pods will be create. It defaults to kube-system so that k3s does not by default create a new namespace. It can be changed if users wish to isolate the pods and apply some policy to them. Signed-off-by: 's avatarDarren Shepherd <darren@acorn.io>
parent f4cc1b87
...@@ -101,6 +101,7 @@ type Server struct { ...@@ -101,6 +101,7 @@ type Server struct {
EtcdS3Folder string EtcdS3Folder string
EtcdS3Timeout time.Duration EtcdS3Timeout time.Duration
EtcdS3Insecure bool EtcdS3Insecure bool
ServiceLBNamespace string
} }
var ( var (
...@@ -221,6 +222,12 @@ var ServerFlags = []cli.Flag{ ...@@ -221,6 +222,12 @@ var ServerFlags = []cli.Flag{
Destination: &ServerConfig.EgressSelectorMode, Destination: &ServerConfig.EgressSelectorMode,
Value: "agent", Value: "agent",
}, },
cli.StringFlag{
Name: "servicelb-namespace",
Usage: "(networking) Namespace of the pods for the servicelb component",
Destination: &ServerConfig.ServiceLBNamespace,
Value: "kube-system",
},
ServerToken, ServerToken,
cli.StringFlag{ cli.StringFlag{
Name: "token-file", Name: "token-file",
......
...@@ -115,6 +115,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont ...@@ -115,6 +115,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
serverConfig.ControlConfig.KubeConfigOutput = cfg.KubeConfigOutput serverConfig.ControlConfig.KubeConfigOutput = cfg.KubeConfigOutput
serverConfig.ControlConfig.KubeConfigMode = cfg.KubeConfigMode serverConfig.ControlConfig.KubeConfigMode = cfg.KubeConfigMode
serverConfig.Rootless = cfg.Rootless serverConfig.Rootless = cfg.Rootless
serverConfig.ServiceLBNamespace = cfg.ServiceLBNamespace
serverConfig.ControlConfig.SANs = cfg.TLSSan serverConfig.ControlConfig.SANs = cfg.TLSSan
serverConfig.ControlConfig.BindAddress = cfg.BindAddress serverConfig.ControlConfig.BindAddress = cfg.BindAddress
serverConfig.ControlConfig.SupervisorPort = cfg.SupervisorPort serverConfig.ControlConfig.SupervisorPort = cfg.SupervisorPort
......
...@@ -212,6 +212,7 @@ func coreControllers(ctx context.Context, sc *Context, config *Config) error { ...@@ -212,6 +212,7 @@ func coreControllers(ctx context.Context, sc *Context, config *Config) error {
sc.Core.Core().V1().Pod(), sc.Core.Core().V1().Pod(),
sc.Core.Core().V1().Service(), sc.Core.Core().V1().Service(),
sc.Core.Core().V1().Endpoints(), sc.Core.Core().V1().Endpoints(),
config.ServiceLBNamespace,
!config.DisableServiceLB, !config.DisableServiceLB,
config.Rootless); err != nil { config.Rootless); err != nil {
return err return err
......
...@@ -8,14 +8,15 @@ import ( ...@@ -8,14 +8,15 @@ import (
) )
type Config struct { type Config struct {
DisableAgent bool DisableAgent bool
DisableServiceLB bool DisableServiceLB bool
ControlConfig config.Control ControlConfig config.Control
Rootless bool Rootless bool
SupervisorPort int ServiceLBNamespace string
StartupHooks []cmds.StartupHook SupervisorPort int
LeaderControllers CustomControllers StartupHooks []cmds.StartupHook
Controllers CustomControllers LeaderControllers CustomControllers
Controllers CustomControllers
} }
type CustomControllers []func(ctx context.Context, sc *Context) error type CustomControllers []func(ctx context.Context, sc *Context) error
...@@ -42,13 +42,8 @@ var ( ...@@ -42,13 +42,8 @@ var (
) )
const ( const (
Ready = condition.Cond("Ready") Ready = condition.Cond("Ready")
ControllerName = "svccontroller" ControllerName = "svccontroller"
KlipperNamespace = "klipper-lb-system"
)
var (
trueVal = true
) )
func Register(ctx context.Context, func Register(ctx context.Context,
...@@ -60,19 +55,21 @@ func Register(ctx context.Context, ...@@ -60,19 +55,21 @@ func Register(ctx context.Context,
pods coreclient.PodController, pods coreclient.PodController,
services coreclient.ServiceController, services coreclient.ServiceController,
endpoints coreclient.EndpointsController, endpoints coreclient.EndpointsController,
klipperLBNamespace string,
enabled, rootless bool) error { enabled, rootless bool) error {
h := &handler{ h := &handler{
rootless: rootless, rootless: rootless,
enabled: enabled, enabled: enabled,
nodeCache: nodes.Cache(), klipperLBNamespace: klipperLBNamespace,
podCache: pods.Cache(), nodeCache: nodes.Cache(),
deploymentCache: deployments.Cache(), podCache: pods.Cache(),
processor: apply.WithSetID(ControllerName).WithCacheTypes(daemonSetController), deploymentCache: deployments.Cache(),
serviceCache: services.Cache(), processor: apply.WithSetID(ControllerName).WithCacheTypes(daemonSetController),
services: kubernetes.CoreV1(), serviceCache: services.Cache(),
daemonsets: kubernetes.AppsV1(), services: kubernetes.CoreV1(),
deployments: kubernetes.AppsV1(), daemonsets: kubernetes.AppsV1(),
recorder: util.BuildControllerEventRecorder(kubernetes, ControllerName, meta.NamespaceAll), deployments: kubernetes.AppsV1(),
recorder: util.BuildControllerEventRecorder(kubernetes, ControllerName, meta.NamespaceAll),
} }
services.OnChange(ctx, ControllerName, h.onChangeService) services.OnChange(ctx, ControllerName, h.onChangeService)
...@@ -83,39 +80,41 @@ func Register(ctx context.Context, ...@@ -83,39 +80,41 @@ func Register(ctx context.Context,
pods, pods,
endpoints) endpoints)
return createOrDeleteKlipperNamespace(ctx, enabled, kubernetes) if enabled {
if err := createServiceLBNamespace(ctx, h.klipperLBNamespace, kubernetes); err != nil {
return err
}
}
return nil
} }
type handler struct { type handler struct {
rootless bool rootless bool
enabled bool klipperLBNamespace string
nodeCache coreclient.NodeCache enabled bool
podCache coreclient.PodCache nodeCache coreclient.NodeCache
deploymentCache appclient.DeploymentCache podCache coreclient.PodCache
processor apply.Apply deploymentCache appclient.DeploymentCache
serviceCache coreclient.ServiceCache processor apply.Apply
services coregetter.ServicesGetter serviceCache coreclient.ServiceCache
daemonsets v1getter.DaemonSetsGetter services coregetter.ServicesGetter
deployments v1getter.DeploymentsGetter daemonsets v1getter.DaemonSetsGetter
recorder record.EventRecorder deployments v1getter.DeploymentsGetter
recorder record.EventRecorder
} }
func createOrDeleteKlipperNamespace(ctx context.Context, enabled bool, k8s kubernetes.Interface) error { func createServiceLBNamespace(ctx context.Context, ns string, k8s kubernetes.Interface) error {
_, err := k8s.CoreV1().Namespaces().Get(ctx, KlipperNamespace, meta.GetOptions{}) _, err := k8s.CoreV1().Namespaces().Get(ctx, ns, meta.GetOptions{})
if !apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return err
}
if enabled {
_, err := k8s.CoreV1().Namespaces().Create(ctx, &core.Namespace{ _, err := k8s.CoreV1().Namespaces().Create(ctx, &core.Namespace{
ObjectMeta: meta.ObjectMeta{ ObjectMeta: meta.ObjectMeta{
Name: KlipperNamespace, Name: ns,
}, },
}, meta.CreateOptions{}) }, meta.CreateOptions{})
return err return err
} }
return err
return k8s.CoreV1().Namespaces().Delete(ctx, KlipperNamespace, meta.DeleteOptions{})
} }
func (h *handler) onResourceChange(name, namespace string, obj runtime.Object) ([]relatedresource.Key, error) { func (h *handler) onResourceChange(name, namespace string, obj runtime.Object) ([]relatedresource.Key, error) {
...@@ -194,7 +193,7 @@ func (h *handler) updateService(svc *core.Service) (runtime.Object, error) { ...@@ -194,7 +193,7 @@ func (h *handler) updateService(svc *core.Service) (runtime.Object, error) {
return svc, nil return svc, nil
} }
pods, err := h.podCache.List(KlipperNamespace, labels.SelectorFromSet(map[string]string{ pods, err := h.podCache.List(h.klipperLBNamespace, labels.SelectorFromSet(map[string]string{
svcNameLabel: svc.Name, svcNameLabel: svc.Name,
svcNamespaceLabel: svc.Namespace, svcNamespaceLabel: svc.Namespace,
})) }))
...@@ -388,7 +387,7 @@ func (h *handler) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) { ...@@ -388,7 +387,7 @@ func (h *handler) newDaemonSet(svc *core.Service) (*apps.DaemonSet, error) {
ds := &apps.DaemonSet{ ds := &apps.DaemonSet{
ObjectMeta: meta.ObjectMeta{ ObjectMeta: meta.ObjectMeta{
Name: name, Name: name,
Namespace: KlipperNamespace, Namespace: h.klipperLBNamespace,
Labels: map[string]string{ Labels: map[string]string{
nodeSelectorLabel: "false", nodeSelectorLabel: "false",
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment