This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): add docker swarm provider
- Loading branch information
Showing
5 changed files
with
830 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/filters" | ||
"github.com/docker/docker/api/types/swarm" | ||
"github.com/docker/docker/client" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type DockerSwarmProvider struct { | ||
Client client.ServiceAPIClient | ||
} | ||
|
||
func NewDockerSwarmProvider() (*DockerSwarmProvider, error) { | ||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
log.Fatal(fmt.Errorf("%+v", "Could not connect to docker API")) | ||
return nil, err | ||
} | ||
return &DockerSwarmProvider{ | ||
Client: cli, | ||
}, nil | ||
} | ||
|
||
func (provider *DockerSwarmProvider) Start(name string) (InstanceState, error) { | ||
return provider.scale(name, 1) | ||
} | ||
|
||
func (provider *DockerSwarmProvider) Stop(name string) (InstanceState, error) { | ||
return provider.scale(name, 0) | ||
} | ||
|
||
func (provider *DockerSwarmProvider) scale(name string, replicas uint64) (InstanceState, error) { | ||
ctx := context.Background() | ||
service, err := provider.getServiceByName(name, ctx) | ||
|
||
if err != nil { | ||
return errorInstanceState(name, err) | ||
} | ||
|
||
foundName := provider.getInstanceName(name, *service) | ||
|
||
if service.Spec.Mode.Replicated == nil { | ||
return unrecoverableInstanceState(foundName, "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 errorInstanceState(foundName, err) | ||
} | ||
|
||
if len(response.Warnings) > 0 { | ||
return unrecoverableInstanceState(foundName, strings.Join(response.Warnings, ", ")) | ||
} | ||
|
||
return notReadyInstanceState(foundName) | ||
} | ||
|
||
func (provider *DockerSwarmProvider) GetState(name string) (InstanceState, error) { | ||
ctx := context.Background() | ||
|
||
service, err := provider.getServiceByName(name, ctx) | ||
if err != nil { | ||
return errorInstanceState(name, err) | ||
} | ||
|
||
foundName := provider.getInstanceName(name, *service) | ||
|
||
if service.Spec.Mode.Replicated == nil { | ||
return unrecoverableInstanceState(foundName, "swarm service is not in \"replicated\" mode") | ||
} | ||
|
||
if service.ServiceStatus.DesiredTasks != service.ServiceStatus.RunningTasks || service.ServiceStatus.DesiredTasks == 0 { | ||
return notReadyInstanceState(foundName) | ||
} | ||
|
||
return readyInstanceState(foundName) | ||
} | ||
|
||
func (provider *DockerSwarmProvider) getServiceByName(name string, ctx context.Context) (*swarm.Service, error) { | ||
opts := types.ServiceListOptions{ | ||
Filters: filters.NewArgs(), | ||
Status: true, | ||
} | ||
opts.Filters.Add("name", name) | ||
|
||
services, err := provider.Client.ServiceList(ctx, opts) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(services) == 0 { | ||
return nil, fmt.Errorf(fmt.Sprintf("service with name %s was not found", name)) | ||
} | ||
|
||
suffixMatches := make([]swarm.Service, 0) | ||
suffixMatchNames := make([]string, 0) | ||
|
||
for _, service := range services { | ||
// Exact match | ||
if service.Spec.Name == name { | ||
return &service, nil | ||
} else if strings.HasSuffix(service.Spec.Name, name) { | ||
suffixMatches = append(suffixMatches, service) | ||
suffixMatchNames = append(suffixMatchNames, service.Spec.Name) | ||
} else { | ||
log.Warnf("service %s was ignored because it did not match %s exactly or on suffix", service.Spec.Name, name) | ||
} | ||
} | ||
|
||
if len(suffixMatches) > 1 { | ||
return nil, fmt.Errorf("ambiguous service names found for \"%s\" (%s)", name, strings.Join(suffixMatchNames, ", ")) | ||
} | ||
|
||
if len(suffixMatches) == 1 { | ||
return &suffixMatches[0], nil | ||
} | ||
|
||
return nil, fmt.Errorf(fmt.Sprintf("service %s was not found because it did not match exactly or on suffix", name)) | ||
} | ||
|
||
func (provider *DockerSwarmProvider) getInstanceName(name string, service swarm.Service) string { | ||
if name == service.Spec.Name { | ||
return name | ||
} | ||
|
||
return fmt.Sprintf("%s (%s)", name, service.Spec.Name) | ||
} |
Oops, something went wrong.