Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kube namespace override and default to in cluster namespace #622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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