diff --git a/pkg/controller/delay.go b/pkg/controller/delay.go index da671a0f0..fb950ccaf 100644 --- a/pkg/controller/delay.go +++ b/pkg/controller/delay.go @@ -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 { @@ -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) } @@ -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) @@ -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 @@ -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) }