Skip to content

Commit

Permalink
Move kubeconfig saving/printing to a common package (#2121)
Browse files Browse the repository at this point in the history
  • Loading branch information
halamix2 authored May 29, 2024
1 parent 5cc9adf commit 87e1e51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
20 changes: 4 additions & 16 deletions internal/cmd/access/access.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package access

import (
"fmt"

"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/kyma-project/cli.v3/internal/kube"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)

Expand Down Expand Up @@ -66,19 +64,9 @@ func runAccess(cfg *accessConfig) clierror.Error {
return clierror.Wrap(err, clierror.New("failed to prepare kubeconfig"))
}

if cfg.output != "" {
err = clientcmd.WriteToFile(*enrichedKubeconfig, cfg.output)
println("Kubeconfig saved to: " + cfg.output)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to save kubeconfig to file"))
}
} else {
message, err := clientcmd.Write(*enrichedKubeconfig)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to print kubeconfig"))
}
fmt.Println(string(message))

err = kube.SaveConfig(enrichedKubeconfig, cfg.output)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to save kubeconfig"))
}

return nil
Expand Down
17 changes: 4 additions & 13 deletions internal/cmd/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/kyma-project/cli.v3/internal/kube"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)

Expand Down Expand Up @@ -98,18 +98,9 @@ func runOIDC(cfg *oidcConfig) clierror.Error {
return clierror.Wrap(err, clierror.New("failed to create kubeconfig"))
}

if cfg.output != "" {
err = clientcmd.WriteToFile(*enrichedKubeconfig, cfg.output)
println("Kubeconfig saved to: " + cfg.output)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to save kubeconfig to file"))
}
} else {
message, err := clientcmd.Write(*enrichedKubeconfig)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to print kubeconfig"))
}
fmt.Println(string(message))
err = kube.SaveConfig(enrichedKubeconfig, cfg.output)
if err != nil {
return clierror.Wrap(err, clierror.New("failed to save kubeconfig"))
}

return nil
Expand Down
20 changes: 20 additions & 0 deletions internal/kube/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kube

import (
"fmt"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand Down Expand Up @@ -50,3 +52,21 @@ func setKubernetesDefaults(config *rest.Config) error {

return rest.SetKubernetesDefaults(config)
}

// SaveConfig saves the kubeconfig to a file or prints it to the console.
func SaveConfig(kubeconfig *api.Config, output string) error {
if output != "" {
err := clientcmd.WriteToFile(*kubeconfig, output)
if err != nil {
return err
}
println("Kubeconfig saved to: " + output)
return nil
}
message, err := clientcmd.Write(*kubeconfig)
if err != nil {
return err
}
fmt.Println(string(message))
return nil
}

0 comments on commit 87e1e51

Please sign in to comment.