Add write-kubeconfig-group flag to server (#9233)

* Add write-kubeconfig-group flag to server
* update kubectl unable to read config message for kubeconfig mode/group

Signed-off-by: Katherine Pata <me@kitty.sh>
(cherry picked from commit 7a0ea3c953)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/10288/head
Katherine Door 6 months ago committed by Brad Davidson
parent 2c50f4aa5b
commit da2625d1a9

@ -45,6 +45,7 @@ type Server struct {
DisableAgent bool
KubeConfigOutput string
KubeConfigMode string
KubeConfigGroup string
HelmJobImage string
TLSSan cli.StringSlice
TLSSanSecurity bool
@ -250,6 +251,12 @@ var ServerFlags = []cli.Flag{
Destination: &ServerConfig.KubeConfigMode,
EnvVar: version.ProgramUpper + "_KUBECONFIG_MODE",
},
&cli.StringFlag{
Name: "write-kubeconfig-group",
Usage: "(client) Write kubeconfig with this group",
Destination: &ServerConfig.KubeConfigGroup,
EnvVar: version.ProgramUpper + "_KUBECONFIG_GROUP",
},
&cli.StringFlag{
Name: "helm-job-image",
Usage: "(helm) Default image to use for helm jobs",

@ -133,6 +133,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
serverConfig.ControlConfig.DataDir = cfg.DataDir
serverConfig.ControlConfig.KubeConfigOutput = cfg.KubeConfigOutput
serverConfig.ControlConfig.KubeConfigMode = cfg.KubeConfigMode
serverConfig.ControlConfig.KubeConfigGroup = cfg.KubeConfigGroup
serverConfig.ControlConfig.HelmJobImage = cfg.HelmJobImage
serverConfig.ControlConfig.Rootless = cfg.Rootless
serverConfig.ControlConfig.ServiceLBNamespace = cfg.ServiceLBNamespace

@ -182,6 +182,7 @@ type Control struct {
ServiceNodePortRange *utilnet.PortRange
KubeConfigOutput string
KubeConfigMode string
KubeConfigGroup string
HelmJobImage string
DataDir string
KineTLS bool

@ -54,7 +54,8 @@ func checkReadConfigPermissions(configFile string) error {
if err != nil {
if os.IsPermission(err) {
return fmt.Errorf("Unable to read %s, please start server "+
"with --write-kubeconfig-mode to modify kube config permissions", configFile)
"with --write-kubeconfig-mode or --write-kubeconfig-group "+
"to modify kube config permissions", configFile)
}
}
file.Close()

@ -465,6 +465,13 @@ func writeKubeConfig(certs string, config *Config) error {
util.SetFileModeForPath(kubeConfig, os.FileMode(0600))
}
if config.ControlConfig.KubeConfigGroup != "" {
err := util.SetFileGroupForPath(kubeConfig, config.ControlConfig.KubeConfigGroup)
if err != nil {
logrus.Errorf("Failed to set %s to group %s: %v", kubeConfig, config.ControlConfig.KubeConfigGroup, err)
}
}
if kubeConfigSymlink != kubeConfig {
if err := writeConfigSymlink(kubeConfig, kubeConfigSymlink); err != nil {
logrus.Errorf("Failed to write kubeconfig symlink: %v", err)

@ -2,7 +2,9 @@ package util
import (
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
@ -14,6 +16,27 @@ func SetFileModeForPath(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}
func SetFileGroupForPath(name string, group string) error {
// Try to use as group id
gid, err := strconv.Atoi(group)
if err == nil {
return os.Chown(name, -1, gid)
}
// Otherwise, it must be a group name
g, err := user.LookupGroup(group)
if err != nil {
return err
}
gid, err = strconv.Atoi(g.Gid)
if err != nil {
return err
}
return os.Chown(name, -1, gid)
}
func SetFileModeForFile(file *os.File, mode os.FileMode) error {
return file.Chmod(mode)
}

Loading…
Cancel
Save