Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for graceful delete through a pod label #163

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

// GracefulShutdownLabelKey is the label to gracefully shut down a pod of a statefulset.
GracefulShutdownLabelKey = "grafana.com/graceful-shutdown"
GracefulShutdownLabelValue = "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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this condition? don't we want to combine the pods which has graceful-shutdown label with existing pods list?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the precedence of the run is for it to enact on rolling out new changes. If all pods are updated, then check for the graceful-shutdown label.

podsSelector = labels.NewSelector().Add(
util.MustNewLabelsRequirement(config.GracefulShutdownLabelKey, selection.Equals, []string{config.GracefulShutdownLabelValue}),
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 pod(s) marked for graceful termination", len(pods)))
}
}

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

Expand Down