Unverified Commit ea834eb2 authored by Darren Shepherd's avatar Darren Shepherd Committed by GitHub

Merge pull request #309 from galal-hussein/generic_flags

Add extra flags for server and agent components
parents 184f6973 7794528a
...@@ -249,6 +249,9 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { ...@@ -249,6 +249,9 @@ func get(envInfo *cmds.Agent) (*config.Node, error) {
os.Setenv("NODE_NAME", nodeConfig.AgentConfig.NodeName) os.Setenv("NODE_NAME", nodeConfig.AgentConfig.NodeName)
v1beta1.KubeletSocket = filepath.Join(envInfo.DataDir, "kubelet/device-plugins/kubelet.sock") v1beta1.KubeletSocket = filepath.Join(envInfo.DataDir, "kubelet/device-plugins/kubelet.sock")
nodeConfig.AgentConfig.ExtraKubeletArgs = envInfo.ExtraKubeletArgs
nodeConfig.AgentConfig.ExtraKubeProxyArgs = envInfo.ExtraKubeProxyArgs
return nodeConfig, nil return nodeConfig, nil
} }
......
...@@ -21,6 +21,8 @@ type Agent struct { ...@@ -21,6 +21,8 @@ type Agent struct {
NoFlannel bool NoFlannel bool
Debug bool Debug bool
AgentShared AgentShared
ExtraKubeletArgs cli.StringSlice
ExtraKubeProxyArgs cli.StringSlice
} }
type AgentShared struct { type AgentShared struct {
...@@ -62,6 +64,16 @@ var ( ...@@ -62,6 +64,16 @@ var (
EnvVar: "K3S_RESOLV_CONF", EnvVar: "K3S_RESOLV_CONF",
Destination: &AgentConfig.ResolvConf, Destination: &AgentConfig.ResolvConf,
} }
ExtraKubeletArgs = cli.StringSliceFlag{
Name: "kubelet-arg",
Usage: "(agent) Customized flag for kubelet process",
Value: &AgentConfig.ExtraKubeletArgs,
}
ExtraKubeProxyArgs = cli.StringSliceFlag{
Name: "kube-proxy-arg",
Usage: "(agent) Customized flag for kube-proxy process",
Value: &AgentConfig.ExtraKubeProxyArgs,
}
) )
func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command { func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
...@@ -107,6 +119,8 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command { ...@@ -107,6 +119,8 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
NodeIPFlag, NodeIPFlag,
CRIEndpointFlag, CRIEndpointFlag,
ResolvConfFlag, ResolvConfFlag,
ExtraKubeletArgs,
ExtraKubeProxyArgs,
}, },
} }
} }
...@@ -5,19 +5,22 @@ import ( ...@@ -5,19 +5,22 @@ import (
) )
type Server struct { type Server struct {
Log string Log string
ClusterCIDR string ClusterCIDR string
ClusterSecret string ClusterSecret string
ServiceCIDR string ServiceCIDR string
ClusterDNS string ClusterDNS string
HTTPSPort int HTTPSPort int
HTTPPort int HTTPPort int
DataDir string DataDir string
DisableAgent bool DisableAgent bool
KubeConfigOutput string KubeConfigOutput string
KubeConfigMode string KubeConfigMode string
KnownIPs cli.StringSlice KnownIPs cli.StringSlice
BindAddress string BindAddress string
ExtraAPIArgs cli.StringSlice
ExtraSchedulerArgs cli.StringSlice
ExtraControllerArgs cli.StringSlice
} }
var ServerConfig Server var ServerConfig Server
...@@ -106,12 +109,29 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command { ...@@ -106,12 +109,29 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command {
Usage: "Add additional hostname or IP as a Subject Alternative Name in the TLS cert", Usage: "Add additional hostname or IP as a Subject Alternative Name in the TLS cert",
Value: &ServerConfig.KnownIPs, Value: &ServerConfig.KnownIPs,
}, },
cli.StringSliceFlag{
Name: "kube-apiserver-arg",
Usage: "Customized flag for kube-apiserver process",
Value: &ServerConfig.ExtraAPIArgs,
},
cli.StringSliceFlag{
Name: "kube-scheduler-arg",
Usage: "Customized flag for kube-scheduler process",
Value: &ServerConfig.ExtraSchedulerArgs,
},
cli.StringSliceFlag{
Name: "kube-controller-arg",
Usage: "Customized flag for kube-controller-manager process",
Value: &ServerConfig.ExtraControllerArgs,
},
NodeIPFlag, NodeIPFlag,
NodeNameFlag, NodeNameFlag,
DockerFlag, DockerFlag,
FlannelFlag, FlannelFlag,
CRIEndpointFlag, CRIEndpointFlag,
ResolvConfFlag, ResolvConfFlag,
ExtraKubeletArgs,
ExtraKubeProxyArgs,
}, },
} }
} }
...@@ -83,6 +83,9 @@ func run(app *cli.Context, cfg *cmds.Server) error { ...@@ -83,6 +83,9 @@ func run(app *cli.Context, cfg *cmds.Server) error {
serverConfig.TLSConfig.HTTPPort = cfg.HTTPPort serverConfig.TLSConfig.HTTPPort = cfg.HTTPPort
serverConfig.TLSConfig.KnownIPs = knownIPs(cfg.KnownIPs) serverConfig.TLSConfig.KnownIPs = knownIPs(cfg.KnownIPs)
serverConfig.TLSConfig.BindAddress = cfg.BindAddress serverConfig.TLSConfig.BindAddress = cfg.BindAddress
serverConfig.ControlConfig.ExtraAPIArgs = cfg.ExtraAPIArgs
serverConfig.ControlConfig.ExtraControllerArgs = cfg.ExtraControllerArgs
serverConfig.ControlConfig.ExtraSchedulerAPIArgs = cfg.ExtraSchedulerArgs
_, serverConfig.ControlConfig.ClusterIPRange, err = net2.ParseCIDR(cfg.ClusterCIDR) _, serverConfig.ControlConfig.ClusterIPRange, err = net2.ParseCIDR(cfg.ClusterCIDR)
if err != nil { if err != nil {
......
...@@ -28,14 +28,14 @@ func Agent(config *config.Agent) error { ...@@ -28,14 +28,14 @@ func Agent(config *config.Agent) error {
return nil return nil
} }
func kubeProxy(config *config.Agent) { func kubeProxy(cfg *config.Agent) {
args := []string{ argsMap := map[string]string{
"--proxy-mode", "iptables", "proxy-mode": "iptables",
"--healthz-bind-address", "127.0.0.1", "healthz-bind-address": "127.0.0.1",
"--kubeconfig", config.KubeConfig, "kubeconfig": cfg.KubeConfig,
"--cluster-cidr", config.ClusterCIDR.String(), "cluster-cidr": cfg.ClusterCIDR.String(),
} }
args = append(args, config.ExtraKubeletArgs...) args := config.GetArgsList(argsMap, cfg.ExtraKubeProxyArgs)
command := app2.NewProxyCommand() command := app2.NewProxyCommand()
command.SetArgs(args) command.SetArgs(args)
...@@ -50,63 +50,64 @@ func kubelet(cfg *config.Agent) { ...@@ -50,63 +50,64 @@ func kubelet(cfg *config.Agent) {
logs.InitLogs() logs.InitLogs()
defer logs.FlushLogs() defer logs.FlushLogs()
args := []string{ argsMap := map[string]string{
"--healthz-bind-address", "127.0.0.1", "healthz-bind-address": "127.0.0.1",
"--read-only-port", "0", "read-only-port": "0",
"--allow-privileged=true", "allow-privileged": "true",
"--cluster-domain", "cluster.local", "cluster-domain": "cluster.local",
"--kubeconfig", cfg.KubeConfig, "kubeconfig": cfg.KubeConfig,
"--eviction-hard", "imagefs.available<5%,nodefs.available<5%", "eviction-hard": "imagefs.available<5%,nodefs.available<5%",
"--eviction-minimum-reclaim", "imagefs.available=10%,nodefs.available=10%", "eviction-minimum-reclaim": "imagefs.available=10%,nodefs.available=10%",
"--fail-swap-on=false", "fail-swap-on": "false",
//"--cgroup-root", "/k3s", //"cgroup-root": "/k3s",
"--cgroup-driver", "cgroupfs", "cgroup-driver": "cgroupfs",
} }
if cfg.RootDir != "" { if cfg.RootDir != "" {
args = append(args, "--root-dir", cfg.RootDir) argsMap["root-dir"] = cfg.RootDir
args = append(args, "--cert-dir", filepath.Join(cfg.RootDir, "pki")) argsMap["cert-dir"] = filepath.Join(cfg.RootDir, "pki")
args = append(args, "--seccomp-profile-root", filepath.Join(cfg.RootDir, "seccomp")) argsMap["seccomp-profile-root"] = filepath.Join(cfg.RootDir, "seccomp")
} }
if cfg.CNIConfDir != "" { if cfg.CNIConfDir != "" {
args = append(args, "--cni-conf-dir", cfg.CNIConfDir) argsMap["cni-conf-dir"] = cfg.CNIConfDir
} }
if cfg.CNIBinDir != "" { if cfg.CNIBinDir != "" {
args = append(args, "--cni-bin-dir", cfg.CNIBinDir) argsMap["cni-bin-dir"] = cfg.CNIBinDir
} }
if len(cfg.ClusterDNS) > 0 { if len(cfg.ClusterDNS) > 0 {
args = append(args, "--cluster-dns", cfg.ClusterDNS.String()) argsMap["cluster-dns"] = cfg.ClusterDNS.String()
} }
if cfg.ResolvConf != "" { if cfg.ResolvConf != "" {
args = append(args, "--resolv-conf", cfg.ResolvConf) argsMap["resolv-conf"] = cfg.ResolvConf
} }
if cfg.RuntimeSocket != "" { if cfg.RuntimeSocket != "" {
args = append(args, "--container-runtime", "remote") argsMap["container-runtime"] = "remote"
args = append(args, "--container-runtime-endpoint", cfg.RuntimeSocket) argsMap["container-runtime-endpoint"] = cfg.RuntimeSocket
args = append(args, "--serialize-image-pulls=false") argsMap["serialize-image-pulls"] = "false"
} }
if cfg.ListenAddress != "" { if cfg.ListenAddress != "" {
args = append(args, "--address", cfg.ListenAddress) argsMap["address"] = cfg.ListenAddress
} }
if cfg.CACertPath != "" { if cfg.CACertPath != "" {
args = append(args, "--anonymous-auth=false", "--client-ca-file", cfg.CACertPath) argsMap["anonymous-auth"] = "false"
argsMap["client-ca-file"] = cfg.CACertPath
} }
if cfg.NodeName != "" { if cfg.NodeName != "" {
args = append(args, "--hostname-override", cfg.NodeName) argsMap["hostname-override"] = cfg.NodeName
} }
defaultIP, err := net.ChooseHostInterface() defaultIP, err := net.ChooseHostInterface()
if err != nil || defaultIP.String() != cfg.NodeIP { if err != nil || defaultIP.String() != cfg.NodeIP {
args = append(args, "--node-ip", cfg.NodeIP) argsMap["node-ip"] = cfg.NodeIP
} }
root, hasCFS := checkCgroups() root, hasCFS := checkCgroups()
if !hasCFS { if !hasCFS {
logrus.Warn("Disabling CPU quotas due to missing cpu.cfs_period_us") logrus.Warn("Disabling CPU quotas due to missing cpu.cfs_period_us")
args = append(args, "--cpu-cfs-quota=false") argsMap["cpu-cfs-quota"] = "false"
} }
if root != "" { if root != "" {
args = append(args, "--runtime-cgroups", root) argsMap["runtime-cgroups"] = root
args = append(args, "--kubelet-cgroups", root) argsMap["kubelet-cgroups"] = root
} }
args = append(args, cfg.ExtraKubeletArgs...) args := config.GetArgsList(argsMap, cfg.ExtraKubeletArgs)
command.SetArgs(args) command.SetArgs(args)
......
...@@ -2,6 +2,7 @@ package config ...@@ -2,6 +2,7 @@ package config
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"net" "net"
"net/http" "net/http"
"strings" "strings"
...@@ -106,3 +107,21 @@ func (a ArgString) String() string { ...@@ -106,3 +107,21 @@ func (a ArgString) String() string {
} }
return b.String() return b.String()
} }
func GetArgsList(argsMap map[string]string, extraArgs []string) []string {
// add extra args to args map to override any default option
for _, arg := range extraArgs {
splitArg := strings.Split(arg, "=")
if len(splitArg) < 2 {
argsMap[splitArg[0]] = "true"
continue
}
argsMap[splitArg[0]] = splitArg[1]
}
var args []string
for arg, value := range argsMap {
cmd := fmt.Sprintf("--%s=%s", arg, value)
args = append(args, cmd)
}
return args
}
...@@ -95,20 +95,22 @@ func Server(ctx context.Context, cfg *config.Control) error { ...@@ -95,20 +95,22 @@ func Server(ctx context.Context, cfg *config.Control) error {
} }
func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) { func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) {
args := []string{ argsMap := map[string]string{
"--kubeconfig", runtime.KubeConfigSystem, "kubeconfig": runtime.KubeConfigSystem,
"--service-account-private-key-file", runtime.ServiceKey, "service-account-private-key-file": runtime.ServiceKey,
"--allocate-node-cidrs", "allocate-node-cidrs": "true",
"--cluster-cidr", cfg.ClusterIPRange.String(), "cluster-cidr": cfg.ClusterIPRange.String(),
"--root-ca-file", runtime.TokenCA, "root-ca-file": runtime.TokenCA,
"--port", "10252", "port": "10252",
"--bind-address", "127.0.0.1", "bind-address": "127.0.0.1",
"--secure-port", "0", "secure-port": "0",
} }
if cfg.NoLeaderElect { if cfg.NoLeaderElect {
args = append(args, "--leader-elect=false") argsMap["leader-elect"] = "false"
} }
args = append(args, cfg.ExtraControllerArgs...)
args := config.GetArgsList(argsMap, cfg.ExtraControllerArgs)
command := cmapp.NewControllerManagerCommand() command := cmapp.NewControllerManagerCommand()
command.SetArgs(args) command.SetArgs(args)
...@@ -119,16 +121,17 @@ func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) { ...@@ -119,16 +121,17 @@ func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) {
} }
func scheduler(cfg *config.Control, runtime *config.ControlRuntime) { func scheduler(cfg *config.Control, runtime *config.ControlRuntime) {
args := []string{ argsMap := map[string]string{
"--kubeconfig", runtime.KubeConfigSystem, "kubeconfig": runtime.KubeConfigSystem,
"--port", "10251", "port": "10251",
"--bind-address", "127.0.0.1", "bind-address": "127.0.0.1",
"--secure-port", "0", "secure-port": "0",
} }
if cfg.NoLeaderElect { if cfg.NoLeaderElect {
args = append(args, "--leader-elect=false") argsMap["leader-elect"] = "false"
} }
args = append(args, cfg.ExtraSchedulerAPIArgs...) args := config.GetArgsList(argsMap, cfg.ExtraSchedulerAPIArgs)
command := sapp.NewSchedulerCommand() command := sapp.NewSchedulerCommand()
command.SetArgs(args) command.SetArgs(args)
...@@ -139,19 +142,18 @@ func scheduler(cfg *config.Control, runtime *config.ControlRuntime) { ...@@ -139,19 +142,18 @@ func scheduler(cfg *config.Control, runtime *config.ControlRuntime) {
} }
func apiServer(ctx context.Context, cfg *config.Control, runtime *config.ControlRuntime) (authenticator.Request, http.Handler, error) { func apiServer(ctx context.Context, cfg *config.Control, runtime *config.ControlRuntime) (authenticator.Request, http.Handler, error) {
var args []string argsMap := make(map[string]string)
if len(cfg.ETCDEndpoints) > 0 { if len(cfg.ETCDEndpoints) > 0 {
args = append(args, "--storage-backend", "etcd3") argsMap["storage-backend"] = "etcd3"
args = append(args, "--etcd-servers", strings.Join(cfg.ETCDEndpoints, ",")) argsMap["etcd-servers"] = strings.Join(cfg.ETCDEndpoints, ",")
if cfg.ETCDKeyFile != "" { if cfg.ETCDKeyFile != "" {
args = append(args, "--etcd-keyfile", cfg.ETCDKeyFile) argsMap["etcd-keyfile"] = cfg.ETCDKeyFile
} }
if cfg.ETCDCAFile != "" { if cfg.ETCDCAFile != "" {
args = append(args, "--etcd-cafile", cfg.ETCDCAFile) argsMap["etcd-cafile"] = cfg.ETCDCAFile
} }
if cfg.ETCDCertFile != "" { if cfg.ETCDCertFile != "" {
args = append(args, "--etcd-certfile", cfg.ETCDCertFile) argsMap["etcd-certfile"] = cfg.ETCDCertFile
} }
} }
...@@ -159,27 +161,28 @@ func apiServer(ctx context.Context, cfg *config.Control, runtime *config.Control ...@@ -159,27 +161,28 @@ func apiServer(ctx context.Context, cfg *config.Control, runtime *config.Control
os.MkdirAll(certDir, 0700) os.MkdirAll(certDir, 0700)
// TODO: sqlite doesn't need the watch cache, but etcd does, so make this dynamic // TODO: sqlite doesn't need the watch cache, but etcd does, so make this dynamic
args = append(args, "--watch-cache=false") argsMap["watch-cache"] = "false"
args = append(args, "--cert-dir", certDir) argsMap["cert-dir"] = certDir
args = append(args, "--allow-privileged=true") argsMap["allow-privileged"] = "true"
args = append(args, "--authorization-mode", strings.Join([]string{modes.ModeNode, modes.ModeRBAC}, ",")) argsMap["authorization-mode"] = strings.Join([]string{modes.ModeNode, modes.ModeRBAC}, ",")
args = append(args, "--service-account-signing-key-file", runtime.ServiceKey) argsMap["service-account-signing-key-file"] = runtime.ServiceKey
args = append(args, "--service-cluster-ip-range", cfg.ServiceIPRange.String()) argsMap["service-cluster-ip-range"] = cfg.ServiceIPRange.String()
args = append(args, "--advertise-port", strconv.Itoa(cfg.AdvertisePort)) argsMap["advertise-port"] = strconv.Itoa(cfg.AdvertisePort)
args = append(args, "--advertise-address", localhostIP.String()) argsMap["advertise-address"] = localhostIP.String()
args = append(args, "--insecure-port", "0") argsMap["insecure-port"] = "0"
args = append(args, "--secure-port", strconv.Itoa(cfg.ListenPort)) argsMap["secure-port"] = strconv.Itoa(cfg.ListenPort)
args = append(args, "--bind-address", localhostIP.String()) argsMap["bind-address"] = localhostIP.String()
args = append(args, "--tls-cert-file", runtime.TLSCert) argsMap["tls-cert-file"] = runtime.TLSCert
args = append(args, "--tls-private-key-file", runtime.TLSKey) argsMap["tls-private-key-file"] = runtime.TLSKey
args = append(args, "--service-account-key-file", runtime.ServiceKey) argsMap["service-account-key-file"] = runtime.ServiceKey
args = append(args, "--service-account-issuer", "k3s") argsMap["service-account-issuer"] = "k3s"
args = append(args, "--api-audiences", "unknown") argsMap["api-audiences"] = "unknown"
args = append(args, "--basic-auth-file", runtime.PasswdFile) argsMap["basic-auth-file"] = runtime.PasswdFile
args = append(args, "--kubelet-client-certificate", runtime.NodeCert) argsMap["kubelet-client-certificate"] = runtime.NodeCert
args = append(args, "--kubelet-client-key", runtime.NodeKey) argsMap["kubelet-client-key"] = runtime.NodeKey
args = append(args, cfg.ExtraAPIArgs...) args := config.GetArgsList(argsMap, cfg.ExtraAPIArgs)
command := app.NewAPIServerCommand(ctx.Done()) command := app.NewAPIServerCommand(ctx.Done())
command.SetArgs(args) command.SetArgs(args)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment