mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-26 07:13:36 +01:00
add list implementation
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/sablierapp/sablier/pkg/provider"
|
||||
"github.com/sablierapp/sablier/pkg/sablier"
|
||||
"os"
|
||||
)
|
||||
@@ -61,11 +60,6 @@ func (d *DockerProvider) Stop(ctx context.Context, name string) error {
|
||||
return d.Client.ContainerStop(ctx, name, container.StopOptions{})
|
||||
}
|
||||
|
||||
func (d *DockerProvider) List(ctx context.Context, opts provider.ListOptions) ([]sablier.InstanceConfig, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (d *DockerProvider) Events(ctx context.Context) (<-chan sablier.Message, <-chan error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
|
||||
@@ -30,10 +30,7 @@ func TestDockerProvider_Info(t *testing.T) {
|
||||
_, err := dind.CreateMimic(ctx, MimicOptions{
|
||||
Name: "test-info-created",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
},
|
||||
name: "test-info-created",
|
||||
},
|
||||
|
||||
42
pkg/provider/docker/list.go
Normal file
42
pkg/provider/docker/list.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/sablierapp/sablier/pkg/provider"
|
||||
"github.com/sablierapp/sablier/pkg/sablier"
|
||||
)
|
||||
|
||||
func (d *DockerProvider) List(ctx context.Context, opts provider.ListOptions) ([]sablier.InstanceConfig, error) {
|
||||
args := filters.NewArgs()
|
||||
args.Add("label", "sablier.enable")
|
||||
args.Add("label", "sablier.enable=true")
|
||||
|
||||
found, err := d.Client.ContainerList(ctx, container.ListOptions{
|
||||
Filters: args,
|
||||
All: opts.All,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("found %d containers\n", len(found))
|
||||
infos := make([]sablier.InstanceConfig, 0, len(found))
|
||||
for _, c := range found {
|
||||
fmt.Printf("container: %v", c)
|
||||
group, ok := c.Labels["sablier.group"]
|
||||
if !ok || group == "" {
|
||||
group = FormatName(c.Names[0]) // Group defaults to the container name
|
||||
}
|
||||
conf := sablier.InstanceConfig{
|
||||
Name: FormatName(c.Names[0]),
|
||||
Group: group,
|
||||
DesiredReplicas: 1,
|
||||
}
|
||||
infos = append(infos, conf)
|
||||
}
|
||||
|
||||
return infos, nil
|
||||
}
|
||||
98
pkg/provider/docker/list_test.go
Normal file
98
pkg/provider/docker/list_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package docker_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/sablierapp/sablier/pkg/provider"
|
||||
"github.com/sablierapp/sablier/pkg/provider/docker"
|
||||
"github.com/sablierapp/sablier/pkg/sablier"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDockerProvider_List(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
type args struct {
|
||||
do func(dind *dindContainer) error
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []sablier.InstanceConfig
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "no container",
|
||||
args: args{
|
||||
do: func(dind *dindContainer) error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
want: []sablier.InstanceConfig{},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "1 registered containers with default group",
|
||||
args: args{
|
||||
do: func(dind *dindContainer) error {
|
||||
_, err := dind.CreateMimic(ctx, MimicOptions{
|
||||
Name: "registered",
|
||||
Registered: true,
|
||||
})
|
||||
return err
|
||||
},
|
||||
},
|
||||
want: []sablier.InstanceConfig{
|
||||
{
|
||||
Name: "registered",
|
||||
Group: "registered",
|
||||
DesiredReplicas: 1,
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
|
||||
{
|
||||
name: "1 registered container with custom group",
|
||||
args: args{
|
||||
do: func(dind *dindContainer) error {
|
||||
_, err := dind.CreateMimic(ctx, MimicOptions{
|
||||
Name: "registered",
|
||||
Registered: true,
|
||||
SablierGroup: "mygroup",
|
||||
})
|
||||
return err
|
||||
},
|
||||
},
|
||||
want: []sablier.InstanceConfig{
|
||||
{
|
||||
Name: "registered",
|
||||
Group: "mygroup",
|
||||
DesiredReplicas: 1,
|
||||
},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dinD, err := setupDinD(t, ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d, err := docker.NewDockerProvider(dinD.client)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = tt.args.do(dinD)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := d.List(ctx, provider.ListOptions{All: true})
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("List")) {
|
||||
return
|
||||
}
|
||||
assert.Equal(t, tt.want, got, "List")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ type MimicOptions struct {
|
||||
WithHealth bool
|
||||
HealthyAfter time.Duration
|
||||
RunningAfter time.Duration
|
||||
Registered bool
|
||||
SablierGroup string
|
||||
}
|
||||
|
||||
@@ -35,14 +36,19 @@ func (d *dindContainer) CreateMimic(ctx context.Context, opts MimicOptions) (con
|
||||
return container.CreateResponse{}, err
|
||||
}
|
||||
|
||||
labels := make(map[string]string)
|
||||
if opts.Registered == true {
|
||||
labels["sablier.enable"] = "true"
|
||||
if opts.SablierGroup != "" {
|
||||
labels["sablier.group"] = opts.SablierGroup
|
||||
}
|
||||
}
|
||||
|
||||
if opts.WithHealth == false {
|
||||
return d.client.ContainerCreate(ctx, &container.Config{
|
||||
Cmd: []string{"/mimic", "-running", "-running-after", opts.RunningAfter.String(), "-healthy=false"},
|
||||
Image: "docker.io/sablierapp/mimic:v0.3.1",
|
||||
Labels: map[string]string{
|
||||
"sablier.enable": "true",
|
||||
"sablier.group": opts.SablierGroup,
|
||||
},
|
||||
Cmd: []string{"/mimic", "-running", "-running-after", opts.RunningAfter.String(), "-healthy=false"},
|
||||
Image: "docker.io/sablierapp/mimic:v0.3.1",
|
||||
Labels: labels,
|
||||
}, nil, nil, nil, opts.Name)
|
||||
}
|
||||
return d.client.ContainerCreate(ctx, &container.Config{
|
||||
@@ -55,11 +61,8 @@ func (d *dindContainer) CreateMimic(ctx context.Context, opts MimicOptions) (con
|
||||
StartInterval: time.Second,
|
||||
Retries: 50,
|
||||
},
|
||||
Image: "docker.io/sablierapp/mimic:v0.3.1",
|
||||
Labels: map[string]string{
|
||||
"sablier.enable": "true",
|
||||
"sablier.group": opts.SablierGroup,
|
||||
},
|
||||
Image: "docker.io/sablierapp/mimic:v0.3.1",
|
||||
Labels: labels,
|
||||
}, nil, nil, nil, opts.Name)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user