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

feat: support writing kubeconfig to a stream #1381

Merged
merged 1 commit into from
Feb 15, 2024
Merged
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
22 changes: 16 additions & 6 deletions pkg/client/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
}
Expand Down