Skip to content

Commit

Permalink
fix: #18 match items by full name
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jul 4, 2024
1 parent 64d4fb2 commit 1aa7686
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 52 deletions.
16 changes: 8 additions & 8 deletions internal/app/ptah-agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

var (
ErrConfigNotFound = fmt.Errorf("config not found")
ErrConfigNotFound = fmt.Errorf("config not found")
ErrServiceNotFound = fmt.Errorf("service not found")
ErrSecretNotFound = fmt.Errorf("secret not found")
)

func (e *taskExecutor) createDockerConfig(ctx context.Context, req *t.CreateConfigReq) (*t.CreateConfigRes, error) {
Expand Down Expand Up @@ -42,13 +44,11 @@ func (e *taskExecutor) getConfigByName(ctx context.Context, name string) (*swarm
return nil, err
}

if len(configs) > 1 {
return nil, fmt.Errorf("multiple configs with name %s found", name)
for _, config := range configs {
if config.Spec.Name == name {
return &config, nil
}
}

if len(configs) == 0 {
return nil, errors.Wrapf(ErrConfigNotFound, "config with name %s not found", name)
}

return &configs[0], nil
return nil, errors.Wrapf(ErrConfigNotFound, "config with name %s not found", name)
}
28 changes: 28 additions & 0 deletions internal/app/ptah-agent/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package ptah_agent

import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/pkg/errors"
t "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
)

Expand All @@ -17,3 +21,27 @@ func (e *taskExecutor) createDockerSecret(ctx context.Context, req *t.CreateSecr

return &res, nil
}

func (e *taskExecutor) getSecretByName(ctx context.Context, name string) (*swarm.Secret, error) {
if name == "" {
return nil, errors.Wrapf(ErrSecretNotFound, "secret name is empty")
}

secrets, err := e.docker.SecretList(ctx, types.SecretListOptions{
Filters: filters.NewArgs(
filters.Arg("name", name),
),
})

if err != nil {
return nil, err
}

for _, secret := range secrets {
if secret.Spec.Name == name {
return &secret, nil
}
}

return nil, errors.Wrapf(ErrSecretNotFound, "secret with name %s not found", name)
}
73 changes: 29 additions & 44 deletions internal/app/ptah-agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,11 @@ func (e *taskExecutor) updateDockerService(ctx context.Context, req *t.UpdateSer
return nil, errors.Wrapf(err, "update docker service")
}

services, err := e.docker.ServiceList(ctx, types.ServiceListOptions{
Filters: filters.NewArgs(
filters.Arg("name", req.SwarmServiceSpec.Name),
),
})

service, err := e.getServiceByName(ctx, req.SwarmServiceSpec.Name)
if err != nil {
return nil, errors.Wrapf(err, "update docker service")
}

if len(services) > 1 {
return nil, fmt.Errorf("multiple services with name %s found", req.SwarmServiceSpec.Name)
}

if len(services) == 0 {
return nil, fmt.Errorf("service with name %s not found", req.SwarmServiceSpec.Name)
return nil, err
}

service := services[0]

_, err = e.docker.ServiceUpdate(ctx, service.ID, service.Version, *spec, types.ServiceUpdateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "update docker service")
Expand Down Expand Up @@ -179,25 +164,12 @@ func (e *taskExecutor) prepareServicePayload(ctx context.Context, servicePayload
}

for _, secret := range spec.TaskTemplate.ContainerSpec.Secrets {
secrets, err := e.docker.SecretList(ctx, types.SecretListOptions{
Filters: filters.NewArgs(
filters.Arg("name", secret.SecretName),
),
})

foundSecret, err := e.getSecretByName(ctx, secret.SecretName)
if err != nil {
return nil, errors.Wrapf(err, "get secret by name %s", secret.SecretName)
}

if len(secrets) > 1 {
return nil, fmt.Errorf("multiple secrets with name %s found", secret.SecretName)
}

if len(secrets) == 0 {
return nil, fmt.Errorf("secret with name %s not found", secret.SecretName)
}

secret.SecretID = secrets[0].ID
secret.SecretID = foundSecret.ID
}

return &spec, nil
Expand All @@ -206,26 +178,39 @@ func (e *taskExecutor) prepareServicePayload(ctx context.Context, servicePayload
func (e *taskExecutor) deleteDockerService(ctx context.Context, req *t.DeleteServiceReq) (*t.DeleteServiceRes, error) {
var res t.DeleteServiceRes

services, err := e.docker.ServiceList(ctx, types.ServiceListOptions{
Filters: filters.NewArgs(
filters.Arg("name", req.ServiceName),
),
})
service, err := e.getServiceByName(ctx, req.ServiceName)
if err != nil {
return nil, err
}

err = e.docker.ServiceRemove(ctx, service.ID)
if err != nil {
return nil, errors.Wrapf(err, "delete docker service")
}

if len(services) == 0 {
// TODO: return warnings if the service has not been found
//return nil, fmt.Errorf("service with name %s not found", req.ServiceName)
return nil, nil
return &res, nil
}

func (e *taskExecutor) getServiceByName(ctx context.Context, name string) (*swarm.Service, error) {
if name == "" {
return nil, errors.Wrapf(ErrServiceNotFound, "service name is empty")
}

err = e.docker.ServiceRemove(ctx, services[0].ID)
services, err := e.docker.ServiceList(ctx, types.ServiceListOptions{
Filters: filters.NewArgs(
filters.Arg("name", name),
),
})

if err != nil {
return nil, errors.Wrapf(err, "delete docker service")
return nil, err
}

return &res, nil
for _, service := range services {
if service.Spec.Name == name {
return &service, nil
}
}

return nil, errors.Wrapf(ErrServiceNotFound, "service with name %s not found", name)
}

0 comments on commit 1aa7686

Please sign in to comment.