Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactors #350

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/discovery/autostop.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func StopAllUnregisteredInstances(ctx context.Context, provider providers.Provid
func stopFunc(ctx context.Context, name string, provider providers.Provider) func() error {
return func() error {
log.Tracef("Stopping %v...", name)
_, err := provider.Stop(ctx, name)
err := provider.Stop(ctx, name)
if err != nil {
log.Errorf("Could not stop %v: %v", name, err)
return err
Expand Down
9 changes: 4 additions & 5 deletions app/discovery/autostop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"github.com/acouvreur/sablier/app/discovery"
"github.com/acouvreur/sablier/app/instance"
"github.com/acouvreur/sablier/app/providers"
"github.com/acouvreur/sablier/app/providers/mock"
"github.com/acouvreur/sablier/app/types"
Expand All @@ -30,8 +29,8 @@ func TestStopAllUnregisteredInstances(t *testing.T) {
}).Return(instances, nil)

// Set up expectations for Stop
mockProvider.On("Stop", ctx, "instance2").Return(instance.State{}, nil)
mockProvider.On("Stop", ctx, "instance3").Return(instance.State{}, nil)
mockProvider.On("Stop", ctx, "instance2").Return(nil)
mockProvider.On("Stop", ctx, "instance3").Return(nil)

// Call the function under test
err := discovery.StopAllUnregisteredInstances(ctx, mockProvider, registered)
Expand Down Expand Up @@ -62,8 +61,8 @@ func TestStopAllUnregisteredInstances_WithError(t *testing.T) {
}).Return(instances, nil)

// Set up expectations for Stop with error
mockProvider.On("Stop", ctx, "instance2").Return(instance.State{}, errors.New("stop error"))
mockProvider.On("Stop", ctx, "instance3").Return(instance.State{}, nil)
mockProvider.On("Stop", ctx, "instance2").Return(errors.New("stop error"))
mockProvider.On("Stop", ctx, "instance3").Return(nil)

// Call the function under test
err := discovery.StopAllUnregisteredInstances(ctx, mockProvider, registered)
Expand Down
35 changes: 8 additions & 27 deletions app/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/acouvreur/sablier/app/discovery"
"github.com/acouvreur/sablier/app/providers"
"io"
"strings"

Expand All @@ -18,6 +19,9 @@ import (
log "github.com/sirupsen/logrus"
)

// Interface guard
var _ providers.Provider = (*DockerClassicProvider)(nil)

type DockerClassicProvider struct {
Client client.APIClient
desiredReplicas int
Expand Down Expand Up @@ -71,39 +75,16 @@ func (provider *DockerClassicProvider) GetGroups(ctx context.Context) (map[strin
return groups, nil
}

func (provider *DockerClassicProvider) Start(ctx context.Context, name string) (instance.State, error) {
err := provider.Client.ContainerStart(ctx, name, container.StartOptions{})

if err != nil {
return instance.ErrorInstanceState(name, err, provider.desiredReplicas)
}

return instance.State{
Name: name,
CurrentReplicas: 0,
DesiredReplicas: provider.desiredReplicas,
Status: instance.NotReady,
}, err
func (provider *DockerClassicProvider) Start(ctx context.Context, name string) error {
return provider.Client.ContainerStart(ctx, name, container.StartOptions{})
}

func (provider *DockerClassicProvider) Stop(ctx context.Context, name string) (instance.State, error) {
err := provider.Client.ContainerStop(ctx, name, container.StopOptions{})

if err != nil {
return instance.ErrorInstanceState(name, err, provider.desiredReplicas)
}

return instance.State{
Name: name,
CurrentReplicas: 0,
DesiredReplicas: provider.desiredReplicas,
Status: instance.NotReady,
}, nil
func (provider *DockerClassicProvider) Stop(ctx context.Context, name string) error {
return provider.Client.ContainerStop(ctx, name, container.StopOptions{})
}

func (provider *DockerClassicProvider) GetState(ctx context.Context, name string) (instance.State, error) {
spec, err := provider.Client.ContainerInspect(ctx, name)

if err != nil {
return instance.ErrorInstanceState(name, err, provider.desiredReplicas)
}
Expand Down
38 changes: 2 additions & 36 deletions app/providers/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ func TestDockerClassicProvider_Stop(t *testing.T) {
name string
fields fields
args args
want instance.State
wantErr bool
err error
}{
Expand All @@ -283,13 +282,6 @@ func TestDockerClassicProvider_Stop(t *testing.T) {
args: args{
name: "nginx",
},
want: instance.State{
Name: "nginx",
CurrentReplicas: 0,
DesiredReplicas: 1,
Status: instance.Unrecoverable,
Message: "container with name \"nginx\" was not found",
},
wantErr: true,
err: fmt.Errorf("container with name \"nginx\" was not found"),
},
Expand All @@ -301,12 +293,6 @@ func TestDockerClassicProvider_Stop(t *testing.T) {
args: args{
name: "nginx",
},
want: instance.State{
Name: "nginx",
CurrentReplicas: 0,
DesiredReplicas: 1,
Status: instance.NotReady,
},
wantErr: false,
err: nil,
},
Expand All @@ -320,14 +306,11 @@ func TestDockerClassicProvider_Stop(t *testing.T) {

tt.fields.Client.On("ContainerStop", mock.Anything, mock.Anything, mock.Anything).Return(tt.err)

got, err := provider.Stop(context.Background(), tt.args.name)
err := provider.Stop(context.Background(), tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("DockerClassicProvider.Stop() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DockerClassicProvider.Stop() = %v, want %v", got, tt.want)
}
})
}
}
Expand All @@ -343,7 +326,6 @@ func TestDockerClassicProvider_Start(t *testing.T) {
name string
fields fields
args args
want instance.State
wantErr bool
err error
}{
Expand All @@ -355,13 +337,6 @@ func TestDockerClassicProvider_Start(t *testing.T) {
args: args{
name: "nginx",
},
want: instance.State{
Name: "nginx",
CurrentReplicas: 0,
DesiredReplicas: 1,
Status: instance.Unrecoverable,
Message: "container with name \"nginx\" was not found",
},
wantErr: true,
err: fmt.Errorf("container with name \"nginx\" was not found"),
},
Expand All @@ -373,12 +348,6 @@ func TestDockerClassicProvider_Start(t *testing.T) {
args: args{
name: "nginx",
},
want: instance.State{
Name: "nginx",
CurrentReplicas: 0,
DesiredReplicas: 1,
Status: instance.NotReady,
},
wantErr: false,
err: nil,
},
Expand All @@ -392,14 +361,11 @@ func TestDockerClassicProvider_Start(t *testing.T) {

tt.fields.Client.On("ContainerStart", mock.Anything, mock.Anything, mock.Anything).Return(tt.err)

got, err := provider.Start(context.Background(), tt.args.name)
err := provider.Start(context.Background(), tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("DockerClassicProvider.Start() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DockerClassicProvider.Start() = %v, want %v", got, tt.want)
}
})
}
}
Expand Down
23 changes: 12 additions & 11 deletions app/providers/dockerswarm/docker_swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/acouvreur/sablier/app/discovery"
"github.com/acouvreur/sablier/app/providers"
"io"
"strings"

Expand All @@ -16,6 +17,9 @@ import (
log "github.com/sirupsen/logrus"
)

// Interface guard
var _ providers.Provider = (*DockerSwarmProvider)(nil)

type DockerSwarmProvider struct {
Client client.APIClient
desiredReplicas int
Expand All @@ -41,40 +45,37 @@ func NewDockerSwarmProvider() (*DockerSwarmProvider, error) {

}

func (provider *DockerSwarmProvider) Start(ctx context.Context, name string) (instance.State, error) {
func (provider *DockerSwarmProvider) Start(ctx context.Context, name string) error {
return provider.scale(ctx, name, uint64(provider.desiredReplicas))
}

func (provider *DockerSwarmProvider) Stop(ctx context.Context, name string) (instance.State, error) {
func (provider *DockerSwarmProvider) Stop(ctx context.Context, name string) error {
return provider.scale(ctx, name, 0)
}

func (provider *DockerSwarmProvider) scale(ctx context.Context, name string, replicas uint64) (instance.State, error) {
func (provider *DockerSwarmProvider) scale(ctx context.Context, name string, replicas uint64) error {
service, err := provider.getServiceByName(name, ctx)

if err != nil {
return instance.ErrorInstanceState(name, err, provider.desiredReplicas)
return err
}

foundName := provider.getInstanceName(name, *service)

if service.Spec.Mode.Replicated == nil {
return instance.UnrecoverableInstanceState(foundName, "swarm service is not in \"replicated\" mode", provider.desiredReplicas)
return errors.New("swarm service is not in \"replicated\" mode")
}

service.Spec.Mode.Replicated.Replicas = &replicas

response, err := provider.Client.ServiceUpdate(ctx, service.ID, service.Meta.Version, service.Spec, types.ServiceUpdateOptions{})

if err != nil {
return instance.ErrorInstanceState(foundName, err, provider.desiredReplicas)
return err
}

if len(response.Warnings) > 0 {
return instance.UnrecoverableInstanceState(foundName, strings.Join(response.Warnings, ", "), provider.desiredReplicas)
return fmt.Errorf("warning received updating swarm service [%s]: %s", foundName, strings.Join(response.Warnings, ", "))
}

return instance.NotReadyInstanceState(foundName, 0, provider.desiredReplicas)
return nil
}

func (provider *DockerSwarmProvider) GetGroups(ctx context.Context) (map[string][]string, error) {
Expand Down
Loading