Skip to content

Commit 856ca7e

Browse files
committed
Fix an issue where clusterClient's temporary kubeconfig files could persist after the application terminates. Add comments for the clusterclient New(...) methods.
1 parent 206c730 commit 856ca7e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

clusterctl/clusterdeployer/clusterclient.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ type clusterClient struct {
4949
closeFn func() error
5050
}
5151

52+
// NewClusterClient creates and returns the address of a clusterClient, the kubeconfig argument is expected to be the string represenattion
53+
// of a valid kubeconfig.
5254
func NewClusterClient(kubeconfig string) (*clusterClient, error) {
5355
f, err := createTempFile(kubeconfig)
5456
if err != nil {
5557
return nil, err
5658
}
59+
defer ifErrRemove(&err, f)
5760
c, err := NewClusterClientFromFile(f)
5861
if err != nil {
5962
return nil, err
@@ -66,6 +69,8 @@ func (c *clusterClient) removeKubeconfigFile() error {
6669
return os.Remove(c.kubeconfigFile)
6770
}
6871

72+
// NewClusterClientFromFile creates and returns the address of a clusterClient, the kubeconfigFile argument is expected to be the path to a
73+
// valid kubeconfig file.
6974
func NewClusterClientFromFile(kubeconfigFile string) (*clusterClient, error) {
7075
c, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfigFile)
7176
if err != nil {
@@ -251,10 +256,21 @@ func createTempFile(contents string) (string, error) {
251256
if err != nil {
252257
return "", err
253258
}
254-
defer f.Close()
259+
defer ifErrRemove(&err, f.Name())
260+
if err = f.Close(); err != nil {
261+
return "", err
262+
}
255263
_, err = f.WriteString(contents)
256264
if err != nil {
257265
return "", err
258266
}
259267
return f.Name(), nil
260268
}
269+
270+
func ifErrRemove(pErr *error, path string) {
271+
if *pErr != nil {
272+
if err := os.Remove(path); err != nil {
273+
glog.Warningf("Error removing file '%v': %v", err)
274+
}
275+
}
276+
}

0 commit comments

Comments
 (0)