• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zalando-incubator / stackset-controller / 5950695477

23 Aug 2023 11:27AM UTC coverage: 73.774% (-1.3%) from 75.025%
5950695477

Pull #521

github

mikkeloscar
Drop HorizontalPodAutoscaler field

Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
Pull Request #521: Drop HorizontalPodAutoscaler field

11 of 11 new or added lines in 3 files covered. (100.0%)

2197 of 2978 relevant lines covered (73.77%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.0
/pkg/core/stack_resources.go
1
package core
2

3
import (
4
        "fmt"
5
        "sort"
6
        "strconv"
7
        "strings"
8

9
        log "github.com/sirupsen/logrus"
10
        rgv1 "github.com/szuecs/routegroup-client/apis/zalando.org/v1"
11
        zv1 "github.com/zalando-incubator/stackset-controller/pkg/apis/zalando.org/v1"
12
        appsv1 "k8s.io/api/apps/v1"
13
        autoscaling "k8s.io/api/autoscaling/v2"
14
        v1 "k8s.io/api/core/v1"
15
        networking "k8s.io/api/networking/v1"
16
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
        "k8s.io/apimachinery/pkg/labels"
18
        "k8s.io/apimachinery/pkg/util/intstr"
19
        "k8s.io/apimachinery/pkg/util/sets"
20
)
21

22
const (
23
        apiVersionAppsV1 = "apps/v1"
24
        kindDeployment   = "Deployment"
25
)
26

27
type ingressOrRouteGroupSpec interface {
28
        GetAnnotations() map[string]string
29
        GetHosts() []string
30
}
31

32
var (
33
        // set implementation with 0 Byte value
34
        selectorLabels = map[string]struct{}{
35
                StacksetHeritageLabelKey: {},
36
                StackVersionLabelKey:     {},
37
        }
38

39
        // PathTypeImplementationSpecific is the used implementation path type
40
        // for k8s.io/api/networking/v1.HTTPIngressPath resources.
41
        PathTypeImplementationSpecific = networking.PathTypeImplementationSpecific
42
)
43

44
func mapCopy(m map[string]string) map[string]string {
1✔
45
        newMap := map[string]string{}
1✔
46
        for k, v := range m {
2✔
47
                newMap[k] = v
1✔
48
        }
1✔
49
        return newMap
1✔
50
}
51

52
// limitLabels returns a limited set of labels based on the validKeys.
53
func limitLabels(labels map[string]string, validKeys map[string]struct{}) map[string]string {
1✔
54
        newLabels := make(map[string]string, len(labels))
1✔
55
        for k, v := range labels {
2✔
56
                if _, ok := validKeys[k]; ok {
2✔
57
                        newLabels[k] = v
1✔
58
                }
1✔
59
        }
60
        return newLabels
1✔
61
}
62

63
// templateObjectMetaInjectLabels injects labels into a pod template spec.
64
func objectMetaInjectLabels(objectMeta metav1.ObjectMeta, labels map[string]string) metav1.ObjectMeta {
1✔
65
        if objectMeta.Labels == nil {
2✔
66
                objectMeta.Labels = map[string]string{}
1✔
67
        }
1✔
68
        for key, value := range labels {
2✔
69
                if _, ok := objectMeta.Labels[key]; !ok {
2✔
70
                        objectMeta.Labels[key] = value
1✔
71
                }
1✔
72
        }
73
        return objectMeta
1✔
74
}
75

76
func (sc *StackContainer) resourceMeta() metav1.ObjectMeta {
1✔
77
        resourceLabels := mapCopy(sc.Stack.Labels)
1✔
78

1✔
79
        return metav1.ObjectMeta{
1✔
80
                Name:      sc.Name(),
1✔
81
                Namespace: sc.Namespace(),
1✔
82
                Annotations: map[string]string{
1✔
83
                        stackGenerationAnnotationKey: strconv.FormatInt(sc.Stack.Generation, 10),
1✔
84
                },
1✔
85
                Labels: resourceLabels,
1✔
86
                OwnerReferences: []metav1.OwnerReference{
1✔
87
                        {
1✔
88
                                APIVersion: APIVersion,
1✔
89
                                Kind:       KindStack,
1✔
90
                                Name:       sc.Name(),
1✔
91
                                UID:        sc.Stack.UID,
1✔
92
                        },
1✔
93
                },
1✔
94
        }
1✔
95
}
1✔
96

97
// getServicePorts gets the service ports to be used for the stack service.
98
func getServicePorts(stackSpec zv1.StackSpec, backendPort *intstr.IntOrString) ([]v1.ServicePort, error) {
1✔
99
        var servicePorts []v1.ServicePort
1✔
100
        if stackSpec.Service == nil || len(stackSpec.Service.Ports) == 0 {
2✔
101
                servicePorts = servicePortsFromContainers(stackSpec.PodTemplate.Spec.Containers)
1✔
102
        } else {
2✔
103
                servicePorts = stackSpec.Service.Ports
1✔
104
        }
1✔
105

106
        // validate that one port in the list maps to the backendPort.
107
        if backendPort != nil {
2✔
108
                for _, port := range servicePorts {
2✔
109
                        switch backendPort.Type {
1✔
110
                        case intstr.Int:
1✔
111
                                if port.Port == backendPort.IntVal {
2✔
112
                                        return servicePorts, nil
1✔
113
                                }
1✔
114
                        case intstr.String:
1✔
115
                                if port.Name == backendPort.StrVal {
2✔
116
                                        return servicePorts, nil
1✔
117
                                }
1✔
118
                        }
119
                }
120

121
                return nil, fmt.Errorf("no service ports matching backendPort '%s'", backendPort.String())
1✔
122
        }
123

124
        return servicePorts, nil
1✔
125
}
126

127
// servicePortsFromTemplate gets service port from pod template.
128
func servicePortsFromContainers(containers []v1.Container) []v1.ServicePort {
1✔
129
        ports := make([]v1.ServicePort, 0)
1✔
130
        for i, container := range containers {
2✔
131
                for j, port := range container.Ports {
2✔
132
                        name := fmt.Sprintf("port-%d-%d", i, j)
1✔
133
                        if port.Name != "" {
2✔
134
                                name = port.Name
1✔
135
                        }
1✔
136
                        servicePort := v1.ServicePort{
1✔
137
                                Name:       name,
1✔
138
                                Protocol:   port.Protocol,
1✔
139
                                Port:       port.ContainerPort,
1✔
140
                                TargetPort: intstr.FromInt(int(port.ContainerPort)),
1✔
141
                        }
1✔
142
                        // set default protocol if not specified
1✔
143
                        if servicePort.Protocol == "" {
2✔
144
                                servicePort.Protocol = v1.ProtocolTCP
1✔
145
                        }
1✔
146
                        ports = append(ports, servicePort)
1✔
147
                }
148
        }
149
        return ports
1✔
150
}
151

152
func (sc *StackContainer) selector() map[string]string {
1✔
153
        return limitLabels(sc.Stack.Labels, selectorLabels)
1✔
154
}
1✔
155

156
func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
1✔
157
        stack := sc.Stack
1✔
158

1✔
159
        desiredReplicas := sc.stackReplicas
1✔
160
        if sc.prescalingActive {
2✔
161
                desiredReplicas = sc.prescalingReplicas
1✔
162
        }
1✔
163

164
        var updatedReplicas *int32
1✔
165

1✔
166
        if desiredReplicas != 0 && !sc.ScaledDown() {
2✔
167
                // Stack scaled up, rescale the deployment if it's at 0 replicas, or if HPA is unused and we don't run autoscaling
1✔
168
                if sc.deploymentReplicas == 0 || (!sc.IsAutoscaled() && desiredReplicas != sc.deploymentReplicas) {
2✔
169
                        updatedReplicas = wrapReplicas(desiredReplicas)
1✔
170
                }
1✔
171
        } else {
1✔
172
                // Stack scaled down (manually or because it doesn't receive traffic), check if we need to scale down the deployment
1✔
173
                if sc.deploymentReplicas != 0 {
2✔
174
                        updatedReplicas = wrapReplicas(0)
1✔
175
                }
1✔
176
        }
177

178
        if updatedReplicas == nil {
2✔
179
                updatedReplicas = wrapReplicas(sc.deploymentReplicas)
1✔
180
        }
1✔
181

182
        var strategy *appsv1.DeploymentStrategy
1✔
183
        if stack.Spec.Strategy != nil {
2✔
184
                strategy = stack.Spec.Strategy.DeepCopy()
1✔
185
        }
1✔
186

187
        embeddedCopy := stack.Spec.PodTemplate.EmbeddedObjectMeta.DeepCopy()
1✔
188

1✔
189
        templateObjectMeta := metav1.ObjectMeta{
1✔
190
                Annotations: embeddedCopy.Annotations,
1✔
191
                Labels:      embeddedCopy.Labels,
1✔
192
        }
1✔
193

1✔
194
        deployment := &appsv1.Deployment{
1✔
195
                ObjectMeta: sc.resourceMeta(),
1✔
196
                Spec: appsv1.DeploymentSpec{
1✔
197
                        Replicas:        updatedReplicas,
1✔
198
                        MinReadySeconds: sc.Stack.Spec.MinReadySeconds,
1✔
199
                        Selector: &metav1.LabelSelector{
1✔
200
                                MatchLabels: sc.selector(),
1✔
201
                        },
1✔
202
                        Template: v1.PodTemplateSpec{
1✔
203
                                ObjectMeta: objectMetaInjectLabels(templateObjectMeta, stack.Labels),
1✔
204
                                Spec:       *stack.Spec.PodTemplate.Spec.DeepCopy(),
1✔
205
                        },
1✔
206
                },
1✔
207
        }
1✔
208
        if strategy != nil {
2✔
209
                deployment.Spec.Strategy = *strategy
1✔
210
        }
1✔
211
        return deployment
1✔
212
}
213

214
func (sc *StackContainer) GenerateHPA() (*autoscaling.HorizontalPodAutoscaler, error) {
1✔
215
        autoscalerSpec := sc.Stack.Spec.Autoscaler
1✔
216
        trafficWeight := sc.actualTrafficWeight
1✔
217

1✔
218
        if autoscalerSpec == nil {
1✔
219
                return nil, nil
×
220
        }
×
221

222
        result := &autoscaling.HorizontalPodAutoscaler{
1✔
223
                ObjectMeta: sc.resourceMeta(),
1✔
224
                TypeMeta: metav1.TypeMeta{
1✔
225
                        Kind:       "HorizontalPodAutoscaler",
1✔
226
                        APIVersion: "autoscaling/v2",
1✔
227
                },
1✔
228
                Spec: autoscaling.HorizontalPodAutoscalerSpec{
1✔
229
                        ScaleTargetRef: autoscaling.CrossVersionObjectReference{
1✔
230
                                APIVersion: apiVersionAppsV1,
1✔
231
                                Kind:       kindDeployment,
1✔
232
                                Name:       sc.Name(),
1✔
233
                        },
1✔
234
                },
1✔
235
        }
1✔
236

1✔
237
        result.Spec.MinReplicas = autoscalerSpec.MinReplicas
1✔
238
        result.Spec.MaxReplicas = autoscalerSpec.MaxReplicas
1✔
239

1✔
240
        metrics, annotations, err := convertCustomMetrics(sc.stacksetName, sc.Name(), sc.Namespace(), autoscalerSpec.Metrics, trafficWeight)
1✔
241
        if err != nil {
1✔
242
                return nil, err
×
243
        }
×
244
        result.Spec.Metrics = metrics
1✔
245
        result.Annotations = mergeLabels(result.Annotations, annotations)
1✔
246
        result.Spec.Behavior = autoscalerSpec.Behavior
1✔
247

1✔
248
        // If prescaling is enabled, ensure we have at least `precalingReplicas` pods
1✔
249
        if sc.prescalingActive && (result.Spec.MinReplicas == nil || *result.Spec.MinReplicas < sc.prescalingReplicas) {
1✔
250
                pr := sc.prescalingReplicas
×
251
                result.Spec.MinReplicas = &pr
×
252
        }
×
253

254
        return result, nil
1✔
255
}
256

257
func (sc *StackContainer) GenerateService() (*v1.Service, error) {
1✔
258
        // get service ports to be used for the service
1✔
259
        var backendPort *intstr.IntOrString
1✔
260
        // Ingress or external managed Ingress
1✔
261
        if sc.HasBackendPort() {
1✔
262
                backendPort = sc.backendPort
×
263
        }
×
264

265
        servicePorts, err := getServicePorts(sc.Stack.Spec, backendPort)
1✔
266
        if err != nil {
1✔
267
                return nil, err
×
268
        }
×
269

270
        metaObj := sc.resourceMeta()
1✔
271
        stackSpec := sc.Stack.Spec
1✔
272
        if stackSpec.Service != nil {
2✔
273
                metaObj.Annotations = mergeLabels(metaObj.Annotations, stackSpec.Service.Annotations)
1✔
274
        }
1✔
275
        return &v1.Service{
1✔
276
                ObjectMeta: metaObj,
1✔
277
                Spec: v1.ServiceSpec{
1✔
278
                        Selector: sc.selector(),
1✔
279
                        Type:     v1.ServiceTypeClusterIP,
1✔
280
                        Ports:    servicePorts,
1✔
281
                },
1✔
282
        }, nil
1✔
283
}
284

285
func (sc *StackContainer) stackHostnames(spec ingressOrRouteGroupSpec, overrides *zv1.StackIngressRouteGroupOverrides) ([]string, error) {
1✔
286
        result := sets.NewString()
1✔
287

1✔
288
        if overrides != nil && len(overrides.Hosts) > 0 {
2✔
289
                for _, host := range overrides.Hosts {
2✔
290
                        interpolated := strings.ReplaceAll(host, "$(STACK_NAME)", sc.Name())
1✔
291
                        if interpolated == host {
2✔
292
                                return nil, fmt.Errorf("override hostname must contain $(STACK_NAME)")
1✔
293
                        }
1✔
294
                        result.Insert(interpolated)
1✔
295
                }
296
        } else {
1✔
297
                // Old-style autogenerated hostnames
1✔
298
                for _, host := range spec.GetHosts() {
2✔
299
                        for _, domain := range sc.clusterDomains {
2✔
300
                                if strings.HasSuffix(host, domain) {
2✔
301
                                        result.Insert(fmt.Sprintf("%s.%s", sc.Name(), domain))
1✔
302
                                } else {
2✔
303
                                        log.Debugf("Ingress host: %s suffix did not match cluster-domain %s", host, domain)
1✔
304
                                }
1✔
305
                        }
306
                }
307
        }
308

309
        return result.List(), nil
1✔
310
}
311

312
func effectiveAnnotations(spec ingressOrRouteGroupSpec, overrides *zv1.StackIngressRouteGroupOverrides) map[string]string {
1✔
313
        if overrides != nil && len(overrides.Annotations) > 0 {
2✔
314
                return overrides.Annotations
1✔
315
        }
1✔
316
        return spec.GetAnnotations()
1✔
317
}
318

319
func (sc *StackContainer) GenerateIngress() (*networking.Ingress, error) {
1✔
320
        if !sc.HasBackendPort() || sc.ingressSpec == nil || !sc.Stack.Spec.IngressOverrides.IsEnabled() {
2✔
321
                return nil, nil
1✔
322
        }
1✔
323

324
        hostnames, err := sc.stackHostnames(sc.ingressSpec, sc.Stack.Spec.IngressOverrides)
1✔
325
        if err != nil {
2✔
326
                return nil, err
1✔
327
        }
1✔
328
        if len(hostnames) == 0 {
1✔
329
                return nil, nil
×
330
        }
×
331

332
        rules := make([]networking.IngressRule, 0, len(hostnames))
1✔
333
        for _, hostname := range hostnames {
2✔
334
                rules = append(rules, networking.IngressRule{
1✔
335
                        IngressRuleValue: networking.IngressRuleValue{
1✔
336
                                HTTP: &networking.HTTPIngressRuleValue{
1✔
337
                                        Paths: []networking.HTTPIngressPath{
1✔
338
                                                {
1✔
339
                                                        PathType: &PathTypeImplementationSpecific,
1✔
340
                                                        Path:     sc.ingressSpec.Path,
1✔
341
                                                        Backend: networking.IngressBackend{
1✔
342
                                                                Service: &networking.IngressServiceBackend{
1✔
343
                                                                        Name: sc.Name(),
1✔
344
                                                                        Port: networking.ServiceBackendPort{
1✔
345
                                                                                Name:   sc.backendPort.StrVal,
1✔
346
                                                                                Number: sc.backendPort.IntVal,
1✔
347
                                                                        },
1✔
348
                                                                },
1✔
349
                                                        },
1✔
350
                                                },
1✔
351
                                        },
1✔
352
                                },
1✔
353
                        },
1✔
354
                        Host: hostname,
1✔
355
                })
1✔
356
        }
1✔
357

358
        // sort rules by hostname for a stable order
359
        sort.Slice(rules, func(i, j int) bool {
2✔
360
                return rules[i].Host < rules[j].Host
1✔
361
        })
1✔
362

363
        result := &networking.Ingress{
1✔
364
                ObjectMeta: sc.resourceMeta(),
1✔
365
                Spec: networking.IngressSpec{
1✔
366
                        Rules: rules,
1✔
367
                },
1✔
368
        }
1✔
369

1✔
370
        // insert annotations
1✔
371
        result.Annotations = mergeLabels(result.Annotations, effectiveAnnotations(sc.ingressSpec, sc.Stack.Spec.IngressOverrides))
1✔
372
        return result, nil
1✔
373
}
374

375
func (sc *StackContainer) GenerateRouteGroup() (*rgv1.RouteGroup, error) {
1✔
376
        if !sc.HasBackendPort() || sc.routeGroupSpec == nil || !sc.Stack.Spec.RouteGroupOverrides.IsEnabled() {
2✔
377
                return nil, nil
1✔
378
        }
1✔
379

380
        hostnames, err := sc.stackHostnames(sc.routeGroupSpec, sc.Stack.Spec.RouteGroupOverrides)
1✔
381
        if err != nil {
2✔
382
                return nil, err
1✔
383
        }
1✔
384
        if len(hostnames) == 0 {
1✔
385
                return nil, nil
×
386
        }
×
387

388
        result := &rgv1.RouteGroup{
1✔
389
                ObjectMeta: sc.resourceMeta(),
1✔
390
                Spec: rgv1.RouteGroupSpec{
1✔
391
                        Hosts: hostnames,
1✔
392
                        Backends: []rgv1.RouteGroupBackend{
1✔
393
                                {
1✔
394
                                        Name:        sc.Name(),
1✔
395
                                        Type:        rgv1.ServiceRouteGroupBackend,
1✔
396
                                        ServiceName: sc.Name(),
1✔
397
                                        ServicePort: sc.backendPort.IntValue(),
1✔
398
                                        Algorithm:   sc.routeGroupSpec.LBAlgorithm,
1✔
399
                                },
1✔
400
                        },
1✔
401
                        DefaultBackends: []rgv1.RouteGroupBackendReference{
1✔
402
                                {
1✔
403
                                        BackendName: sc.Name(),
1✔
404
                                        Weight:      100,
1✔
405
                                },
1✔
406
                        },
1✔
407
                        Routes: sc.routeGroupSpec.Routes,
1✔
408
                },
1✔
409
        }
1✔
410

1✔
411
        // validate not overlapping with main backend
1✔
412
        for _, backend := range sc.routeGroupSpec.AdditionalBackends {
1✔
413
                if backend.Name == sc.Name() {
×
414
                        return nil, fmt.Errorf("invalid additionalBackend '%s', overlaps with Stack name", backend.Name)
×
415
                }
×
416
                if backend.ServiceName == sc.Name() {
×
417
                        return nil, fmt.Errorf("invalid additionalBackend '%s', serviceName '%s' overlaps with Stack name", backend.Name, backend.ServiceName)
×
418
                }
×
419
                result.Spec.Backends = append(result.Spec.Backends, backend)
×
420
        }
421

422
        // sort backends to ensure have a consistent generated RoutGroup resource
423
        sort.Slice(result.Spec.Backends, func(i, j int) bool {
1✔
424
                return result.Spec.Backends[i].Name < result.Spec.Backends[j].Name
×
425
        })
×
426

427
        // insert annotations
428
        result.Annotations = mergeLabels(result.Annotations, effectiveAnnotations(sc.routeGroupSpec, sc.Stack.Spec.RouteGroupOverrides))
1✔
429

1✔
430
        return result, nil
1✔
431
}
432

433
func (sc *StackContainer) GenerateStackStatus() *zv1.StackStatus {
1✔
434
        prescaling := zv1.PrescalingStatus{}
1✔
435
        if sc.prescalingActive {
2✔
436
                prescaling = zv1.PrescalingStatus{
1✔
437
                        Active:               sc.prescalingActive,
1✔
438
                        Replicas:             sc.prescalingReplicas,
1✔
439
                        DesiredTrafficWeight: sc.prescalingDesiredTrafficWeight,
1✔
440
                        LastTrafficIncrease:  wrapTime(sc.prescalingLastTrafficIncrease),
1✔
441
                }
1✔
442
        }
1✔
443
        return &zv1.StackStatus{
1✔
444
                ActualTrafficWeight:  sc.actualTrafficWeight,
1✔
445
                DesiredTrafficWeight: sc.desiredTrafficWeight,
1✔
446
                Replicas:             sc.createdReplicas,
1✔
447
                ReadyReplicas:        sc.readyReplicas,
1✔
448
                UpdatedReplicas:      sc.updatedReplicas,
1✔
449
                DesiredReplicas:      sc.deploymentReplicas,
1✔
450
                Prescaling:           prescaling,
1✔
451
                NoTrafficSince:       wrapTime(sc.noTrafficSince),
1✔
452
                LabelSelector:        labels.Set(sc.selector()).String(),
1✔
453
        }
1✔
454
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc