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

zalando-incubator / stackset-controller / 5968491960

24 Aug 2023 08:16PM UTC coverage: 74.335% (-0.7%) from 75.025%
5968491960

Pull #518

github

gargravarr
Remove debug printfs.

Signed-off-by: Rodrigo Reis <rodrigo.gargravarr@gmail.com>
Pull Request #518: (WIP) Include ingress and/or routegroup definitions in Stack template.

138 of 138 new or added lines in 7 files covered. (100.0%)

2265 of 3047 relevant lines covered (74.34%)

0.83 hits per line

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

91.01
/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
        hpaSpec := sc.Stack.Spec.StackSpec.HorizontalPodAutoscaler
1✔
221
        trafficWeight := sc.actualTrafficWeight
1✔
222

1✔
223
        if autoscalerSpec == nil && hpaSpec == nil {
1✔
224
                return nil, nil
×
225
        }
×
226

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

1✔
242
        if autoscalerSpec != nil {
2✔
243
                result.Spec.MinReplicas = autoscalerSpec.MinReplicas
1✔
244
                result.Spec.MaxReplicas = autoscalerSpec.MaxReplicas
1✔
245

1✔
246
                metrics, annotations, err := convertCustomMetrics(sc.stacksetName, sc.Name(), sc.Namespace(), autoscalerSpec.Metrics, trafficWeight)
1✔
247
                if err != nil {
1✔
248
                        return nil, err
×
249
                }
×
250
                result.Spec.Metrics = metrics
1✔
251
                result.Annotations = mergeLabels(result.Annotations, annotations)
1✔
252
                result.Spec.Behavior = autoscalerSpec.Behavior
1✔
253
        } else {
1✔
254
                result.Spec.MinReplicas = hpaSpec.MinReplicas
1✔
255
                result.Spec.MaxReplicas = hpaSpec.MaxReplicas
1✔
256
                metrics := make([]autoscaling.MetricSpec, 0, len(hpaSpec.Metrics))
1✔
257
                for _, m := range hpaSpec.Metrics {
2✔
258
                        m := m
1✔
259
                        metric := autoscaling.MetricSpec{}
1✔
260
                        err := Convert_v2beta1_MetricSpec_To_autoscaling_MetricSpec(&m, &metric, nil)
1✔
261
                        if err != nil {
1✔
262
                                return nil, err
×
263
                        }
×
264
                        metrics = append(metrics, metric)
1✔
265
                }
266
                result.Spec.Metrics = metrics
1✔
267
                result.Spec.Behavior = hpaSpec.Behavior
1✔
268
        }
269

270
        // If prescaling is enabled, ensure we have at least `precalingReplicas` pods
271
        if sc.prescalingActive && (result.Spec.MinReplicas == nil || *result.Spec.MinReplicas < sc.prescalingReplicas) {
1✔
272
                pr := sc.prescalingReplicas
×
273
                result.Spec.MinReplicas = &pr
×
274
        }
×
275

276
        return result, nil
1✔
277
}
278

279
func (sc *StackContainer) GenerateService() (*v1.Service, error) {
1✔
280
        // get service ports to be used for the service
1✔
281
        var backendPort *intstr.IntOrString
1✔
282
        // Ingress or external managed Ingress
1✔
283
        if sc.HasBackendPort() {
1✔
284
                backendPort = sc.backendPort
×
285
        }
×
286

287
        servicePorts, err := getServicePorts(sc.Stack.Spec, backendPort)
1✔
288
        if err != nil {
1✔
289
                return nil, err
×
290
        }
×
291

292
        metaObj := sc.resourceMeta()
1✔
293
        stackSpec := sc.Stack.Spec
1✔
294
        if stackSpec.StackSpec.Service != nil {
2✔
295
                metaObj.Annotations = mergeLabels(
1✔
296
                        metaObj.Annotations,
1✔
297
                        stackSpec.StackSpec.Service.Annotations,
1✔
298
                )
1✔
299
        }
1✔
300
        return &v1.Service{
1✔
301
                ObjectMeta: metaObj,
1✔
302
                Spec: v1.ServiceSpec{
1✔
303
                        Selector: sc.selector(),
1✔
304
                        Type:     v1.ServiceTypeClusterIP,
1✔
305
                        Ports:    servicePorts,
1✔
306
                },
1✔
307
        }, nil
1✔
308
}
309

310
func (sc *StackContainer) stackHostnames(spec ingressOrRouteGroupSpec) ([]string, error) {
1✔
311
        result := sets.NewString()
1✔
312

1✔
313
        // Old-style autogenerated hostnames
1✔
314
        for _, host := range spec.GetHosts() {
2✔
315
                for _, domain := range sc.clusterDomains {
2✔
316
                        if strings.HasSuffix(host, domain) {
2✔
317
                                result.Insert(fmt.Sprintf("%s.%s", sc.Name(), domain))
1✔
318
                        } else {
2✔
319
                                log.Debugf("Ingress host: %s suffix did not match cluster-domain %s", host, domain)
1✔
320
                        }
1✔
321
                }
322
        }
323

324
        return result.List(), nil
1✔
325
}
326

327
func (sc *StackContainer) GenerateIngress() (*networking.Ingress, error) {
1✔
328
        ingressSpec := sc.Stack.Spec.Ingress
1✔
329
        if ingressSpec == nil {
2✔
330
                // fallback to parent StackSet ingress spec, for backward compatibility
1✔
331
                ingressSpec = sc.ingressSpec
1✔
332
        }
1✔
333

334
        if !sc.HasBackendPort() || ingressSpec == nil {
2✔
335
                return nil, nil
1✔
336
        }
1✔
337

338
        hostnames, err := sc.stackHostnames(ingressSpec)
1✔
339
        if err != nil {
1✔
340
                return nil, err
×
341
        }
×
342
        if len(hostnames) == 0 {
1✔
343
                return nil, nil
×
344
        }
×
345

346
        rules := make([]networking.IngressRule, 0, len(hostnames))
1✔
347
        for _, hostname := range hostnames {
2✔
348
                rules = append(rules, networking.IngressRule{
1✔
349
                        IngressRuleValue: networking.IngressRuleValue{
1✔
350
                                HTTP: &networking.HTTPIngressRuleValue{
1✔
351
                                        Paths: []networking.HTTPIngressPath{
1✔
352
                                                {
1✔
353
                                                        PathType: &PathTypeImplementationSpecific,
1✔
354
                                                        Path:     ingressSpec.Path,
1✔
355
                                                        Backend: networking.IngressBackend{
1✔
356
                                                                Service: &networking.IngressServiceBackend{
1✔
357
                                                                        Name: sc.Name(),
1✔
358
                                                                        Port: networking.ServiceBackendPort{
1✔
359
                                                                                Name:   sc.backendPort.StrVal,
1✔
360
                                                                                Number: sc.backendPort.IntVal,
1✔
361
                                                                        },
1✔
362
                                                                },
1✔
363
                                                        },
1✔
364
                                                },
1✔
365
                                        },
1✔
366
                                },
1✔
367
                        },
1✔
368
                        Host: hostname,
1✔
369
                })
1✔
370
        }
1✔
371

372
        // sort rules by hostname for a stable order
373
        sort.Slice(rules, func(i, j int) bool {
1✔
374
                return rules[i].Host < rules[j].Host
×
375
        })
×
376

377
        result := &networking.Ingress{
1✔
378
                ObjectMeta: sc.resourceMeta(),
1✔
379
                Spec: networking.IngressSpec{
1✔
380
                        Rules: rules,
1✔
381
                },
1✔
382
        }
1✔
383

1✔
384
        // insert annotations
1✔
385
        result.Annotations = mergeLabels(result.Annotations, sc.ingressSpec.GetAnnotations())
1✔
386
        return result, nil
1✔
387
}
388

389
func (sc *StackContainer) GenerateRouteGroup() (*rgv1.RouteGroup, error) {
1✔
390
        if !sc.HasBackendPort() || sc.routeGroupSpec == nil {
2✔
391
                return nil, nil
1✔
392
        }
1✔
393

394
        hostnames, err := sc.stackHostnames(sc.routeGroupSpec)
1✔
395
        if err != nil {
1✔
396
                return nil, err
×
397
        }
×
398
        if len(hostnames) == 0 {
1✔
399
                return nil, nil
×
400
        }
×
401

402
        result := &rgv1.RouteGroup{
1✔
403
                ObjectMeta: sc.resourceMeta(),
1✔
404
                Spec: rgv1.RouteGroupSpec{
1✔
405
                        Hosts: hostnames,
1✔
406
                        Backends: []rgv1.RouteGroupBackend{
1✔
407
                                {
1✔
408
                                        Name:        sc.Name(),
1✔
409
                                        Type:        rgv1.ServiceRouteGroupBackend,
1✔
410
                                        ServiceName: sc.Name(),
1✔
411
                                        ServicePort: sc.backendPort.IntValue(),
1✔
412
                                        Algorithm:   sc.routeGroupSpec.LBAlgorithm,
1✔
413
                                },
1✔
414
                        },
1✔
415
                        DefaultBackends: []rgv1.RouteGroupBackendReference{
1✔
416
                                {
1✔
417
                                        BackendName: sc.Name(),
1✔
418
                                        Weight:      100,
1✔
419
                                },
1✔
420
                        },
1✔
421
                        Routes: sc.routeGroupSpec.Routes,
1✔
422
                },
1✔
423
        }
1✔
424

1✔
425
        // validate not overlapping with main backend
1✔
426
        for _, backend := range sc.routeGroupSpec.AdditionalBackends {
1✔
427
                if backend.Name == sc.Name() {
×
428
                        return nil, fmt.Errorf("invalid additionalBackend '%s', overlaps with Stack name", backend.Name)
×
429
                }
×
430
                if backend.ServiceName == sc.Name() {
×
431
                        return nil, fmt.Errorf("invalid additionalBackend '%s', serviceName '%s' overlaps with Stack name", backend.Name, backend.ServiceName)
×
432
                }
×
433
                result.Spec.Backends = append(result.Spec.Backends, backend)
×
434
        }
435

436
        // sort backends to ensure have a consistent generated RoutGroup resource
437
        sort.Slice(result.Spec.Backends, func(i, j int) bool {
1✔
438
                return result.Spec.Backends[i].Name < result.Spec.Backends[j].Name
×
439
        })
×
440

441
        // insert annotations
442
        result.Annotations = mergeLabels(result.Annotations, sc.routeGroupSpec.GetAnnotations())
1✔
443

1✔
444
        return result, nil
1✔
445
}
446

447
func (sc *StackContainer) GenerateStackStatus() *zv1.StackStatus {
1✔
448
        prescaling := zv1.PrescalingStatus{}
1✔
449
        if sc.prescalingActive {
2✔
450
                prescaling = zv1.PrescalingStatus{
1✔
451
                        Active:               sc.prescalingActive,
1✔
452
                        Replicas:             sc.prescalingReplicas,
1✔
453
                        DesiredTrafficWeight: sc.prescalingDesiredTrafficWeight,
1✔
454
                        LastTrafficIncrease:  wrapTime(sc.prescalingLastTrafficIncrease),
1✔
455
                }
1✔
456
        }
1✔
457
        return &zv1.StackStatus{
1✔
458
                ActualTrafficWeight:  sc.actualTrafficWeight,
1✔
459
                DesiredTrafficWeight: sc.desiredTrafficWeight,
1✔
460
                Replicas:             sc.createdReplicas,
1✔
461
                ReadyReplicas:        sc.readyReplicas,
1✔
462
                UpdatedReplicas:      sc.updatedReplicas,
1✔
463
                DesiredReplicas:      sc.deploymentReplicas,
1✔
464
                Prescaling:           prescaling,
1✔
465
                NoTrafficSince:       wrapTime(sc.noTrafficSince),
1✔
466
                LabelSelector:        labels.Set(sc.selector()).String(),
1✔
467
        }
1✔
468
}
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