Skip to content

Commit

Permalink
Add --kube-context flag to CLI (#2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
edeNFed authored Dec 18, 2024
1 parent 0a568c2 commit 8d4e7d9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
6 changes: 4 additions & 2 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ Get started with Odigos today to effortlessly improve the observability of your
client := kube.GetCLIClientOrExit(cmd)
ctx = cmdcontext.ContextWithKubeClient(ctx, client)

details := autodetect.GetK8SClusterDetails(ctx, kubeConfig, client)
details := autodetect.GetK8SClusterDetails(ctx, kubeConfig, kubeContext, client)
ctx = cmdcontext.ContextWithClusterDetails(ctx, details)

cmd.SetContext(ctx)
},
}

var (
kubeConfig string
kubeConfig string
kubeContext string
)

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -51,4 +52,5 @@ func Execute() {

func init() {
rootCmd.PersistentFlags().StringVar(&kubeConfig, "kubeconfig", env.GetDefaultKubeConfigPath(), "(optional) absolute path to the kubeconfig file")
rootCmd.PersistentFlags().StringVar(&kubeContext, "kube-context", "", "(optional) name of the kubeconfig context to use")
}
4 changes: 2 additions & 2 deletions cli/pkg/autodetect/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type ClusterDetails struct {
K8SVersion *version.Version
}

func GetK8SClusterDetails(ctx context.Context, kc string, client *kube.Client) *ClusterDetails {
func GetK8SClusterDetails(ctx context.Context, kc string, kContext string, client *kube.Client) *ClusterDetails {
clusterDetails := &ClusterDetails{}
details := k8sutils.GetCurrentClusterDetails(kc)
details := k8sutils.GetCurrentClusterDetails(kc, kContext)
serverVersion, err := client.Discovery().ServerVersion()
if err != nil {
clusterDetails.K8SVersion = nil
Expand Down
4 changes: 2 additions & 2 deletions cli/pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func GetCLIClientOrExit(cmd *cobra.Command) *Client {

func createClient(cmd *cobra.Command) (*Client, error) {
kc := cmd.Flag("kubeconfig").Value.String()

config, err := k8sutils.GetClientConfig(kc)
kContext := cmd.Flag("kube-context").Value.String()
config, err := k8sutils.GetClientConfigWithContext(kc, kContext)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type Client struct {
MetadataClient metadata.Interface
}

func CreateClient(kubeConfig string) (*Client, error) {
config, err := k8sutils.GetClientConfig(kubeConfig)
func CreateClient(kubeConfig string, kContext string) (*Client, error) {
config, err := k8sutils.GetClientConfigWithContext(kubeConfig, kContext)
if err != nil {
return nil, err
}
Expand Down
18 changes: 10 additions & 8 deletions frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ const (
)

type Flags struct {
Version bool
Address string
Port int
LegacyPort int
Debug bool
KubeConfig string
Namespace string
Version bool
Address string
Port int
LegacyPort int
Debug bool
KubeConfig string
KubeContext string
Namespace string
}

//go:embed all:webapp/out/*
Expand All @@ -71,13 +72,14 @@ func parseFlags() Flags {
flag.IntVar(&flags.LegacyPort, "legacy-port", legacyPort, "Port to listen on for legacy UI")
flag.BoolVar(&flags.Debug, "debug", false, "Enable debug mode")
flag.StringVar(&flags.KubeConfig, "kubeconfig", defaultKubeConfig, "Path to kubeconfig file")
flag.StringVar(&flags.KubeContext, "kube-context", "", "Name of the kubeconfig context to use")
flag.StringVar(&flags.Namespace, "namespace", consts.DefaultOdigosNamespace, "Kubernetes namespace where Odigos is installed")
flag.Parse()
return flags
}

func initKubernetesClient(flags *Flags) error {
client, err := kube.CreateClient(flags.KubeConfig)
client, err := kube.CreateClient(flags.KubeConfig, flags.KubeContext)
if err != nil {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}
Expand Down
33 changes: 29 additions & 4 deletions k8sutils/pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package client

import (
"fmt"
"os"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func GetClientConfig(kc string) (*rest.Config, error) {
func GetClientConfigWithContext(kc string, context string) (*rest.Config, error) {
var kubeConfig *rest.Config
var err error

if IsRunningInKubernetes() {
// Running inside a Kubernetes cluster
kubeConfig, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
} else {
kubeConfig, err = clientcmd.BuildConfigFromFlags("", kc)
// Loading kubeconfig from file with optional context override
loadingRules := &clientcmd.ClientConfigLoadingRules{
ExplicitPath: kc, // Path to kubeconfig
}

configOverrides := &clientcmd.ConfigOverrides{}
if context != "" {
configOverrides.CurrentContext = context
}

// Build the kubeconfig
kubeConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules,
configOverrides,
).ClientConfig()

if err != nil {
return nil, err
}
Expand All @@ -36,16 +53,24 @@ type ClusterDetails struct {
ServerEndpoint string
}

func GetCurrentClusterDetails(kc string) ClusterDetails {
func GetCurrentClusterDetails(kc string, kContext string) ClusterDetails {
config, err := clientcmd.LoadFromFile(kc)
if err != nil {
return ClusterDetails{}
}

ctx := config.CurrentContext
var ctx string
if kContext != "" {
ctx = kContext
} else {
ctx = config.CurrentContext
}
cluster := ""
if val, ok := config.Contexts[ctx]; ok {
cluster = val.Cluster
} else if kContext != "" { // If context is provided, but not found in kubeconfig
fmt.Printf("Context %s not found in kubeconfig, bailing\n", kContext)
os.Exit(1)
}

server := ""
Expand Down

0 comments on commit 8d4e7d9

Please sign in to comment.