-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedded.go
144 lines (123 loc) · 3.46 KB
/
embedded.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
package embedded
import (
"context"
"fmt"
"os"
"strings"
"github.com/coreos/etcd/etcdmain"
"github.com/rancher/rancher/pkg/hyperkube"
"github.com/rancher/rancher/pkg/k8scheck"
"github.com/rancher/rancher/pkg/librke"
"github.com/rancher/rancher/pkg/rkecerts"
"github.com/rancher/rke/services"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/tools/clientcmd"
)
var (
copyProcesses = []string{
services.KubeAPIContainerName,
services.SchedulerContainerName,
services.KubeControllerContainerName,
services.EtcdContainerName,
}
)
func Run(ctx context.Context) (context.Context, string, error) {
rkeConfig, err := localConfig()
if err != nil {
return ctx, "", err
}
bundle, err := rkecerts.Stage(rkeConfig)
if err != nil {
return ctx, "", err
}
plan, err := librke.New().GeneratePlan(ctx, rkeConfig, nil)
if err != nil {
return ctx, "", err
}
processes := getProcesses(plan)
eg, resultCtx := errgroup.WithContext(ctx)
for name, process := range processes {
runFn := func(ctx context.Context, args []string) {
runK8s(ctx, bundle.KubeConfig(), args)
}
if name == "etcd" {
runFn = runEtcd
}
eg.Go(runProcessFunc(ctx, name, process, runFn))
}
return resultCtx, bundle.KubeConfig(), nil
}
func runProcessFunc(ctx context.Context, name string, process v3.Process, f func(context.Context, []string)) func() error {
return func() error {
runProcess(ctx, name, process, f)
return fmt.Errorf("%s exited", name)
}
}
func setEnv(env []string) {
for _, part := range env {
parts := strings.SplitN(part, "=", 2)
if len(parts) == 1 {
os.Setenv(parts[0], "")
} else {
os.Setenv(parts[0], parts[1])
}
}
}
func runEtcd(ctx context.Context, args []string) {
os.Args = args
logrus.Info("Running ", strings.Join(args, " "))
etcdmain.Main()
logrus.Errorf("etcd exited")
}
func runK8s(ctx context.Context, kubeConfig string, args []string) {
if logrus.GetLevel() != logrus.DebugLevel {
args = append(args, "-v=1")
}
args = append(args, "--logtostderr=false")
args = append(args, "--alsologtostderr=false")
if args[0] != "kube-apiserver" {
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
logrus.Errorf("Failed to build client: %v", err)
return
}
if err := k8scheck.Wait(ctx, *restConfig); err != nil {
logrus.Errorf("Failed to build client: %v", err)
return
}
}
hk := hyperkube.HyperKube{
Name: "hyperkube",
Long: "This is an all-in-one binary that can run any of the various Kubernetes servers.",
}
hk.AddServer(hyperkube.NewKubeAPIServer())
hk.AddServer(hyperkube.NewKubeControllerManager())
hk.AddServer(hyperkube.NewScheduler())
logrus.Info("Running ", strings.Join(args, " "))
if err := hk.Run(args, ctx.Done()); err != nil {
logrus.Errorf("%s exited with error: %v", args[0], err)
}
}
func runProcess(ctx context.Context, name string, p v3.Process, f func(context.Context, []string)) {
env := append([]string{}, os.Environ()...)
env = append(env, p.Env...)
args := append([]string{}, p.Command...)
args = append(args, p.Args...)
for i, part := range args {
if strings.HasPrefix(part, "-") {
args = append([]string{name}, args[i:]...)
break
}
}
setEnv(env)
f(ctx, args)
}
func getProcesses(plan v3.RKEPlan) map[string]v3.Process {
processes := map[string]v3.Process{}
for _, name := range copyProcesses {
processes[name] = plan.Nodes[0].Processes[name]
}
return processes
}