Skip to content

Commit

Permalink
Update stack status to reflect service status (#1759)
Browse files Browse the repository at this point in the history
* Updated CLI tests
  • Loading branch information
Neha Viswanathan authored and qube committed Nov 16, 2017
1 parent a7ed383 commit 6904b57
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 193 deletions.
23 changes: 15 additions & 8 deletions api/rpc/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *Server) List(ctx context.Context, in *ListRequest) (*ListReply, error)
return nil, convertError(err)
}
for _, stack := range stacks {
entry, err := s.toStackListEntry(ctx, stack)
entry, err := s.toStackEntry(ctx, stack)
if err != nil {
return nil, convertError(err)
}
Expand All @@ -91,13 +91,20 @@ func (s *Server) List(ctx context.Context, in *ListRequest) (*ListReply, error)
return reply, nil
}

func (s *Server) toStackListEntry(ctx context.Context, stack *stacks.Stack) (*StackListEntry, error) {
func (s *Server) toStackEntry(ctx context.Context, stack *stacks.Stack) (*StackEntry, error) {
status, err := s.Docker.StackStatus(ctx, stack.Name)
if err != nil {
return nil, convertError(err)
}
log.Infoln("[stack] Stack", stack.Name, "is", status.Status, "with", status.RunningServices, "out of", status.TotalServices, "services and", status.FailedServices, "failed services")
return &StackListEntry{Stack: stack, RunningServices: status.RunningServices, FailedServices: status.FailedServices, TotalServices: status.TotalServices, Status: status.Status}, nil
log.Infoln("[stack] Stack", stack.Name, "is", status.Status, "with", status.RunningServices, "out of", status.TotalServices, "services")
return &StackEntry{
Stack: stack,
RunningServices: status.RunningServices,
TotalServices: status.TotalServices,
Status: status.Status,
CompleteServices: status.CompleteServices,
PreparingServices: status.PreparingServices,
}, nil
}

// Remove implements stack.Server
Expand Down Expand Up @@ -136,7 +143,7 @@ func (s *Server) Remove(ctx context.Context, in *RemoveRequest) (*RemoveReply, e
func (s *Server) Services(ctx context.Context, in *ServicesRequest) (*ServicesReply, error) {
log.Infoln("[stack] Services", in.String())

stack, err := s.Stacks.GetByFragmentOrName(ctx, in.StackName)
stack, err := s.Stacks.GetByFragmentOrName(ctx, in.Stack)
if err != nil {
return nil, convertError(err)
}
Expand All @@ -152,7 +159,7 @@ func (s *Server) Services(ctx context.Context, in *ServicesRequest) (*ServicesRe

cols := strings.Split(output, "\n")
ans := &ServicesReply{
Services: []*StackService{},
Services: []*ServiceEntry{},
}
for _, col := range cols[1:] {
service := s.getOneServiceListLine(ctx, col)
Expand All @@ -163,10 +170,10 @@ func (s *Server) Services(ctx context.Context, in *ServicesRequest) (*ServicesRe
return ans, nil
}

func (s *Server) getOneServiceListLine(ctx context.Context, line string) *StackService {
func (s *Server) getOneServiceListLine(ctx context.Context, line string) *ServiceEntry {
cols := strings.Split(line, " ")
nn := 0
service := &StackService{}
service := &ServiceEntry{}
for _, val := range cols {
if val != "" {
nn++
Expand Down
172 changes: 90 additions & 82 deletions api/rpc/stack/stack.pb.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions api/rpc/stack/stack.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions api/rpc/stack/stack.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ service Stack {
}
rpc Services (ServicesRequest) returns (ServicesReply) {
option (google.api.http) = {
get: "/v1/stacks/{stack_name}/services"
get: "/v1/stacks/{stack}/services"
};
}
}
Expand All @@ -46,15 +46,16 @@ message ListRequest {
}

message ListReply {
repeated StackListEntry entries = 1;
repeated StackEntry entries = 1;
}

message StackListEntry {
message StackEntry {
stacks.Stack stack = 1;
int32 failed_services = 2;
int32 running_services = 3;
int32 total_services = 4;
string status = 5;
int32 running_services = 2;
int32 total_services = 3;
string status = 4;
int32 complete_services = 5;
int32 preparing_services = 6;
}

message RemoveRequest {
Expand All @@ -66,10 +67,10 @@ message RemoveReply {
}

message ServicesRequest {
string stack_name = 1;
string stack = 1;
}

message StackService {
message ServiceEntry {
string id = 1;
string name = 2;
string mode = 3;
Expand All @@ -78,5 +79,5 @@ message StackService {
}

message ServicesReply {
repeated StackService services = 1;
repeated ServiceEntry services = 1;
}
72 changes: 38 additions & 34 deletions api/rpc/stack/stack.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@
]
}
},
"/v1/stacks/{stack_name}/services": {
"get": {
"operationId": "Services",
"/v1/stacks/{stack}": {
"delete": {
"operationId": "Remove",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/stackServicesReply"
"$ref": "#/definitions/stackRemoveReply"
}
}
},
"parameters": [
{
"name": "stack_name",
"name": "stack",
"in": "path",
"required": true,
"type": "string"
Expand All @@ -79,14 +79,14 @@
]
}
},
"/v1/stacks/{stack}": {
"delete": {
"operationId": "Remove",
"/v1/stacks/{stack}/services": {
"get": {
"operationId": "Services",
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/stackRemoveReply"
"$ref": "#/definitions/stackServicesReply"
}
}
},
Expand Down Expand Up @@ -158,7 +158,7 @@
"entries": {
"type": "array",
"items": {
"$ref": "#/definitions/stackStackListEntry"
"$ref": "#/definitions/stackStackEntry"
}
}
}
Expand All @@ -171,27 +171,43 @@
}
}
},
"stackServiceEntry": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"mode": {
"type": "string"
},
"replicas": {
"type": "string"
},
"image": {
"type": "string"
}
}
},
"stackServicesReply": {
"type": "object",
"properties": {
"services": {
"type": "array",
"items": {
"$ref": "#/definitions/stackStackService"
"$ref": "#/definitions/stackServiceEntry"
}
}
}
},
"stackStackListEntry": {
"stackStackEntry": {
"type": "object",
"properties": {
"stack": {
"$ref": "#/definitions/stacksStack"
},
"failed_services": {
"type": "integer",
"format": "int32"
},
"running_services": {
"type": "integer",
"format": "int32"
Expand All @@ -202,26 +218,14 @@
},
"status": {
"type": "string"
}
}
},
"stackStackService": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"mode": {
"type": "string"
},
"replicas": {
"type": "string"
"complete_services": {
"type": "integer",
"format": "int32"
},
"image": {
"type": "string"
"preparing_services": {
"type": "integer",
"format": "int32"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/stack/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func list(c cli.Interface, opts listStackOptions) error {
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, cli.Padding, ' ', 0)
fmt.Fprintln(w, "ID\tNAME\tSERVICES\tFAILED SERVICES\tSTATUS\tOWNER")
fmt.Fprintln(w, "ID\tNAME\tRUNNING\tCOMPLETE\tPREPARING\tTOTAL\tSERVICES\tSTATUS\tOWNER")
for _, entry := range reply.Entries {
fmt.Fprintf(w, "%s\t%s\t%d/%d\t%d\t%s\t%s\n", entry.Stack.Id, entry.Stack.Name, entry.RunningServices, entry.TotalServices, entry.FailedServices, entry.Status, entry.Stack.Owner.User)
fmt.Fprintf(w, "%s\t%s\t%d\t%d\t%d\t%d\t%d/%d\t%s\t%s\n", entry.Stack.Id, entry.Stack.Name, entry.RunningServices, entry.CompleteServices, entry.PreparingServices, entry.TotalServices, entry.RunningServices, entry.TotalServices, entry.Status, entry.Stack.Owner.User)
}
w.Flush()
return nil
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stack/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewServicesCommand(c cli.Interface) *cobra.Command {
}

func services(c cli.Interface, stackName string) error {
req := &stack.ServicesRequest{StackName: stackName}
req := &stack.ServicesRequest{Stack: stackName}
client := stack.NewStackClient(c.ClientConn())
reply, err := client.Services(context.Background(), req)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/docker/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func (d *Docker) NodeList(ctx context.Context, options types.NodeListOptions) ([
}

// ExpectedNumberOfTasks returns expected number of tasks of a service
func (d *Docker) ExpectedNumberOfTasks(ctx context.Context, serviceID string) (int, error) {
var expectedTasks int
func (d *Docker) ExpectedNumberOfTasks(ctx context.Context, serviceID string) (int32, error) {
var expectedTasks int32
serviceInfo, err := d.ServiceInspect(ctx, serviceID)
if err != nil {
return 0, err
Expand All @@ -41,13 +41,13 @@ func (d *Docker) ExpectedNumberOfTasks(ctx context.Context, serviceID string) (i
if serviceInfo.Spec.Mode.Global != nil {
expectedTasks = matchingNodeCount
} else {
expectedTasks = int(*serviceInfo.Spec.Mode.Replicated.Replicas)
expectedTasks = int32(*serviceInfo.Spec.Mode.Replicated.Replicas)
}
return expectedTasks, nil
}

// numberOfMatchingNodes returns number of nodes matching placement constraints
func (d *Docker) numberOfMatchingNodes(ctx context.Context, serviceInfo swarm.Service) (int, error) {
func (d *Docker) numberOfMatchingNodes(ctx context.Context, serviceInfo swarm.Service) (int32, error) {
// placement constraints
constraints, _ := constraint.Parse(serviceInfo.Spec.TaskTemplate.Placement.Constraints)
// list all nodes in the swarm
Expand All @@ -56,7 +56,7 @@ func (d *Docker) numberOfMatchingNodes(ctx context.Context, serviceInfo swarm.Se
return 0, err
}
// inspect every node on the swarm to check for satisfying constraints
matchingNodes := 0
var matchingNodes int32
for _, node := range nodes {
apiNode := d.nodeToNode(ctx, node)
if constraint.NodeMatches(constraints, apiNode) {
Expand Down
Loading

0 comments on commit 6904b57

Please sign in to comment.