github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/daemon/cluster/helpers.go (about)

     1  package cluster
     2  
     3  import (
     4  	"fmt"
     5  
     6  	swarmapi "github.com/docker/swarmkit/api"
     7  	"github.com/pkg/errors"
     8  	"golang.org/x/net/context"
     9  )
    10  
    11  func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) {
    12  	rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{})
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  
    17  	if len(rl.Clusters) == 0 {
    18  		return nil, errors.WithStack(errNoSwarm)
    19  	}
    20  
    21  	// TODO: assume one cluster only
    22  	return rl.Clusters[0], nil
    23  }
    24  
    25  func getNode(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Node, error) {
    26  	// GetNode to match via full ID.
    27  	if rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input}); err == nil {
    28  		return rg.Node, nil
    29  	}
    30  
    31  	// If any error (including NotFound), ListNodes to match via full name.
    32  	rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{
    33  		Filters: &swarmapi.ListNodesRequest_Filters{
    34  			Names: []string{input},
    35  		},
    36  	})
    37  	if err != nil || len(rl.Nodes) == 0 {
    38  		// If any error or 0 result, ListNodes to match via ID prefix.
    39  		rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{
    40  			Filters: &swarmapi.ListNodesRequest_Filters{
    41  				IDPrefixes: []string{input},
    42  			},
    43  		})
    44  	}
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	if len(rl.Nodes) == 0 {
    50  		err := fmt.Errorf("node %s not found", input)
    51  		return nil, notFoundError{err}
    52  	}
    53  
    54  	if l := len(rl.Nodes); l > 1 {
    55  		return nil, ambiguousResultsError{fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)}
    56  	}
    57  
    58  	return rl.Nodes[0], nil
    59  }
    60  
    61  func getService(ctx context.Context, c swarmapi.ControlClient, input string, insertDefaults bool) (*swarmapi.Service, error) {
    62  	// GetService to match via full ID.
    63  	if rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input, InsertDefaults: insertDefaults}); err == nil {
    64  		return rg.Service, nil
    65  	}
    66  
    67  	// If any error (including NotFound), ListServices to match via full name.
    68  	rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{
    69  		Filters: &swarmapi.ListServicesRequest_Filters{
    70  			Names: []string{input},
    71  		},
    72  	})
    73  	if err != nil || len(rl.Services) == 0 {
    74  		// If any error or 0 result, ListServices to match via ID prefix.
    75  		rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{
    76  			Filters: &swarmapi.ListServicesRequest_Filters{
    77  				IDPrefixes: []string{input},
    78  			},
    79  		})
    80  	}
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	if len(rl.Services) == 0 {
    86  		err := fmt.Errorf("service %s not found", input)
    87  		return nil, notFoundError{err}
    88  	}
    89  
    90  	if l := len(rl.Services); l > 1 {
    91  		return nil, ambiguousResultsError{fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)}
    92  	}
    93  
    94  	if !insertDefaults {
    95  		return rl.Services[0], nil
    96  	}
    97  
    98  	rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: rl.Services[0].ID, InsertDefaults: true})
    99  	if err == nil {
   100  		return rg.Service, nil
   101  	}
   102  	return nil, err
   103  }
   104  
   105  func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
   106  	// GetTask to match via full ID.
   107  	if rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input}); err == nil {
   108  		return rg.Task, nil
   109  	}
   110  
   111  	// If any error (including NotFound), ListTasks to match via full name.
   112  	rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{
   113  		Filters: &swarmapi.ListTasksRequest_Filters{
   114  			Names: []string{input},
   115  		},
   116  	})
   117  	if err != nil || len(rl.Tasks) == 0 {
   118  		// If any error or 0 result, ListTasks to match via ID prefix.
   119  		rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{
   120  			Filters: &swarmapi.ListTasksRequest_Filters{
   121  				IDPrefixes: []string{input},
   122  			},
   123  		})
   124  	}
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	if len(rl.Tasks) == 0 {
   130  		err := fmt.Errorf("task %s not found", input)
   131  		return nil, notFoundError{err}
   132  	}
   133  
   134  	if l := len(rl.Tasks); l > 1 {
   135  		return nil, ambiguousResultsError{fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)}
   136  	}
   137  
   138  	return rl.Tasks[0], nil
   139  }
   140  
   141  func getSecret(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Secret, error) {
   142  	// attempt to lookup secret by full ID
   143  	if rg, err := c.GetSecret(ctx, &swarmapi.GetSecretRequest{SecretID: input}); err == nil {
   144  		return rg.Secret, nil
   145  	}
   146  
   147  	// If any error (including NotFound), ListSecrets to match via full name.
   148  	rl, err := c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
   149  		Filters: &swarmapi.ListSecretsRequest_Filters{
   150  			Names: []string{input},
   151  		},
   152  	})
   153  	if err != nil || len(rl.Secrets) == 0 {
   154  		// If any error or 0 result, ListSecrets to match via ID prefix.
   155  		rl, err = c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
   156  			Filters: &swarmapi.ListSecretsRequest_Filters{
   157  				IDPrefixes: []string{input},
   158  			},
   159  		})
   160  	}
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	if len(rl.Secrets) == 0 {
   166  		err := fmt.Errorf("secret %s not found", input)
   167  		return nil, notFoundError{err}
   168  	}
   169  
   170  	if l := len(rl.Secrets); l > 1 {
   171  		return nil, ambiguousResultsError{fmt.Errorf("secret %s is ambiguous (%d matches found)", input, l)}
   172  	}
   173  
   174  	return rl.Secrets[0], nil
   175  }
   176  
   177  func getConfig(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Config, error) {
   178  	// attempt to lookup config by full ID
   179  	if rg, err := c.GetConfig(ctx, &swarmapi.GetConfigRequest{ConfigID: input}); err == nil {
   180  		return rg.Config, nil
   181  	}
   182  
   183  	// If any error (including NotFound), ListConfigs to match via full name.
   184  	rl, err := c.ListConfigs(ctx, &swarmapi.ListConfigsRequest{
   185  		Filters: &swarmapi.ListConfigsRequest_Filters{
   186  			Names: []string{input},
   187  		},
   188  	})
   189  	if err != nil || len(rl.Configs) == 0 {
   190  		// If any error or 0 result, ListConfigs to match via ID prefix.
   191  		rl, err = c.ListConfigs(ctx, &swarmapi.ListConfigsRequest{
   192  			Filters: &swarmapi.ListConfigsRequest_Filters{
   193  				IDPrefixes: []string{input},
   194  			},
   195  		})
   196  	}
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	if len(rl.Configs) == 0 {
   202  		err := fmt.Errorf("config %s not found", input)
   203  		return nil, notFoundError{err}
   204  	}
   205  
   206  	if l := len(rl.Configs); l > 1 {
   207  		return nil, ambiguousResultsError{fmt.Errorf("config %s is ambiguous (%d matches found)", input, l)}
   208  	}
   209  
   210  	return rl.Configs[0], nil
   211  }
   212  
   213  func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) {
   214  	// GetNetwork to match via full ID.
   215  	if rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input}); err == nil {
   216  		return rg.Network, nil
   217  	}
   218  
   219  	// If any error (including NotFound), ListNetworks to match via ID prefix and full name.
   220  	rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
   221  		Filters: &swarmapi.ListNetworksRequest_Filters{
   222  			Names: []string{input},
   223  		},
   224  	})
   225  	if err != nil || len(rl.Networks) == 0 {
   226  		rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
   227  			Filters: &swarmapi.ListNetworksRequest_Filters{
   228  				IDPrefixes: []string{input},
   229  			},
   230  		})
   231  	}
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  
   236  	if len(rl.Networks) == 0 {
   237  		return nil, fmt.Errorf("network %s not found", input)
   238  	}
   239  
   240  	if l := len(rl.Networks); l > 1 {
   241  		return nil, ambiguousResultsError{fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)}
   242  	}
   243  
   244  	return rl.Networks[0], nil
   245  }