Skip to content

Commit

Permalink
Add changes to change/manipulate group of the k3s configuration file …
Browse files Browse the repository at this point in the history
…generated
  • Loading branch information
sonicaj committed Dec 7, 2023
1 parent 1766681 commit d9b4a8b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"runtime/debug"
Expand Down Expand Up @@ -469,6 +471,12 @@ func writeKubeConfig(certs string, config *Config) error {
util.SetFileModeForPath(kubeConfig, os.FileMode(0600))
}

if config.ControlConfig.KubeConfigGroup != "" {
if err := chownKubeConfigGroup(kubeConfig, config.ControlConfig.KubeConfigGroup); err != nil {
logrus.Errorf("Failed to add and chown group: %v", err)
}
}

if kubeConfigSymlink != kubeConfig {
if err := writeConfigSymlink(kubeConfig, kubeConfigSymlink); err != nil {
logrus.Errorf("Failed to write kubeconfig symlink: %v", err)
Expand All @@ -482,6 +490,24 @@ func writeKubeConfig(certs string, config *Config) error {
return nil
}

func chownKubeConfigGroup(filePath string, groupName string) error {
group, err := user.LookupGroup(groupName)
if err != nil {
cmd := exec.Command("groupadd", groupName)
err = cmd.Run()
group, err = user.LookupGroup(groupName)
if err != nil {
return err
}
}
gid, err := strconv.Atoi(group.Gid)
if err != nil {
return err
}
os.Chown(filePath, os.Getuid(), gid)
return nil
}

func setupDataDirAndChdir(config *config.Control) error {
var (
err error
Expand Down

0 comments on commit d9b4a8b

Please sign in to comment.