-
Notifications
You must be signed in to change notification settings - Fork 14
/
proxy_boot.go
153 lines (137 loc) · 3.38 KB
/
proxy_boot.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
149
150
151
152
153
package k8s
import (
"context"
"fmt"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/cke/op"
"github.com/cybozu-go/cke/op/common"
"k8s.io/client-go/tools/clientcmd"
)
const (
proxyKubeconfigPath = "/etc/kubernetes/proxy/kubeconfig"
proxyConfigPath = "/etc/kubernetes/proxy/config.yml"
)
type kubeProxyBootOp struct {
nodes []*cke.Node
cluster string
ap string
params cke.ProxyParams
step int
files *common.FilesBuilder
}
// KubeProxyBootOp returns an Operator to boot kube-proxy.
func KubeProxyBootOp(nodes []*cke.Node, cluster, ap string, params cke.ProxyParams) cke.Operator {
return &kubeProxyBootOp{
nodes: nodes,
ap: ap,
cluster: cluster,
params: params,
files: common.NewFilesBuilder(nodes),
}
}
func (o *kubeProxyBootOp) Name() string {
return "kube-proxy-bootstrap"
}
func (o *kubeProxyBootOp) NextCommand() cke.Commander {
switch o.step {
case 0:
o.step++
return common.ImagePullCommand(o.nodes, cke.KubernetesImage)
case 1:
o.step++
return prepareProxyFilesCommand{cluster: o.cluster, ap: o.ap, files: o.files, params: o.params}
case 2:
o.step++
return o.files
case 3:
o.step++
opts := []string{
"--tmpfs=/run",
"--privileged",
}
paramsMap := make(map[string]cke.ServiceParams)
for _, n := range o.nodes {
params := ProxyParams()
paramsMap[n.Address] = params
}
return common.RunContainerCommand(o.nodes, op.KubeProxyContainerName, cke.KubernetesImage,
common.WithOpts(opts),
common.WithParamsMap(paramsMap),
common.WithExtra(o.params.ServiceParams))
default:
return nil
}
}
func (o *kubeProxyBootOp) Targets() []string {
ips := make([]string, len(o.nodes))
for i, n := range o.nodes {
ips[i] = n.Address
}
return ips
}
type prepareProxyFilesCommand struct {
cluster string
ap string
files *common.FilesBuilder
params cke.ProxyParams
}
func (c prepareProxyFilesCommand) Run(ctx context.Context, inf cke.Infrastructure, _ string) error {
storage := inf.Storage()
ca, err := storage.GetCACertificate(ctx, "kubernetes")
if err != nil {
return err
}
g := func(ctx context.Context, n *cke.Node) ([]byte, error) {
crt, key, err := cke.KubernetesCA{}.IssueForProxy(ctx, inf)
if err != nil {
return nil, err
}
cfg := proxyKubeconfig(c.cluster, ca, crt, key, c.ap)
return clientcmd.Write(*cfg)
}
if err := c.files.AddFile(ctx, proxyKubeconfigPath, g); err != nil {
return err
}
return c.files.AddFile(ctx, proxyConfigPath, func(ctx context.Context, n *cke.Node) ([]byte, error) {
cfg := GenerateProxyConfiguration(c.params, n)
return encodeToYAML(cfg)
})
}
func (c prepareProxyFilesCommand) Command() cke.Command {
return cke.Command{
Name: "prepare-proxy-files",
}
}
// ProxyParams returns parameters for kube-proxy.
func ProxyParams() cke.ServiceParams {
args := []string{
"kube-proxy",
fmt.Sprintf("--config=%s", proxyConfigPath),
}
return cke.ServiceParams{
ExtraArguments: args,
ExtraBinds: []cke.Mount{
{
Source: "/etc/machine-id",
Destination: "/etc/machine-id",
ReadOnly: true,
Propagation: "",
Label: "",
},
{
Source: "/etc/kubernetes",
Destination: "/etc/kubernetes",
ReadOnly: true,
Propagation: "",
Label: cke.LabelShared,
},
{
Source: "/lib/modules",
Destination: "/lib/modules",
ReadOnly: true,
Propagation: "",
Label: "",
},
},
}
}