Skip to content

Fix an issue where clusterClient's temporary kubeconfig files could persist after the application terminates. Add comments for the clusterclient New(...) methods. #390

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

Merged
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
18 changes: 17 additions & 1 deletion clusterctl/clusterdeployer/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ type clusterClient struct {
closeFn func() error
}

// NewClusterClient creates and returns the address of a clusterClient, the kubeconfig argument is expected to be the string represenattion
// of a valid kubeconfig.
func NewClusterClient(kubeconfig string) (*clusterClient, error) {
f, err := createTempFile(kubeconfig)
if err != nil {
return nil, err
}
defer ifErrRemove(&err, f)
c, err := NewClusterClientFromFile(f)
if err != nil {
return nil, err
Expand All @@ -66,6 +69,8 @@ func (c *clusterClient) removeKubeconfigFile() error {
return os.Remove(c.kubeconfigFile)
}

// NewClusterClientFromFile creates and returns the address of a clusterClient, the kubeconfigFile argument is expected to be the path to a
// valid kubeconfig file.
func NewClusterClientFromFile(kubeconfigFile string) (*clusterClient, error) {
c, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfigFile)
if err != nil {
Expand Down Expand Up @@ -251,10 +256,21 @@ func createTempFile(contents string) (string, error) {
if err != nil {
return "", err
}
defer f.Close()
defer ifErrRemove(&err, f.Name())
if err = f.Close(); err != nil {
return "", err
}
_, err = f.WriteString(contents)
if err != nil {
return "", err
}
return f.Name(), nil
}

func ifErrRemove(pErr *error, path string) {
if *pErr != nil {
if err := os.Remove(path); err != nil {
glog.Warningf("Error removing file '%v': %v", err)
}
}
}