github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/cli/command/stack/common.go (about)

     1  package stack
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/api/types/filters"
     8  	"github.com/docker/docker/api/types/swarm"
     9  	"github.com/docker/docker/client"
    10  )
    11  
    12  const (
    13  	labelNamespace = "com.docker.stack.namespace"
    14  )
    15  
    16  func getStackLabels(namespace string, labels map[string]string) map[string]string {
    17  	if labels == nil {
    18  		labels = make(map[string]string)
    19  	}
    20  	labels[labelNamespace] = namespace
    21  	return labels
    22  }
    23  
    24  func getStackFilter(namespace string) filters.Args {
    25  	filter := filters.NewArgs()
    26  	filter.Add("label", labelNamespace+"="+namespace)
    27  	return filter
    28  }
    29  
    30  func getServices(
    31  	ctx context.Context,
    32  	apiclient client.APIClient,
    33  	namespace string,
    34  ) ([]swarm.Service, error) {
    35  	return apiclient.ServiceList(
    36  		ctx,
    37  		types.ServiceListOptions{Filters: getStackFilter(namespace)})
    38  }
    39  
    40  func getStackNetworks(
    41  	ctx context.Context,
    42  	apiclient client.APIClient,
    43  	namespace string,
    44  ) ([]types.NetworkResource, error) {
    45  	return apiclient.NetworkList(
    46  		ctx,
    47  		types.NetworkListOptions{Filters: getStackFilter(namespace)})
    48  }
    49  
    50  type namespace struct {
    51  	name string
    52  }
    53  
    54  func (n namespace) scope(name string) string {
    55  	return n.name + "_" + name
    56  }