Skip to content

Commit

Permalink
update downscale
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchen-db committed Oct 13, 2024
1 parent 7d10753 commit 36b3396
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/controller/delay.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (
"github.com/grafana/rollout-operator/pkg/config"
)

func getStsSvcName(sts *v1.StatefulSet) string {
if sts.Spec.ServiceName != "" {
return sts.Spec.ServiceName
}
return sts.GetName()
}

func cancelDelayedDownscaleIfConfigured(ctx context.Context, logger log.Logger, sts *v1.StatefulSet, httpClient httpClient, replicas int32) {
delay, prepareURL, err := parseDelayedDownscaleAnnotations(sts.GetAnnotations())
if delay == 0 || prepareURL == nil {
Expand All @@ -31,7 +38,7 @@ func cancelDelayedDownscaleIfConfigured(ctx context.Context, logger log.Logger,
return
}

endpoints := createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), 0, int(replicas), prepareURL)
endpoints := createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), getStsSvcName(sts), 0, int(replicas), prepareURL)

callCancelDelayedDownscale(ctx, logger, httpClient, endpoints)
}
Expand All @@ -54,19 +61,19 @@ func checkScalingDelay(ctx context.Context, logger log.Logger, sts *v1.StatefulS
}

if desiredReplicas >= currentReplicas {
callCancelDelayedDownscale(ctx, logger, httpClient, createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), 0, int(currentReplicas), prepareURL))
callCancelDelayedDownscale(ctx, logger, httpClient, createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), getStsSvcName(sts), 0, int(currentReplicas), prepareURL))
// Proceed even if calling cancel of delayed downscale fails. We call cancellation repeatedly, so it will happen during next reconcile.
return desiredReplicas, nil
}

{
// Replicas in [0, desired) interval should cancel any delayed downscale, if they have any.
cancelEndpoints := createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), 0, int(desiredReplicas), prepareURL)
cancelEndpoints := createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), getStsSvcName(sts), 0, int(desiredReplicas), prepareURL)
callCancelDelayedDownscale(ctx, logger, httpClient, cancelEndpoints)
}

// Replicas in [desired, current) interval are going to be stopped.
downscaleEndpoints := createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), int(desiredReplicas), int(currentReplicas), prepareURL)
downscaleEndpoints := createPrepareDownscaleEndpoints(sts.Namespace, sts.GetName(), getStsSvcName(sts), int(desiredReplicas), int(currentReplicas), prepareURL)
elapsedTimeSinceDownscaleInitiated, err := callPrepareDownscaleAndReturnElapsedDurationsSinceInitiatedDownscale(ctx, logger, httpClient, downscaleEndpoints)
if err != nil {
return currentReplicas, fmt.Errorf("failed prepare pods for delayed downscale: %v", err)
Expand Down Expand Up @@ -131,7 +138,7 @@ type endpoint struct {
}

// Create prepare-downscale endpoints for pods with index in [from, to) range. URL is fully reused except for host, which is replaced with pod's FQDN.
func createPrepareDownscaleEndpoints(namespace, serviceName string, from, to int, url *url.URL) []endpoint {
func createPrepareDownscaleEndpoints(namespace, stsName, serviceName string, from, to int, url *url.URL) []endpoint {
eps := make([]endpoint, 0, to-from)

// The DNS entry for a pod of a stateful set is
Expand All @@ -142,12 +149,13 @@ func createPrepareDownscaleEndpoints(namespace, serviceName string, from, to int
for index := from; index < to; index++ {
ep := endpoint{
namespace: namespace,
podName: fmt.Sprintf("%v-%v", serviceName, index),
podName: fmt.Sprintf("%v-%v", stsName, index),
replica: index,
}

ep.url = *url
ep.url.Host = fmt.Sprintf("%s.%v.%v.svc.cluster.local.", ep.podName, serviceName, ep.namespace)
port := ep.url.Port()
ep.url.Host = fmt.Sprintf("%s.%v.%v.svc.cluster.local:%s", ep.podName, serviceName, ep.namespace, port)

eps = append(eps, ep)
}
Expand Down

0 comments on commit 36b3396

Please sign in to comment.