forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
96 lines (78 loc) · 2.42 KB
/
worker.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
package rkenodeconfigserver
import (
"context"
"github.com/rancher/rancher/pkg/clusteryaml"
"github.com/rancher/rancher/pkg/rkecerts"
"github.com/rancher/rancher/pkg/rkeworker"
"github.com/rancher/rke/cluster"
"github.com/rancher/rke/services"
"github.com/rancher/types/apis/management.cattle.io/v3"
)
var (
copyProcesses = []string{
services.KubeletContainerName,
services.KubeproxyContainerName,
}
)
func buildRKEConfig(node v3.RKEConfigNode) (*v3.RancherKubernetesEngineConfig, error) {
rkeConfig, err := clusteryaml.LocalConfig()
if err != nil {
return nil, err
}
node.Role = []string{services.WorkerRole}
rkeConfig.Nodes = append(rkeConfig.Nodes, node)
return rkeConfig, err
}
func buildPlan(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig) (*v3.RKEConfigNodePlan, error) {
myCluster, err := cluster.ParseCluster(ctx, rkeConfig, "", "", nil, nil, nil)
if err != nil {
return nil, err
}
myCluster.WorkerHosts[0].DockerInfo.DockerRootDir = "/var/lib/docker"
nodePlan := cluster.BuildRKEConfigNodePlan(ctx, myCluster, myCluster.WorkerHosts[0])
return &nodePlan, nil
}
func buildProcesses(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig) (map[string]v3.Process, error) {
nodePlan, err := buildPlan(ctx, rkeConfig)
if err != nil {
return nil, err
}
return filterProcesses(*nodePlan), nil
}
func buildCerts(rkeConfig *v3.RancherKubernetesEngineConfig, node *v3.RKEConfigNode, server, token string) (string, error) {
bundle, err := rkecerts.Load()
if err != nil {
return "", err
}
nodeBundle, err := bundle.ForNode(rkeConfig, node, server, token)
if err != nil {
return "", err
}
return nodeBundle.Marshal()
}
func AgentConfig(ctx context.Context, node v3.RKEConfigNode, server, token string) (*rkeworker.NodeConfig, error) {
rkeConfig, err := buildRKEConfig(node)
if err != nil {
return nil, err
}
processes, err := buildProcesses(ctx, rkeConfig)
if err != nil {
return nil, err
}
certs, err := buildCerts(rkeConfig, &rkeConfig.Nodes[len(rkeConfig.Nodes)-1], server, token)
if err != nil {
return nil, err
}
return &rkeworker.NodeConfig{
APIProxyAddress: "10.233.0.1:6443",
Certs: certs,
Processes: processes,
}, nil
}
func filterProcesses(nodePlan v3.RKEConfigNodePlan) map[string]v3.Process {
processes := map[string]v3.Process{}
for _, name := range copyProcesses {
processes[name] = nodePlan.Processes[name]
}
return processes
}