Skip to content

Commit

Permalink
Allow for graceful delete
Browse files Browse the repository at this point in the history
  • Loading branch information
aallawala committed Jul 19, 2024
1 parent 53c59f6 commit 3d94ef8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const (
PrepareDownscaleLabelKey = "grafana.com/prepare-downscale"
PrepareDownscaleLabelValue = "true"

// GracefulShutdownAnnotationKey is the annotation to gracefully shutdown a pod of a statefulset.
GracefulShutdownAnnotationKey = "grafana.com/graceful-shutdown"
GracefulShutdownAnnotationValue = "delete-pod"

// RolloutGroupLabelKey is the group to which multiple statefulsets belong and must be operated on together.
RolloutGroupLabelKey = "rollout-group"
// RolloutMaxUnavailableAnnotationKey is the max number of pods in each statefulset that may be stopped at
Expand Down
18 changes: 16 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func (c *RolloutController) listPods(sel labels.Selector) ([]*corev1.Pod, error)
func (c *RolloutController) updateStatefulSetPods(ctx context.Context, sts *v1.StatefulSet) (bool, error) {
level.Debug(c.logger).Log("msg", "reconciling StatefulSet", "statefulset", sts.Name)

podsToUpdate, err := c.podsNotMatchingUpdateRevision(sts)
podsToUpdate, err := c.getPodsToUpdate(sts)
if err != nil {
return false, errors.Wrap(err, "failed to get pods to update")
}
Expand Down Expand Up @@ -565,7 +565,7 @@ func (c *RolloutController) updateStatefulSetPods(ctx context.Context, sts *v1.S
return false, nil
}

func (c *RolloutController) podsNotMatchingUpdateRevision(sts *v1.StatefulSet) ([]*corev1.Pod, error) {
func (c *RolloutController) getPodsToUpdate(sts *v1.StatefulSet) ([]*corev1.Pod, error) {
var (
currRev = sts.Status.CurrentRevision
updateRev = sts.Status.UpdateRevision
Expand All @@ -592,6 +592,20 @@ func (c *RolloutController) podsNotMatchingUpdateRevision(sts *v1.StatefulSet) (
return nil, err
}

// If there are no pods to be found, check if there are pods that need to be gracefully terminated.
if len(pods) == 0 {
podsSelector = labels.NewSelector().Add(
util.MustNewLabelsRequirement(config.GracefulShutdownAnnotationKey, selection.Equals, []string{config.GracefulShutdownAnnotationValue}),
util.MustNewLabelsRequirement("name", selection.Equals, []string{sts.Spec.Template.Labels["name"]}),
)

if pods, err = c.listPods(podsSelector); err != nil {
return nil, err
} else if len(pods) > 0 {
level.Debug(c.logger).Log("msg", fmt.Sprintf("found %d pods that need to be gracefully terminated", len(pods)))
}
}

// Sort pods in order to provide a deterministic behaviour.
util.SortPods(pods)

Expand Down

0 comments on commit 3d94ef8

Please sign in to comment.