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

Respect namespace of current KubeConfig context #40

Merged
merged 1 commit into from
Mar 31, 2023
Merged
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
19 changes: 15 additions & 4 deletions cmd/timoni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
package main

import (
"github.com/fluxcd/pkg/oci"
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"os"
"time"

"github.com/fluxcd/pkg/oci"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
_ "k8s.io/client-go/plugin/pkg/client/auth"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
)

var VERSION = "0.0.0-dev.0"
Expand All @@ -48,16 +49,26 @@ var (

var kubeconfigArgs = genericclioptions.NewConfigFlags(false)

// namespaceOrDefault returns the namespace from the current context or "default"
// if no namespace is set.
func namespaceOrDefault() string {
if ns, _, err := kubeconfigArgs.ToRawKubeConfigLoader().Namespace(); err == nil {
return ns
}
return "default"
}

func init() {
rootCmd.PersistentFlags().DurationVar(&rootArgs.timeout, "timeout", time.Minute,
"The length of time to wait before giving up on the current operation.")

// Nil the following fields to ensure they are not added by AddFlags.
kubeconfigArgs.Timeout = nil
kubeconfigArgs.Namespace = nil
kubeconfigArgs.AddFlags(rootCmd.PersistentFlags())

defaultNamespace := "default"
kubeconfigArgs.Namespace = &defaultNamespace
namespace := namespaceOrDefault()
kubeconfigArgs.Namespace = &namespace
rootCmd.PersistentFlags().StringVarP(kubeconfigArgs.Namespace, "namespace", "n", *kubeconfigArgs.Namespace, "The instance namespace.")

rootCmd.DisableAutoGenTag = true
Expand Down