mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-09-18 00:37:45 +00:00
kubens: implement namespace switching
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
@@ -6,12 +6,9 @@ import (
|
||||
)
|
||||
|
||||
func (k *Kubeconfig) DeleteContextEntry(deleteName string) error {
|
||||
contexts := valueOf(k.rootNode, "contexts")
|
||||
if contexts == nil {
|
||||
return errors.New("there are no contexts in kubeconfig")
|
||||
}
|
||||
if contexts.Kind != yaml.SequenceNode {
|
||||
return errors.New("'contexts' key is not a sequence")
|
||||
contexts, err := k.contextsNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
i := -1
|
||||
@@ -51,11 +48,9 @@ func (k *Kubeconfig) ModifyCurrentContext(name string) error {
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) ModifyContextName(old, new string) error {
|
||||
contexts := valueOf(k.rootNode, "contexts")
|
||||
if contexts == nil {
|
||||
return errors.New("\"contexts\" entry is nil")
|
||||
} else if contexts.Kind != yaml.SequenceNode {
|
||||
return errors.New("\"contexts\" is not a sequence node")
|
||||
contexts, err := k.contextsNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var changed bool
|
||||
|
@@ -1,9 +1,35 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func (k *Kubeconfig) contextsNode() (*yaml.Node, error) {
|
||||
contexts := valueOf(k.rootNode, "contexts")
|
||||
if contexts == nil {
|
||||
return nil, errors.New("\"contexts\" entry is nil")
|
||||
} else if contexts.Kind != yaml.SequenceNode {
|
||||
return nil, errors.New("\"contexts\" is not a sequence node")
|
||||
}
|
||||
return contexts, nil
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) contextNode(name string) (*yaml.Node, error) {
|
||||
contexts, err := k.contextsNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, contextNode := range contexts.Content {
|
||||
nameNode := valueOf(contextNode, "name")
|
||||
if nameNode.Kind == yaml.ScalarNode && nameNode.Value == name {
|
||||
return contextNode, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Errorf("context with name %q not found", name)
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) ContextNames() []string {
|
||||
contexts := valueOf(k.rootNode, "contexts")
|
||||
if contexts == nil {
|
||||
|
@@ -1,31 +1,11 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
const (
|
||||
defaultNamespace = "default"
|
||||
)
|
||||
|
||||
func (k *Kubeconfig) contextNode(name string) (*yaml.Node, error) {
|
||||
contexts := valueOf(k.rootNode, "contexts")
|
||||
if contexts == nil {
|
||||
return nil, errors.New("\"contexts\" entry is nil")
|
||||
} else if contexts.Kind != yaml.SequenceNode {
|
||||
return nil, errors.New("\"contexts\" is not a sequence node")
|
||||
}
|
||||
|
||||
for _, contextNode := range contexts.Content {
|
||||
nameNode := valueOf(contextNode, "name")
|
||||
if nameNode.Kind == yaml.ScalarNode && nameNode.Value == name {
|
||||
return contextNode, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.Errorf("context with name %q not found", name)
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) NamespaceOfContext(contextName string) (string, error) {
|
||||
ctx, err := k.contextNode(contextName)
|
||||
if err != nil {
|
||||
@@ -37,3 +17,26 @@ func (k *Kubeconfig) NamespaceOfContext(contextName string) (string, error) {
|
||||
}
|
||||
return ns.Value, nil
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) SetNamespace(ctxName string, ns string) error {
|
||||
ctx, err := k.contextNode(ctxName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nsNode := valueOf(ctx, "namespace")
|
||||
if nsNode != nil {
|
||||
nsNode.Value = ns
|
||||
return nil
|
||||
}
|
||||
|
||||
keyNode := &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: "namespace",
|
||||
Tag: "!!str"}
|
||||
valueNode := &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: ns,
|
||||
Tag: "!!str"}
|
||||
ctx.Content = append(ctx.Content, keyNode, valueNode)
|
||||
return nil
|
||||
}
|
||||
|
@@ -3,6 +3,8 @@ package kubeconfig
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/testutil"
|
||||
)
|
||||
|
||||
@@ -44,3 +46,35 @@ func TestKubeconfig_NamespaceOfContext(t *testing.T) {
|
||||
t.Fatalf("c2: expected=%q got=%q", expected, v2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_SetNamespace(t *testing.T) {
|
||||
l := WithMockKubeconfigLoader(testutil.KC().
|
||||
WithCtxs(
|
||||
testutil.Ctx("c1"),
|
||||
testutil.Ctx("c2").Ns("c2n1")).ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(l)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := kc.SetNamespace("c3", "foo"); err == nil {
|
||||
t.Fatalf("expected error for non-existing ctx")
|
||||
}
|
||||
|
||||
if err := kc.SetNamespace("c1", "c1n1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kc.SetNamespace("c2", "c2n2"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kc.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := testutil.KC().WithCtxs(
|
||||
testutil.Ctx("c1").Ns("c1n1"),
|
||||
testutil.Ctx("c2").Ns("c2n2")).ToYAML(t)
|
||||
if diff := cmp.Diff(l.Output(), expected); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user