forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
49 lines (41 loc) · 1.12 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package service
import (
"fmt"
"strconv"
"golang.org/x/net/context"
"github.com/docker/swarmkit/api"
)
func getService(ctx context.Context, c api.ControlClient, input string) (*api.Service, error) {
// GetService to match via full ID.
rg, err := c.GetService(ctx, &api.GetServiceRequest{ServiceID: input})
if err != nil {
// If any error (including NotFound), ListServices to match via ID prefix and full name.
rl, err := c.ListServices(ctx,
&api.ListServicesRequest{
Filters: &api.ListServicesRequest_Filters{
Names: []string{input},
},
},
)
if err != nil {
return nil, err
}
if len(rl.Services) == 0 {
return nil, fmt.Errorf("service %s not found", input)
}
if l := len(rl.Services); l > 1 {
return nil, fmt.Errorf("service %s is ambigious (%d matches found)", input, l)
}
return rl.Services[0], nil
}
return rg.Service, nil
}
func getServiceInstancesTxt(s *api.Service) string {
switch t := s.Spec.GetMode().(type) {
case *api.ServiceSpec_Global:
return "global"
case *api.ServiceSpec_Replicated:
return strconv.FormatUint(t.Replicated.Instances, 10)
}
return ""
}