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

Migrate to kyaml #412

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 21 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ require (
github.com/google/go-cmp v0.5.9
github.com/mattn/go-isatty v0.0.14
github.com/pkg/errors v0.9.1
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/apimachinery v0.28.5
k8s.io/client-go v0.28.5
sigs.k8s.io/kustomize/kyaml v0.16.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.9 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -34,21 +35,22 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.27.3 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.5 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
424 changes: 46 additions & 378 deletions go.sum

Large diffs are not rendered by default.

59 changes: 17 additions & 42 deletions internal/kubeconfig/contextmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,44 @@
package kubeconfig

import (
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"errors"

"sigs.k8s.io/kustomize/kyaml/yaml"
)

func (k *Kubeconfig) DeleteContextEntry(deleteName string) error {
contexts, err := k.contextsNode()
if err != nil {
return err
}

i := -1
for j, ctxNode := range contexts.Content {
nameNode := valueOf(ctxNode, "name")
if nameNode != nil && nameNode.Kind == yaml.ScalarNode && nameNode.Value == deleteName {
i = j
break
}
}
if i >= 0 {
copy(contexts.Content[i:], contexts.Content[i+1:])
contexts.Content[len(contexts.Content)-1] = nil
contexts.Content = contexts.Content[:len(contexts.Content)-1]
if err := contexts.PipeE(
yaml.ElementSetter{
Keys: []string{"name"},
Values: []string{deleteName},
},
); err != nil {
return err
}
return nil
}

func (k *Kubeconfig) ModifyCurrentContext(name string) error {
currentCtxNode := valueOf(k.rootNode, "current-context")
if currentCtxNode != nil {
currentCtxNode.Value = name
return nil
if err := k.config.PipeE(yaml.SetField("current-context", yaml.NewScalarRNode(name))); err != nil {
return err
}

// if current-context field doesn't exist, create new field
keyNode := &yaml.Node{
Kind: yaml.ScalarNode,
Value: "current-context",
Tag: "!!str"}
valueNode := &yaml.Node{
Kind: yaml.ScalarNode,
Value: name,
Tag: "!!str"}
k.rootNode.Content = append(k.rootNode.Content, keyNode, valueNode)
return nil
}

func (k *Kubeconfig) ModifyContextName(old, new string) error {
contexts, err := k.contextsNode()
context, err := k.config.Pipe(yaml.Lookup("contexts", "[name="+old+"]"))
if err != nil {
return err
}

var changed bool
for _, contextNode := range contexts.Content {
nameNode := valueOf(contextNode, "name")
if nameNode.Kind == yaml.ScalarNode && nameNode.Value == old {
nameNode.Value = new
changed = true
break
}
if context == nil {
return errors.New("\"contexts\" entry is nil")
}
if !changed {
return errors.New("no changes were made")
if err := context.PipeE(yaml.SetField("name", yaml.NewScalarRNode(new))); err != nil {
return err
}
return nil
}
56 changes: 20 additions & 36 deletions internal/kubeconfig/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,47 @@ package kubeconfig

import (
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

func (k *Kubeconfig) contextsNode() (*yaml.Node, error) {
contexts := valueOf(k.rootNode, "contexts")
func (k *Kubeconfig) contextsNode() (*yaml.RNode, error) {
contexts, err := k.config.Pipe(yaml.Get("contexts"))
if err != nil {
return nil, err
}
if contexts == nil {
return nil, errors.New("\"contexts\" entry is nil")
} else if contexts.Kind != yaml.SequenceNode {
} else if contexts.YNode().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) {
func (k *Kubeconfig) contextNode(name string) (*yaml.RNode, 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
}
context, err := contexts.Pipe(yaml.Lookup("[name=" + name + "]"))
if err != nil {
return nil, err
}
if context == nil {
return nil, errors.Errorf("context with name \"%s\" not found", name)
}
return nil, errors.Errorf("context with name \"%s\" not found", name)
return context, nil
}

func (k *Kubeconfig) ContextNames() []string {
contexts := valueOf(k.rootNode, "contexts")
if contexts == nil {
contexts, err := k.config.Pipe(yaml.Get("contexts"))
if err != nil {
return nil
}
if contexts.Kind != yaml.SequenceNode {
names, err := contexts.ElementValues("name")
if err != nil {
return nil
}

var ctxNames []string
for _, ctx := range contexts.Content {
nameVal := valueOf(ctx, "name")
if nameVal != nil {
ctxNames = append(ctxNames, nameVal.Value)
}
}
return ctxNames
return names
}

func (k *Kubeconfig) ContextExists(name string) bool {
Expand All @@ -72,15 +68,3 @@ func (k *Kubeconfig) ContextExists(name string) bool {
}
return false
}

func valueOf(mapNode *yaml.Node, key string) *yaml.Node {
if mapNode.Kind != yaml.MappingNode {
return nil
}
for i, ch := range mapNode.Content {
if i%2 == 0 && ch.Kind == yaml.ScalarNode && ch.Value == key {
return mapNode.Content[i+1]
}
}
return nil
}
14 changes: 8 additions & 6 deletions internal/kubeconfig/currentcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@

package kubeconfig

import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// GetCurrentContext returns "current-context" value in given
// kubeconfig object Node, or returns "" if not found.
func (k *Kubeconfig) GetCurrentContext() string {
v := valueOf(k.rootNode, "current-context")
if v == nil {
v, err := k.config.Pipe(yaml.Get("current-context"))
if err != nil {
return ""
}
return v.Value
return yaml.GetValue(v)
}

func (k *Kubeconfig) UnsetCurrentContext() error {
curCtxValNode := valueOf(k.rootNode, "current-context")
curCtxValNode.Value = ""
return nil
return k.config.PipeE(yaml.SetField("current-context", yaml.NewStringRNode("")))
}
18 changes: 11 additions & 7 deletions internal/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"io"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

type ReadWriteResetCloser interface {
Expand All @@ -35,8 +35,8 @@ type Loader interface {
type Kubeconfig struct {
loader Loader

f ReadWriteResetCloser
rootNode *yaml.Node
f ReadWriteResetCloser
config *yaml.RNode
}

func (k *Kubeconfig) WithLoader(l Loader) *Kubeconfig {
Expand Down Expand Up @@ -65,15 +65,19 @@ func (k *Kubeconfig) Parse() error {
if err := yaml.NewDecoder(f).Decode(&v); err != nil {
return errors.Wrap(err, "failed to decode")
}
k.rootNode = v.Content[0]
if k.rootNode.Kind != yaml.MappingNode {
k.config = yaml.NewRNode(&v)
if k.config.YNode().Kind != yaml.MappingNode {
return errors.New("kubeconfig file is not a map document")
}
return nil
}

func (k *Kubeconfig) Bytes() ([]byte, error) {
return yaml.Marshal(k.rootNode)
str, err := k.config.String()
if err != nil {
return nil, err
}
return []byte(str), nil
}

func (k *Kubeconfig) Save() error {
Expand All @@ -82,5 +86,5 @@ func (k *Kubeconfig) Save() error {
}
enc := yaml.NewEncoder(k.f)
enc.SetIndent(0)
return enc.Encode(k.rootNode)
return enc.Encode(k.config.YNode())
}
54 changes: 13 additions & 41 deletions internal/kubeconfig/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package kubeconfig

import "gopkg.in/yaml.v3"
import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)

const (
defaultNamespace = "default"
Expand All @@ -25,53 +27,23 @@ func (k *Kubeconfig) NamespaceOfContext(contextName string) (string, error) {
if err != nil {
return "", err
}
ctxBody := valueOf(ctx, "context")
if ctxBody == nil {
return defaultNamespace, nil
}
ns := valueOf(ctxBody, "namespace")
if ns == nil || ns.Value == "" {
return defaultNamespace, nil
namespace, err := ctx.Pipe(yaml.Lookup("context", "namespace"))
if namespace == nil || err != nil {
return defaultNamespace, err
}
return ns.Value, nil
return yaml.GetValue(namespace), nil
}

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

var ctxBodyNodeWasEmpty bool // actual namespace value is in contexts[index].context.namespace, but .context might not exist
ctxBodyNode := valueOf(ctxNode, "context")
if ctxBodyNode == nil {
ctxBodyNodeWasEmpty = true
ctxBodyNode = &yaml.Node{
Kind: yaml.MappingNode,
}
}

nsNode := valueOf(ctxBodyNode, "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"}
ctxBodyNode.Content = append(ctxBodyNode.Content, keyNode, valueNode)
if ctxBodyNodeWasEmpty {
ctxNode.Content = append(ctxNode.Content, &yaml.Node{
Kind: yaml.ScalarNode,
Value: "context",
Tag: "!!str",
}, ctxBodyNode)
if err := ctx.PipeE(
yaml.LookupCreate(yaml.MappingNode, "context"),
yaml.SetField("namespace", yaml.NewStringRNode(ns)),
); err != nil {
return err
}
return nil
}
Loading