Skip to content

Commit 6592e62

Browse files
authored
Merge pull request #4289 from twz123/keepalived
Apply some idiomatic patterns to CPLB component
2 parents 82f11db + 4c0a820 commit 6592e62

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

pkg/component/controller/cplb_unix.go

+22-34
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ limitations under the License.
1919
package controller
2020

2121
import (
22+
"bufio"
2223
"context"
2324
"errors"
2425
"fmt"
25-
"io/fs"
26+
"io"
2627
"net"
2728
"os"
2829
"path/filepath"
2930
"slices"
3031
"text/template"
3132

32-
"github.com/k0sproject/k0s/internal/pkg/dir"
33+
"github.com/k0sproject/k0s/internal/pkg/file"
3334
"github.com/k0sproject/k0s/internal/pkg/users"
3435
k0sAPI "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
3536
"github.com/k0sproject/k0s/pkg/assets"
@@ -48,6 +49,7 @@ type Keepalived struct {
4849
uid int
4950
supervisor *supervisor.Supervisor
5051
log *logrus.Entry
52+
configFilePath string
5153
}
5254

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

66-
basepath := filepath.Dir(k.K0sVars.KeepalivedConfigFile)
67-
if err = dir.Init(basepath, constant.KeepalivedDirMode); err != nil {
68-
return fmt.Errorf("failed to create keepalived data dir: %w", err)
69-
}
70-
71-
if err = os.Chown(basepath, k.uid, -1); err != nil {
72-
return fmt.Errorf("failed to chown keepalived data dir: %w", err)
73-
}
74-
68+
k.configFilePath = filepath.Join(k.K0sVars.RunDir, "keepalived.conf")
7569
return assets.Stage(k.K0sVars.BinDir, "keepalived", constant.BinDirMode)
7670
}
7771

@@ -96,7 +90,7 @@ func (k *Keepalived) Start(_ context.Context) error {
9690
args := []string{
9791
"--dont-fork",
9892
"--use-file",
99-
k.K0sVars.KeepalivedConfigFile,
93+
k.configFilePath,
10094
"--no-syslog",
10195
"--log-console",
10296
}
@@ -110,8 +104,8 @@ func (k *Keepalived) Start(_ context.Context) error {
110104
Name: "keepalived",
111105
BinPath: assets.BinPath("keepalived", k.K0sVars.BinDir),
112106
Args: args,
113-
RunDir: filepath.Dir(k.K0sVars.KeepalivedConfigFile),
114-
DataDir: filepath.Dir(k.K0sVars.KeepalivedConfigFile),
107+
RunDir: k.K0sVars.RunDir,
108+
DataDir: k.K0sVars.DataDir,
115109
UID: k.uid,
116110
}
117111
return k.supervisor.Supervise()
@@ -274,31 +268,25 @@ func (*Keepalived) getLinkAddresses(link netlink.Link) ([]netlink.Addr, []string
274268
}
275269

276270
func (k *Keepalived) generateKeepalivedTemplate() error {
277-
f, err := os.OpenFile(k.K0sVars.KeepalivedConfigFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fs.FileMode(0500))
278-
if err != nil {
279-
return fmt.Errorf("failed to open keepalived config file: %w", err)
280-
}
281-
defer f.Close()
282-
283-
template, err := template.New("keepalived").Parse(keepalivedConfigTemplate)
284-
if err != nil {
285-
return fmt.Errorf("failed to parse keepalived template: %w", err)
286-
}
287-
271+
template := template.Must(template.New("keepalived").Parse(keepalivedConfigTemplate))
288272
kc := keepalivedConfig{
289273
VRRPInstances: k.Config.VRRPInstances,
290274
}
291-
if err = template.Execute(f, kc); err != nil {
292-
return fmt.Errorf("failed to execute keepalived template: %w", err)
293-
}
294275

295-
// TODO: Do we really need to this every single time?
296-
if err = os.Chown(k.K0sVars.KeepalivedConfigFile, k.uid, -1); err != nil {
297-
return fmt.Errorf("failed to chown keepalived config file: %w", err)
298-
}
299-
if err = os.Chmod(k.K0sVars.KeepalivedConfigFile, fs.FileMode(0400)); err != nil {
300-
return fmt.Errorf("failed to chmod keepalived config file: %w", err)
276+
if err := file.WriteAtomically(k.configFilePath, 0400, func(file io.Writer) error {
277+
if err := file.(*os.File).Chown(k.uid, -1); err != nil {
278+
return err
279+
}
280+
281+
w := bufio.NewWriter(file)
282+
if err := template.Execute(w, kc); err != nil {
283+
return err
284+
}
285+
return w.Flush()
286+
}); err != nil {
287+
return fmt.Errorf("failed to write keepalived config file: %w", err)
301288
}
289+
302290
return nil
303291
}
304292

pkg/config/cfgvars.go

-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ type CfgVars struct {
4949
EtcdCertDir string // EtcdCertDir contains etcd certificates
5050
EtcdDataDir string // EtcdDataDir contains etcd state
5151
KineSocketPath string // The unix socket path for kine
52-
KeepalivedConfigFile string // location for keepalived data
5352
KonnectivitySocketDir string // location of konnectivity's socket path
5453
KubeletAuthConfigPath string // KubeletAuthConfigPath defines the default kubelet auth config path
5554
KubeletVolumePluginDir string // location for kubelet plugins volume executables
@@ -179,7 +178,6 @@ func NewCfgVars(cobraCmd command, dirs ...string) (*CfgVars, error) {
179178
EtcdCertDir: filepath.Join(certDir, "etcd"),
180179
EtcdDataDir: filepath.Join(dataDir, "etcd"),
181180
KineSocketPath: filepath.Join(runDir, constant.KineSocket),
182-
KeepalivedConfigFile: filepath.Join(dataDir, "keepalived", "keepalived.conf"),
183181
KonnectivitySocketDir: filepath.Join(runDir, "konnectivity-server"),
184182
KubeletAuthConfigPath: filepath.Join(dataDir, "kubelet.conf"),
185183
KubeletVolumePluginDir: constant.KubeletVolumePluginDir,

0 commit comments

Comments
 (0)