MCPcopy
hub / github.com/spacecloud-io/space-cloud / GetContainers

Function GetContainers

space-cli/cmd/utils/docker.go:22–54  ·  view source on GitHub ↗

GetContainers gets container running in a specific cluster

(ctx context.Context, dockerClient *client.Client, clusterName, containerType string)

Source from the content-addressed store, hash-verified

20
21// GetContainers gets container running in a specific cluster
22func GetContainers(ctx context.Context, dockerClient *client.Client, clusterName, containerType string) ([]types.Container, error) {
23 argsName := filters.Arg("name", "space-cloud")
24 containers, err := dockerClient.ContainerList(ctx, types.ContainerListOptions{Filters: filters.NewArgs(argsName), All: true})
25 if err != nil {
26 _ = LogError(fmt.Sprintf("Unable to list containers - %s", err.Error()), nil)
27 return nil, err
28 }
29
30 clusterContainers := make([]types.Container, 0)
31 for _, container := range containers {
32 bigArr := strings.Split(container.Names[0], "--")
33 smallArr := strings.Split(bigArr[0], "-")
34 if clusterName == "default" {
35 if len(smallArr) == 3 || len(smallArr) == 2 {
36 if containerType == model.DbContainers && len(smallArr) != 2 {
37 continue
38 }
39 // Containers running in a default cluster
40 if isTypeSpecificContainer(containerType, container.Labels) {
41 clusterContainers = append(clusterContainers, container)
42 }
43 }
44 continue
45 }
46 if len(smallArr) >= 3 && smallArr[2] == clusterName {
47 if isTypeSpecificContainer(containerType, container.Labels) {
48 // Containers running in a specific cluster
49 clusterContainers = append(clusterContainers, container)
50 }
51 }
52 }
53 return clusterContainers, nil
54}
55
56func isTypeSpecificContainer(cType string, labels map[string]string) bool {
57 switch cType {

Callers

nothing calls this directly

Calls 3

LogErrorFunction · 0.85
isTypeSpecificContainerFunction · 0.85
ErrorMethod · 0.65

Tested by

no test coverage detected