Skip to content

Commit ae8b458

Browse files
committed
reafctor: change to pathc all resource creations
Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
1 parent 77c2e0c commit ae8b458

File tree

3 files changed

+48
-55
lines changed

3 files changed

+48
-55
lines changed

pkg/core/stack_resources.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ func objectMetaInjectLabels(objectMeta metav1.ObjectMeta, labels map[string]stri
7676
return objectMeta
7777
}
7878

79+
// patchForwardBackend rewrites a RouteGroupSpec to send all traffic to another cluster chosen by the operator of skipper
80+
func patchForwardBackend(rg *rgv1.RouteGroupSpec) {
81+
rg.Backends = []rgv1.RouteGroupBackend{
82+
{
83+
Name: forwardBackendName,
84+
Type: rgv1.ForwardRouteGroupBackend,
85+
},
86+
}
87+
for _, route := range rg.Routes {
88+
route.Backends = []rgv1.RouteGroupBackendReference{
89+
{
90+
BackendName: forwardBackendName,
91+
},
92+
}
93+
}
94+
}
95+
7996
func (sc *StackContainer) objectMeta(segment bool) metav1.ObjectMeta {
8097
resourceLabels := mapCopy(sc.Stack.Labels)
8198

@@ -224,6 +241,12 @@ func (sc *StackContainer) GenerateDeployment() *appsv1.Deployment {
224241
if strategy != nil {
225242
deployment.Spec.Strategy = *strategy
226243
}
244+
245+
if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
246+
i := int32(1)
247+
deployment.Spec.Replicas = &i
248+
}
249+
227250
return deployment
228251
}
229252

@@ -281,6 +304,12 @@ func (sc *StackContainer) GenerateHPA() (
281304
result.Spec.MinReplicas = &pr
282305
}
283306

307+
if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
308+
i := int32(1)
309+
result.Spec.MinReplicas = &i
310+
result.Spec.MaxReplicas = i
311+
}
312+
284313
return result, nil
285314
}
286315

@@ -430,6 +459,9 @@ func (sc *StackContainer) generateIngress(segment bool) (
430459
Rules: rules,
431460
},
432461
}
462+
if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
463+
result.Annotations["zalando.org/skipper-backend"] = "forward"
464+
}
433465

434466
// insert annotations
435467
result.Annotations = mergeLabels(
@@ -529,6 +561,10 @@ func (sc *StackContainer) generateRouteGroup(segment bool) (
529561
sc.routeGroupSpec.GetAnnotations(),
530562
)
531563

564+
if _, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]; clusterMigration {
565+
patchForwardBackend(&result.Spec)
566+
}
567+
532568
return result, nil
533569
}
534570

pkg/core/stackset.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func (ssc *StackSetContainer) NewStack() (*StackContainer, string) {
7777

7878
if ssc.StackSet.Spec.ExternalIngress != nil {
7979
spec.ExternalIngress = ssc.StackSet.Spec.ExternalIngress.DeepCopy()
80+
if forwardMigration {
81+
spec.ExternalIngress.Annotations[forwardBackendAnnotation] = forwardBackendName
82+
}
8083
}
8184

8285
if ssc.StackSet.Spec.RouteGroup != nil {

pkg/core/types.go

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,7 @@ func (sc *StackContainer) updateStackResources() error {
427427
// cluster. RouteGroup will get a patched backend, such that it does
428428
// the same as ingress.
429429
func (sc *StackContainer) updateFromResources() {
430-
fwdVal, clusterMigration := sc.Stack.Annotations[forwardBackendAnnotation]
431-
432-
if clusterMigration {
433-
sc.Stack.Spec.ExternalIngress.Annotations[forwardBackendAnnotation] = fwdVal
434-
// we do not use these pods so reduce wasted resources!
435-
sc.stackReplicas = 1
436-
} else {
437-
sc.stackReplicas = effectiveReplicas(sc.Stack.Spec.StackSpec.Replicas)
438-
}
430+
sc.stackReplicas = effectiveReplicas(sc.Stack.Spec.StackSpec.Replicas)
439431

440432
var deploymentUpdated, serviceUpdated, ingressUpdated, routeGroupUpdated, hpaUpdated bool
441433
var ingressSegmentUpdated, routeGroupSegmentUpdated bool
@@ -444,18 +436,11 @@ func (sc *StackContainer) updateFromResources() {
444436
if sc.Resources.Deployment != nil {
445437
deployment := sc.Resources.Deployment
446438

447-
if clusterMigration {
448-
// we do not use these pods so reduce wasted resources!
449-
sc.deploymentReplicas = 1
450-
sc.createdReplicas = 1
451-
sc.readyReplicas = 1
452-
sc.updatedReplicas = 1
453-
} else {
454-
sc.deploymentReplicas = effectiveReplicas(deployment.Spec.Replicas)
455-
sc.createdReplicas = deployment.Status.Replicas
456-
sc.readyReplicas = deployment.Status.ReadyReplicas
457-
sc.updatedReplicas = deployment.Status.UpdatedReplicas
458-
}
439+
sc.deploymentReplicas = effectiveReplicas(deployment.Spec.Replicas)
440+
sc.createdReplicas = deployment.Status.Replicas
441+
sc.readyReplicas = deployment.Status.ReadyReplicas
442+
sc.updatedReplicas = deployment.Status.UpdatedReplicas
443+
459444
deploymentUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.Deployment.ObjectMeta) && deployment.Status.ObservedGeneration == deployment.Generation
460445
}
461446

@@ -467,18 +452,12 @@ func (sc *StackContainer) updateFromResources() {
467452
// the per-stack ingress must either be present and up-to-date, or not present and not expected.
468453
// the per-stack ingress is not expected if the stack has no hostnames matching the cluster domain.
469454
if sc.Resources.Ingress != nil {
470-
if clusterMigration {
471-
sc.Resources.Ingress.Annotations["zalando.org/skipper-backend"] = "forward"
472-
}
473455
ingressUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.Ingress.ObjectMeta)
474456
} else {
475457
hostnames := sc.stackHostnames(sc.ingressSpec, false)
476458
ingressUpdated = len(hostnames) == 0
477459
}
478460
if sc.Resources.IngressSegment != nil {
479-
if clusterMigration {
480-
sc.Resources.IngressSegment.Annotations["zalando.org/skipper-backend"] = "forward"
481-
}
482461
ingressSegmentUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.IngressSegment.ObjectMeta)
483462
}
484463
} else {
@@ -492,20 +471,13 @@ func (sc *StackContainer) updateFromResources() {
492471
// the per-stack route group must either be present and up-to-date, or not present and not expected.
493472
// the per-stack route group is not expected if the stack has no hostnames matching the cluster domain.
494473
if sc.Resources.RouteGroup != nil {
495-
if clusterMigration {
496-
patchForwardBackend(&sc.Resources.RouteGroup.Spec)
497-
}
498474
routeGroupUpdated = IsResourceUpToDate(sc.Stack, sc.Resources.RouteGroup.ObjectMeta)
499475
} else {
500476
hostnames := sc.stackHostnames(sc.routeGroupSpec, false)
501477
routeGroupUpdated = len(hostnames) == 0
502478
}
503479

504480
if sc.Resources.RouteGroupSegment != nil {
505-
if clusterMigration {
506-
patchForwardBackend(&sc.Resources.RouteGroupSegment.Spec)
507-
}
508-
509481
routeGroupSegmentUpdated = IsResourceUpToDate(
510482
sc.Stack,
511483
sc.Resources.RouteGroupSegment.ObjectMeta,
@@ -518,10 +490,8 @@ func (sc *StackContainer) updateFromResources() {
518490
}
519491

520492
// hpa not used if we migrate cluster to reduce wasted resources
521-
if sc.IsAutoscaled() && !clusterMigration {
493+
if sc.IsAutoscaled() {
522494
hpaUpdated = sc.Resources.HPA != nil && IsResourceUpToDate(sc.Stack, sc.Resources.HPA.ObjectMeta)
523-
} else {
524-
hpaUpdated = sc.Resources.HPA == nil
525495
}
526496

527497
// aggregated 'resources updated' for the readiness
@@ -535,27 +505,11 @@ func (sc *StackContainer) updateFromResources() {
535505

536506
status := sc.Stack.Status
537507
sc.noTrafficSince = unwrapTime(status.NoTrafficSince)
538-
// do not prescale on cluster migration to reduce wasted resources
539-
if status.Prescaling.Active && !clusterMigration {
508+
509+
if status.Prescaling.Active {
540510
sc.prescalingActive = true
541511
sc.prescalingReplicas = status.Prescaling.Replicas
542512
sc.prescalingDesiredTrafficWeight = status.Prescaling.DesiredTrafficWeight
543513
sc.prescalingLastTrafficIncrease = unwrapTime(status.Prescaling.LastTrafficIncrease)
544514
}
545515
}
546-
547-
func patchForwardBackend(rg *rgv1.RouteGroupSpec) {
548-
rg.Backends = []rgv1.RouteGroupBackend{
549-
{
550-
Name: forwardBackendName,
551-
Type: rgv1.ForwardRouteGroupBackend,
552-
},
553-
}
554-
for _, route := range rg.Routes {
555-
route.Backends = []rgv1.RouteGroupBackendReference{
556-
{
557-
BackendName: forwardBackendName,
558-
},
559-
}
560-
}
561-
}

0 commit comments

Comments
 (0)