37 lines
903 B
Go
37 lines
903 B
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
)
|
|
|
|
const StackNamespace = "com.docker.stack.namespace"
|
|
|
|
type StackStatus struct {
|
|
Services []swarm.Service
|
|
Err error
|
|
}
|
|
|
|
func QueryStackStatus(contextName string) StackStatus {
|
|
cl, err := NewClientWithContext(contextName)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "does not exist") {
|
|
// No local context found, bail out gracefully
|
|
return StackStatus{[]swarm.Service{}, nil}
|
|
}
|
|
return StackStatus{[]swarm.Service{}, err}
|
|
}
|
|
ctx := context.Background()
|
|
filter := filters.NewArgs()
|
|
filter.Add("label", StackNamespace)
|
|
services, err := cl.ServiceList(ctx, types.ServiceListOptions{Filters: filter})
|
|
if err != nil {
|
|
return StackStatus{[]swarm.Service{}, err}
|
|
}
|
|
return StackStatus{services, nil}
|
|
}
|