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

zalando-incubator / stackset-controller / 5977363805

25 Aug 2023 02:56PM UTC coverage: 73.025% (-1.3%) from 74.302%
5977363805

push

github

web-flow
Drop HorizontalPodAutoscaler field (#521)

Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>

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

2209 of 3025 relevant lines covered (73.02%)

0.82 hits per line

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

91.1
/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.StackSpecInternal, backendPort *intstr.IntOrString) ([]v1.ServicePort, error) {
1✔
99
        var servicePorts []v1.ServicePort
1✔
100
        if stackSpec.StackSpec.Service == nil ||
1✔
101
                len(stackSpec.StackSpec.Service.Ports) == 0 {
2✔
102

1✔
103
                servicePorts = servicePortsFromContainers(
1✔
104
                        stackSpec.StackSpec.PodTemplate.Spec.Containers,
1✔
105
                )
1✔
106
        } else {
2✔
107
                servicePorts = stackSpec.StackSpec.Service.Ports
1✔
108
        }
1✔
109

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

125
                return nil, fmt.Errorf("no service ports matching backendPort '%s'", backendPort.String())
1✔
126
        }
127

128
        return servicePorts, nil
1✔
129
}
130

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

156
func (sc *StackContainer) selector() map[string]string {
1✔
157
        return limitLabels(sc.Stack.Labels, selectorLabels)
1✔
158
}
1✔
159

160
func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
1✔
161
        stack := sc.Stack
1✔
162

1✔
163
        desiredReplicas := sc.stackReplicas
1✔
164
        if sc.prescalingActive {
2✔
165
                desiredReplicas = sc.prescalingReplicas
1✔
166
        }
1✔
167

168
        var updatedReplicas *int32
1✔
169

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

182
        if updatedReplicas == nil {
2✔
183
                updatedReplicas = wrapReplicas(sc.deploymentReplicas)
1✔
184
        }
1✔
185

186
        var strategy *appsv1.DeploymentStrategy
1✔
187
        if stack.Spec.StackSpec.Strategy != nil {
2✔
188
                strategy = stack.Spec.StackSpec.Strategy.DeepCopy()
1✔
189
        }
1✔
190

191
        embeddedCopy := stack.Spec.StackSpec.PodTemplate.EmbeddedObjectMeta.DeepCopy()
1✔
192

1✔
193
        templateObjectMeta := metav1.ObjectMeta{
1✔
194
                Annotations: embeddedCopy.Annotations,
1✔
195
                Labels:      embeddedCopy.Labels,
1✔
196
        }
1✔
197

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

218
func (sc *StackContainer) GenerateHPA() (*autoscaling.HorizontalPodAutoscaler, error) {
1✔
219
        autoscalerSpec := sc.Stack.Spec.StackSpec.Autoscaler
1✔
220
        trafficWeight := sc.actualTrafficWeight
1✔
221

1✔
222
        if autoscalerSpec == nil {
1✔
223
                return nil, nil
×
224
        }
×
225

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

1✔
241
        result.Spec.MinReplicas = autoscalerSpec.MinReplicas
1✔
242
        result.Spec.MaxReplicas = autoscalerSpec.MaxReplicas
1✔
243

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

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

258
        return result, nil
1✔
259
}
260

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

269
        servicePorts, err := getServicePorts(sc.Stack.Spec, backendPort)
1✔
270
        if err != nil {
1✔
271
                return nil, err
×
272
        }
×
273

274
        metaObj := sc.resourceMeta()
1✔
275
        stackSpec := sc.Stack.Spec
1✔
276
        if stackSpec.StackSpec.Service != nil {
2✔
277
                metaObj.Annotations = mergeLabels(
1✔
278
                        metaObj.Annotations,
1✔
279
                        stackSpec.StackSpec.Service.Annotations,
1✔
280
                )
1✔
281
        }
1✔
282
        return &v1.Service{
1✔
283
                ObjectMeta: metaObj,
1✔
284
                Spec: v1.ServiceSpec{
1✔
285
                        Selector: sc.selector(),
1✔
286
                        Type:     v1.ServiceTypeClusterIP,
1✔
287
                        Ports:    servicePorts,
1✔
288
                },
1✔
289
        }, nil
1✔
290
}
291

292
func (sc *StackContainer) stackHostnames(spec ingressOrRouteGroupSpec) ([]string, error) {
1✔
293
        result := sets.NewString()
1✔
294

1✔
295
        // Old-style autogenerated hostnames
1✔
296
        for _, host := range spec.GetHosts() {
2✔
297
                for _, domain := range sc.clusterDomains {
2✔
298
                        if strings.HasSuffix(host, domain) {
2✔
299
                                result.Insert(fmt.Sprintf("%s.%s", sc.Name(), domain))
1✔
300
                        } else {
2✔
301
                                log.Debugf("Ingress host: %s suffix did not match cluster-domain %s", host, domain)
1✔
302
                        }
1✔
303
                }
304
        }
305

306
        return result.List(), nil
1✔
307
}
308

309
func (sc *StackContainer) GenerateIngress() (*networking.Ingress, error) {
1✔
310
        if !sc.HasBackendPort() || sc.ingressSpec == nil {
2✔
311
                return nil, nil
1✔
312
        }
1✔
313

314
        hostnames, err := sc.stackHostnames(sc.ingressSpec)
1✔
315
        if err != nil {
1✔
316
                return nil, err
×
317
        }
×
318
        if len(hostnames) == 0 {
1✔
319
                return nil, nil
×
320
        }
×
321

322
        rules := make([]networking.IngressRule, 0, len(hostnames))
1✔
323
        for _, hostname := range hostnames {
2✔
324
                rules = append(rules, networking.IngressRule{
1✔
325
                        IngressRuleValue: networking.IngressRuleValue{
1✔
326
                                HTTP: &networking.HTTPIngressRuleValue{
1✔
327
                                        Paths: []networking.HTTPIngressPath{
1✔
328
                                                {
1✔
329
                                                        PathType: &PathTypeImplementationSpecific,
1✔
330
                                                        Path:     sc.ingressSpec.Path,
1✔
331
                                                        Backend: networking.IngressBackend{
1✔
332
                                                                Service: &networking.IngressServiceBackend{
1✔
333
                                                                        Name: sc.Name(),
1✔
334
                                                                        Port: networking.ServiceBackendPort{
1✔
335
                                                                                Name:   sc.backendPort.StrVal,
1✔
336
                                                                                Number: sc.backendPort.IntVal,
1✔
337
                                                                        },
1✔
338
                                                                },
1✔
339
                                                        },
1✔
340
                                                },
1✔
341
                                        },
1✔
342
                                },
1✔
343
                        },
1✔
344
                        Host: hostname,
1✔
345
                })
1✔
346
        }
1✔
347

348
        // sort rules by hostname for a stable order
349
        sort.Slice(rules, func(i, j int) bool {
1✔
350
                return rules[i].Host < rules[j].Host
×
351
        })
×
352

353
        result := &networking.Ingress{
1✔
354
                ObjectMeta: sc.resourceMeta(),
1✔
355
                Spec: networking.IngressSpec{
1✔
356
                        Rules: rules,
1✔
357
                },
1✔
358
        }
1✔
359

1✔
360
        // insert annotations
1✔
361
        result.Annotations = mergeLabels(result.Annotations, sc.ingressSpec.GetAnnotations())
1✔
362
        return result, nil
1✔
363
}
364

365
func (sc *StackContainer) GenerateRouteGroup() (*rgv1.RouteGroup, error) {
1✔
366
        if !sc.HasBackendPort() || sc.routeGroupSpec == nil {
2✔
367
                return nil, nil
1✔
368
        }
1✔
369

370
        hostnames, err := sc.stackHostnames(sc.routeGroupSpec)
1✔
371
        if err != nil {
1✔
372
                return nil, err
×
373
        }
×
374
        if len(hostnames) == 0 {
1✔
375
                return nil, nil
×
376
        }
×
377

378
        result := &rgv1.RouteGroup{
1✔
379
                ObjectMeta: sc.resourceMeta(),
1✔
380
                Spec: rgv1.RouteGroupSpec{
1✔
381
                        Hosts: hostnames,
1✔
382
                        Backends: []rgv1.RouteGroupBackend{
1✔
383
                                {
1✔
384
                                        Name:        sc.Name(),
1✔
385
                                        Type:        rgv1.ServiceRouteGroupBackend,
1✔
386
                                        ServiceName: sc.Name(),
1✔
387
                                        ServicePort: sc.backendPort.IntValue(),
1✔
388
                                        Algorithm:   sc.routeGroupSpec.LBAlgorithm,
1✔
389
                                },
1✔
390
                        },
1✔
391
                        DefaultBackends: []rgv1.RouteGroupBackendReference{
1✔
392
                                {
1✔
393
                                        BackendName: sc.Name(),
1✔
394
                                        Weight:      100,
1✔
395
                                },
1✔
396
                        },
1✔
397
                        Routes: sc.routeGroupSpec.Routes,
1✔
398
                },
1✔
399
        }
1✔
400

1✔
401
        // validate not overlapping with main backend
1✔
402
        for _, backend := range sc.routeGroupSpec.AdditionalBackends {
1✔
403
                if backend.Name == sc.Name() {
×
404
                        return nil, fmt.Errorf("invalid additionalBackend '%s', overlaps with Stack name", backend.Name)
×
405
                }
×
406
                if backend.ServiceName == sc.Name() {
×
407
                        return nil, fmt.Errorf("invalid additionalBackend '%s', serviceName '%s' overlaps with Stack name", backend.Name, backend.ServiceName)
×
408
                }
×
409
                result.Spec.Backends = append(result.Spec.Backends, backend)
×
410
        }
411

412
        // sort backends to ensure have a consistent generated RoutGroup resource
413
        sort.Slice(result.Spec.Backends, func(i, j int) bool {
1✔
414
                return result.Spec.Backends[i].Name < result.Spec.Backends[j].Name
×
415
        })
×
416

417
        // insert annotations
418
        result.Annotations = mergeLabels(result.Annotations, sc.routeGroupSpec.GetAnnotations())
1✔
419

1✔
420
        return result, nil
1✔
421
}
422

423
func (sc *StackContainer) GenerateStackStatus() *zv1.StackStatus {
1✔
424
        prescaling := zv1.PrescalingStatus{}
1✔
425
        if sc.prescalingActive {
2✔
426
                prescaling = zv1.PrescalingStatus{
1✔
427
                        Active:               sc.prescalingActive,
1✔
428
                        Replicas:             sc.prescalingReplicas,
1✔
429
                        DesiredTrafficWeight: sc.prescalingDesiredTrafficWeight,
1✔
430
                        LastTrafficIncrease:  wrapTime(sc.prescalingLastTrafficIncrease),
1✔
431
                }
1✔
432
        }
1✔
433
        return &zv1.StackStatus{
1✔
434
                ActualTrafficWeight:  sc.actualTrafficWeight,
1✔
435
                DesiredTrafficWeight: sc.desiredTrafficWeight,
1✔
436
                Replicas:             sc.createdReplicas,
1✔
437
                ReadyReplicas:        sc.readyReplicas,
1✔
438
                UpdatedReplicas:      sc.updatedReplicas,
1✔
439
                DesiredReplicas:      sc.deploymentReplicas,
1✔
440
                Prescaling:           prescaling,
1✔
441
                NoTrafficSince:       wrapTime(sc.noTrafficSince),
1✔
442
                LabelSelector:        labels.Set(sc.selector()).String(),
1✔
443
        }
1✔
444
}
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

© 2026 Coveralls, Inc