forked from k3s-io/k3s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
148 lines (134 loc) · 3.87 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package agent
import (
"bufio"
"context"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
"github.com/rancher/k3s/pkg/daemons/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/net"
"k8s.io/apiserver/pkg/util/logs"
app2 "k8s.io/kubernetes/cmd/kube-proxy/app"
"k8s.io/kubernetes/cmd/kubelet/app"
_ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration
_ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration
)
func Agent(config *config.Agent) error {
rand.Seed(time.Now().UTC().UnixNano())
kubelet(config)
kubeProxy(config)
return nil
}
func kubeProxy(config *config.Agent) {
args := []string{
"--proxy-mode", "iptables",
"--healthz-bind-address", "127.0.0.1",
"--kubeconfig", config.KubeConfig,
"--cluster-cidr", config.ClusterCIDR.String(),
}
args = append(args, config.ExtraKubeletArgs...)
command := app2.NewProxyCommand()
command.SetArgs(args)
go func() {
err := command.Execute()
logrus.Fatalf("kube-proxy exited: %v", err)
}()
}
func kubelet(cfg *config.Agent) {
command := app.NewKubeletCommand(context.Background().Done())
logs.InitLogs()
defer logs.FlushLogs()
args := []string{
"--healthz-bind-address", "127.0.0.1",
"--read-only-port", "0",
"--allow-privileged=true",
"--cluster-domain", "cluster.local",
"--kubeconfig", cfg.KubeConfig,
"--eviction-hard", "imagefs.available<5%,nodefs.available<5%",
"--eviction-minimum-reclaim", "imagefs.available=10%,nodefs.available=10%",
"--fail-swap-on=false",
//"--cgroup-root", "/k3s",
"--cgroup-driver", "cgroupfs",
}
if cfg.RootDir != "" {
args = append(args, "--root-dir", cfg.RootDir)
args = append(args, "--cert-dir", filepath.Join(cfg.RootDir, "pki"))
args = append(args, "--seccomp-profile-root", filepath.Join(cfg.RootDir, "seccomp"))
}
if cfg.CNIConfDir != "" {
args = append(args, "--cni-conf-dir", cfg.CNIConfDir)
}
if cfg.CNIBinDir != "" {
args = append(args, "--cni-bin-dir", cfg.CNIBinDir)
}
if len(cfg.ClusterDNS) > 0 {
args = append(args, "--cluster-dns", cfg.ClusterDNS.String())
}
if cfg.RuntimeSocket != "" {
args = append(args, "--container-runtime", "remote")
args = append(args, "--container-runtime-endpoint", cfg.RuntimeSocket)
}
if cfg.ListenAddress != "" {
args = append(args, "--address", cfg.ListenAddress)
}
if cfg.CACertPath != "" {
args = append(args, "--anonymous-auth=false", "--client-ca-file", cfg.CACertPath)
}
if cfg.NodeName != "" {
args = append(args, "--hostname-override", cfg.NodeName)
}
defaultIP, err := net.ChooseHostInterface()
if err != nil || defaultIP.String() != cfg.NodeIP {
args = append(args, "--node-ip", cfg.NodeIP)
}
root, hasCFS := checkCgroups()
if !hasCFS {
logrus.Warn("Disabling CPU quotas due to missing cpu.cfs_period_us")
args = append(args, "--cpu-cfs-quota=false")
}
if root != "" {
args = append(args, "--runtime-cgroups", root)
args = append(args, "--kubelet-cgroups", root)
}
args = append(args, cfg.ExtraKubeletArgs...)
command.SetArgs(args)
go func() {
logrus.Infof("Running kubelet %s", config.ArgString(args))
logrus.Fatalf("kubelet exited: %v", command.Execute())
}()
}
func checkCgroups() (string, bool) {
f, err := os.Open("/proc/self/cgroup")
if err != nil {
return "", false
}
defer f.Close()
ret := false
root := ""
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 {
if system == "cpu" {
p := filepath.Join("/sys/fs/cgroup", parts[1], parts[2], "cpu.cfs_period_us")
if _, err := os.Stat(p); err == nil {
ret = true
}
} else if system == "name=systemd" {
last := parts[len(parts)-1]
i := strings.LastIndex(last, ".slice")
if i > 0 {
root = "/systemd" + last[:i+len(".slice")]
}
}
}
}
return root, ret
}