Skip to content

Commit

Permalink
Use k0s's RunDir for keepalived config file
Browse files Browse the repository at this point in the history
The file is ephemeral and doesn't need to live on a persistent path.
This also saves us from ensuring the existence of the base directory
in the cplb component, as the RunDir's existence is ensured during
controller startup.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Apr 16, 2024
1 parent 018d997 commit 2d207aa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
20 changes: 6 additions & 14 deletions pkg/component/controller/cplb_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"slices"
"text/template"

"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/users"
k0sAPI "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
"github.com/k0sproject/k0s/pkg/assets"
Expand All @@ -48,6 +47,7 @@ type Keepalived struct {
uid int
supervisor *supervisor.Supervisor
log *logrus.Entry
configFilePath string
}

// Init extracts the needed binaries and creates the directories
Expand All @@ -63,15 +63,7 @@ func (k *Keepalived) Init(_ context.Context) error {
k.log.Warnf("Unable to get %s UID running keepalived as root: %v", constant.KeepalivedUser, err)
}

basepath := filepath.Dir(k.K0sVars.KeepalivedConfigFile)
if err = dir.Init(basepath, constant.KeepalivedDirMode); err != nil {
return fmt.Errorf("failed to create keepalived data dir: %w", err)
}

if err = os.Chown(basepath, k.uid, -1); err != nil {
return fmt.Errorf("failed to chown keepalived data dir: %w", err)
}

k.configFilePath = filepath.Join(k.K0sVars.RunDir, "keepalived.conf")
return assets.Stage(k.K0sVars.BinDir, "keepalived", constant.BinDirMode)
}

Expand All @@ -96,7 +88,7 @@ func (k *Keepalived) Start(_ context.Context) error {
args := []string{
"--dont-fork",
"--use-file",
k.K0sVars.KeepalivedConfigFile,
k.configFilePath,
"--no-syslog",
"--log-console",
}
Expand Down Expand Up @@ -274,7 +266,7 @@ func (*Keepalived) getLinkAddresses(link netlink.Link) ([]netlink.Addr, []string
}

func (k *Keepalived) generateKeepalivedTemplate() error {
f, err := os.OpenFile(k.K0sVars.KeepalivedConfigFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fs.FileMode(0500))
f, err := os.OpenFile(k.configFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fs.FileMode(0500))
if err != nil {
return fmt.Errorf("failed to open keepalived config file: %w", err)
}
Expand All @@ -293,10 +285,10 @@ func (k *Keepalived) generateKeepalivedTemplate() error {
}

// TODO: Do we really need to this every single time?
if err = os.Chown(k.K0sVars.KeepalivedConfigFile, k.uid, -1); err != nil {
if err = os.Chown(k.configFilePath, k.uid, -1); err != nil {
return fmt.Errorf("failed to chown keepalived config file: %w", err)
}
if err = os.Chmod(k.K0sVars.KeepalivedConfigFile, fs.FileMode(0400)); err != nil {
if err = os.Chmod(k.configFilePath, fs.FileMode(0400)); err != nil {
return fmt.Errorf("failed to chmod keepalived config file: %w", err)
}
return nil
Expand Down
2 changes: 0 additions & 2 deletions pkg/config/cfgvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type CfgVars struct {
EtcdCertDir string // EtcdCertDir contains etcd certificates
EtcdDataDir string // EtcdDataDir contains etcd state
KineSocketPath string // The unix socket path for kine
KeepalivedConfigFile string // location for keepalived data
KonnectivitySocketDir string // location of konnectivity's socket path
KubeletAuthConfigPath string // KubeletAuthConfigPath defines the default kubelet auth config path
KubeletVolumePluginDir string // location for kubelet plugins volume executables
Expand Down Expand Up @@ -179,7 +178,6 @@ func NewCfgVars(cobraCmd command, dirs ...string) (*CfgVars, error) {
EtcdCertDir: filepath.Join(certDir, "etcd"),
EtcdDataDir: filepath.Join(dataDir, "etcd"),
KineSocketPath: filepath.Join(runDir, constant.KineSocket),
KeepalivedConfigFile: filepath.Join(dataDir, "keepalived", "keepalived.conf"),
KonnectivitySocketDir: filepath.Join(runDir, "konnectivity-server"),
KubeletAuthConfigPath: filepath.Join(dataDir, "kubelet.conf"),
KubeletVolumePluginDir: constant.KubeletVolumePluginDir,
Expand Down

0 comments on commit 2d207aa

Please sign in to comment.