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

zalando-incubator / stackset-controller / 19443784616

17 Nov 2025 08:36PM UTC coverage: 50.075% (+0.3%) from 49.791%
19443784616

Pull #716

github

szuecs
fix: deployment nil

Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
Pull Request #716: feature: zalando.org/forward-backend annotation support to enable migration to eks

41 of 50 new or added lines in 4 files covered. (82.0%)

82 existing lines in 2 files now uncovered.

2662 of 5316 relevant lines covered (50.08%)

0.56 hits per line

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

95.03
/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
        SegmentSuffix       = "-traffic-segment"
27
        IngressPredicateKey = "zalando.org/skipper-predicate"
28
)
29

30
type ingressOrRouteGroupSpec interface {
31
        GetAnnotations() map[string]string
32
        GetHosts() []string
33
}
34

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

42
        // PathTypeImplementationSpecific is the used implementation path type
43
        // for k8s.io/api/networking/v1.HTTPIngressPath resources.
44
        PathTypeImplementationSpecific = networking.PathTypeImplementationSpecific
45
)
46

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

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

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

79
// patchForwardBackend rewrites a RouteGroupSpec to send all traffic to another cluster chosen by the operator of skipper
80
func patchForwardBackend(rg *rgv1.RouteGroupSpec) {
1✔
81
        rg.Backends = []rgv1.RouteGroupBackend{
1✔
82
                {
1✔
83
                        Name: forwardBackendName,
1✔
84
                        Type: rgv1.ForwardRouteGroupBackend,
1✔
85
                },
1✔
86
        }
1✔
87
        rg.DefaultBackends = []rgv1.RouteGroupBackendReference{
1✔
88
                {
1✔
89
                        BackendName: forwardBackendName,
1✔
90
                },
1✔
91
        }
1✔
92
        for i := range rg.Routes {
2✔
93
                rg.Routes[i].Backends = []rgv1.RouteGroupBackendReference{
1✔
94
                        {
1✔
95
                                BackendName: forwardBackendName,
1✔
96
                        },
1✔
97
                }
1✔
98
        }
1✔
99
}
100

101
func (sc *StackContainer) objectMeta(segment bool) metav1.ObjectMeta {
1✔
102
        resourceLabels := mapCopy(sc.Stack.Labels)
1✔
103

1✔
104
        name := sc.Name()
1✔
105
        if segment {
2✔
106
                name += SegmentSuffix
1✔
107
        }
1✔
108

109
        return metav1.ObjectMeta{
1✔
110
                Name:      name,
1✔
111
                Namespace: sc.Namespace(),
1✔
112
                Annotations: map[string]string{
1✔
113
                        stackGenerationAnnotationKey: strconv.FormatInt(sc.Stack.Generation, 10),
1✔
114
                },
1✔
115
                Labels: resourceLabels,
1✔
116
                OwnerReferences: []metav1.OwnerReference{
1✔
117
                        {
1✔
118
                                APIVersion: APIVersion,
1✔
119
                                Kind:       KindStack,
1✔
120
                                Name:       sc.Name(),
1✔
121
                                UID:        sc.Stack.UID,
1✔
122
                        },
1✔
123
                },
1✔
124
        }
1✔
125
}
126

127
func (sc *StackContainer) resourceMeta() metav1.ObjectMeta {
1✔
128
        return sc.objectMeta(false)
1✔
129
}
1✔
130

131
// getServicePorts gets the service ports to be used for the stack service.
132
func getServicePorts(stackSpec zv1.StackSpecInternal, backendPort *intstr.IntOrString) ([]v1.ServicePort, error) {
1✔
133
        var servicePorts []v1.ServicePort
1✔
134
        if stackSpec.StackSpec.Service == nil ||
1✔
135
                len(stackSpec.StackSpec.Service.Ports) == 0 {
2✔
136

1✔
137
                servicePorts = servicePortsFromContainers(
1✔
138
                        stackSpec.StackSpec.PodTemplate.Spec.Containers,
1✔
139
                )
1✔
140
        } else {
2✔
141
                servicePorts = stackSpec.StackSpec.Service.Ports
1✔
142
        }
1✔
143

144
        // validate that one port in the list maps to the backendPort.
145
        if backendPort != nil {
2✔
146
                for _, port := range servicePorts {
2✔
147
                        switch backendPort.Type {
1✔
148
                        case intstr.Int:
1✔
149
                                if port.Port == backendPort.IntVal {
2✔
150
                                        return servicePorts, nil
1✔
151
                                }
1✔
152
                        case intstr.String:
1✔
153
                                if port.Name == backendPort.StrVal {
2✔
154
                                        return servicePorts, nil
1✔
155
                                }
1✔
156
                        }
157
                }
158

159
                return nil, fmt.Errorf("no service ports matching backendPort '%s'", backendPort.String())
1✔
160
        }
161

162
        return servicePorts, nil
1✔
163
}
164

165
// servicePortsFromTemplate gets service port from pod template.
166
func servicePortsFromContainers(containers []v1.Container) []v1.ServicePort {
1✔
167
        ports := make([]v1.ServicePort, 0)
1✔
168
        for i, container := range containers {
2✔
169
                for j, port := range container.Ports {
2✔
170
                        name := fmt.Sprintf("port-%d-%d", i, j)
1✔
171
                        if port.Name != "" {
2✔
172
                                name = port.Name
1✔
173
                        }
1✔
174
                        servicePort := v1.ServicePort{
1✔
175
                                Name:       name,
1✔
176
                                Protocol:   port.Protocol,
1✔
177
                                Port:       port.ContainerPort,
1✔
178
                                TargetPort: intstr.FromInt(int(port.ContainerPort)),
1✔
179
                        }
1✔
180
                        // set default protocol if not specified
1✔
181
                        if servicePort.Protocol == "" {
2✔
182
                                servicePort.Protocol = v1.ProtocolTCP
1✔
183
                        }
1✔
184
                        ports = append(ports, servicePort)
1✔
185
                }
186
        }
187
        return ports
1✔
188
}
189

190
func (sc *StackContainer) selector() map[string]string {
1✔
191
        return limitLabels(sc.Stack.Labels, selectorLabels)
1✔
192
}
1✔
193

194
// GenerateDeployment generates a deployment as configured in the
195
// stack.  On cluster migrations set by stackset annotation
196
// "zalando.org/forward-backend", the deployment will be set to
197
// replicas 1.
198
func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
1✔
199
        if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
2✔
200
                return nil
1✔
201
        }
1✔
202

203
        stack := sc.Stack
1✔
204

1✔
205
        desiredReplicas := sc.stackReplicas
1✔
206
        if sc.prescalingActive {
2✔
207
                desiredReplicas = sc.prescalingReplicas
1✔
208
        }
1✔
209

210
        var updatedReplicas *int32
1✔
211

1✔
212
        if desiredReplicas != 0 && !sc.ScaledDown() {
2✔
213
                // Stack scaled up, rescale the deployment if it's at 0 replicas, or if HPA is unused and we don't run autoscaling
1✔
214
                if sc.deploymentReplicas == 0 || (!sc.IsAutoscaled() && desiredReplicas != sc.deploymentReplicas) {
2✔
215
                        updatedReplicas = wrapReplicas(desiredReplicas)
1✔
216
                }
1✔
217
        } else {
1✔
218
                // Stack scaled down (manually or because it doesn't receive traffic), check if we need to scale down the deployment
1✔
219
                if sc.deploymentReplicas != 0 {
2✔
220
                        updatedReplicas = wrapReplicas(0)
1✔
221
                }
1✔
222
        }
223

224
        if updatedReplicas == nil {
2✔
225
                updatedReplicas = wrapReplicas(sc.deploymentReplicas)
1✔
226
        }
1✔
227

228
        var strategy *appsv1.DeploymentStrategy
1✔
229
        if stack.Spec.StackSpec.Strategy != nil {
2✔
230
                strategy = stack.Spec.StackSpec.Strategy.DeepCopy()
1✔
231
        }
1✔
232

233
        embeddedCopy := stack.Spec.StackSpec.PodTemplate.EmbeddedObjectMeta.DeepCopy()
1✔
234

1✔
235
        templateObjectMeta := metav1.ObjectMeta{
1✔
236
                Annotations: embeddedCopy.Annotations,
1✔
237
                Labels:      embeddedCopy.Labels,
1✔
238
        }
1✔
239

1✔
240
        deployment := &appsv1.Deployment{
1✔
241
                ObjectMeta: sc.resourceMeta(),
1✔
242
                Spec: appsv1.DeploymentSpec{
1✔
243
                        Replicas:        updatedReplicas,
1✔
244
                        MinReadySeconds: sc.Stack.Spec.StackSpec.MinReadySeconds,
1✔
245
                        Selector: &metav1.LabelSelector{
1✔
246
                                MatchLabels: sc.selector(),
1✔
247
                        },
1✔
248
                        Template: v1.PodTemplateSpec{
1✔
249
                                ObjectMeta: objectMetaInjectLabels(templateObjectMeta, stack.Labels),
1✔
250
                                Spec:       *stack.Spec.StackSpec.PodTemplate.Spec.DeepCopy(),
1✔
251
                        },
1✔
252
                },
1✔
253
        }
1✔
254
        if strategy != nil {
2✔
255
                deployment.Spec.Strategy = *strategy
1✔
256
        }
1✔
257

258
        return deployment
1✔
259
}
260

261
// GenerateHPA generates a hpa as configured in the
262
// stack.  On cluster migrations set by stackset annotation
263
// "zalando.org/forward-backend", the hpa will be set to
264
// minReplicas = maxReplicass = 1.
265
func (sc *StackContainer) GenerateHPA() (
266
        *autoscaling.HorizontalPodAutoscaler,
267
        error,
268
) {
1✔
269
        if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
2✔
270
                return nil, nil
1✔
271
        }
1✔
272

273
        autoscalerSpec := sc.Stack.Spec.StackSpec.Autoscaler
1✔
274
        trafficWeight := sc.actualTrafficWeight
1✔
275

1✔
276
        if autoscalerSpec == nil {
1✔
UNCOV
277
                return nil, nil
×
UNCOV
278
        }
×
279

280
        if sc.ScaledDown() {
2✔
281
                return nil, nil
1✔
282
        }
1✔
283

284
        result := &autoscaling.HorizontalPodAutoscaler{
1✔
285
                ObjectMeta: sc.resourceMeta(),
1✔
286
                TypeMeta: metav1.TypeMeta{
1✔
287
                        Kind:       "HorizontalPodAutoscaler",
1✔
288
                        APIVersion: "autoscaling/v2",
1✔
289
                },
1✔
290
                Spec: autoscaling.HorizontalPodAutoscalerSpec{
1✔
291
                        ScaleTargetRef: autoscaling.CrossVersionObjectReference{
1✔
292
                                APIVersion: apiVersionAppsV1,
1✔
293
                                Kind:       kindDeployment,
1✔
294
                                Name:       sc.Name(),
1✔
295
                        },
1✔
296
                },
1✔
297
        }
1✔
298

1✔
299
        result.Spec.MinReplicas = autoscalerSpec.MinReplicas
1✔
300
        result.Spec.MaxReplicas = autoscalerSpec.MaxReplicas
1✔
301

1✔
302
        metrics, annotations, err := convertCustomMetrics(
1✔
303
                sc.Name()+SegmentSuffix,
1✔
304
                sc.Name(),
1✔
305
                sc.Namespace(),
1✔
306
                autoscalerMetricsList(autoscalerSpec.Metrics),
1✔
307
                trafficWeight,
1✔
308
        )
1✔
309

1✔
310
        if err != nil {
1✔
UNCOV
311
                return nil, err
×
UNCOV
312
        }
×
313
        result.Spec.Metrics = metrics
1✔
314
        result.Annotations = mergeLabels(result.Annotations, annotations)
1✔
315
        result.Spec.Behavior = autoscalerSpec.Behavior
1✔
316

1✔
317
        // If prescaling is enabled, ensure we have at least `precalingReplicas` pods
1✔
318
        if sc.prescalingActive && (result.Spec.MinReplicas == nil || *result.Spec.MinReplicas < sc.prescalingReplicas) {
1✔
UNCOV
319
                pr := sc.prescalingReplicas
×
UNCOV
320
                result.Spec.MinReplicas = &pr
×
321
        }
×
322

323
        return result, nil
1✔
324
}
325

326
func (sc *StackContainer) GenerateService() (*v1.Service, error) {
1✔
327
        // get service ports to be used for the service
1✔
328
        var backendPort *intstr.IntOrString
1✔
329
        // Ingress or external managed Ingress
1✔
330
        if sc.HasBackendPort() {
1✔
UNCOV
331
                backendPort = sc.backendPort
×
UNCOV
332
        }
×
333

334
        servicePorts, err := getServicePorts(sc.Stack.Spec, backendPort)
1✔
335
        if err != nil {
1✔
336
                return nil, err
×
UNCOV
337
        }
×
338

339
        metaObj := sc.resourceMeta()
1✔
340
        stackSpec := sc.Stack.Spec
1✔
341
        if stackSpec.StackSpec.Service != nil {
2✔
342
                metaObj.Annotations = mergeLabels(
1✔
343
                        metaObj.Annotations,
1✔
344
                        stackSpec.StackSpec.Service.Annotations,
1✔
345
                )
1✔
346
        }
1✔
347
        return &v1.Service{
1✔
348
                ObjectMeta: metaObj,
1✔
349
                Spec: v1.ServiceSpec{
1✔
350
                        Selector: sc.selector(),
1✔
351
                        Type:     v1.ServiceTypeClusterIP,
1✔
352
                        Ports:    servicePorts,
1✔
353
                },
1✔
354
        }, nil
1✔
355
}
356

357
func (sc *StackContainer) stackHostnames(
358
        spec ingressOrRouteGroupSpec,
359
        segment bool,
360
) []string {
1✔
361
        // The Ingress segment uses the original hostnames
1✔
362
        if segment {
2✔
363
                return spec.GetHosts()
1✔
364
        }
1✔
365
        result := sets.NewString()
1✔
366

1✔
367
        // Old-style autogenerated hostnames
1✔
368
        for _, host := range spec.GetHosts() {
2✔
369
                for _, domain := range sc.clusterDomains {
2✔
370
                        if strings.HasSuffix(host, domain) {
2✔
371
                                result.Insert(fmt.Sprintf("%s.%s", sc.Name(), domain))
1✔
372
                        } else {
2✔
373
                                log.Debugf(
1✔
374
                                        "Ingress host: %s suffix did not match cluster-domain %s",
1✔
375
                                        host,
1✔
376
                                        domain,
1✔
377
                                )
1✔
378
                        }
1✔
379
                }
380
        }
381
        return result.List()
1✔
382
}
383

384
func (sc *StackContainer) GenerateIngress() (*networking.Ingress, error) {
1✔
385
        return sc.generateIngress(false)
1✔
386
}
1✔
387

388
func (sc *StackContainer) GenerateIngressSegment() (
389
        *networking.Ingress,
390
        error,
391
) {
1✔
392
        res, err := sc.generateIngress(true)
1✔
393
        if err != nil || res == nil {
2✔
394
                return res, err
1✔
395
        }
1✔
396

397
        // Synchronize annotations specified in the StackSet.
398
        res.Annotations = syncAnnotations(
1✔
399
                res.Annotations,
1✔
400
                sc.syncAnnotationsInIngress,
1✔
401
                sc.ingressAnnotationsToSync,
1✔
402
        )
1✔
403

1✔
404
        if predVal, ok := res.Annotations[IngressPredicateKey]; !ok || predVal == "" {
2✔
405
                res.Annotations = mergeLabels(
1✔
406
                        res.Annotations,
1✔
407
                        map[string]string{IngressPredicateKey: sc.trafficSegment()},
1✔
408
                )
1✔
409
        } else {
2✔
410
                res.Annotations = mergeLabels(
1✔
411
                        res.Annotations,
1✔
412
                        map[string]string{
1✔
413
                                IngressPredicateKey: sc.trafficSegment() + " && " + predVal,
1✔
414
                        },
1✔
415
                )
1✔
416
        }
1✔
417

418
        return res, nil
1✔
419
}
420

421
// generateIngress generates an ingress as configured in the stack.
422
// On cluster migrations set by stackset annotation
423
// "zalando.org/forward-backend", the annotation will be copied to the
424
// ingress.
425
func (sc *StackContainer) generateIngress(segment bool) (
426
        *networking.Ingress,
427
        error,
428
) {
1✔
429

1✔
430
        if !sc.HasBackendPort() || sc.ingressSpec == nil {
2✔
431
                return nil, nil
1✔
432
        }
1✔
433

434
        hostnames := sc.stackHostnames(sc.ingressSpec, segment)
1✔
435
        if len(hostnames) == 0 {
2✔
436
                return nil, nil
1✔
437
        }
1✔
438

439
        rules := make([]networking.IngressRule, 0, len(hostnames))
1✔
440
        for _, hostname := range hostnames {
2✔
441
                rules = append(rules, networking.IngressRule{
1✔
442
                        IngressRuleValue: networking.IngressRuleValue{
1✔
443
                                HTTP: &networking.HTTPIngressRuleValue{
1✔
444
                                        Paths: []networking.HTTPIngressPath{
1✔
445
                                                {
1✔
446
                                                        PathType: &PathTypeImplementationSpecific,
1✔
447
                                                        Path:     sc.ingressSpec.Path,
1✔
448
                                                        Backend: networking.IngressBackend{
1✔
449
                                                                Service: &networking.IngressServiceBackend{
1✔
450
                                                                        Name: sc.Name(),
1✔
451
                                                                        Port: networking.ServiceBackendPort{
1✔
452
                                                                                Name:   sc.backendPort.StrVal,
1✔
453
                                                                                Number: sc.backendPort.IntVal,
1✔
454
                                                                        },
1✔
455
                                                                },
1✔
456
                                                        },
1✔
457
                                                },
1✔
458
                                        },
1✔
459
                                },
1✔
460
                        },
1✔
461
                        Host: hostname,
1✔
462
                })
1✔
463
        }
1✔
464

465
        // sort rules by hostname for a stable order
466
        sort.Slice(rules, func(i, j int) bool {
2✔
467
                return rules[i].Host < rules[j].Host
1✔
468
        })
1✔
469

470
        result := &networking.Ingress{
1✔
471
                ObjectMeta: sc.objectMeta(segment),
1✔
472
                Spec: networking.IngressSpec{
1✔
473
                        Rules: rules,
1✔
474
                },
1✔
475
        }
1✔
476
        if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
2✔
477
                // see https://opensource.zalando.com/skipper/kubernetes/ingress-usage/#skipper-ingress-annotations
1✔
478
                result.Annotations["zalando.org/skipper-backend"] = "forward"
1✔
479
        }
1✔
480

481
        // insert annotations
482
        result.Annotations = mergeLabels(
1✔
483
                result.Annotations,
1✔
484
                sc.ingressSpec.GetAnnotations(),
1✔
485
        )
1✔
486

1✔
487
        return result, nil
1✔
488
}
489

490
func (sc *StackContainer) GenerateRouteGroup() (*rgv1.RouteGroup, error) {
1✔
491
        return sc.generateRouteGroup(false)
1✔
492
}
1✔
493

494
func (sc *StackContainer) GenerateRouteGroupSegment() (
495
        *rgv1.RouteGroup,
496
        error,
497
) {
1✔
498
        res, err := sc.generateRouteGroup(true)
1✔
499
        if err != nil || res == nil {
2✔
500
                return res, err
1✔
501
        }
1✔
502

503
        // Synchronize annotations specified in the StackSet.
504
        res.Annotations = syncAnnotations(
1✔
505
                res.Annotations,
1✔
506
                sc.syncAnnotationsInRouteGroup,
1✔
507
                sc.ingressAnnotationsToSync,
1✔
508
        )
1✔
509

1✔
510
        segmentedRoutes := []rgv1.RouteGroupRouteSpec{}
1✔
511
        for _, r := range res.Spec.Routes {
2✔
512
                r.Predicates = append(r.Predicates, sc.trafficSegment())
1✔
513
                segmentedRoutes = append(segmentedRoutes, r)
1✔
514
        }
1✔
515
        res.Spec.Routes = segmentedRoutes
1✔
516

1✔
517
        return res, nil
1✔
518
}
519

520
// generateRouteGroup generates an RouteGroup as configured in the
521
// stack.  On cluster migrations set by stackset annotation
522
// "zalando.org/forward-backend", the RouteGroup will be patched by
523
// patchForwardBackend() to execute the migration.
524
func (sc *StackContainer) generateRouteGroup(segment bool) (
525
        *rgv1.RouteGroup,
526
        error,
527
) {
1✔
528
        if !sc.HasBackendPort() || sc.routeGroupSpec == nil {
2✔
529
                return nil, nil
1✔
530
        }
1✔
531

532
        hostnames := sc.stackHostnames(sc.routeGroupSpec, segment)
1✔
533
        if len(hostnames) == 0 {
2✔
534
                return nil, nil
1✔
535
        }
1✔
536

537
        result := &rgv1.RouteGroup{
1✔
538
                ObjectMeta: sc.objectMeta(segment),
1✔
539
                Spec: rgv1.RouteGroupSpec{
1✔
540
                        Hosts: hostnames,
1✔
541
                        Backends: []rgv1.RouteGroupBackend{
1✔
542
                                {
1✔
543
                                        Name:        sc.Name(),
1✔
544
                                        Type:        rgv1.ServiceRouteGroupBackend,
1✔
545
                                        ServiceName: sc.Name(),
1✔
546
                                        ServicePort: sc.backendPort.IntValue(),
1✔
547
                                        Algorithm:   sc.routeGroupSpec.LBAlgorithm,
1✔
548
                                },
1✔
549
                        },
1✔
550
                        DefaultBackends: []rgv1.RouteGroupBackendReference{
1✔
551
                                {
1✔
552
                                        BackendName: sc.Name(),
1✔
553
                                        Weight:      100,
1✔
554
                                },
1✔
555
                        },
1✔
556
                },
1✔
557
        }
1✔
558

1✔
559
        result.Spec.Routes = sc.routeGroupSpec.Routes
1✔
560

1✔
561
        // validate not overlapping with main backend
1✔
562
        for _, backend := range sc.routeGroupSpec.AdditionalBackends {
1✔
UNCOV
563
                if backend.Name == sc.Name() {
×
UNCOV
564
                        return nil, fmt.Errorf("invalid additionalBackend '%s', overlaps with Stack name", backend.Name)
×
UNCOV
565
                }
×
UNCOV
566
                if backend.ServiceName == sc.Name() {
×
567
                        return nil, fmt.Errorf("invalid additionalBackend '%s', serviceName '%s' overlaps with Stack name", backend.Name, backend.ServiceName)
×
568
                }
×
569
                result.Spec.Backends = append(result.Spec.Backends, backend)
×
570
        }
571

572
        // sort backends to ensure have a consistent generated RouteGroup resource
573
        sort.Slice(result.Spec.Backends, func(i, j int) bool {
1✔
UNCOV
574
                return result.Spec.Backends[i].Name < result.Spec.Backends[j].Name
×
UNCOV
575
        })
×
576

577
        // insert annotations
578
        result.Annotations = mergeLabels(
1✔
579
                result.Annotations,
1✔
580
                sc.routeGroupSpec.GetAnnotations(),
1✔
581
        )
1✔
582

1✔
583
        if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
2✔
584
                patchForwardBackend(&result.Spec)
1✔
585
        }
1✔
586

587
        return result, nil
1✔
588
}
589

590
func (sc *StackContainer) trafficSegment() string {
1✔
591
        return fmt.Sprintf(
1✔
592
                segmentString,
1✔
593
                sc.segmentLowerLimit,
1✔
594
                sc.segmentUpperLimit,
1✔
595
        )
1✔
596
}
1✔
597

598
func (sc *StackContainer) UpdateObjectMeta(objMeta *metav1.ObjectMeta) *metav1.ObjectMeta {
1✔
599
        metaObj := sc.resourceMeta()
1✔
600
        objMeta.OwnerReferences = metaObj.OwnerReferences
1✔
601
        objMeta.Labels = mergeLabels(metaObj.Labels, objMeta.Labels)
1✔
602
        objMeta.Annotations = mergeLabels(metaObj.Annotations, objMeta.Annotations)
1✔
603

1✔
604
        return objMeta
1✔
605
}
1✔
606

607
func (sc *StackContainer) GenerateStackStatus() *zv1.StackStatus {
1✔
608
        prescaling := zv1.PrescalingStatus{}
1✔
609
        if sc.prescalingActive {
2✔
610
                prescaling = zv1.PrescalingStatus{
1✔
611
                        Active:               sc.prescalingActive,
1✔
612
                        Replicas:             sc.prescalingReplicas,
1✔
613
                        DesiredTrafficWeight: sc.prescalingDesiredTrafficWeight,
1✔
614
                        LastTrafficIncrease:  wrapTime(sc.prescalingLastTrafficIncrease),
1✔
615
                }
1✔
616
        }
1✔
617
        return &zv1.StackStatus{
1✔
618
                ActualTrafficWeight:  sc.actualTrafficWeight,
1✔
619
                DesiredTrafficWeight: sc.desiredTrafficWeight,
1✔
620
                Replicas:             sc.createdReplicas,
1✔
621
                ReadyReplicas:        sc.readyReplicas,
1✔
622
                UpdatedReplicas:      sc.updatedReplicas,
1✔
623
                DesiredReplicas:      sc.deploymentReplicas,
1✔
624
                Prescaling:           prescaling,
1✔
625
                NoTrafficSince:       wrapTime(sc.noTrafficSince),
1✔
626
                LabelSelector:        labels.Set(sc.selector()).String(),
1✔
627
        }
1✔
628
}
629

630
func (sc *StackContainer) GeneratePlatformCredentialsSet(pcs *zv1.PCS) (*zv1.PlatformCredentialsSet, error) {
1✔
631
        if pcs.Tokens == nil {
1✔
UNCOV
632
                return nil, fmt.Errorf("platformCredentialsSet has no tokens")
×
UNCOV
633
        }
×
634

635
        metaObj := sc.resourceMeta()
1✔
636
        if _, ok := sc.Stack.Labels["application"]; !ok {
1✔
637
                return nil, fmt.Errorf("stack has no label application")
×
UNCOV
638
        }
×
639
        metaObj.Name = pcs.Name
1✔
640

1✔
641
        result := &zv1.PlatformCredentialsSet{
1✔
642
                ObjectMeta: metaObj,
1✔
643
                TypeMeta: metav1.TypeMeta{
1✔
644
                        Kind:       "PlatformCredentialsSet",
1✔
645
                        APIVersion: "zalando.org/v1",
1✔
646
                },
1✔
647
                Spec: zv1.PlatformCredentialsSpec{
1✔
648
                        Application:  metaObj.Labels["application"],
1✔
649
                        TokenVersion: "v2",
1✔
650
                        Tokens:       pcs.Tokens,
1✔
651
                },
1✔
652
        }
1✔
653

1✔
654
        return result, nil
1✔
655
}
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