Skip to content

Commit 9e70b0e

Browse files
barckcodeclaude
andcommitted
Feat: Add kubeconfig context option to debug command
- Add --context flag to debug command for kubeconfig context override - Update K8s client to support context switching - Fix build command in README - Bump version to v0.1.2 Closes #2 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 48896e3 commit 9e70b0e

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cd kubectl-ai
3939
# build the binary for your OS/ARCH
4040
GOOS=$(uname -s | tr '[:upper:]' '[:lower:]') \
4141
GOARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') \
42-
go build -o kubectl-ai ./...
42+
go build
4343
# add it to PATH
4444
sudo mv kubectl-ai /usr/local/bin/
4545
```

cmd/debug.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
var (
2020
kubeconfig string
2121
namespace string
22+
kubeContext string
2223
resources []string
2324
allResources bool
2425
outputFormat string
@@ -53,6 +54,7 @@ Examples:
5354
}
5455

5556
cmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "Kubernetes namespace")
57+
cmd.Flags().StringVar(&kubeContext, "context", "", "Kubeconfig context (overrides current-context)")
5658
cmd.Flags().StringSliceVarP(&resources, "resource", "r", []string{}, "Resources to analyze (e.g., deployment/nginx, pod/nginx-xxx)")
5759
cmd.Flags().BoolVar(&allResources, "all", false, "Analyze all resources in the namespace")
5860
cmd.Flags().StringVarP(&outputFormat, "output", "o", "human", "Output format (human, json, yaml)")
@@ -91,7 +93,7 @@ func runDebug(cmd *cobra.Command, args []string) error {
9193
}
9294

9395
// Initialize K8s client
94-
k8sClient, err := k8s.NewClient(kubeconfig)
96+
k8sClient, err := k8s.NewClient(kubeconfig, kubeContext)
9597
if err != nil {
9698
s.Stop()
9799
return fmt.Errorf("failed to connect to cluster: %w", err)

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/spf13/cobra"
87
"github.com/helmcode/kubectl-ai/cmd"
8+
"github.com/spf13/cobra"
99
)
1010

1111
var (
12-
version = "dev" // Overwritten at build time
12+
version = "v0.1.2" // Overwritten at build time
1313
)
1414

1515
func main() {
@@ -49,4 +49,4 @@ func newVersionCmd() *cobra.Command {
4949
fmt.Printf("kubectl-ai version %s\n", version)
5050
},
5151
}
52-
}
52+
}

pkg/k8s/client.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ type Client struct {
3131
}
3232

3333
// NewClient creates a new Kubernetes client with discovery capabilities
34-
func NewClient(kubeconfig string) (*Client, error) {
34+
// If contextName is not empty, it will be used instead of the current context in kubeconfig.
35+
func NewClient(kubeconfig string, contextName string) (*Client, error) {
3536
var config *rest.Config
3637
var err error
3738

3839
// Try in-cluster config first
3940
config, err = rest.InClusterConfig()
4041
if err != nil {
41-
// Fall back to kubeconfig
42-
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
42+
// Fall back to kubeconfig with optional context override
43+
loadingRules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}
44+
overrides := &clientcmd.ConfigOverrides{}
45+
if contextName != "" {
46+
overrides.CurrentContext = contextName
47+
}
48+
cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)
49+
config, err = cfg.ClientConfig()
4350
if err != nil {
4451
return nil, fmt.Errorf("failed to create config: %w", err)
4552
}

0 commit comments

Comments
 (0)