Skip to content

Commit bcb8b01

Browse files
committed
fix(ps): give ps:restart a better understanding between RC and Deployment pods
And add some parsing tests
1 parent 993267e commit bcb8b01

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

cmd/ps.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,9 @@ func PsRestart(appID, target string) error {
8787
return err
8888
}
8989

90-
psType := ""
91-
psName := ""
92-
90+
psType, psName := "", ""
9391
if target != "" {
94-
if strings.Contains(target, "-") {
95-
replaced := strings.Replace(target, appID + "-", "", 1)
96-
parts := strings.Split(replaced, "-")
97-
// the API requires the type, for now
98-
psType = parts[0]
99-
// process name is the full pod
100-
psName = target
101-
} else {
102-
psType = target
103-
}
92+
psType, psName = parseType(target, appID)
10493
}
10594

10695
fmt.Printf("Restarting processes... but first, %s!\n", drinkOfChoice())
@@ -139,3 +128,26 @@ func printProcesses(appID string, processes []api.Pods) {
139128
}
140129
}
141130
}
131+
132+
func parseType(target string, appID string) (string, string) {
133+
psType, psName := "", ""
134+
135+
if strings.Contains(target, "-") {
136+
replaced := strings.Replace(target, appID + "-", "", 1)
137+
parts := strings.Split(replaced, "-")
138+
// the API requires the type, for now
139+
// regex matches against how Deployment pod name is constructed
140+
regex := regexp.MustCompile("[0-9]{8,10}-[a-z0-9]{5}$")
141+
if regex.MatchString(replaced) {
142+
psType = parts[0]
143+
} else {
144+
psType = parts[1]
145+
}
146+
// process name is the full pod
147+
psName = target
148+
} else {
149+
psType = target
150+
}
151+
152+
return psType, psName
153+
}

cmd/ps_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import "testing"
4+
5+
func TestParseType(t *testing.T) {
6+
t.Parallel()
7+
8+
// test RC pod name
9+
appID := "earthy-underdog"
10+
rcPod := "earthy-underdog-v2-cmd-8yngj"
11+
psType, psName := parseType(rcPod, appID)
12+
if psType != "cmd" || psName != rcPod {
13+
t.Errorf("type was not cmd (got %s) or psName was not %s (got %s)", psType, rcPod, psName)
14+
}
15+
16+
// test Deployment pod name - they are longer due to hash
17+
appID = "nonfat-yearbook"
18+
deployPod := "nonfat-yearbook-cmd-2180299075-7na91"
19+
psType, psName = parseType(deployPod, appID)
20+
if psType != "cmd" || psName != deployPod {
21+
t.Errorf("type was not cmd (got %s) or psName was not %s (got %s)", psType, deployPod, psName)
22+
}
23+
24+
25+
// test type by itself
26+
psType, psName = parseType("cmd", "fake")
27+
if psType != "cmd" || psName != "" {
28+
t.Error("type was not cmd")
29+
}
30+
31+
}

0 commit comments

Comments
 (0)