forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube_proxy.go
281 lines (237 loc) · 7.77 KB
/
kube_proxy.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package model
import (
"fmt"
"k8s.io/kops/pkg/dns"
"k8s.io/kops/pkg/flagbuilder"
"k8s.io/kops/pkg/k8scodecs"
"k8s.io/kops/pkg/kubemanifest"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
"k8s.io/kops/util/pkg/exec"
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// KubeProxyBuilder installs kube-proxy
type KubeProxyBuilder struct {
*NodeupModelContext
}
var _ fi.ModelBuilder = &KubeAPIServerBuilder{}
// Build is responsible for building the kube-proxy manifest
// @TODO we should probaby change this to a daemonset in the future and follow the kubeadm path
func (b *KubeProxyBuilder) Build(c *fi.ModelBuilderContext) error {
if b.Cluster.Spec.KubeProxy.Enabled != nil && *b.Cluster.Spec.KubeProxy.Enabled == false {
glog.V(2).Infof("Kube-proxy is disabled, will not create configuration for it.")
return nil
}
if b.IsMaster {
// If this is a master that is not isolated, run it as a normal node also (start kube-proxy etc)
// This lets e.g. daemonset pods communicate with other pods in the system
if fi.BoolValue(b.Cluster.Spec.IsolateMasters) {
glog.V(2).Infof("Running on Master with IsolateMaster=true; skipping kube-proxy installation")
return nil
}
}
{
pod, err := b.buildPod()
if err != nil {
return fmt.Errorf("error building kube-proxy manifest: %v", err)
}
manifest, err := k8scodecs.ToVersionedYaml(pod)
if err != nil {
return fmt.Errorf("error marshalling manifest to yaml: %v", err)
}
c.AddTask(&nodetasks.File{
Path: "/etc/kubernetes/manifests/kube-proxy.manifest",
Contents: fi.NewBytesResource(manifest),
Type: nodetasks.FileType_File,
})
}
{
kubeconfig, err := b.BuildPKIKubeconfig("kube-proxy")
if err != nil {
return err
}
c.AddTask(&nodetasks.File{
Path: "/var/lib/kube-proxy/kubeconfig",
Contents: fi.NewStringResource(kubeconfig),
Type: nodetasks.FileType_File,
Mode: s("0400"),
})
}
{
c.AddTask(&nodetasks.File{
Path: "/var/log/kube-proxy.log",
Contents: fi.NewStringResource(""),
Type: nodetasks.FileType_File,
Mode: s("0400"),
IfNotExists: true,
})
}
return nil
}
// buildPod is responsble constructing the pod spec
func (b *KubeProxyBuilder) buildPod() (*v1.Pod, error) {
c := b.Cluster.Spec.KubeProxy
if c == nil {
return nil, fmt.Errorf("KubeProxy not configured")
}
if c.Master == "" {
if b.IsMaster {
// As a special case, if this is the master, we point kube-proxy to the local IP
// This prevents a circular dependency where kube-proxy can't come up until DNS comes up,
// which would mean that DNS can't rely on API to come up
if b.IsKubernetesGTE("1.6") {
c.Master = "https://127.0.0.1"
} else {
c.Master = "http://127.0.0.1:8080"
}
} else {
c.Master = "https://" + b.Cluster.Spec.MasterInternalName
}
}
resourceRequests := v1.ResourceList{}
resourceLimits := v1.ResourceList{}
cpuRequest, err := resource.ParseQuantity(c.CPURequest)
if err != nil {
return nil, fmt.Errorf("Error parsing CPURequest=%q", c.CPURequest)
}
resourceRequests["cpu"] = cpuRequest
if c.CPULimit != "" {
cpuLimit, err := resource.ParseQuantity(c.CPULimit)
if err != nil {
return nil, fmt.Errorf("Error parsing CPULimit=%q", c.CPULimit)
}
resourceLimits["cpu"] = cpuLimit
}
if c.MemoryRequest != "" {
memoryRequest, err := resource.ParseQuantity(c.MemoryRequest)
if err != nil {
return nil, fmt.Errorf("Error parsing MemoryRequest=%q", c.MemoryRequest)
}
resourceRequests["memory"] = memoryRequest
}
if c.MemoryLimit != "" {
memoryLimit, err := resource.ParseQuantity(c.MemoryLimit)
if err != nil {
return nil, fmt.Errorf("Error parsing MemoryLimit=%q", c.MemoryLimit)
}
resourceLimits["memory"] = memoryLimit
}
if c.ConntrackMaxPerCore == nil {
defaultConntrackMaxPerCore := int32(131072)
c.ConntrackMaxPerCore = &defaultConntrackMaxPerCore
}
flags, err := flagbuilder.BuildFlagsList(c)
if err != nil {
return nil, fmt.Errorf("error building kubeproxy flags: %v", err)
}
image := c.Image
flags = append(flags, []string{
"--kubeconfig=/var/lib/kube-proxy/kubeconfig",
"--oom-score-adj=-998",
`--resource-container=""`}...)
container := &v1.Container{
Name: "kube-proxy",
Image: image,
Command: exec.WithTee(
"/usr/local/bin/kube-proxy",
sortedStrings(flags),
"/var/log/kube-proxy.log"),
Resources: v1.ResourceRequirements{
Requests: resourceRequests,
Limits: resourceLimits,
},
SecurityContext: &v1.SecurityContext{
Privileged: fi.Bool(true),
},
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-proxy",
Namespace: "kube-system",
Labels: map[string]string{
"k8s-app": "kube-proxy",
"tier": "node",
},
},
Spec: v1.PodSpec{
HostNetwork: true,
Tolerations: tolerateMasterTaints(),
},
}
{
addHostPathMapping(pod, container, "kubeconfig", "/var/lib/kube-proxy/kubeconfig")
addHostPathMapping(pod, container, "logfile", "/var/log/kube-proxy.log").ReadOnly = false
// @note: mapping the host modules directory to fix the missing ipvs kernel module
addHostPathMapping(pod, container, "modules", "/lib/modules")
// Map SSL certs from host: /usr/share/ca-certificates -> /etc/ssl/certs
sslCertsHost := addHostPathMapping(pod, container, "ssl-certs-hosts", "/usr/share/ca-certificates")
sslCertsHost.MountPath = "/etc/ssl/certs"
}
if dns.IsGossipHostname(b.Cluster.Name) {
// Map /etc/hosts from host, so that we see the updates that are made by protokube
addHostPathMapping(pod, container, "etchosts", "/etc/hosts")
}
// Mount the iptables lock file
if b.IsKubernetesGTE("1.9") {
addHostPathMapping(pod, container, "iptableslock", "/run/xtables.lock").ReadOnly = false
vol := pod.Spec.Volumes[len(pod.Spec.Volumes)-1]
if vol.Name != "iptableslock" {
// Sanity check
glog.Fatalf("expected volume to be last volume added")
}
hostPathType := v1.HostPathFileOrCreate
vol.HostPath.Type = &hostPathType
}
pod.Spec.Containers = append(pod.Spec.Containers, *container)
// Note that e.g. kubeadm has this as a daemonset, but this doesn't have a lot of test coverage AFAICT
//ServiceAccountName: "kube-proxy",
//d := &v1beta1.DaemonSet{
// ObjectMeta: metav1.ObjectMeta{
// Labels: map[string]string{
// "k8s-app": "kube-proxy",
// },
// Name: "kube-proxy",
// Namespace: "kube-proxy",
// },
// Spec: v1beta1.DeploymentSpec{
// Selector: &metav1.LabelSelector{
// MatchLabels: map[string]string{
// "k8s-app": "kube-proxy",
// },
// },
// Template: template,
// },
//}
// This annotation ensures that kube-proxy does not get evicted if the node
// supports critical pod annotation based priority scheme.
// Note that kube-proxy runs as a static pod so this annotation does NOT have
// any effect on rescheduler (default scheduler and rescheduler are not
// involved in scheduling kube-proxy).
kubemanifest.MarkPodAsCritical(pod)
return pod, nil
}
func tolerateMasterTaints() []v1.Toleration {
tolerations := []v1.Toleration{}
// As long as we are a static pod, we don't need any special tolerations
// {
// Key: MasterTaintKey,
// Effect: NoSchedule,
// },
//}
return tolerations
}