Skip to content

Commit 6846398

Browse files
authored
Merge pull request #46 from pngmbh/fix-pod-name-parsing
fix(cmd): fix pod name parsing for newer k8s
2 parents 3a7143e + b83abd8 commit 6846398

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

cmd/ps.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func parseType(target string, appID string) (string, string) {
125125
parts := strings.Split(replaced, "-")
126126
// the API requires the type, for now
127127
// regex matches against how Deployment pod name is constructed
128-
regex := regexp.MustCompile("[0-9]{8,10}-[a-z0-9]{5}$")
129-
if regex.MatchString(replaced) {
128+
regex := regexp.MustCompile("[a-z0-9]{8,10}-[a-z0-9]{5}$")
129+
if regex.MatchString(replaced) || len(parts) == 2 {
130130
psType = parts[0]
131131
} else {
132132
psType = parts[1]

cmd/ps_test.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,35 @@ import (
1515
func TestParseType(t *testing.T) {
1616
t.Parallel()
1717

18-
// test RC pod name
19-
appID := "earthy-underdog"
20-
rcPod := "earthy-underdog-v2-cmd-8yngj"
21-
psType, psName := parseType(rcPod, appID)
22-
if psType != "cmd" || psName != rcPod {
23-
t.Errorf("type was not cmd (got %s) or psName was not %s (got %s)", psType, rcPod, psName)
18+
var input = map[string]string{
19+
// RC pod name
20+
"earthy-underdog": "earthy-underdog-v2-cmd-8yngj",
21+
// Deployment pod name - they are longer due to hash
22+
"nonfat-yearbook": "nonfat-yearbook-cmd-2180299075-7na91",
23+
// newer style of Deployment pod name
24+
"foo-bar": "foo-bar-cmd-57f6c4bb68-7na91",
25+
// same as above but leaving out the app-name from the pod name
26+
"earthy-underdog2": "cmd-8yngj",
27+
"nonfat-yearbook2": "cmd-2180299075-7na91",
28+
"foo-bar2": "cmd-57f6c4bb68-7na91",
29+
// same as above but with app names without hyphens
30+
"earthy": "earthy-v2-cmd-8yngj",
31+
"nonfat": "nonfat-cmd-2180299075-7na91",
32+
"foo": "foo-cmd-57f6c4bb68-7na91",
33+
"earthy2": "cmd-8yngj",
34+
"nonfat2": "cmd-2180299075-7na91",
35+
"foo2": "cmd-57f6c4bb68-7na91",
2436
}
2537

26-
// test Deployment pod name - they are longer due to hash
27-
appID = "nonfat-yearbook"
28-
deployPod := "nonfat-yearbook-cmd-2180299075-7na91"
29-
psType, psName = parseType(deployPod, appID)
30-
if psType != "cmd" || psName != deployPod {
31-
t.Errorf("type was not cmd (got %s) or psName was not %s (got %s)", psType, deployPod, psName)
38+
for appID, podName := range input {
39+
psType, psName := parseType(podName, appID)
40+
if psType != "cmd" || psName != podName {
41+
t.Errorf("parseType(%#v, %#v): type was not cmd (got %s) or psName was not %s (got %s)", podName, appID, psType, podName, psName)
42+
}
3243
}
3344

3445
// test type by itself
35-
psType, psName = parseType("cmd", "fake")
46+
psType, psName := parseType("cmd", "fake")
3647
if psType != "cmd" || psName != "" {
3748
t.Error("type was not cmd")
3849
}

0 commit comments

Comments
 (0)