Skip to content

Commit 13c98eb

Browse files
committed
refactor: move resource patching into the stack
feature: reduce resource waste by setting replicas to 1 for the new unused stack, that will not receive any traffic Signed-off-by: Sandor Szücs <[email protected]>
1 parent b449107 commit 13c98eb

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

pkg/core/stackset.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ func sanitizeServicePorts(service *zv1.StackServiceSpec) *zv1.StackServiceSpec {
5252
return service
5353
}
5454

55-
func patchForwardBackend(rg *zv1.RouteGroupSpec) {
56-
rg.AdditionalBackends = []rgv1.RouteGroupBackend{
57-
{
58-
Name: forwardBackendName,
59-
Type: rgv1.ForwardRouteGroupBackend,
60-
},
61-
}
62-
for _, route := range rg.Routes {
63-
route.Backends = []rgv1.RouteGroupBackendReference{
64-
{
65-
BackendName: forwardBackendName,
66-
},
67-
}
68-
}
69-
}
70-
7155
// NewStack returns an (optional) stack that should be created
7256
func (ssc *StackSetContainer) NewStack() (*StackContainer, string) {
7357
_, forwardMigration := ssc.StackSet.ObjectMeta.Annotations[forwardBackendAnnotation]
@@ -89,9 +73,6 @@ func (ssc *StackSetContainer) NewStack() (*StackContainer, string) {
8973

9074
if ssc.StackSet.Spec.Ingress != nil {
9175
spec.Ingress = ssc.StackSet.Spec.Ingress.DeepCopy()
92-
if forwardMigration {
93-
spec.Ingress.EmbeddedObjectMetaWithAnnotations.Annotations["zalando.org/skipper-backend"] = "forward"
94-
}
9576
}
9677

9778
if ssc.StackSet.Spec.ExternalIngress != nil {
@@ -100,9 +81,11 @@ func (ssc *StackSetContainer) NewStack() (*StackContainer, string) {
10081

10182
if ssc.StackSet.Spec.RouteGroup != nil {
10283
spec.RouteGroup = ssc.StackSet.Spec.RouteGroup.DeepCopy()
103-
if forwardMigration {
104-
patchForwardBackend(spec.RouteGroup)
105-
}
84+
}
85+
86+
stackAnnotations := ssc.StackSet.Spec.StackTemplate.Annotations
87+
if forwardMigration {
88+
stackAnnotations[forwardBackendAnnotation] = forwardBackendName
10689
}
10790

10891
return &StackContainer{
@@ -125,7 +108,7 @@ func (ssc *StackSetContainer) NewStack() (*StackContainer, string) {
125108
ssc.StackSet.Labels,
126109
map[string]string{StackVersionLabelKey: stackVersion},
127110
),
128-
Annotations: ssc.StackSet.Spec.StackTemplate.Annotations,
111+
Annotations: stackAnnotations,
129112
},
130113
Spec: *spec,
131114
},

pkg/core/types.go

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,34 @@ func (sc *StackContainer) updateStackResources() error {
419419
}
420420

421421
func (sc *StackContainer) updateFromResources() {
422-
sc.stackReplicas = effectiveReplicas(sc.Stack.Spec.StackSpec.Replicas)
422+
_, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]
423+
424+
if clusterMigration {
425+
// we do not use these so reduce wasted resources!
426+
sc.stackReplicas = 1
427+
} else {
428+
sc.stackReplicas = effectiveReplicas(sc.Stack.Spec.StackSpec.Replicas)
429+
}
423430

424431
var deploymentUpdated, serviceUpdated, ingressUpdated, routeGroupUpdated, hpaUpdated bool
425432
var ingressSegmentUpdated, routeGroupSegmentUpdated bool
426433

427434
// deployment
428435
if sc.Resources.Deployment != nil {
429436
deployment := sc.Resources.Deployment
430-
sc.deploymentReplicas = effectiveReplicas(deployment.Spec.Replicas)
431-
sc.createdReplicas = deployment.Status.Replicas
432-
sc.readyReplicas = deployment.Status.ReadyReplicas
433-
sc.updatedReplicas = deployment.Status.UpdatedReplicas
437+
438+
if clusterMigration {
439+
// we do not use these so reduce wasted resources!
440+
sc.deploymentReplicas = 1
441+
sc.createdReplicas = 1
442+
sc.readyReplicas = 1
443+
sc.updatedReplicas = 1
444+
} else {
445+
sc.deploymentReplicas = effectiveReplicas(deployment.Spec.Replicas)
446+
sc.createdReplicas = deployment.Status.Replicas
447+
sc.readyReplicas = deployment.Status.ReadyReplicas
448+
sc.updatedReplicas = deployment.Status.UpdatedReplicas
449+
}
434450
deploymentUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.Deployment.ObjectMeta) && deployment.Status.ObservedGeneration == deployment.Generation
435451
}
436452

@@ -442,13 +458,20 @@ func (sc *StackContainer) updateFromResources() {
442458
// the per-stack ingress must either be present and up-to-date, or not present and not expected.
443459
// the per-stack ingress is not expected if the stack has no hostnames matching the cluster domain.
444460
if sc.Resources.Ingress != nil {
461+
if clusterMigration {
462+
sc.Resources.Ingress.Annotations["zalando.org/skipper-backend"] = "forward"
463+
}
445464
ingressUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.Ingress.ObjectMeta)
446465
} else {
447466
hostnames := sc.stackHostnames(sc.ingressSpec, false)
448467
ingressUpdated = len(hostnames) == 0
449468
}
450-
ingressSegmentUpdated = sc.Resources.IngressSegment != nil &&
451-
IsResourceUpToDate(sc.Stack, sc.Resources.IngressSegment.ObjectMeta)
469+
if sc.Resources.IngressSegment != nil {
470+
if clusterMigration {
471+
sc.Resources.IngressSegment.Annotations["zalando.org/skipper-backend"] = "forward"
472+
}
473+
ingressSegmentUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.IngressSegment.ObjectMeta)
474+
}
452475
} else {
453476
// ignore if ingress is not set
454477
ingressUpdated = sc.Resources.Ingress == nil
@@ -460,24 +483,33 @@ func (sc *StackContainer) updateFromResources() {
460483
// the per-stack route group must either be present and up-to-date, or not present and not expected.
461484
// the per-stack route group is not expected if the stack has no hostnames matching the cluster domain.
462485
if sc.Resources.RouteGroup != nil {
486+
if clusterMigration {
487+
patchForwardBackend(&sc.Resources.RouteGroup.Spec)
488+
}
463489
routeGroupUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.RouteGroup.ObjectMeta)
464490
} else {
465491
hostnames := sc.stackHostnames(sc.routeGroupSpec, false)
466492
routeGroupUpdated = len(hostnames) == 0
467493
}
468-
routeGroupSegmentUpdated = sc.Resources.RouteGroupSegment != nil &&
469-
IsResourceUpToDate(
494+
495+
if sc.Resources.RouteGroupSegment != nil {
496+
if clusterMigration {
497+
patchForwardBackend(&sc.Resources.RouteGroupSegment.Spec)
498+
}
499+
500+
routeGroupSegmentUpdated = IsResourceUpToDate(
470501
sc.Stack,
471502
sc.Resources.RouteGroupSegment.ObjectMeta,
472503
)
504+
}
473505
} else {
474506
// ignore if route group is not set
475507
routeGroupUpdated = sc.Resources.RouteGroup == nil
476508
routeGroupSegmentUpdated = sc.Resources.RouteGroupSegment == nil
477509
}
478510

479-
// hpa
480-
if sc.IsAutoscaled() {
511+
// hpa not used if we migrate cluster to reduce wasted resources
512+
if sc.IsAutoscaled() && !clusterMigration {
481513
hpaUpdated = sc.Resources.HPA != nil && IsResourceUpToDate(sc.Stack, sc.Resources.HPA.ObjectMeta)
482514
} else {
483515
hpaUpdated = sc.Resources.HPA == nil
@@ -494,10 +526,27 @@ func (sc *StackContainer) updateFromResources() {
494526

495527
status := sc.Stack.Status
496528
sc.noTrafficSince = unwrapTime(status.NoTrafficSince)
497-
if status.Prescaling.Active {
529+
// do not prescale on cluste rmigration to reduce wasted resources
530+
if status.Prescaling.Active && !clusterMigration {
498531
sc.prescalingActive = true
499532
sc.prescalingReplicas = status.Prescaling.Replicas
500533
sc.prescalingDesiredTrafficWeight = status.Prescaling.DesiredTrafficWeight
501534
sc.prescalingLastTrafficIncrease = unwrapTime(status.Prescaling.LastTrafficIncrease)
502535
}
503536
}
537+
538+
func patchForwardBackend(rg *rgv1.RouteGroupSpec) {
539+
rg.Backends = []rgv1.RouteGroupBackend{
540+
{
541+
Name: forwardBackendName,
542+
Type: rgv1.ForwardRouteGroupBackend,
543+
},
544+
}
545+
for _, route := range rg.Routes {
546+
route.Backends = []rgv1.RouteGroupBackendReference{
547+
{
548+
BackendName: forwardBackendName,
549+
},
550+
}
551+
}
552+
}

0 commit comments

Comments
 (0)