Skip to content

Commit

Permalink
fix: wrong fh flag breaking the write operation (#25)
Browse files Browse the repository at this point in the history
* fix: wrong fh flag

* bump version
  • Loading branch information
MqllR committed Jul 1, 2022
1 parent 5027a40 commit d0644c9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/helpers/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewKubeConfig() (*k8s.KubeConfig, error) {
}

func SaveKubeConfig(kubeconfig *k8s.KubeConfig) error {
fh, err := os.OpenFile(config.GetKubeConfig(), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
fh, err := os.OpenFile(config.GetKubeConfig(), os.O_RDWR, 0600)
if err != nil {
return fmt.Errorf("Cannot open the kubeconfig: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/use_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func useContext(opts *useContextOptions) {
contexts := kubeconfig.GetContextNames()

var selectedContext string
var err error

if opts.context != "" {
if !kubeconfig.IsContextExist(opts.context) {
Expand All @@ -47,14 +48,13 @@ func useContext(opts *useContextOptions) {
sort.Strings(contexts)

p := prompt.NewPrompt("Select the context", contexts)
var err error
selectedContext, err = p.PromptSelect()
if err != nil {
klog.Fatalf("Cannot get the answer from the prompt: %s", err)
}
}

err := kubeconfig.SetCurrentContext(selectedContext)
err = kubeconfig.SetCurrentContext(selectedContext)
if err != nil {
klog.Fatalf("Cannot set the current context %s: %s", selectedContext, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
version = "1.2.0"
version = "1.2.1"
)

type versionOptions struct {
Expand Down
8 changes: 1 addition & 7 deletions pkg/k8s/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ func NewKubeConfigFromReader(r io.Reader) (*KubeConfig, error) {

// Save writes the kubeconfig in the given file
func (kubeConfig *KubeConfig) Save(w io.Writer) error {
config, err := kubeConfig.marshal()
if err != nil {
return nil
}

_, err = w.Write(config)
return err
return yaml.NewEncoder(w).Encode(kubeConfig)
}

// WriteTempFile writes the kubeconfig in a temporary file
Expand Down
65 changes: 57 additions & 8 deletions pkg/k8s/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
testingConfig = `
testingConfig1 = `
apiVersion: v1
clusters:
- cluster:
Expand All @@ -38,7 +38,34 @@ users:
command: aws-iam-authenticator
`

testingBigConfig = `
testingConfig2 = `
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: FAKEVALUE2
server: https://fakeurl2.com
name: fakecluster2
contexts:
- context:
cluster: fakecluster2
namespace: fakens2
user: fakeuser2
name: fakecontext2
kind: Config
preferences: {}
users:
- name: fakeuser2
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
args:
- token
- -i
- fakecluster2
command: aws-iam-authenticator
`

testingMergedConfig = `
apiVersion: v1
clusters:
- cluster:
Expand Down Expand Up @@ -134,7 +161,7 @@ func TestNewKubeConfig(t *testing.T) {
}

func TestNewKubeConfigFromReader(t *testing.T) {
kubeconfig, err := loadKubeConfig(testingConfig)
kubeconfig, err := loadKubeConfig(testingConfig1)

if err != nil {
t.Errorf("Unexpeted err: %s", err)
Expand Down Expand Up @@ -164,7 +191,7 @@ var (
)

func TestGetContextByContextName(t *testing.T) {
kubeconfig, err := loadKubeConfig(testingConfig)
kubeconfig, err := loadKubeConfig(testingConfig1)
if err != nil {
t.Error(err)
}
Expand All @@ -186,7 +213,7 @@ func TestGetContextByContextName(t *testing.T) {
}

func TestGetUserByContextName(t *testing.T) {
kubeconfig, err := loadKubeConfig(testingConfig)
kubeconfig, err := loadKubeConfig(testingConfig1)
if err != nil {
t.Error(err)
}
Expand All @@ -208,7 +235,7 @@ func TestGetUserByContextName(t *testing.T) {
}

func TestGetClusterByContextName(t *testing.T) {
kubeconfig, err := loadKubeConfig(testingConfig)
kubeconfig, err := loadKubeConfig(testingConfig1)
if err != nil {
t.Error(err)
}
Expand All @@ -230,7 +257,7 @@ func TestGetClusterByContextName(t *testing.T) {
}

func TestGetKubeConfigByContextName(t *testing.T) {
kubeconfig, err := loadKubeConfig(testingBigConfig)
kubeconfig, err := loadKubeConfig(testingMergedConfig)
if err != nil {
t.Error(err)
}
Expand All @@ -252,7 +279,7 @@ func TestGetKubeConfigByContextName(t *testing.T) {
}

func TestSaveFile(t *testing.T) {
kubeconfig, err := loadKubeConfig(testingConfig)
kubeconfig, err := loadKubeConfig(testingConfig1)
if err != nil {
t.Error(err)
}
Expand All @@ -265,6 +292,28 @@ func TestSaveFile(t *testing.T) {
}
}

func TestAppend(t *testing.T) {
kubeconfig1, err := loadKubeConfig(testingConfig1)
if err != nil {
t.Error(err)
}

kubeconfig2, err := loadKubeConfig(testingConfig2)
if err != nil {
t.Error(err)
}

kubeconfigMerged, err := loadKubeConfig(testingMergedConfig)
if err != nil {
t.Error(err)
}

kubeconfig2.Append(kubeconfig1)
if !reflect.DeepEqual(kubeconfig2, kubeconfigMerged) {
t.Error("kubeconfig1 not equal the merged kubeconfig")
}
}

func loadKubeConfig(kubeConfig string) (*k8s.KubeConfig, error) {
r := strings.NewReader(kubeConfig)
kubeconfig, err := k8s.NewKubeConfigFromReader(r)
Expand Down

0 comments on commit d0644c9

Please sign in to comment.