Skip to content

Commit 40d68b5

Browse files
committed
fix(ps:restart): make ps:restart work with kubernetes
Fixes #214
1 parent 792cbcc commit 40d68b5

11 files changed

Lines changed: 228 additions & 126 deletions

File tree

client/cmd/ps.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,15 @@ func PsRestart(appID, target string) error {
8989
}
9090

9191
psType := ""
92-
psNum := -1
92+
psName := ""
9393

9494
if target != "" {
95-
if strings.Contains(target, ".") {
96-
parts := strings.Split(target, ".")
97-
psType = parts[0]
98-
psNum, err = strconv.Atoi(parts[1])
99-
100-
if err != nil {
101-
return err
102-
}
95+
if strings.Contains(target, "-") {
96+
parts := strings.Split(target, "-")
97+
// the API requires the type, for now
98+
psType = parts[len(parts)-2]
99+
// process name is the full pod
100+
psName = target
103101
} else {
104102
psType = target
105103
}
@@ -109,7 +107,7 @@ func PsRestart(appID, target string) error {
109107
startTime := time.Now()
110108
quit := progress()
111109

112-
_, err = ps.Restart(c, appID, psType, psNum)
110+
processes, err := ps.Restart(c, appID, psType, psName)
113111

114112
quit <- true
115113
<-quit
@@ -118,14 +116,13 @@ func PsRestart(appID, target string) error {
118116
return err
119117
}
120118

121-
fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
122-
123-
processes, _, err := ps.List(c, appID, c.ResponseLimit)
124-
if err != nil {
125-
return err
119+
if len(processes) == 0 {
120+
fmt.Println("Could not find any processes to restart")
121+
} else {
122+
fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
123+
printProcesses(appID, processes)
126124
}
127125

128-
printProcesses(appID, processes)
129126
return nil
130127
}
131128

client/controller/api/ps.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
package api
22

3-
// Process defines the structure of a process.
4-
type Process struct {
5-
Owner string `json:"owner"`
6-
App string `json:"app"`
7-
Release string `json:"release"`
8-
Created string `json:"created"`
9-
Updated string `json:"updated"`
10-
UUID string `json:"uuid"`
11-
Type string `json:"type"`
12-
Num int `json:"num"`
13-
State string `json:"state"`
14-
}
3+
import "github.com/deis/pkg/time"
154

165
// Pods defines the structure of a process.
176
type Pods struct {
18-
Release string `json:"release"`
19-
Type string `json:"type"`
20-
Name string `json:"name"`
21-
State string `json:"state"`
7+
Release string `json:"release"`
8+
Type string `json:"type"`
9+
Name string `json:"name"`
10+
State string `json:"state"`
11+
Started time.Time `json:"started"`
2212
}

client/controller/models/ps/ps.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ps
33
import (
44
"encoding/json"
55
"fmt"
6-
"strconv"
76

87
"github.com/deis/workflow/client/controller/api"
98
"github.com/deis/workflow/client/controller/client"
@@ -40,28 +39,28 @@ func Scale(c *client.Client, appID string, targets map[string]int) error {
4039
}
4140

4241
// Restart an app's processes.
43-
func Restart(c *client.Client, appID string, procType string, num int) ([]api.Process, error) {
44-
u := fmt.Sprintf("/v2/apps/%s/containers/", appID)
42+
func Restart(c *client.Client, appID string, procType string, name string) ([]api.Pods, error) {
43+
u := fmt.Sprintf("/v2/apps/%s/pods/", appID)
4544

4645
if procType == "" {
4746
u += "restart/"
4847
} else {
49-
if num == -1 {
48+
if name == "" {
5049
u += procType + "/restart/"
5150
} else {
52-
u += procType + "/" + strconv.Itoa(num) + "/restart/"
51+
u += procType + "/" + name + "/restart/"
5352
}
5453
}
5554

5655
body, err := c.BasicRequest("POST", u, nil)
5756

5857
if err != nil {
59-
return []api.Process{}, err
58+
return []api.Pods{}, err
6059
}
6160

62-
procs := []api.Process{}
61+
procs := []api.Pods{}
6362
if err = json.Unmarshal([]byte(body), &procs); err != nil {
64-
return []api.Process{}, err
63+
return []api.Pods{}, err
6564
}
6665

6766
return procs, nil

client/controller/models/ps/ps_test.go

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"reflect"
1010
"testing"
1111

12+
"github.com/deis/pkg/time"
1213
"github.com/deis/workflow/client/controller/api"
1314
"github.com/deis/workflow/client/controller/client"
1415
"github.com/deis/workflow/client/version"
@@ -24,52 +25,41 @@ const processesFixture string = `
2425
"release": "v2",
2526
"type": "web",
2627
"name": "example-go-v2-web-45678",
27-
"state": "up"
28+
"state": "up",
29+
"started": "2016-02-13T00:47:52"
2830
}
2931
]
3032
}`
3133

3234
const restartAllFixture string = `[
3335
{
34-
"owner": "test",
35-
"app": "example-go",
3636
"release": "v2",
37-
"created": "2014-01-01T00:00:00UTC",
38-
"updated": "2014-01-01T00:00:00UTC",
39-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
4037
"type": "web",
41-
"num": 1,
42-
"state": "up"
38+
"name": "example-go-v2-web-45678",
39+
"state": "up",
40+
"started": "2016-02-13T00:47:52"
4341
}
4442
]
4543
`
4644

4745
const restartWorkerFixture string = `[
4846
{
49-
"owner": "test",
50-
"app": "example-go",
5147
"release": "v2",
52-
"created": "2014-01-01T00:00:00UTC",
53-
"updated": "2014-01-01T00:00:00UTC",
54-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
5548
"type": "worker",
56-
"num": 1,
57-
"state": "up"
49+
"name": "example-go-v2-worker-45678",
50+
"state": "up",
51+
"started": "2016-02-13T00:47:52"
5852
}
5953
]
6054
`
6155

6256
const restartWebTwoFixture string = `[
6357
{
64-
"owner": "test",
65-
"app": "example-go",
6658
"release": "v2",
67-
"created": "2014-01-01T00:00:00UTC",
68-
"updated": "2014-01-01T00:00:00UTC",
69-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
7059
"type": "web",
71-
"num": 2,
72-
"state": "up"
60+
"name": "example-go-v2-web-45678",
61+
"state": "up",
62+
"started": "2016-02-13T00:47:52"
7363
}
7464
]
7565
`
@@ -86,18 +76,23 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
8676
return
8777
}
8878

89-
if req.URL.Path == "/v2/apps/example-go/containers/restart/" && req.Method == "POST" {
79+
if req.URL.Path == "/v2/apps/example-go/pods/restart/" && req.Method == "POST" {
9080
res.Write([]byte(restartAllFixture))
9181
return
9282
}
9383

94-
if req.URL.Path == "/v2/apps/example-go/containers/worker/restart/" && req.Method == "POST" {
84+
if req.URL.Path == "/v2/apps/example-go/pods/web/restart/" && req.Method == "POST" {
85+
res.Write([]byte(restartWebTwoFixture))
86+
return
87+
}
88+
89+
if req.URL.Path == "/v2/apps/example-go/pods/worker/example-go-v2-worker-45678/restart/" && req.Method == "POST" {
9590
res.Write([]byte(restartWorkerFixture))
9691
return
9792
}
9893

99-
if req.URL.Path == "/v2/apps/example-go/containers/web/2/restart/" && req.Method == "POST" {
100-
res.Write([]byte(restartWebTwoFixture))
94+
if req.URL.Path == "/v2/apps/example-go/pods/worker/worker-45678/restart/" && req.Method == "POST" {
95+
res.Write([]byte(restartWorkerFixture))
10196
return
10297
}
10398

@@ -130,12 +125,15 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
130125
func TestProcessesList(t *testing.T) {
131126
t.Parallel()
132127

128+
started := time.Time{}
129+
started.UnmarshalText([]byte("2016-02-13T00:47:52"))
133130
expected := []api.Pods{
134131
{
135132
Release: "v2",
136133
Type: "web",
137134
Name: "example-go-v2-web-45678",
138135
State: "up",
136+
Started: started,
139137
},
140138
}
141139

@@ -165,63 +163,66 @@ func TestProcessesList(t *testing.T) {
165163
}
166164

167165
type testExpected struct {
168-
Num int
166+
Name string
169167
Type string
170-
Expected []api.Process
168+
Expected []api.Pods
171169
}
172170

173171
func TestAppsRestart(t *testing.T) {
174172
t.Parallel()
175173

174+
started := time.Time{}
175+
started.UnmarshalText([]byte("2016-02-13T00:47:52"))
176176
tests := []testExpected{
177177
{
178-
Num: -1,
178+
Name: "",
179179
Type: "",
180-
Expected: []api.Process{
180+
Expected: []api.Pods{
181181
{
182-
Owner: "test",
183-
App: "example-go",
184182
Release: "v2",
185-
Created: "2014-01-01T00:00:00UTC",
186-
Updated: "2014-01-01T00:00:00UTC",
187-
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
188183
Type: "web",
189-
Num: 1,
184+
Name: "example-go-v2-web-45678",
185+
State: "up",
186+
Started: started,
187+
},
188+
},
189+
},
190+
{
191+
Name: "example-go-v2-worker-45678",
192+
Type: "worker",
193+
Expected: []api.Pods{
194+
{
195+
Release: "v2",
196+
Type: "worker",
197+
Name: "example-go-v2-worker-45678",
190198
State: "up",
199+
Started: started,
191200
},
192201
},
193202
},
194203
{
195-
Num: -1,
204+
Name: "worker-45678",
196205
Type: "worker",
197-
Expected: []api.Process{
206+
Expected: []api.Pods{
198207
{
199-
Owner: "test",
200-
App: "example-go",
201208
Release: "v2",
202-
Created: "2014-01-01T00:00:00UTC",
203-
Updated: "2014-01-01T00:00:00UTC",
204-
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
205209
Type: "worker",
206-
Num: 1,
210+
Name: "example-go-v2-worker-45678",
207211
State: "up",
212+
Started: started,
208213
},
209214
},
210215
},
211216
{
212-
Num: 2,
217+
Name: "",
213218
Type: "web",
214-
Expected: []api.Process{
219+
Expected: []api.Pods{
215220
{
216-
Owner: "test",
217-
App: "example-go",
218221
Release: "v2",
219-
Created: "2014-01-01T00:00:00UTC",
220-
Updated: "2014-01-01T00:00:00UTC",
221-
UUID: "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
222222
Type: "web",
223-
Num: 2,
223+
Name: "example-go-v2-web-45678",
224224
State: "up",
225+
Started: started,
225226
},
226227
},
227228
},
@@ -242,7 +243,7 @@ func TestAppsRestart(t *testing.T) {
242243
client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
243244

244245
for _, test := range tests {
245-
actual, err := Restart(&client, "example-go", test.Type, test.Num)
246+
actual, err := Restart(&client, "example-go", test.Type, test.Name)
246247

247248
if err != nil {
248249
t.Error(err)

client/glide.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/parser/ps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Usage: deis ps:restart [<type>] [options]
6868
Arguments:
6969
<type>
7070
the process name as defined in your Procfile, such as 'web' or 'worker'.
71-
To restart a particular process, use 'web.1'.
71+
To restart a particular process, use 'web-asdfg' or 'app-v2-web-asdfg'.
7272
7373
Options:
7474
-a --app=<app>

0 commit comments

Comments
 (0)