Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Map pod names to instance_ids and vice versa
Browse files Browse the repository at this point in the history
This is a very naive implementation of this maaping: we get all the
pods, sort their names alphabetically and map the sorted names to
ordered indexes. This approach gives us stable mapping but does not
guarantee that during scaling up instance X would be older than instance
X-1

[cloudfoundry/eirini-release#207]

Co-authored-by: Mario Nitchev <[email protected]>
  • Loading branch information
danail-branekov and mnitchev committed Apr 21, 2021
1 parent ef89bbd commit 025573e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions k8s/stset/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package stset

import (
"context"
"sort"
"strings"

"code.cloudfoundry.org/eirini"
"code.cloudfoundry.org/eirini/api"
"code.cloudfoundry.org/eirini/k8s/utils"
"code.cloudfoundry.org/eirini/util"
"code.cloudfoundry.org/lager"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -73,6 +73,11 @@ func (g *Getter) GetInstances(ctx context.Context, identifier api.LRPIdentifier)
return nil, errors.Wrap(err, "failed to list pods")
}

sort.Slice(pods, func(i, j int) bool { return pods[i].Name < pods[j].Name })
podToInstanceID := map[string]int{}
for i, pod := range pods {
podToInstanceID[pod.Name] = i
}
instances := []*api.Instance{}

for _, pod := range pods {
Expand All @@ -87,7 +92,7 @@ func (g *Getter) GetInstances(ctx context.Context, identifier api.LRPIdentifier)
continue
}

index, _ := util.ParseAppIndex(pod.Name)
index := podToInstanceID[pod.Name]

since := int64(0)
if pod.Status.StartTime != nil {
Expand Down
18 changes: 16 additions & 2 deletions k8s/stset/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package stset

import (
"context"
"fmt"
"sort"

"code.cloudfoundry.org/eirini"
"code.cloudfoundry.org/eirini/api"
"code.cloudfoundry.org/lager"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/util/retry"
)
Expand All @@ -21,6 +22,7 @@ type StatefulSetDeleter interface {

type PodDeleter interface {
Delete(ctx context.Context, namespace, name string) error
GetByLRPIdentifier(ctx context.Context, id api.LRPIdentifier) ([]corev1.Pod, error)
}

type Stopper struct {
Expand Down Expand Up @@ -97,7 +99,19 @@ func (s *Stopper) StopInstance(ctx context.Context, identifier api.LRPIdentifier
return eirini.ErrInvalidInstanceIndex
}

err = s.podDeleter.Delete(ctx, statefulset.Namespace, fmt.Sprintf("%s-%d", statefulset.Name, index))
pods, err := s.podDeleter.GetByLRPIdentifier(ctx, identifier)
if err != nil {
return errors.Wrap(err, "failed to get pods")
}

sort.Slice(pods, func(i, j int) bool { return pods[i].Name < pods[j].Name })
indexToPod := map[uint]string{}
for i, pod := range pods {
indexToPod[uint(i)] = pod.Name
}

podName := indexToPod[index]
err = s.podDeleter.Delete(ctx, statefulset.Namespace, podName)
if k8serrors.IsNotFound(err) {
logger.Debug("pod-does-not-exist")

Expand Down

0 comments on commit 025573e

Please sign in to comment.