From d7d53f78224444a76fdd96f56363a38f11ebbd23 Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 8 Dec 2023 06:47:43 +0000 Subject: [PATCH] feat: support writting kubeconfig to a stream --- .gitpod.yml | 11 +++++++++++ pkg/client/kubeconfig.go | 22 ++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000000..c9e5283ea3 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,11 @@ +# This configuration file was automatically generated by Gitpod. +# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml) +# and commit this file to your remote git repository to share the goodness with others. + +# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart + +tasks: + - init: go get && go build ./... && go test ./... && make + command: go run . + + diff --git a/pkg/client/kubeconfig.go b/pkg/client/kubeconfig.go index bad49e3bc5..31ea94bf79 100644 --- a/pkg/client/kubeconfig.go +++ b/pkg/client/kubeconfig.go @@ -207,18 +207,28 @@ func KubeconfigWriteToPath(ctx context.Context, kubeconfig *clientcmdapi.Config, defer output.Close() } + err = KubeconfigWriteToStream(ctx, kubeconfig, output) + if err != nil { + return fmt.Errorf("failed to write file '%s': %w", output.Name(), err) + } + + l.Log().Debugf("Wrote kubeconfig to '%s'", output.Name()) + + return nil +} + +// KubeconfigWriteToStream takes a kubeconfig and writes it to stream +func KubeconfigWriteToStream(ctx context.Context, kubeconfig *clientcmdapi.Config, writer io.Writer) error { kubeconfigBytes, err := clientcmd.Write(*kubeconfig) if err != nil { return fmt.Errorf("failed to write kubeconfig: %w", err) } - _, err = output.Write(kubeconfigBytes) + _, err = writer.Write(kubeconfigBytes) if err != nil { - return fmt.Errorf("failed to write file '%s': %w", output.Name(), err) + return fmt.Errorf("failed to write stream '%s'", err) } - l.Log().Debugf("Wrote kubeconfig to '%s'", output.Name()) - return nil } @@ -230,7 +240,7 @@ func KubeconfigMerge(ctx context.Context, newKubeConfig *clientcmdapi.Config, ex for k, v := range newKubeConfig.Clusters { if _, ok := existingKubeConfig.Clusters[k]; ok { if !overwriteConflicting { - return fmt.Errorf("Cluster '%s' already exists in target KubeConfig", k) + return fmt.Errorf("cluster '%s' already exists in target KubeConfig", k) } } existingKubeConfig.Clusters[k] = v @@ -247,7 +257,7 @@ func KubeconfigMerge(ctx context.Context, newKubeConfig *clientcmdapi.Config, ex for k, v := range newKubeConfig.Contexts { if _, ok := existingKubeConfig.Contexts[k]; ok && !overwriteConflicting { - return fmt.Errorf("Context '%s' already exists in target KubeConfig", k) + return fmt.Errorf("context '%s' already exists in target KubeConfig", k) } existingKubeConfig.Contexts[k] = v }