2019-01-01 08:23:01 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2019-01-22 21:14:58 +00:00
|
|
|
"bufio"
|
2019-01-01 08:23:01 +00:00
|
|
|
"math/rand"
|
2019-01-22 21:14:58 +00:00
|
|
|
"os"
|
2019-01-09 16:54:15 +00:00
|
|
|
"path/filepath"
|
2019-01-22 21:14:58 +00:00
|
|
|
"strings"
|
2019-01-01 08:23:01 +00:00
|
|
|
"time"
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/system"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2020-04-27 17:09:58 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/executor"
|
2019-01-01 08:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-22 21:14:58 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/net"
|
2019-04-08 17:53:52 +00:00
|
|
|
"k8s.io/component-base/logs"
|
2019-04-26 22:02:30 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
|
2019-03-08 22:47:44 +00:00
|
|
|
|
2019-12-12 01:23:55 +00:00
|
|
|
_ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
|
|
|
|
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
|
2019-01-01 08:23:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Agent(config *config.Agent) error {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
2019-11-05 09:45:07 +00:00
|
|
|
logs.InitLogs()
|
|
|
|
defer logs.FlushLogs()
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
if err := startKubelet(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-27 16:31:25 +00:00
|
|
|
|
|
|
|
if !config.DisableKubeProxy {
|
|
|
|
return startKubeProxy(config)
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:31:25 +00:00
|
|
|
func startKubeProxy(cfg *config.Agent) error {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap := map[string]string{
|
|
|
|
"proxy-mode": "iptables",
|
|
|
|
"healthz-bind-address": "127.0.0.1",
|
2019-05-29 18:53:51 +00:00
|
|
|
"kubeconfig": cfg.KubeConfigKubeProxy,
|
2019-04-05 00:43:00 +00:00
|
|
|
"cluster-cidr": cfg.ClusterCIDR.String(),
|
|
|
|
}
|
2019-11-05 09:45:07 +00:00
|
|
|
if cfg.NodeName != "" {
|
|
|
|
argsMap["hostname-override"] = cfg.NodeName
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-11-05 09:45:07 +00:00
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraKubeProxyArgs)
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kube-proxy %s", config.ArgString(args))
|
|
|
|
return executor.KubeProxy(args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
func startKubelet(cfg *config.Agent) error {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap := map[string]string{
|
|
|
|
"healthz-bind-address": "127.0.0.1",
|
|
|
|
"read-only-port": "0",
|
2019-04-12 06:06:35 +00:00
|
|
|
"cluster-domain": cfg.ClusterDomain,
|
2019-05-29 18:53:51 +00:00
|
|
|
"kubeconfig": cfg.KubeConfigKubelet,
|
2019-04-05 00:43:00 +00:00
|
|
|
"eviction-hard": "imagefs.available<5%,nodefs.available<5%",
|
|
|
|
"eviction-minimum-reclaim": "imagefs.available=10%,nodefs.available=10%",
|
|
|
|
"fail-swap-on": "false",
|
|
|
|
//"cgroup-root": "/k3s",
|
2019-04-10 18:09:38 +00:00
|
|
|
"cgroup-driver": "cgroupfs",
|
|
|
|
"authentication-token-webhook": "true",
|
2019-08-27 04:36:56 +00:00
|
|
|
"anonymous-auth": "false",
|
2019-04-26 22:02:30 +00:00
|
|
|
"authorization-mode": modes.ModeWebhook,
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2020-04-27 16:41:57 +00:00
|
|
|
if cfg.PodManifests != "" && argsMap["pod-manifest-path"] == "" {
|
|
|
|
argsMap["pod-manifest-path"] = cfg.PodManifests
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(argsMap["pod-manifest-path"], 0755); err != nil {
|
|
|
|
logrus.Errorf("Failed to mkdir %s: %v", argsMap["pod-manifest-path"], err)
|
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.RootDir != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["root-dir"] = cfg.RootDir
|
|
|
|
argsMap["cert-dir"] = filepath.Join(cfg.RootDir, "pki")
|
|
|
|
argsMap["seccomp-profile-root"] = filepath.Join(cfg.RootDir, "seccomp")
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.CNIConfDir != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["cni-conf-dir"] = cfg.CNIConfDir
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.CNIBinDir != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["cni-bin-dir"] = cfg.CNIBinDir
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-04-30 20:12:02 +00:00
|
|
|
if cfg.CNIPlugin {
|
|
|
|
argsMap["network-plugin"] = "cni"
|
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
if len(cfg.ClusterDNS) > 0 {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["cluster-dns"] = cfg.ClusterDNS.String()
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-03-26 22:15:16 +00:00
|
|
|
if cfg.ResolvConf != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["resolv-conf"] = cfg.ResolvConf
|
2019-03-26 22:15:16 +00:00
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.RuntimeSocket != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["container-runtime"] = "remote"
|
|
|
|
argsMap["container-runtime-endpoint"] = cfg.RuntimeSocket
|
2020-01-16 17:21:19 +00:00
|
|
|
argsMap["containerd"] = cfg.RuntimeSocket
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["serialize-image-pulls"] = "false"
|
2019-12-10 23:16:26 +00:00
|
|
|
} else if cfg.PauseImage != "" {
|
|
|
|
argsMap["pod-infra-container-image"] = cfg.PauseImage
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-09 16:54:15 +00:00
|
|
|
if cfg.ListenAddress != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["address"] = cfg.ListenAddress
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
if cfg.ClientCA != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["anonymous-auth"] = "false"
|
2019-05-29 18:53:51 +00:00
|
|
|
argsMap["client-ca-file"] = cfg.ClientCA
|
2019-04-19 18:20:34 +00:00
|
|
|
}
|
2019-05-29 18:53:51 +00:00
|
|
|
if cfg.ServingKubeletCert != "" && cfg.ServingKubeletKey != "" {
|
|
|
|
argsMap["tls-cert-file"] = cfg.ServingKubeletCert
|
|
|
|
argsMap["tls-private-key-file"] = cfg.ServingKubeletKey
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|
|
|
|
if cfg.NodeName != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["hostname-override"] = cfg.NodeName
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|
|
|
|
defaultIP, err := net.ChooseHostInterface()
|
|
|
|
if err != nil || defaultIP.String() != cfg.NodeIP {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["node-ip"] = cfg.NodeIP
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|
2019-04-12 23:45:59 +00:00
|
|
|
root, hasCFS, hasPIDs := checkCgroups()
|
2019-03-04 05:00:47 +00:00
|
|
|
if !hasCFS {
|
2019-01-22 21:14:58 +00:00
|
|
|
logrus.Warn("Disabling CPU quotas due to missing cpu.cfs_period_us")
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["cpu-cfs-quota"] = "false"
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2019-04-12 23:45:59 +00:00
|
|
|
if !hasPIDs {
|
|
|
|
logrus.Warn("Disabling pod PIDs limit feature due to missing cgroup pids support")
|
|
|
|
argsMap["cgroups-per-qos"] = "false"
|
|
|
|
argsMap["enforce-node-allocatable"] = ""
|
|
|
|
argsMap["feature-gates"] = addFeatureGate(argsMap["feature-gates"], "SupportPodPidsLimit=false")
|
|
|
|
}
|
2019-03-04 05:00:47 +00:00
|
|
|
if root != "" {
|
2019-04-05 00:43:00 +00:00
|
|
|
argsMap["runtime-cgroups"] = root
|
|
|
|
argsMap["kubelet-cgroups"] = root
|
2019-03-04 05:00:47 +00:00
|
|
|
}
|
2019-03-08 22:47:44 +00:00
|
|
|
if system.RunningInUserNS() {
|
2019-04-12 23:45:59 +00:00
|
|
|
argsMap["feature-gates"] = addFeatureGate(argsMap["feature-gates"], "DevicePlugins=false")
|
2019-03-08 22:47:44 +00:00
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2019-05-07 23:47:07 +00:00
|
|
|
argsMap["node-labels"] = strings.Join(cfg.NodeLabels, ",")
|
|
|
|
if len(cfg.NodeTaints) > 0 {
|
|
|
|
argsMap["register-with-taints"] = strings.Join(cfg.NodeTaints, ",")
|
|
|
|
}
|
2019-10-15 21:17:26 +00:00
|
|
|
if !cfg.DisableCCM {
|
|
|
|
argsMap["cloud-provider"] = "external"
|
|
|
|
}
|
|
|
|
|
2019-10-19 10:18:51 +00:00
|
|
|
if cfg.Rootless {
|
|
|
|
// flags are from https://github.com/rootless-containers/usernetes/blob/v20190826.0/boot/kubelet.sh
|
|
|
|
argsMap["cgroup-driver"] = "none"
|
|
|
|
argsMap["feature-gates=SupportNoneCgroupDriver"] = "true"
|
|
|
|
argsMap["cgroups-per-qos"] = "false"
|
|
|
|
argsMap["enforce-node-allocatable"] = ""
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:31:56 +00:00
|
|
|
if cfg.ProtectKernelDefaults {
|
|
|
|
argsMap["protect-kernel-defaults"] = "true"
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
args := config.GetArgsList(argsMap, cfg.ExtraKubeletArgs)
|
2020-04-27 17:09:58 +00:00
|
|
|
logrus.Infof("Running kubelet %s", config.ArgString(args))
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2020-04-27 17:09:58 +00:00
|
|
|
return executor.Kubelet(args)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
|
2019-04-12 23:45:59 +00:00
|
|
|
func addFeatureGate(current, new string) string {
|
|
|
|
if current == "" {
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
return current + "," + new
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCgroups() (root string, hasCFS bool, hasPIDs bool) {
|
2019-01-22 21:14:58 +00:00
|
|
|
f, err := os.Open("/proc/self/cgroup")
|
|
|
|
if err != nil {
|
2019-04-12 23:45:59 +00:00
|
|
|
return "", false, false
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(f)
|
|
|
|
for scan.Scan() {
|
|
|
|
parts := strings.Split(scan.Text(), ":")
|
|
|
|
if len(parts) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
systems := strings.Split(parts[1], ",")
|
|
|
|
for _, system := range systems {
|
2019-04-12 23:45:59 +00:00
|
|
|
if system == "pids" {
|
|
|
|
hasPIDs = true
|
|
|
|
} else if system == "cpu" {
|
2019-01-22 21:14:58 +00:00
|
|
|
p := filepath.Join("/sys/fs/cgroup", parts[1], parts[2], "cpu.cfs_period_us")
|
|
|
|
if _, err := os.Stat(p); err == nil {
|
2019-04-12 23:45:59 +00:00
|
|
|
hasCFS = true
|
2019-03-04 05:00:47 +00:00
|
|
|
}
|
2020-12-07 03:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Examine process ID 1 to see if there is a cgroup assigned to it.
|
|
|
|
// When we are not in a container, process 1 is likely to be systemd or some other service manager.
|
2020-12-08 18:26:54 +00:00
|
|
|
// It either lives at `/` or `/init.scope` according to https://man7.org/linux/man-pages/man7/systemd.special.7.html
|
2020-12-07 03:23:44 +00:00
|
|
|
// When containerized, process 1 will be generally be in a cgroup, otherwise, we may be running in
|
|
|
|
// a host PID scenario but we don't support this.
|
|
|
|
g, err := os.Open("/proc/1/cgroup")
|
|
|
|
if err != nil {
|
|
|
|
return "", false, false
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
root = ""
|
|
|
|
scan = bufio.NewScanner(g)
|
|
|
|
for scan.Scan() {
|
|
|
|
parts := strings.Split(scan.Text(), ":")
|
|
|
|
if len(parts) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
systems := strings.Split(parts[1], ",")
|
|
|
|
for _, system := range systems {
|
|
|
|
if system == "name=systemd" {
|
2019-03-04 05:00:47 +00:00
|
|
|
last := parts[len(parts)-1]
|
2020-12-08 18:26:54 +00:00
|
|
|
if last != "/" && last != "/init.scope" {
|
2019-08-02 06:52:21 +00:00
|
|
|
root = "/systemd"
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 23:45:59 +00:00
|
|
|
return root, hasCFS, hasPIDs
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|