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

Move kubeconfig saving/printing to a common package #2121

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions internal/cmd/access/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -80,6 +81,10 @@ func runAccess(cfg *accessConfig) clierror.Error {
fmt.Println(string(message))

}
err = kube.SaveConfig(enrichedKubeconfig, cfg.output)
anoipm marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading