Skip to content

Commit

Permalink
Merge pull request #4289 from twz123/keepalived
Browse files Browse the repository at this point in the history
Apply some idiomatic patterns to CPLB component
  • Loading branch information
twz123 authored Apr 25, 2024
2 parents 82f11db + 4c0a820 commit 6592e62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
56 changes: 22 additions & 34 deletions pkg/component/controller/cplb_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ limitations under the License.
package controller

import (
"bufio"
"context"
"errors"
"fmt"
"io/fs"
"io"
"net"
"os"
"path/filepath"
"slices"
"text/template"

"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/file"
"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 +49,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 +65,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 +90,7 @@ func (k *Keepalived) Start(_ context.Context) error {
args := []string{
"--dont-fork",
"--use-file",
k.K0sVars.KeepalivedConfigFile,
k.configFilePath,
"--no-syslog",
"--log-console",
}
Expand All @@ -110,8 +104,8 @@ func (k *Keepalived) Start(_ context.Context) error {
Name: "keepalived",
BinPath: assets.BinPath("keepalived", k.K0sVars.BinDir),
Args: args,
RunDir: filepath.Dir(k.K0sVars.KeepalivedConfigFile),
DataDir: filepath.Dir(k.K0sVars.KeepalivedConfigFile),
RunDir: k.K0sVars.RunDir,
DataDir: k.K0sVars.DataDir,
UID: k.uid,
}
return k.supervisor.Supervise()
Expand Down Expand Up @@ -274,31 +268,25 @@ 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))
if err != nil {
return fmt.Errorf("failed to open keepalived config file: %w", err)
}
defer f.Close()

template, err := template.New("keepalived").Parse(keepalivedConfigTemplate)
if err != nil {
return fmt.Errorf("failed to parse keepalived template: %w", err)
}

template := template.Must(template.New("keepalived").Parse(keepalivedConfigTemplate))
kc := keepalivedConfig{
VRRPInstances: k.Config.VRRPInstances,
}
if err = template.Execute(f, kc); err != nil {
return fmt.Errorf("failed to execute keepalived template: %w", err)
}

// TODO: Do we really need to this every single time?
if err = os.Chown(k.K0sVars.KeepalivedConfigFile, 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 {
return fmt.Errorf("failed to chmod keepalived config file: %w", err)
if err := file.WriteAtomically(k.configFilePath, 0400, func(file io.Writer) error {
if err := file.(*os.File).Chown(k.uid, -1); err != nil {
return err
}

w := bufio.NewWriter(file)
if err := template.Execute(w, kc); err != nil {
return err
}
return w.Flush()
}); err != nil {
return fmt.Errorf("failed to write 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 6592e62

Please sign in to comment.