Skip to content

Commit bc481a2

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 f7cec80 commit bc481a2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

clusterctl/clusterdeployer/clusterclient.go

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

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

71+
// NewClusterClientFromFile creates and returns the address of a clusterClient, the kubeconfigFile argument is expected to be the path to a
72+
// valid kubeconfig file.
6873
func NewClusterClientFromFile(kubeconfigFile string) (*clusterClient, error) {
6974
c, err := clientcmd.NewClusterApiClientForDefaultSearchPath(kubeconfigFile)
7075
if err != nil {
@@ -238,10 +243,22 @@ func createTempFile(contents string) (string, error) {
238243
if err != nil {
239244
return "", err
240245
}
241-
defer f.Close()
246+
defer ifErrRemove(&err, f.Name())
247+
if err = f.Close(); err != nil {
248+
return "", err
249+
}
242250
_, err = f.WriteString(contents)
243251
if err != nil {
244252
return "", err
245253
}
246254
return f.Name(), nil
247255
}
256+
257+
func ifErrRemove(pErr *error, path string) {
258+
if *pErr != nil {
259+
err := os.Remove(path)
260+
if err != nil {
261+
glog.Warningf("Error removing file '%v': %v", err)
262+
}
263+
}
264+
}

0 commit comments

Comments
 (0)