Skip to content

Commit

Permalink
feat: Allow specifying the kube context to use on cmdline (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <[email protected]>
  • Loading branch information
jannfis authored Mar 1, 2024
1 parent b35c9bc commit eaaac40
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewAgentRunCommand() *cobra.Command {
insecure bool
rootCAPath string
kubeConfig string
kubeContext string
namespace string
agentMode string
creds string
Expand Down Expand Up @@ -84,7 +85,7 @@ func NewAgentRunCommand() *cobra.Command {
cmd.Fatal("No remote specified")
}

kubeConfig, err := cmd.GetKubeConfig(ctx, namespace, kubeConfig)
kubeConfig, err := cmd.GetKubeConfig(ctx, namespace, kubeConfig, kubeContext)
if err != nil {
cmd.Fatal("Could not load Kubernetes config: %v", err)
}
Expand All @@ -107,6 +108,7 @@ func NewAgentRunCommand() *cobra.Command {
command.Flags().BoolVar(&insecure, "insecure-tls", false, "INSECURE: Do not verify remote TLS certificate")
command.Flags().StringVar(&rootCAPath, "root-ca-path", "", "Path to a file containing root CA certificate for verifying remote TLS")
command.Flags().StringVar(&kubeConfig, "kubeconfig", "", "Path to a kubeconfig file to use")
command.Flags().StringVar(&kubeContext, "kubecontext", "", "Override the default kube context")
command.Flags().StringVarP(&namespace, "namespace", "n", "argocd", "Namespace to manage applications in")
command.Flags().StringVar(&agentMode, "agent-mode", "autonomous", "Mode of operation")
command.Flags().StringVar(&creds, "creds", "", "Credentials to use when connecting to server")
Expand Down
4 changes: 2 additions & 2 deletions cmd/cmd/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/jannfis/argocd-agent/internal/kube"
)

func GetKubeConfig(ctx context.Context, namespace string, kubeConfig string) (*kube.KubernetesClient, error) {
func GetKubeConfig(ctx context.Context, namespace string, kubeConfig string, kubecontext string) (*kube.KubernetesClient, error) {
var fullKubeConfigPath string
var kubeClient *kube.KubernetesClient
var err error
Expand All @@ -20,7 +20,7 @@ func GetKubeConfig(ctx context.Context, namespace string, kubeConfig string) (*k
}
}

kubeClient, err = kube.NewKubernetesClientFromConfig(ctx, namespace, fullKubeConfigPath)
kubeClient, err = kube.NewKubernetesClientFromConfig(ctx, namespace, fullKubeConfigPath, kubecontext)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/principal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewPrincipalRunCommand() *cobra.Command {
namespace string
allowedNamespaces []string
kubeConfig string
kubeContext string
tlsCert string
tlsKey string
jwtKey string
Expand All @@ -50,7 +51,7 @@ func NewPrincipalRunCommand() *cobra.Command {
logrus.SetLevel(lvl)
}

kubeConfig, err := cmd.GetKubeConfig(ctx, namespace, kubeConfig)
kubeConfig, err := cmd.GetKubeConfig(ctx, namespace, kubeConfig, kubeContext)
if err != nil {
cmd.Fatal("Could not load Kubernetes config: %v", err)
}
Expand Down Expand Up @@ -141,6 +142,7 @@ func NewPrincipalRunCommand() *cobra.Command {
command.Flags().StringVarP(&namespace, "namespace", "n", "", "The namespace the server will use for configuration. Set only when running out of cluster.")
command.Flags().StringSliceVar(&allowedNamespaces, "allowed-namespaces", []string{}, "List of namespaces the server is allowed to operate in")
command.Flags().StringVar(&kubeConfig, "kubeconfig", "", "Path to a kubeconfig file to use")
command.Flags().StringVar(&kubeContext, "kubecontext", "", "Override the default kube context")
command.Flags().StringVar(&tlsCert, "tls-cert", "", "Use TLS certificate from path")
command.Flags().StringVar(&tlsKey, "tls-key", "", "Use TLS private key from path")
command.Flags().StringVar(&jwtKey, "jwt-key", "", "Use JWT signing key from path")
Expand Down
5 changes: 4 additions & 1 deletion internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ func NewKubernetesClient(ctx context.Context, client kubernetes.Interface, appli
// NewKubernetesClient creates a new Kubernetes client object from given
// configuration file. If configuration file is the empty string, in-cluster
// client will be created.
func NewKubernetesClientFromConfig(ctx context.Context, namespace string, kubeconfig string) (*KubernetesClient, error) {
func NewKubernetesClientFromConfig(ctx context.Context, namespace string, kubeconfig string, kubecontext string) (*KubernetesClient, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
loadingRules.ExplicitPath = kubeconfig
overrides := clientcmd.ConfigOverrides{}
if kubecontext != "" {
overrides.CurrentContext = kubecontext
}
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin)

config, err := clientConfig.ClientConfig()
Expand Down

0 comments on commit eaaac40

Please sign in to comment.