/
util.go
160 lines (140 loc) · 4.3 KB
/
util.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// 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 k8s
import (
"time"
pbpod "github.com/uber/peloton/.gen/peloton/api/v1alpha/pod"
"github.com/pborman/uuid"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// K8S requires pod spec to contain an image name, default to busybox.
_defaultImageName = "busybox"
// K8S enforces minimum mem limit for container to be 4MB. KinD enforces
// this limit as 100MB.
_defaultMinMemMb = 100.0
)
// K8S node and pod informers will resync all nodes and pods at this
// interval. This will be used for reconciliation of pods and hostcache.
var _defaultResyncInterval = 30 * time.Second
// Convert peloton container specs to k8s container specs
func toK8SContainerSpecs(
containerSpecs []*pbpod.ContainerSpec,
) []corev1.Container {
var containers []corev1.Container
for _, c := range containerSpecs {
containers = append(containers, toK8SContainerSpec(c))
}
return containers
}
// Convert peloton container spec to k8s container spec
func toK8SContainerSpec(c *pbpod.ContainerSpec) corev1.Container {
// TODO:
// add ports, health check, readiness check, affinity
var kEnvs []corev1.EnvVar
for _, e := range c.GetEnvironment() {
kEnvs = append(kEnvs, corev1.EnvVar{
Name: e.GetName(),
Value: e.GetValue(),
})
}
var ports []corev1.ContainerPort
for _, p := range c.GetPorts() {
ports = append(ports, corev1.ContainerPort{
Name: p.GetName(),
ContainerPort: int32(p.GetValue()),
})
}
cname := c.GetName()
if cname == "" {
cname = uuid.New()
}
cimage := c.GetImage()
if cimage == "" {
cimage = _defaultImageName
}
memMb := c.GetResource().GetMemLimitMb()
if memMb < _defaultMinMemMb {
memMb = _defaultMinMemMb
}
volumeMounts := []corev1.VolumeMount{}
for _, v := range c.GetVolumeMounts() {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: v.GetName(),
ReadOnly: v.GetReadOnly(),
MountPath: v.GetMountPath(),
})
}
k8sSpec := corev1.Container{
Name: cname,
Image: cimage,
Env: kEnvs,
Ports: ports,
VolumeMounts: volumeMounts,
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewMilliQuantity(
int64(c.GetResource().GetCpuLimit()*1000),
resource.DecimalSI,
),
corev1.ResourceMemory: *resource.NewMilliQuantity(
int64(memMb*1000000000),
resource.DecimalSI,
),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewMilliQuantity(
int64(c.GetResource().GetCpuLimit()*1000),
resource.DecimalSI,
),
corev1.ResourceMemory: *resource.NewMilliQuantity(
int64(memMb*1000000000),
resource.DecimalSI,
),
},
},
}
if c.GetEntrypoint().GetValue() != "" {
k8sSpec.Command = []string{c.GetEntrypoint().GetValue()}
k8sSpec.Args = c.GetEntrypoint().GetArguments()
}
return k8sSpec
}
// Convert peloton podspec to k8s podspec.
func toK8SPodSpec(podSpec *pbpod.PodSpec) *corev1.Pod {
// Create pod template spec and apply configurations to spec.
labels := make(map[string]string)
for _, label := range podSpec.GetLabels() {
labels[label.GetKey()] = label.GetValue()
}
termGracePeriod := int64(podSpec.GetKillGracePeriodSeconds())
podTemp := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: toK8SContainerSpecs(podSpec.GetContainers()),
InitContainers: toK8SContainerSpecs(podSpec.GetInitContainers()),
RestartPolicy: "Never",
TerminationGracePeriodSeconds: &termGracePeriod,
},
}
// Bind node and create pod.
return &corev1.Pod{
ObjectMeta: podTemp.ObjectMeta,
Spec: podTemp.Spec,
}
}