Skip to content

Commit cd89b5a

Browse files
authored
Merge pull request #5581 from twz123/early-dir-init
Initialize common directories once
2 parents 94d53af + 4c51169 commit cd89b5a

File tree

19 files changed

+34
-50
lines changed

19 files changed

+34
-50
lines changed

cmd/controller/controller.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@ func (c *command) start(ctx context.Context, flags *config.ControllerOptions) er
160160
if err := dir.Init(c.K0sVars.DataDir, constant.DataDirMode); err != nil {
161161
return err
162162
}
163-
if err := dir.Init(c.K0sVars.CertRootDir, constant.CertRootDirMode); err != nil {
163+
if err := dir.Init(c.K0sVars.RunDir, constant.RunDirMode); err != nil {
164164
return err
165165
}
166-
// let's make sure run-dir exists
167-
if err := dir.Init(c.K0sVars.RunDir, constant.RunDirMode); err != nil {
168-
return fmt.Errorf("failed to initialize dir: %w", err)
166+
if err := dir.Init(c.K0sVars.BinDir, constant.BinDirMode); err != nil {
167+
return err
168+
}
169+
if err := dir.Init(c.K0sVars.CertRootDir, constant.CertRootDirMode); err != nil {
170+
return err
169171
}
170172

171173
rtc, err := config.NewRuntimeConfig(c.K0sVars, nodeConfig)

cmd/kubeconfig/admin_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func TestAdmin(t *testing.T) {
6161
RuntimeConfigPath: filepath.Join(dataDir, "run", "k0s.yaml"),
6262
StartupConfigPath: configPath,
6363
}
64+
require.NoError(t, os.Mkdir(filepath.Dir(k0sVars.RuntimeConfigPath), 0700))
6465
cfg, err := config.NewRuntimeConfig(k0sVars, nil)
6566
require.NoError(t, err)
6667
t.Cleanup(func() { assert.NoError(t, cfg.Spec.Cleanup()) })
@@ -101,6 +102,7 @@ func TestAdmin_NoAdminConfig(t *testing.T) {
101102
RuntimeConfigPath: filepath.Join(dataDir, "run", "k0s.yaml"),
102103
StartupConfigPath: filepath.Join(dataDir, "k0s.yaml"),
103104
}
105+
require.NoError(t, os.Mkdir(filepath.Dir(k0sVars.RuntimeConfigPath), 0700))
104106

105107
cfg, err := config.NewRuntimeConfig(k0sVars, nil)
106108
require.NoError(t, err)

cmd/worker/worker.go

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"runtime"
2727
"syscall"
2828

29+
"github.com/k0sproject/k0s/internal/pkg/dir"
2930
"github.com/k0sproject/k0s/internal/pkg/file"
3031
"github.com/k0sproject/k0s/internal/pkg/flags"
3132
internallog "github.com/k0sproject/k0s/internal/pkg/log"
@@ -39,6 +40,7 @@ import (
3940
"github.com/k0sproject/k0s/pkg/component/worker/containerd"
4041
"github.com/k0sproject/k0s/pkg/component/worker/nllb"
4142
"github.com/k0sproject/k0s/pkg/config"
43+
"github.com/k0sproject/k0s/pkg/constant"
4244
"github.com/k0sproject/k0s/pkg/kubernetes"
4345
"github.com/k0sproject/k0s/pkg/node"
4446
"github.com/k0sproject/k0s/pkg/token"
@@ -117,6 +119,17 @@ func NewWorkerCmd() *cobra.Command {
117119
logrus.Infof("The file %s is no longer used and can safely be deleted", legacyCAFile)
118120
}
119121

122+
// create directories early with the proper permissions
123+
if err := dir.Init(c.K0sVars.DataDir, constant.DataDirMode); err != nil {
124+
return err
125+
}
126+
if err := dir.Init(c.K0sVars.RunDir, constant.RunDirMode); err != nil {
127+
return err
128+
}
129+
if err := dir.Init(c.K0sVars.BinDir, constant.BinDirMode); err != nil {
130+
return err
131+
}
132+
120133
return c.Start(ctx, nodeName, kubeletExtraArgs, getBootstrapKubeconfig, nil)
121134
},
122135
}

pkg/assets/stage.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"os/exec"
2525
"path/filepath"
2626

27-
"github.com/k0sproject/k0s/internal/pkg/dir"
2827
"github.com/sirupsen/logrus"
2928
)
3029

@@ -63,15 +62,10 @@ func BinPath(name string, binDir string) string {
6362
}
6463

6564
// Stage ...
66-
func Stage(dataDir string, name string, filemode os.FileMode) error {
65+
func Stage(dataDir string, name string) error {
6766
p := filepath.Join(dataDir, name)
6867
logrus.Infof("Staging '%s'", p)
6968

70-
err := dir.Init(filepath.Dir(p), filemode)
71-
if err != nil {
72-
return fmt.Errorf("failed to create dir '%s': %w", filepath.Dir(p), err)
73-
}
74-
7569
selfexe, err := os.Executable()
7670
if err != nil {
7771
return fmt.Errorf("unable to determine current executable: %w", err)

pkg/component/controller/apiserver.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (a *APIServer) Init(_ context.Context) error {
9696
a.uid = users.RootUID
9797
logrus.WithError(err).Warn("Running Kubernetes API server as root")
9898
}
99-
return assets.Stage(a.K0sVars.BinDir, kubeAPIComponentName, constant.BinDirMode)
99+
return assets.Stage(a.K0sVars.BinDir, kubeAPIComponentName)
100100
}
101101

102102
// Run runs kube api

pkg/component/controller/controllermanager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (a *Manager) Init(_ context.Context) error {
8080
if err := os.Chown(path.Join(a.K0sVars.CertRootDir, "ca.key"), a.uid, -1); err != nil && os.Geteuid() == 0 {
8181
logrus.Warn("failed to change permissions for the ca.key: ", err)
8282
}
83-
return assets.Stage(a.K0sVars.BinDir, kubeControllerManagerComponent, constant.BinDirMode)
83+
return assets.Stage(a.K0sVars.BinDir, kubeControllerManagerComponent)
8484
}
8585

8686
// Run runs kube Manager

pkg/component/controller/cplb/cplb_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (k *Keepalived) Init(_ context.Context) error {
8383
}
8484

8585
k.configFilePath = filepath.Join(k.K0sVars.RunDir, "keepalived.conf")
86-
return assets.Stage(k.K0sVars.BinDir, "keepalived", constant.BinDirMode)
86+
return assets.Stage(k.K0sVars.BinDir, "keepalived")
8787
}
8888

8989
// Start generates the keepalived configuration and starts the keepalived process

pkg/component/controller/etcd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (e *Etcd) Init(_ context.Context) error {
9292
return err
9393
}
9494
}
95-
return assets.Stage(e.K0sVars.BinDir, "etcd", constant.BinDirMode)
95+
return assets.Stage(e.K0sVars.BinDir, "etcd")
9696
}
9797

9898
func (e *Etcd) syncEtcdConfig(ctx context.Context, etcdRequest v1beta1.EtcdRequest, etcdCaCert, etcdCaCertKey string) ([]string, error) {

pkg/component/controller/kine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (k *Kine) Init(_ context.Context) error {
106106
if err != nil {
107107
return fmt.Errorf("can't create bypass etcd client: %w", err)
108108
}
109-
return assets.Stage(k.K0sVars.BinDir, "kine", constant.BinDirMode)
109+
return assets.Stage(k.K0sVars.BinDir, "kine")
110110
}
111111

112112
// Run runs kine

pkg/component/controller/konnectivity.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (k *Konnectivity) Init(ctx context.Context) error {
8484
}
8585

8686
k.log = logrus.WithFields(logrus.Fields{"component": "konnectivity"})
87-
if err := assets.Stage(k.K0sVars.BinDir, "konnectivity-server", constant.BinDirMode); err != nil {
87+
if err := assets.Stage(k.K0sVars.BinDir, "konnectivity-server"); err != nil {
8888
k.EmitWithPayload("failed to stage konnectivity-server", err)
8989
return fmt.Errorf("failed to stage konnectivity-server binary %w", err)
9090

pkg/component/controller/scheduler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (a *Scheduler) Init(_ context.Context) error {
5858
a.uid = users.RootUID
5959
logrus.WithError(err).Warn("Running kube-scheduler as root")
6060
}
61-
return assets.Stage(a.K0sVars.BinDir, kubeSchedulerComponentName, constant.BinDirMode)
61+
return assets.Stage(a.K0sVars.BinDir, kubeSchedulerComponentName)
6262
}
6363

6464
// Run runs kube scheduler

pkg/component/iptables/iptables.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *Component) Stop() error {
6969
func extractIPTablesBinaries(k0sBinDir string, iptablesMode string) (error, string) {
7070
cmds := []string{"xtables-legacy-multi", "xtables-nft-multi"}
7171
for _, cmd := range cmds {
72-
err := assets.Stage(k0sBinDir, cmd, constant.BinDirMode)
72+
err := assets.Stage(k0sBinDir, cmd)
7373
if err != nil {
7474
return err, ""
7575
}

pkg/component/status/status.go

-6
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import (
2222
"context"
2323
"encoding/json"
2424
"errors"
25-
"fmt"
2625
"net"
2726
"net/http"
2827
"os"
2928
"strconv"
3029
"time"
3130

32-
"github.com/k0sproject/k0s/internal/pkg/dir"
3331
"github.com/k0sproject/k0s/pkg/component/manager"
3432
"github.com/k0sproject/k0s/pkg/component/prober"
3533
kubeutil "github.com/k0sproject/k0s/pkg/kubernetes"
@@ -81,10 +79,6 @@ func (s *Status) Init(_ context.Context) error {
8179
s.httpserver = http.Server{
8280
Handler: mux,
8381
}
84-
err = dir.Init(s.StatusInformation.K0sVars.RunDir, 0755)
85-
if err != nil {
86-
return fmt.Errorf("failed to create %s: %w", s.Socket, err)
87-
}
8882

8983
removeLeftovers(s.Socket)
9084
s.listener, err = net.Listen("unix", s.Socket)

pkg/component/worker/containerd/component.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"github.com/k0sproject/k0s/pkg/component/manager"
4444
workerconfig "github.com/k0sproject/k0s/pkg/component/worker/config"
4545
"github.com/k0sproject/k0s/pkg/config"
46-
"github.com/k0sproject/k0s/pkg/constant"
4746
containerruntime "github.com/k0sproject/k0s/pkg/container/runtime"
4847
"github.com/k0sproject/k0s/pkg/debounce"
4948
"github.com/k0sproject/k0s/pkg/supervisor"
@@ -100,7 +99,7 @@ func (c *Component) Init(ctx context.Context) error {
10099
g, _ := errgroup.WithContext(ctx)
101100
for _, bin := range c.binaries {
102101
g.Go(func() error {
103-
err := assets.Stage(c.K0sVars.BinDir, bin, constant.BinDirMode)
102+
err := assets.Stage(c.K0sVars.BinDir, bin)
104103
// Simply ignore the "running executable" problem on Windows for
105104
// now. Whenever there's a permission error on Windows and the
106105
// target file exists, log the error and continue.

pkg/component/worker/kubelet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ var _ manager.Component = (*Kubelet)(nil)
7373
func (k *Kubelet) Init(_ context.Context) error {
7474

7575
if runtime.GOOS == "windows" {
76-
err := assets.Stage(k.K0sVars.BinDir, "kubelet.exe", constant.BinDirMode)
76+
err := assets.Stage(k.K0sVars.BinDir, "kubelet.exe")
7777
return err
7878
}
7979

8080
if runtime.GOOS == "linux" {
81-
if err := assets.Stage(k.K0sVars.BinDir, "kubelet", constant.BinDirMode); err != nil {
81+
if err := assets.Stage(k.K0sVars.BinDir, "kubelet"); err != nil {
8282
return err
8383
}
8484
}

pkg/config/runtime.go

-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424

25-
"github.com/k0sproject/k0s/internal/pkg/dir"
2625
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
27-
"github.com/k0sproject/k0s/pkg/constant"
2826

2927
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3028

@@ -100,10 +98,6 @@ func ParseRuntimeConfig(content []byte) (*RuntimeConfig, error) {
10098
}
10199

102100
func NewRuntimeConfig(k0sVars *CfgVars, nodeConfig *v1beta1.ClusterConfig) (*RuntimeConfig, error) {
103-
if err := dir.Init(filepath.Dir(k0sVars.RuntimeConfigPath), constant.RunDirMode); err != nil {
104-
logrus.Warnf("failed to initialize runtime config dir: %v", err)
105-
}
106-
107101
// A file lock is acquired using `flock(2)` to ensure that only one
108102
// instance of the `k0s` process can modify the runtime configuration
109103
// at a time. The lock is tied to the lifetime of the `k0s` process,

pkg/config/runtime_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ func TestNewRuntimeConfig(t *testing.T) {
5858
require.NoError(t, err)
5959
require.NoError(t, os.WriteFile(startupConfigPath, startupConfig, 0644))
6060

61-
// Define runtime configuration file in a not yet existing directory
62-
rtConfigPath := filepath.Join(tempDir, "runtime", "config")
61+
rtConfigPath := filepath.Join(tempDir, "runtime-config")
6362

6463
// prepare k0sVars
6564
k0sVars := &CfgVars{

pkg/supervisor/supervisor.go

-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ func (s *Supervisor) Supervise() error {
131131
}
132132
s.log = logrus.WithField("component", s.Name)
133133
s.PidFile = path.Join(s.RunDir, s.Name) + ".pid"
134-
if err := dir.Init(s.RunDir, constant.RunDirMode); err != nil {
135-
s.log.Warnf("failed to initialize dir: %v", err)
136-
return err
137-
}
138134

139135
if s.TimeoutStop == 0 {
140136
s.TimeoutStop = 5 * time.Second

pkg/supervisor/supervisor_test.go

-9
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ func TestSupervisorStart(t *testing.T) {
7878
RunDir: t.TempDir(),
7979
},
8080
},
81-
{
82-
expectedErrMsg: "mkdir " + sleep.binPath,
83-
proc: Supervisor{
84-
Name: "supervisor-test-rundir-init-fail",
85-
BinPath: sleep.binPath,
86-
Args: sleep.binArgs,
87-
RunDir: filepath.Join(sleep.binPath, "obstructed"),
88-
},
89-
},
9081
}
9182

9283
for _, s := range testSupervisors {

0 commit comments

Comments
 (0)