Files
sablier/app/providers/docker/list.go
Alexis Couvreur dfb9bacf59 feat(providers): add provider.auto-stop-on-startup argument (#346)
This feature adds the capability to stop unregistered running instances upon startup.

Previously, you had to stop running instances manually or issue an initial request that will shut down instances afterwards.

With this change, all discovered instances will be shutdown. They need to be registered using labels. E.g.: sablier.enable=true

Fixes #153
2024-10-01 17:30:14 -07:00

61 lines
1.5 KiB
Go

package docker
import (
"context"
"fmt"
"github.com/acouvreur/sablier/app/discovery"
"github.com/acouvreur/sablier/app/providers"
"github.com/acouvreur/sablier/app/types"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"strings"
)
func (provider *DockerClassicProvider) InstanceList(ctx context.Context, options providers.InstanceListOptions) ([]types.Instance, error) {
args := filters.NewArgs()
for _, label := range options.Labels {
args.Add("label", label)
args.Add("label", fmt.Sprintf("%s=true", label))
}
containers, err := provider.Client.ContainerList(ctx, container.ListOptions{
All: options.All,
Filters: args,
})
if err != nil {
return nil, err
}
instances := make([]types.Instance, 0, len(containers))
for _, c := range containers {
instance := containerToInstance(c)
instances = append(instances, instance)
}
return instances, nil
}
func containerToInstance(c dockertypes.Container) types.Instance {
var group string
if _, ok := c.Labels[discovery.LabelEnable]; ok {
if g, ok := c.Labels[discovery.LabelGroup]; ok {
group = g
} else {
group = discovery.LabelGroupDefaultValue
}
}
return types.Instance{
Name: strings.TrimPrefix(c.Names[0], "/"), // Containers name are reported with a leading slash
Kind: "container",
Status: c.Status,
// Replicas: c.Status,
// DesiredReplicas: 1,
ScalingReplicas: 1,
Group: group,
}
}