Skip to content

Commit 5e11c56

Browse files
authored
Use custom datadir in kubeconfig command (#536)
* Use custom datadir in kubeconfig command Signed-off-by: Kimmo Lehto <[email protected]> * Dummy change to trigger build Signed-off-by: Kimmo Lehto <[email protected]> * This is getting weird Signed-off-by: Kimmo Lehto <[email protected]> --------- Signed-off-by: Kimmo Lehto <[email protected]>
1 parent 8e05858 commit 5e11c56

File tree

14 files changed

+46
-41
lines changed

14 files changed

+46
-41
lines changed

cmd/config_edit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var configEditCommand = &cli.Command{
6565
return err
6666
}
6767

68-
oldCfg, err := h.ExecOutput(h.Configurer.K0sCmdf("kubectl --data-dir=%s -n kube-system get clusterconfig k0s -o yaml", h.DataDir), exec.Sudo(h))
68+
oldCfg, err := h.ExecOutput(h.Configurer.K0sCmdf("kubectl --data-dir=%s -n kube-system get clusterconfig k0s -o yaml", h.K0sDataDir()), exec.Sudo(h))
6969
if err != nil {
7070
return fmt.Errorf("%s: %w", h, err)
7171
}
@@ -102,7 +102,7 @@ var configEditCommand = &cli.Command{
102102
return fmt.Errorf("configuration was not changed, aborting")
103103
}
104104

105-
if err := h.Exec(h.Configurer.K0sCmdf("kubectl apply --data-dir=%s -n kube-system -f -", h.DataDir), exec.Stdin(newCfg), exec.Sudo(h)); err != nil {
105+
if err := h.Exec(h.Configurer.K0sCmdf("kubectl apply --data-dir=%s -n kube-system -f -", h.K0sDataDir()), exec.Stdin(newCfg), exec.Sudo(h)); err != nil {
106106
return err
107107
}
108108

cmd/config_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var configStatusCommand = &cli.Command{
4747
format = "-o " + format
4848
}
4949

50-
output, err := h.ExecOutput(h.Configurer.K0sCmdf("kubectl --data-dir=%s -n kube-system get event --field-selector involvedObject.name=k0s %s", h.DataDir, format), exec.Sudo(h))
50+
output, err := h.ExecOutput(h.Configurer.K0sCmdf("kubectl --data-dir=%s -n kube-system get event --field-selector involvedObject.name=k0s %s", h.K0sDataDir(), format), exec.Sudo(h))
5151
if err != nil {
5252
return fmt.Errorf("%s: %w", h, err)
5353
}

configurer/linux.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configurer
22

33
import (
44
"fmt"
5+
"path"
56
"regexp"
67
"strconv"
78
"strings"
@@ -17,7 +18,7 @@ type PathFuncs interface {
1718
K0sBinaryPath() string
1819
K0sConfigPath() string
1920
K0sJoinTokenPath() string
20-
KubeconfigPath(h os.Host) string
21+
KubeconfigPath(os.Host, string) string
2122
DataDirDefaultPath() string
2223
}
2324

@@ -137,12 +138,15 @@ func (l Linux) MoveFile(h os.Host, src, dst string) error {
137138
}
138139

139140
// KubeconfigPath returns the path to a kubeconfig on the host
140-
func (l Linux) KubeconfigPath(h os.Host) string {
141+
func (l Linux) KubeconfigPath(h os.Host, dataDir string) string {
141142
linux := &os.Linux{}
142-
if linux.FileExist(h, "/var/lib/k0s/pki/admin.conf") {
143-
return "/var/lib/k0s/pki/admin.conf"
143+
144+
// if admin.conf exists, use that
145+
adminConfPath := path.Join(dataDir, "pki/admin.conf")
146+
if linux.FileExist(h, adminConfPath) {
147+
return adminConfPath
144148
}
145-
return "/var/lib/k0s/kubelet.conf"
149+
return path.Join(dataDir, "kubelet.conf")
146150
}
147151

148152
// DataDirPath returns the location of k0s data dir
@@ -151,8 +155,8 @@ func (l Linux) DataDirDefaultPath() string {
151155
}
152156

153157
// KubectlCmdf returns a command line in sprintf manner for running kubectl on the host using the kubeconfig from KubeconfigPath
154-
func (l Linux) KubectlCmdf(h os.Host, s string, args ...interface{}) string {
155-
return fmt.Sprintf(`env "KUBECONFIG=%s" %s`, l.PathFuncs.KubeconfigPath(h), l.K0sCmdf(`kubectl %s`, fmt.Sprintf(s, args...)))
158+
func (l Linux) KubectlCmdf(h os.Host, dataDir, s string, args ...interface{}) string {
159+
return fmt.Sprintf(`env "KUBECONFIG=%s" %s`, l.PathFuncs.KubeconfigPath(h, dataDir), l.K0sCmdf(`kubectl %s`, fmt.Sprintf(s, args...)))
156160
}
157161

158162
// HTTPStatus makes a HTTP GET request to the url and returns the status code or an error

configurer/linux/linux_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func TestPaths(t *testing.T) {
6565
require.Equal(t, "/opt/bin/k0s --help", fc.K0sCmdf("--help"))
6666
require.Equal(t, "/usr/local/bin/k0s --help", ubuntu.K0sCmdf("--help"))
6767

68-
require.Equal(t, "/var/lib/k0s/pki/admin.conf", fc.KubeconfigPath(h1))
69-
require.Equal(t, "/var/lib/k0s/pki/admin.conf", ubuntu.KubeconfigPath(h1))
68+
require.Equal(t, "/var/lib/k0s/pki/admin.conf", fc.KubeconfigPath(h1, fc.DataDirDefaultPath()))
69+
require.Equal(t, "/var/lib/k0s/pki/admin.conf", ubuntu.KubeconfigPath(h1, ubuntu.DataDirDefaultPath()))
7070

71-
require.Equal(t, "/var/lib/k0s/kubelet.conf", fc.KubeconfigPath(h2))
72-
require.Equal(t, "/var/lib/k0s/kubelet.conf", ubuntu.KubeconfigPath(h2))
71+
require.Equal(t, "/var/lib/k0s/kubelet.conf", fc.KubeconfigPath(h2, fc.DataDirDefaultPath()))
72+
require.Equal(t, "/var/lib/k0s/kubelet.conf", ubuntu.KubeconfigPath(h2, ubuntu.DataDirDefaultPath()))
7373
}

phase/configure_k0s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (p *ConfigureK0s) Run() error {
4141

4242
var cmd string
4343
if p.leader.Exec(p.leader.Configurer.K0sCmdf("config create --help"), exec.Sudo(p.leader)) == nil {
44-
cmd = p.leader.Configurer.K0sCmdf("config create --data-dir=%s", p.leader.DataDir)
44+
cmd = p.leader.Configurer.K0sCmdf("config create --data-dir=%s", p.leader.K0sDataDir())
4545
} else {
4646
cmd = p.leader.Configurer.K0sCmdf("default-config")
4747
}

phase/get_kubeconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (p *GetKubeconfig) Title() string {
2020
}
2121

2222
var readKubeconfig = func(h *cluster.Host) (string, error) {
23-
return h.Configurer.ReadFile(h, h.Configurer.KubeconfigPath(h))
23+
return h.Configurer.ReadFile(h, h.Configurer.KubeconfigPath(h, h.K0sDataDir()))
2424
}
2525

2626
// Run the phase

phase/install_controllers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (p *InstallControllers) Run() error {
6767
}
6868
log.Debugf("%s: join token ID: %s", p.leader, tokenID)
6969
defer func() {
70-
if err := p.leader.Exec(p.leader.Configurer.K0sCmdf("token invalidate --data-dir=%s %s", h.DataDir, tokenID), exec.Sudo(p.leader), exec.RedactString(token)); err != nil {
70+
if err := p.leader.Exec(p.leader.Configurer.K0sCmdf("token invalidate --data-dir=%s %s", h.K0sDataDir(), tokenID), exec.Sudo(p.leader), exec.RedactString(token)); err != nil {
7171
log.Warnf("%s: failed to invalidate the controller join token", p.leader)
7272
}
7373
}()

phase/install_workers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (p *InstallWorkers) Run() error {
8585

8686
if !NoWait {
8787
defer func() {
88-
if err := p.leader.Exec(p.leader.Configurer.K0sCmdf("token invalidate --data-dir=%s %s", p.leader.DataDir, tokenID), exec.Sudo(p.leader), exec.RedactString(token)); err != nil {
88+
if err := p.leader.Exec(p.leader.Configurer.K0sCmdf("token invalidate --data-dir=%s %s", p.leader.K0sDataDir(), tokenID), exec.Sudo(p.leader), exec.RedactString(token)); err != nil {
8989
log.Warnf("%s: failed to invalidate the worker join token", p.leader)
9090
}
9191
}()

phase/prepare_hosts.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package phase
33
import (
44
"strings"
55

6-
"github.com/alessio/shellescape"
76
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
87
"github.com/k0sproject/rig/os"
98
log "github.com/sirupsen/logrus"
@@ -70,11 +69,5 @@ func (p *PrepareHosts) prepareHost(h *cluster.Host) error {
7069
}
7170
}
7271

73-
if h.DataDir == "" {
74-
log.Debugf("%s: data-dir is not set, using default", h)
75-
h.DataDir = h.Configurer.DataDirDefaultPath()
76-
}
77-
h.DataDir = shellescape.Quote(h.DataDir)
78-
7972
return nil
8073
}

phase/reset_controllers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (p *ResetControllers) Run() error {
103103
log.Debugf("%s: leaving etcd completed", h)
104104

105105
log.Debugf("%s: resetting k0s...", h)
106-
out, err := h.ExecOutput(h.Configurer.K0sCmdf("reset --data-dir=%s", h.DataDir), exec.Sudo(h))
106+
out, err := h.ExecOutput(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), exec.Sudo(h))
107107
if err != nil {
108108
log.Debugf("%s: k0s reset failed: %s", h, out)
109109
log.Warnf("%s: k0s reported failure: %v", h, err)

0 commit comments

Comments
 (0)