Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

348: initial impl for unsetting kubens #349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/kubens/flags.go
Original file line number Diff line number Diff line change
@@ -51,6 +51,9 @@ func parseArgs(argv []string) Op {
if v == "--current" || v == "-c" {
return CurrentOp{}
}
if v == "--unset" || v == "-u" {
return UnsetOp{}
}
if strings.HasPrefix(v, "-") && v != "-" {
return UnsupportedOp{Err: fmt.Errorf("unsupported option '%s'", v)}
}
48 changes: 48 additions & 0 deletions cmd/kubens/unset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"io"

"github.com/ahmetb/kubectx/internal/kubeconfig"
"github.com/ahmetb/kubectx/internal/printer"
"github.com/pkg/errors"
)

// UnsetOp indicates intention to remove current namespace preference.
type UnsetOp struct{}

func (u UnsetOp) Run(_, stderr io.Writer) error {
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
defer kc.Close()
if err := kc.Parse(); err != nil {
return errors.Wrap(err, "kubeconfig error")
}
ctx := kc.GetCurrentContext()
if ctx == "" {
return errors.New("current-context is not set")
}
err := kc.UnsetNamespace(ctx)
if err != nil {
return errors.Wrap(err, "could not unset namespace for the current-context")
}
if err := kc.Save(); err != nil {
return errors.Wrap(err, "failed to save kubeconfig file")
}

err = printer.Success(stderr, "Active namespace is unset")
return err
}
34 changes: 33 additions & 1 deletion internal/kubeconfig/namespace.go
Original file line number Diff line number Diff line change
@@ -14,7 +14,11 @@

package kubeconfig

import "gopkg.in/yaml.v3"
import (
"errors"

"gopkg.in/yaml.v3"
)

const (
defaultNamespace = "default"
@@ -75,3 +79,31 @@ func (k *Kubeconfig) SetNamespace(ctxName string, ns string) error {
}
return nil
}

func (k *Kubeconfig) UnsetNamespace(ctxName string) error {
ctxNode, err := k.contextNode(ctxName)
if err != nil {
return err
}

ctxBodyNode := valueOf(ctxNode, "context")
if ctxBodyNode == nil {
return errors.New("invalid context key")
}

newKVParis := []*yaml.Node{}
if ctxBodyNode.Kind == yaml.MappingNode {
for i := 0; i < len(ctxBodyNode.Content); i++ {
ch := ctxBodyNode.Content[i]
if ch.Kind == yaml.ScalarNode && ch.Value == "namespace" {
// Skip the value belonging to `namespace` key
i++
continue
}
newKVParis = append(newKVParis, ch)
}
}

ctxBodyNode.Content = newKVParis
return nil
}