Skip to content

Commit

Permalink
Add kube namespace override and default to in cluster namespace (#622)
Browse files Browse the repository at this point in the history
See tinkerbell/hegel#106 for explanation.
  • Loading branch information
mergify[bot] authored May 17, 2022
2 parents 7a8ca47 + 9b173f8 commit 7b60369
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 8 additions & 1 deletion cmd/tink-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type DaemonConfig struct {

KubeconfigPath string
KubeAPI string
KubeNamespace string
}

const (
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 14 additions & 4 deletions server/kubernetes_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 7b60369

Please sign in to comment.