From 9b173f863a2187c763c0597d473f9f4bc5b90c5a Mon Sep 17 00:00:00 2001 From: Chris Doherty Date: Thu, 12 May 2022 21:03:48 -0400 Subject: [PATCH] Add kube namespace override and default to incluster namespace. Signed-off-by: Chris Doherty --- cmd/tink-server/main.go | 9 ++++++++- server/kubernetes_api.go | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/tink-server/main.go b/cmd/tink-server/main.go index 5316ff71f..b631cc56c 100644 --- a/cmd/tink-server/main.go +++ b/cmd/tink-server/main.go @@ -44,6 +44,7 @@ type DaemonConfig struct { KubeconfigPath string KubeAPI string + KubeNamespace string } const ( @@ -69,6 +70,7 @@ func (c *DaemonConfig) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&c.Backend, "backend", backendPostgres, fmt.Sprintf("The backend datastore to use. Must be one of %s", strings.Join(backends(), ", "))) fs.StringVar(&c.KubeconfigPath, "kubeconfig", "", "The path to the Kubeconfig. Only takes effect if `--backend=kubernetes`") fs.StringVar(&c.KubeAPI, "kubernetes", "", "The Kubernetes API URL, used for in-cluster client construction. Only takes effect if `--backend=kubernetes`") + fs.StringVar(&c.KubeNamespace, "kube-namespace", "", "The Kubernetes namespace to target") } func (c *DaemonConfig) PopulateFromLegacyEnvVar() { @@ -154,7 +156,12 @@ func NewRootCommand(config *DaemonConfig, logger log.Logger) *cobra.Command { switch config.Backend { case backendKubernetes: var err error - registrar, err = server.NewKubeBackedServer(logger, config.KubeconfigPath, config.KubeAPI) + registrar, err = server.NewKubeBackedServer( + logger, + config.KubeconfigPath, + config.KubeAPI, + config.KubeNamespace, + ) if err != nil { return err } diff --git a/server/kubernetes_api.go b/server/kubernetes_api.go index f1d2c11ef..52c97dd67 100644 --- a/server/kubernetes_api.go +++ b/server/kubernetes_api.go @@ -19,17 +19,25 @@ import ( // +kubebuilder:rbac:groups=tinkerbell.org,resources=workflows;workflows/status,verbs=get;list;watch;update;patch // NewKubeBackedServer returns a server that implements the Workflow server interface for a given kubeconfig. -func NewKubeBackedServer(logger log.Logger, kubeconfig, apiserver string) (*KubernetesBackedServer, error) { +func NewKubeBackedServer(logger log.Logger, kubeconfig, apiserver, namespace string) (*KubernetesBackedServer, error) { ccfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}, - &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: apiserver}}) + &clientcmd.ConfigOverrides{ + ClusterInfo: clientcmdapi.Cluster{ + Server: apiserver, + }, + Context: clientcmdapi.Context{ + Namespace: namespace, + }, + }, + ) cfg, err := ccfg.ClientConfig() if err != nil { return nil, err } - namespace, _, err := ccfg.Namespace() + namespace, _, err = ccfg.Namespace() if err != nil { return nil, err } @@ -43,7 +51,9 @@ func NewKubeBackedServer(logger log.Logger, kubeconfig, apiserver string) (*Kube // NewKubeBackedServerFromREST returns a server that implements the Workflow // server interface with the given Kubernetes rest client and namespace. func NewKubeBackedServerFromREST(logger log.Logger, config *rest.Config, namespace string) *KubernetesBackedServer { - manager := controllers.NewManagerOrDie(config, controllers.GetServerOptions()) + options := controllers.GetServerOptions() + options.Namespace = namespace + manager := controllers.NewManagerOrDie(config, options) go func() { err := manager.Start(context.Background()) if err != nil {