-
Notifications
You must be signed in to change notification settings - Fork 27
/
stack_resources.go
316 lines (279 loc) · 8.64 KB
/
stack_resources.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package core
import (
"fmt"
"strconv"
zv1 "github.com/zalando-incubator/stackset-controller/pkg/apis/zalando.org/v1"
appsv1 "k8s.io/api/apps/v1"
autoscaling "k8s.io/api/autoscaling/v2beta1"
v1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
apiVersionAppsV1 = "apps/v1"
kindDeployment = "Deployment"
)
var (
// set implementation with 0 Byte value
selectorLabels = map[string]struct{}{
StacksetHeritageLabelKey: {},
StackVersionLabelKey: {},
}
)
func mapCopy(m map[string]string) map[string]string {
newMap := map[string]string{}
for k, v := range m {
newMap[k] = v
}
return newMap
}
// limitLabels returns a limited set of labels based on the validKeys.
func limitLabels(labels map[string]string, validKeys map[string]struct{}) map[string]string {
newLabels := make(map[string]string, len(labels))
for k, v := range labels {
if _, ok := validKeys[k]; ok {
newLabels[k] = v
}
}
return newLabels
}
// templateInjectLabels injects labels into a pod template spec.
func templateInjectLabels(template *v1.PodTemplateSpec, labels map[string]string) *v1.PodTemplateSpec {
if template.ObjectMeta.Labels == nil {
template.ObjectMeta.Labels = map[string]string{}
}
for key, value := range labels {
if _, ok := template.ObjectMeta.Labels[key]; !ok {
template.ObjectMeta.Labels[key] = value
}
}
return template
}
func (sc *StackContainer) resourceMeta() metav1.ObjectMeta {
resourceLabels := mapCopy(sc.Stack.Labels)
return metav1.ObjectMeta{
Name: sc.Name(),
Namespace: sc.Namespace(),
Annotations: map[string]string{
stackGenerationAnnotationKey: strconv.FormatInt(sc.Stack.Generation, 10),
},
Labels: resourceLabels,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: APIVersion,
Kind: KindStack,
Name: sc.Name(),
UID: sc.Stack.UID,
},
},
}
}
// getServicePorts gets the service ports to be used for the stack service.
func getServicePorts(stackSpec zv1.StackSpec, backendPort *intstr.IntOrString) ([]v1.ServicePort, error) {
var servicePorts []v1.ServicePort
if stackSpec.Service == nil || len(stackSpec.Service.Ports) == 0 {
servicePorts = servicePortsFromContainers(stackSpec.PodTemplate.Spec.Containers)
} else {
servicePorts = stackSpec.Service.Ports
}
// validate that one port in the list maps to the backendPort.
if backendPort != nil {
for _, port := range servicePorts {
switch backendPort.Type {
case intstr.Int:
if port.Port == backendPort.IntVal {
return servicePorts, nil
}
case intstr.String:
if port.Name == backendPort.StrVal {
return servicePorts, nil
}
}
}
return nil, fmt.Errorf("no service ports matching backendPort '%s'", backendPort.String())
}
return servicePorts, nil
}
// servicePortsFromTemplate gets service port from pod template.
func servicePortsFromContainers(containers []v1.Container) []v1.ServicePort {
ports := make([]v1.ServicePort, 0)
for i, container := range containers {
for j, port := range container.Ports {
name := fmt.Sprintf("port-%d-%d", i, j)
if port.Name != "" {
name = port.Name
}
servicePort := v1.ServicePort{
Name: name,
Protocol: port.Protocol,
Port: port.ContainerPort,
TargetPort: intstr.FromInt(int(port.ContainerPort)),
}
// set default protocol if not specified
if servicePort.Protocol == "" {
servicePort.Protocol = v1.ProtocolTCP
}
ports = append(ports, servicePort)
}
}
return ports
}
func (sc *StackContainer) selector() map[string]string {
return limitLabels(sc.Stack.Labels, selectorLabels)
}
func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
stack := sc.Stack
desiredReplicas := sc.stackReplicas
if sc.prescalingActive {
desiredReplicas = sc.prescalingReplicas
}
var updatedReplicas *int32
if desiredReplicas != 0 && !sc.ScaledDown() {
// Stack scaled up, rescale the deployment if it's at 0 replicas, or if HPA is unused and we don't run autoscaling
if sc.deploymentReplicas == 0 || (!sc.IsAutoscaled() && desiredReplicas != sc.deploymentReplicas) {
updatedReplicas = wrapReplicas(desiredReplicas)
}
} else {
// Stack scaled down (manually or because it doesn't receive traffic), check if we need to scale down the deployment
if sc.deploymentReplicas != 0 {
updatedReplicas = wrapReplicas(0)
}
}
if updatedReplicas == nil {
updatedReplicas = wrapReplicas(sc.deploymentReplicas)
}
var strategy *appsv1.DeploymentStrategy
if stack.Spec.Strategy != nil {
strategy = stack.Spec.Strategy.DeepCopy()
}
deployment := &appsv1.Deployment{
ObjectMeta: sc.resourceMeta(),
Spec: appsv1.DeploymentSpec{
Replicas: updatedReplicas,
Selector: &metav1.LabelSelector{
MatchLabels: sc.selector(),
},
Template: *templateInjectLabels(stack.Spec.PodTemplate.DeepCopy(), stack.Labels),
},
}
if strategy != nil {
deployment.Spec.Strategy = *strategy
}
return deployment
}
func (sc *StackContainer) GenerateHPA() (*autoscaling.HorizontalPodAutoscaler, error) {
autoscalerSpec := sc.Stack.Spec.Autoscaler
hpaSpec := sc.Stack.Spec.HorizontalPodAutoscaler
if autoscalerSpec == nil && hpaSpec == nil {
return nil, nil
}
result := &autoscaling.HorizontalPodAutoscaler{
ObjectMeta: sc.resourceMeta(),
TypeMeta: metav1.TypeMeta{
Kind: "HorizontalPodAutoscaler",
APIVersion: "autoscaling/v2beta1",
},
Spec: autoscaling.HorizontalPodAutoscalerSpec{
ScaleTargetRef: autoscaling.CrossVersionObjectReference{
APIVersion: apiVersionAppsV1,
Kind: kindDeployment,
Name: sc.Name(),
},
},
}
if autoscalerSpec != nil {
result.Spec.MinReplicas = autoscalerSpec.MinReplicas
result.Spec.MaxReplicas = autoscalerSpec.MaxReplicas
metrics, annotations, err := convertCustomMetrics(sc.stacksetName, sc.Name(), autoscalerSpec.Metrics)
if err != nil {
return nil, err
}
result.Spec.Metrics = metrics
result.Annotations = mergeLabels(result.Annotations, annotations)
} else {
result.Spec.MinReplicas = hpaSpec.MinReplicas
result.Spec.MaxReplicas = hpaSpec.MaxReplicas
result.Spec.Metrics = hpaSpec.Metrics
}
// If prescaling is enabled, ensure we have at least `precalingReplicas` pods
if sc.prescalingActive && (result.Spec.MinReplicas == nil || *result.Spec.MinReplicas < sc.prescalingReplicas) {
pr := sc.prescalingReplicas
result.Spec.MinReplicas = &pr
}
return result, nil
}
func (sc *StackContainer) GenerateService() (*v1.Service, error) {
// get service ports to be used for the service
var backendPort *intstr.IntOrString
// Ingress or external managed Ingress
if sc.HasBackendPort() {
backendPort = sc.backendPort
}
servicePorts, err := getServicePorts(sc.Stack.Spec, backendPort)
if err != nil {
return nil, err
}
return &v1.Service{
ObjectMeta: sc.resourceMeta(),
Spec: v1.ServiceSpec{
Selector: sc.selector(),
Type: v1.ServiceTypeClusterIP,
Ports: servicePorts,
},
}, nil
}
func (sc *StackContainer) GenerateIngress() (*extensions.Ingress, error) {
if !sc.HasBackendPort() || sc.ingressSpec == nil {
return nil, nil
}
result := &extensions.Ingress{
ObjectMeta: sc.resourceMeta(),
Spec: extensions.IngressSpec{
Rules: []extensions.IngressRule{
{
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: []extensions.HTTPIngressPath{
{
Path: sc.ingressSpec.Path,
Backend: extensions.IngressBackend{
ServiceName: sc.Name(),
ServicePort: *sc.backendPort,
},
},
},
},
},
Host: fmt.Sprintf("%s.%s", sc.Name(), sc.clusterDomain),
},
},
},
}
// insert annotations
result.Annotations = mergeLabels(result.Annotations, sc.ingressSpec.Annotations)
return result, nil
}
func (sc *StackContainer) GenerateStackStatus() *zv1.StackStatus {
prescaling := zv1.PrescalingStatus{}
if sc.prescalingActive {
prescaling = zv1.PrescalingStatus{
Active: sc.prescalingActive,
Replicas: sc.prescalingReplicas,
DesiredTrafficWeight: sc.prescalingDesiredTrafficWeight,
LastTrafficIncrease: wrapTime(sc.prescalingLastTrafficIncrease),
}
}
return &zv1.StackStatus{
ActualTrafficWeight: sc.actualTrafficWeight,
DesiredTrafficWeight: sc.desiredTrafficWeight,
Replicas: sc.createdReplicas,
ReadyReplicas: sc.readyReplicas,
UpdatedReplicas: sc.updatedReplicas,
DesiredReplicas: sc.deploymentReplicas,
Prescaling: prescaling,
NoTrafficSince: wrapTime(sc.noTrafficSince),
LabelSelector: labels.Set(sc.selector()).String(),
}
}