Exemplo n.º 1
0
func getNode(ctx context.Context, c api.ControlClient, input string) (*api.Node, error) {
	// GetNode to match via full ID.
	rg, err := c.GetNode(ctx, &api.GetNodeRequest{NodeID: input})
	if err != nil {
		// If any error (including NotFound), ListServices to match via ID prefix and full name.
		rl, err := c.ListNodes(ctx,
			&api.ListNodesRequest{
				Filters: &api.ListNodesRequest_Filters{
					NamePrefixes: []string{input},
				},
			},
		)
		if err != nil {
			return nil, err
		}

		if len(rl.Nodes) == 0 {
			return nil, fmt.Errorf("node %s not found", input)
		}

		if l := len(rl.Nodes); l > 1 {
			return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
		}

		return rl.Nodes[0], nil
	}
	return rg.Node, nil
}
Exemplo n.º 2
0
func getSecret(ctx context.Context, c api.ControlClient, input string) (*api.Secret, error) {
	// not sure what it is, match by name or id prefix
	resp, err := c.ListSecrets(ctx,
		&api.ListSecretsRequest{
			Filters: &api.ListSecretsRequest_Filters{
				Names:      []string{input},
				IDPrefixes: []string{input},
			},
		},
	)
	if err != nil {
		return nil, err
	}

	switch len(resp.Secrets) {
	case 0:
		return nil, fmt.Errorf("secret %s not found", input)
	case 1:
		return resp.Secrets[0], nil
	default:
		// ok, multiple matches.  Prefer exact ID over exact name.  If no exact matches, return an error
		for _, s := range resp.Secrets {
			if s.ID == input {
				return s, nil
			}
		}
		for _, s := range resp.Secrets {
			if s.Spec.Annotations.Name == input {
				return s, nil
			}
		}
		return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", input, len(resp.Secrets))
	}
}
Exemplo n.º 3
0
// GetNetwork tries to query for a network as an ID and if it can't be
// found tries to query as a name. If the name query returns exactly
// one entry then it is returned to the caller. Otherwise an error is
// returned.
func GetNetwork(ctx context.Context, c api.ControlClient, input string) (*api.Network, error) {
	// GetService to match via full ID.
	rg, err := c.GetNetwork(ctx, &api.GetNetworkRequest{NetworkID: input})
	if err != nil {
		// If any error (including NotFound), ListServices to match via full name.
		rl, err := c.ListNetworks(ctx,
			&api.ListNetworksRequest{
				Filters: &api.ListNetworksRequest_Filters{
					Names: []string{input},
				},
			},
		)
		if err != nil {
			return nil, err
		}

		if len(rl.Networks) == 0 {
			return nil, fmt.Errorf("network %s not found", input)
		}

		if l := len(rl.Networks); l > 1 {
			return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)
		}

		return rl.Networks[0], nil
	}

	return rg.Network, nil
}
Exemplo n.º 4
0
func getCluster(ctx context.Context, c api.ControlClient, input string) (*api.Cluster, error) {
	rg, err := c.GetCluster(ctx, &api.GetClusterRequest{ClusterID: input})
	if err == nil {
		return rg.Cluster, nil
	}
	rl, err := c.ListClusters(ctx,
		&api.ListClustersRequest{
			Filters: &api.ListClustersRequest_Filters{
				NamePrefixes: []string{input},
			},
		},
	)
	if err != nil {
		return nil, err
	}

	if len(rl.Clusters) == 0 {
		return nil, fmt.Errorf("cluster %s not found", input)
	}

	if l := len(rl.Clusters); l > 1 {
		return nil, fmt.Errorf("cluster %s is ambiguous (%d matches found)", input, l)
	}

	return rl.Clusters[0], nil
}
Exemplo n.º 5
0
func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) {
	rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{})
	if err != nil {
		return nil, err
	}

	if len(rl.Clusters) == 0 {
		return nil, fmt.Errorf("swarm not found")
	}

	// TODO: assume one cluster only
	return rl.Clusters[0], nil
}
Exemplo n.º 6
0
func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) {
	rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{})
	if err != nil {
		return nil, err
	}

	if len(rl.Clusters) == 0 {
		return nil, errors.NewRequestNotFoundError(errNoSwarm)
	}

	// TODO: assume one cluster only
	return rl.Clusters[0], nil
}
Exemplo n.º 7
0
func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) {
	// GetNetwork to match via full ID.
	rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input})
	if err != nil {
		// If any error (including NotFound), ListNetworks to match via ID prefix and full name.
		rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{Filters: &swarmapi.ListNetworksRequest_Filters{Names: []string{input}}})
		if err != nil || len(rl.Networks) == 0 {
			rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{Filters: &swarmapi.ListNetworksRequest_Filters{IDPrefixes: []string{input}}})
		}

		if err != nil {
			return nil, err
		}

		if len(rl.Networks) == 0 {
			return nil, fmt.Errorf("network %s not found", input)
		}

		if l := len(rl.Networks); l > 1 {
			return nil, fmt.Errorf("network %s is ambigious (%d matches found)", input, l)
		}

		return rl.Networks[0], nil
	}
	return rg.Network, nil
}
Exemplo n.º 8
0
func getService(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Service, error) {
	// GetService to match via full ID.
	rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input})
	if err != nil {
		// If any error (including NotFound), ListServices to match via full name.
		rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{Names: []string{input}}})
		if err != nil || len(rl.Services) == 0 {
			// If any error or 0 result, ListServices to match via ID prefix.
			rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{IDPrefixes: []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
}
Exemplo n.º 9
0
func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
	// GetTask to match via full ID.
	rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input})
	if err != nil {
		// If any error (including NotFound), ListTasks to match via full name.
		rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{Names: []string{input}}})

		if err != nil || len(rl.Tasks) == 0 {
			// If any error or 0 result, ListTasks to match via ID prefix.
			rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{IDPrefixes: []string{input}}})
		}

		if err != nil {
			return nil, err
		}

		if len(rl.Tasks) == 0 {
			err := fmt.Errorf("task %s not found", input)
			return nil, errors.NewRequestNotFoundError(err)
		}

		if l := len(rl.Tasks); l > 1 {
			return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
		}

		return rl.Tasks[0], nil
	}
	return rg.Task, nil
}
Exemplo n.º 10
0
func managerStats(client swarmapi.ControlClient, currentNodeID string) (current bool, reachable int, unreachable int, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	nodes, err := client.ListNodes(ctx, &swarmapi.ListNodesRequest{})
	if err != nil {
		return false, 0, 0, err
	}
	for _, n := range nodes.Nodes {
		if n.ManagerStatus != nil {
			if n.ManagerStatus.Reachability == swarmapi.RaftMemberStatus_REACHABLE {
				reachable++
				if n.ID == currentNodeID {
					current = true
				}
			}
			if n.ManagerStatus.Reachability == swarmapi.RaftMemberStatus_UNREACHABLE {
				unreachable++
			}
		}
	}
	return
}