Skip to content

Commit

Permalink
cmd: fix name prefix matching
Browse files Browse the repository at this point in the history
Currently if there are two services, and one service is named
busybox while the other is busybox-top. It is not possible to
remove the busybox service by giving its fullname which is also
a common name prefix of both. The only way out is removing the
busybox-top service first. This looks not an acceptable behavior.

The same problem applies to clusters, nodes and networks.

Fix it by finding a full name match if prefix matches are more
than one.

Another way to fix it is that start a request with a full name
filter before doing it with a name prefix filter. But I think
this way just presents the same result but adds one more call
overhead to server.

Signed-off-by: Jin Xu <jinuxstyle@hotmail.com>
v2: do not use node's Spec.Annotations.Name as defaut
  • Loading branch information
jinuxstyle committed Aug 5, 2016
1 parent af23e13 commit 2a644ad
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cmd/swarmctl/cluster/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func getCluster(ctx context.Context, c api.ControlClient, input string) (*api.Cl
}

if l := len(rl.Clusters); l > 1 {
for _, c := range rl.Clusters {
if c.Spec.Annotations.Name == input {
// Found a full name match
return c, nil
}
}
return nil, fmt.Errorf("cluster %s is ambiguous (%d matches found)", input, l)
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/swarmctl/network/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func GetNetwork(ctx context.Context, c api.ControlClient, input string) (*api.Ne
}

if l := len(rl.Networks); l > 1 {
for _, n := range rl.Networks {
if n.Spec.Annotations.Name == input {
// Found a full name match
return n, nil
}
}
return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/swarmctl/node/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ func getNode(ctx context.Context, c api.ControlClient, input string) (*api.Node,
}

if l := len(rl.Nodes); l > 1 {
for _, n := range rl.Nodes {
name := ""
if n.Description != nil {
name = n.Description.Hostname
}
if name == input {
// Found a full name match
return n, nil
}
}
return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/swarmctl/node/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ var (
common.PrintHeader(w, "ID", "Name", "Membership", "Status", "Availability", "Manager Status")
output = func(n *api.Node) {
spec := &n.Spec
name := spec.Annotations.Name
name := ""
availability := spec.Availability.String()
membership := spec.Membership.String()

if name == "" && n.Description != nil {
if n.Description != nil {
name = n.Description.Hostname
}
reachability := ""
Expand Down
6 changes: 6 additions & 0 deletions cmd/swarmctl/service/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func getService(ctx context.Context, c api.ControlClient, input string) (*api.Se
}

if l := len(rl.Services); l > 1 {
for _, s := range rl.Services {
if s.Spec.Annotations.Name == input {
// Found a full name match
return s, nil
}
}
return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
}

Expand Down

0 comments on commit 2a644ad

Please sign in to comment.