Skip to content

Commit ba80df7

Browse files
committed
adding kubeconfig to kubectl calls so it works even without $HOME set
1 parent cc7842c commit ba80df7

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

executor/executable/kubectltask.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"os"
3535
"os/exec"
3636
"os/user"
37+
"path/filepath"
3738
"strconv"
3839
"strings"
3940
"sync/atomic"
@@ -58,6 +59,32 @@ const (
5859
// TRANSITION_TIMEOUT = 10 * time.Second // inside controllable task
5960
)
6061

62+
var kubeconfigDir string
63+
64+
func init() {
65+
if kc := os.Getenv("KUBECONFIG"); kc != "" {
66+
kubeconfigDir = kc
67+
return
68+
}
69+
if u, err := user.Current(); err == nil {
70+
tempPath := filepath.Join(u.HomeDir, ".kube", "config")
71+
if _, err = os.Stat(tempPath); err == nil {
72+
kubeconfigDir = tempPath
73+
}
74+
}
75+
}
76+
77+
func kubectl(ctx context.Context, arg ...string) *exec.Cmd {
78+
if kubeconfigDir == "" {
79+
log.Warn(`kubectl config was not set, thus kubectl might not be able to find a cluster.
80+
Either KUBECONFIG env var was not found, or current user was not determined, or home/.kube/config does not exist.
81+
Using kubectl builtin defaults`)
82+
return exec.CommandContext(ctx, KUBECTL, arg...)
83+
} else {
84+
return exec.CommandContext(ctx, KUBECTL, append([]string{"--kubeconfig", kubeconfigDir}, arg...)...)
85+
}
86+
}
87+
6188
type KubectlTask struct {
6289
taskBase
6390
rpc *executorcmd.RpcClient
@@ -164,13 +191,14 @@ func (task *KubectlTask) Launch() error {
164191
defer cancel()
165192

166193
// Apply via Stdin (-)
167-
command := exec.CommandContext(ctx, KUBECTL, APPLY, "-f", "-")
194+
command := kubectl(ctx, APPLY, "-f", "-")
168195
command.Stdin = strings.NewReader(expandedYaml)
169196

170197
log.WithFields(logrus.Fields{
171-
"controlmode": task.Tci.ControlMode,
172-
"name": task.ti.Name,
173-
"command": command,
198+
"controlmode": task.Tci.ControlMode,
199+
"name": task.ti.Name,
200+
"command": command,
201+
"expandedYaml": expandedYaml,
174202
}).Info("Starting kubectl apply via Stdin")
175203

176204
var stdoutBuf bytes.Buffer
@@ -199,7 +227,7 @@ func (task *KubectlTask) Kill() error {
199227
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
200228
defer cancel()
201229

202-
command := exec.CommandContext(ctx, KUBECTL, DELETE, "-f", task.configYaml)
230+
command := kubectl(ctx, DELETE, "-f", task.configYaml)
203231

204232
command.Stdout = os.Stdout
205233
command.Stderr = os.Stderr
@@ -245,7 +273,7 @@ func (task *KubectlTask) Transition(transition *executorcmd.ExecutorCommand_Tran
245273
{"op": "add", "path": "/spec/arguments", "value": %s}
246274
]`, strings.ToLower(transition.Destination), string(argsJSON))
247275

248-
command := exec.CommandContext(ctx, KUBECTL, PATCH, "-f", task.configYaml, "--type=json", "-p", transitionJSON)
276+
command := kubectl(ctx, PATCH, "-f", task.configYaml, "--type=json", "-p", transitionJSON)
249277

250278
command.Stdout = os.Stdout
251279
command.Stderr = os.Stderr
@@ -303,8 +331,8 @@ loop:
303331
func (task *KubectlTask) getTaskStatus() (string, error) {
304332
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
305333
defer cancel()
306-
// command := exec.CommandContext(ctx, KUBECTL, GET, TASK, task.ti.Name, "-o", "jsonpath={.status.state}")
307-
command := exec.CommandContext(ctx, KUBECTL, GET, "-f", task.configYaml, "-o", "jsonpath={.status.state}")
334+
// command := exec.CommandContext(ctx, KUBECTL, GET, "-f", task.configYaml, "-o", "jsonpath={.status.state}")
335+
command := kubectl(ctx, GET, "-f", task.configYaml, "-o", "jsonpath={.status.state}")
308336

309337
var stdoutBuf bytes.Buffer
310338

0 commit comments

Comments
 (0)