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

Commit

Permalink
Use pod generated name uid as instance index
Browse files Browse the repository at this point in the history
The uid is alphanumeric, which means it can be interpreted as a base-36
number and converted to an integer

[cloudfoundry/eirini-release#207]

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

import (
"context"
"sort"
"strconv"
"strings"

"code.cloudfoundry.org/eirini"
Expand Down Expand Up @@ -60,6 +60,17 @@ func (g *Getter) Get(ctx context.Context, identifier api.LRPIdentifier) (*api.LR
return g.getLRP(ctx, logger, identifier)
}

func podToInstanceID(podName string) (int, error) {
nameSegments := strings.Split(podName, "-")
instanceName := nameSegments[len(nameSegments)-1]
instanceNumber, err := strconv.ParseInt(instanceName, 36, 32)
if err != nil {
return 0, errors.Wrapf(err, "could not parse instanceName %q as a 36-base number", instanceName)
}

return int(instanceNumber), nil
}

func (g *Getter) GetInstances(ctx context.Context, identifier api.LRPIdentifier) ([]*api.Instance, error) {
logger := g.logger.Session("get-instance", lager.Data{"guid": identifier.GUID, "version": identifier.Version})
if _, err := g.getLRP(ctx, logger, identifier); errors.Is(err, eirini.ErrNotFound) {
Expand All @@ -73,13 +84,7 @@ 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 {
events, err := g.eventGetter.GetByPod(ctx, pod)
if err != nil {
Expand All @@ -92,7 +97,11 @@ func (g *Getter) GetInstances(ctx context.Context, identifier api.LRPIdentifier)
continue
}

index := podToInstanceID[pod.Name]
index, err := podToInstanceID(pod.Name)
if err != nil {
logger.Error("failed-to-convert-pod-name", err)
continue
}

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

import (
"context"
"sort"
"fmt"

"code.cloudfoundry.org/eirini"
"code.cloudfoundry.org/eirini/api"
Expand Down Expand Up @@ -104,13 +104,24 @@ func (s *Stopper) StopInstance(ctx context.Context, identifier api.LRPIdentifier
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 := ""
for _, pod := range pods {
podIndex, err := podToInstanceID(pod.Name)
if err != nil {
logger.Error("could-not-parse-pod-index", err)
continue
}

if uint(podIndex) == index {
podName = pod.Name
}
}

if podName == "" {
logger.Debug("failed-to-find-pod", lager.Data{"index": index})
return fmt.Errorf("failed to find pod with index %d", index)
}

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 f2f07e1

Please sign in to comment.