2016-10-19 12:22:02 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-11-03 14:09:13 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-10-19 12:22:02 -04:00
|
|
|
)
|
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
// ParseSecrets retrieves the secrets with the requested names and fills
|
|
|
|
// secret IDs into the secret references.
|
|
|
|
func ParseSecrets(client client.SecretAPIClient, requestedSecrets []*swarmtypes.SecretReference) ([]*swarmtypes.SecretReference, error) {
|
2016-10-27 20:57:38 -04:00
|
|
|
secretRefs := make(map[string]*swarmtypes.SecretReference)
|
2016-10-19 12:22:02 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, secret := range requestedSecrets {
|
2017-03-16 13:54:18 -04:00
|
|
|
if _, exists := secretRefs[secret.File.Name]; exists {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, errors.Errorf("duplicate secret target for %s not allowed", secret.SecretName)
|
2016-11-15 10:04:36 -05:00
|
|
|
}
|
2017-03-16 13:54:18 -04:00
|
|
|
secretRef := new(swarmtypes.SecretReference)
|
|
|
|
*secretRef = *secret
|
|
|
|
secretRefs[secret.File.Name] = secretRef
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
args := filters.NewArgs()
|
2016-10-27 20:57:38 -04:00
|
|
|
for _, s := range secretRefs {
|
2016-12-05 11:08:43 -05:00
|
|
|
args.Add("name", s.SecretName)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
secrets, err := client.SecretList(ctx, types.SecretListOptions{
|
2016-11-03 14:09:13 -04:00
|
|
|
Filters: args,
|
2016-10-19 12:22:02 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-27 03:41:32 -04:00
|
|
|
foundSecrets := make(map[string]string)
|
2016-10-19 12:22:02 -04:00
|
|
|
for _, secret := range secrets {
|
2016-10-27 03:41:32 -04:00
|
|
|
foundSecrets[secret.Spec.Annotations.Name] = secret.ID
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addedSecrets := []*swarmtypes.SecretReference{}
|
|
|
|
|
2016-10-27 20:57:38 -04:00
|
|
|
for _, ref := range secretRefs {
|
|
|
|
id, ok := foundSecrets[ref.SecretName]
|
2016-10-27 20:18:12 -04:00
|
|
|
if !ok {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, errors.Errorf("secret not found: %s", ref.SecretName)
|
2016-10-27 20:18:12 -04:00
|
|
|
}
|
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
// set the id for the ref to properly assign in swarm
|
|
|
|
// since swarm needs the ID instead of the name
|
2016-10-27 20:57:38 -04:00
|
|
|
ref.SecretID = id
|
|
|
|
addedSecrets = append(addedSecrets, ref)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return addedSecrets, nil
|
|
|
|
}
|
2017-05-08 13:36:04 -04:00
|
|
|
|
|
|
|
// ParseConfigs retrieves the configs from the requested names and converts
|
|
|
|
// them to config references to use with the spec
|
|
|
|
func ParseConfigs(client client.ConfigAPIClient, requestedConfigs []*swarmtypes.ConfigReference) ([]*swarmtypes.ConfigReference, error) {
|
|
|
|
configRefs := make(map[string]*swarmtypes.ConfigReference)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, config := range requestedConfigs {
|
|
|
|
if _, exists := configRefs[config.File.Name]; exists {
|
|
|
|
return nil, errors.Errorf("duplicate config target for %s not allowed", config.ConfigName)
|
|
|
|
}
|
|
|
|
|
|
|
|
configRef := new(swarmtypes.ConfigReference)
|
|
|
|
*configRef = *config
|
|
|
|
configRefs[config.File.Name] = configRef
|
|
|
|
}
|
|
|
|
|
|
|
|
args := filters.NewArgs()
|
|
|
|
for _, s := range configRefs {
|
|
|
|
args.Add("name", s.ConfigName)
|
|
|
|
}
|
|
|
|
|
|
|
|
configs, err := client.ConfigList(ctx, types.ConfigListOptions{
|
|
|
|
Filters: args,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
foundConfigs := make(map[string]string)
|
|
|
|
for _, config := range configs {
|
|
|
|
foundConfigs[config.Spec.Annotations.Name] = config.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
addedConfigs := []*swarmtypes.ConfigReference{}
|
|
|
|
|
|
|
|
for _, ref := range configRefs {
|
|
|
|
id, ok := foundConfigs[ref.ConfigName]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("config not found: %s", ref.ConfigName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the id for the ref to properly assign in swarm
|
|
|
|
// since swarm needs the ID instead of the name
|
|
|
|
ref.ConfigID = id
|
|
|
|
addedConfigs = append(addedConfigs, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addedConfigs, nil
|
|
|
|
}
|