Skip to content

Commit 34eabde

Browse files
bussyjdmanusa
authored andcommitted
feat(helm): share kubeconfig/context/namespace with Hel
1 parent 91dec08 commit 34eabde

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

pkg/helm/helm.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ type Helm struct {
1616
settings *cli.EnvSettings
1717
}
1818

19-
// NewHelm creates a new Helm instance (optionally for a specific kubeconfig)
20-
func NewHelm() *Helm {
21-
return &Helm{
22-
settings: cli.New(),
19+
// NewHelm creates a new Helm instance using kubeconfig, context, and namespace settings
20+
func NewHelm(kubeconfig, kubeContext, namespace string) *Helm {
21+
settings := cli.New()
22+
if kubeconfig != "" {
23+
settings.KubeConfig = kubeconfig
2324
}
25+
if kubeContext != "" {
26+
settings.KubeContext = kubeContext
27+
}
28+
if namespace != "" {
29+
settings.SetNamespace(namespace)
30+
}
31+
return &Helm{settings: settings}
2432
}
2533

2634
// ReleasesList lists Helm releases in a specific namespace (or all namespaces if namespace is empty)

pkg/helm/helm_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestNewHelm(t *testing.T) {
9-
h := NewHelm()
9+
h := NewHelm("", "", "")
1010
if h == nil {
1111
t.Fatal("expected non-nil Helm instance")
1212
}
@@ -16,7 +16,7 @@ func TestNewHelm(t *testing.T) {
1616
}
1717

1818
func TestHelm_ReleasesList_DefaultNamespace(t *testing.T) {
19-
h := NewHelm()
19+
h := NewHelm("", "", "")
2020
// Use a namespace that is likely to exist, or leave empty for default
2121
releases, err := h.ReleasesList(context.Background(), "")
2222
if err != nil {
@@ -29,7 +29,7 @@ func TestHelm_ReleasesList_DefaultNamespace(t *testing.T) {
2929
}
3030

3131
func TestHelm_ReleasesList_AllNamespaces(t *testing.T) {
32-
h := NewHelm()
32+
h := NewHelm("", "", "")
3333
releases, err := h.ReleasesList(context.Background(), "all")
3434
if err != nil {
3535
t.Skipf("skipping: could not list all releases (likely no cluster/helm configured): %v", err)

pkg/kubernetes/kubernetes.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,27 @@ func marshal(v any) (string, error) {
124124
return string(ret), nil
125125
}
126126

127-
func (k *Kubernetes) configuredNamespace() string {
127+
// KubeconfigPath returns the kubeconfig path used by this Kubernetes client
128+
func (k *Kubernetes) KubeconfigPath() string {
129+
return k.Kubeconfig
130+
}
131+
132+
// CurrentContext returns the current context from the kubeconfig
133+
func (k *Kubernetes) CurrentContext() string {
134+
if k.clientCmdConfig == nil {
135+
return ""
136+
}
137+
if rawConfig, err := k.clientCmdConfig.RawConfig(); err == nil {
138+
return rawConfig.CurrentContext
139+
}
140+
return ""
141+
}
142+
143+
// ConfiguredNamespace returns the namespace configured in the kubeconfig/context
144+
func (k *Kubernetes) ConfiguredNamespace() string {
145+
if k.clientCmdConfig == nil {
146+
return ""
147+
}
128148
if ns, _, nsErr := k.clientCmdConfig.Namespace(); nsErr == nil {
129149
return ns
130150
}
@@ -133,7 +153,7 @@ func (k *Kubernetes) configuredNamespace() string {
133153

134154
func (k *Kubernetes) namespaceOrDefault(namespace string) string {
135155
if namespace == "" {
136-
return k.configuredNamespace()
156+
return k.ConfiguredNamespace()
137157
}
138158
return namespace
139159
}

pkg/kubernetes/resources.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (k *Kubernetes) resourcesList(ctx context.Context, gvk *schema.GroupVersion
7777
// Check if operation is allowed for all namespaces (applicable for namespaced resources)
7878
isNamespaced, _ := k.isNamespaced(gvk)
7979
if isNamespaced && !k.canIUse(ctx, gvr, namespace, "list") && namespace == "" {
80-
namespace = k.configuredNamespace()
80+
namespace = k.ConfiguredNamespace()
8181
}
8282
return k.dynamicClient.Resource(*gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
8383
}

pkg/mcp/mcp.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ func NewSever(configuration Configuration) (*Server, error) {
3131
server.WithToolCapabilities(true),
3232
server.WithLogging(),
3333
),
34-
helm: helm.NewHelm(),
3534
}
3635
if err := s.reloadKubernetesClient(); err != nil {
3736
return nil, err
3837
}
38+
// After Kubernetes client is initialized, set up Helm with the same config
39+
if s.k != nil {
40+
kubeconfig := s.k.KubeconfigPath()
41+
kubeContext := s.k.CurrentContext()
42+
namespace := s.k.ConfiguredNamespace()
43+
s.helm = helm.NewHelm(kubeconfig, kubeContext, namespace)
44+
}
3945
s.k.WatchKubeConfig(s.reloadKubernetesClient)
4046
return s, nil
4147
}

0 commit comments

Comments
 (0)