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 11, 2024
1 parent 5c4365f commit b730840
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
30 changes: 11 additions & 19 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 @@ -42,11 +41,12 @@ import (

// Keepalived is the controller for the keepalived process in the control plane load balancing
type Keepalived struct {
K0sVars *config.CfgVars
Config *k0sAPI.ControlPlaneLoadBalancingSpec
uid int
supervisor *supervisor.Supervisor
log *logrus.Entry
K0sVars *config.CfgVars
Config *k0sAPI.ControlPlaneLoadBalancingSpec
uid int
supervisor *supervisor.Supervisor
log *logrus.Entry
configFilePath string
}

// Init extracts the needed binaries and creates the directories
Expand All @@ -62,15 +62,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: %v", err)
}

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

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

Expand Down Expand Up @@ -99,7 +91,7 @@ func (k *Keepalived) Start(_ context.Context) error {
Args: []string{
"--dont-fork",
"--use-file",
k.K0sVars.KeepalivedConfigFile,
k.configFilePath,
},
RunDir: k.K0sVars.RunDir,
DataDir: k.K0sVars.DataDir,
Expand Down Expand Up @@ -265,7 +257,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: %v", err)
}
Expand All @@ -284,10 +276,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: %v", 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: %v", 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 b730840

Please sign in to comment.