Skip to content

Commit

Permalink
Merge pull request #46 from pngmbh/fix-pod-name-parsing
Browse files Browse the repository at this point in the history
fix(cmd): fix pod name parsing for newer k8s
  • Loading branch information
Cryptophobia authored Nov 14, 2018
2 parents 3a7143e + b83abd8 commit 6846398
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func parseType(target string, appID string) (string, string) {
parts := strings.Split(replaced, "-")
// the API requires the type, for now
// regex matches against how Deployment pod name is constructed
regex := regexp.MustCompile("[0-9]{8,10}-[a-z0-9]{5}$")
if regex.MatchString(replaced) {
regex := regexp.MustCompile("[a-z0-9]{8,10}-[a-z0-9]{5}$")
if regex.MatchString(replaced) || len(parts) == 2 {
psType = parts[0]
} else {
psType = parts[1]
Expand Down
37 changes: 24 additions & 13 deletions cmd/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,35 @@ import (
func TestParseType(t *testing.T) {
t.Parallel()

// test RC pod name
appID := "earthy-underdog"
rcPod := "earthy-underdog-v2-cmd-8yngj"
psType, psName := parseType(rcPod, appID)
if psType != "cmd" || psName != rcPod {
t.Errorf("type was not cmd (got %s) or psName was not %s (got %s)", psType, rcPod, psName)
var input = map[string]string{
// RC pod name
"earthy-underdog": "earthy-underdog-v2-cmd-8yngj",
// Deployment pod name - they are longer due to hash
"nonfat-yearbook": "nonfat-yearbook-cmd-2180299075-7na91",
// newer style of Deployment pod name
"foo-bar": "foo-bar-cmd-57f6c4bb68-7na91",
// same as above but leaving out the app-name from the pod name
"earthy-underdog2": "cmd-8yngj",
"nonfat-yearbook2": "cmd-2180299075-7na91",
"foo-bar2": "cmd-57f6c4bb68-7na91",
// same as above but with app names without hyphens
"earthy": "earthy-v2-cmd-8yngj",
"nonfat": "nonfat-cmd-2180299075-7na91",
"foo": "foo-cmd-57f6c4bb68-7na91",
"earthy2": "cmd-8yngj",
"nonfat2": "cmd-2180299075-7na91",
"foo2": "cmd-57f6c4bb68-7na91",
}

// test Deployment pod name - they are longer due to hash
appID = "nonfat-yearbook"
deployPod := "nonfat-yearbook-cmd-2180299075-7na91"
psType, psName = parseType(deployPod, appID)
if psType != "cmd" || psName != deployPod {
t.Errorf("type was not cmd (got %s) or psName was not %s (got %s)", psType, deployPod, psName)
for appID, podName := range input {
psType, psName := parseType(podName, appID)
if psType != "cmd" || psName != podName {
t.Errorf("parseType(%#v, %#v): type was not cmd (got %s) or psName was not %s (got %s)", podName, appID, psType, podName, psName)
}
}

// test type by itself
psType, psName = parseType("cmd", "fake")
psType, psName := parseType("cmd", "fake")
if psType != "cmd" || psName != "" {
t.Error("type was not cmd")
}
Expand Down

0 comments on commit 6846398

Please sign in to comment.