From a68c940f1a8688e7451c946caeeb122e749e6d61 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 6 Nov 2017 17:56:36 -0500 Subject: [PATCH] Remove duplication in compose/convert Signed-off-by: Daniel Nephin --- cli/compose/convert/compose.go | 42 ++++++---- cli/compose/convert/service.go | 147 +++++++++++++++++++-------------- cli/compose/types/types.go | 7 +- 3 files changed, 111 insertions(+), 85 deletions(-) diff --git a/cli/compose/convert/compose.go b/cli/compose/convert/compose.go index 02b1dccb6a..699c7b852b 100644 --- a/cli/compose/convert/compose.go +++ b/cli/compose/convert/compose.go @@ -101,18 +101,11 @@ func Secrets(namespace Namespace, secrets map[string]composetypes.SecretConfig) continue } - data, err := ioutil.ReadFile(secret.File) + obj, err := fileObjectConfig(namespace, name, composetypes.FileObjectConfig(secret)) if err != nil { return nil, err } - - result = append(result, swarm.SecretSpec{ - Annotations: swarm.Annotations{ - Name: namespace.Scope(name), - Labels: AddStackLabel(namespace, secret.Labels), - }, - Data: data, - }) + result = append(result, swarm.SecretSpec{Annotations: obj.Annotations, Data: obj.Data}) } return result, nil } @@ -125,18 +118,31 @@ func Configs(namespace Namespace, configs map[string]composetypes.ConfigObjConfi continue } - data, err := ioutil.ReadFile(config.File) + obj, err := fileObjectConfig(namespace, name, composetypes.FileObjectConfig(config)) if err != nil { return nil, err } - - result = append(result, swarm.ConfigSpec{ - Annotations: swarm.Annotations{ - Name: namespace.Scope(name), - Labels: AddStackLabel(namespace, config.Labels), - }, - Data: data, - }) + result = append(result, swarm.ConfigSpec{Annotations: obj.Annotations, Data: obj.Data}) } return result, nil } + +type swarmFileObject struct { + Annotations swarm.Annotations + Data []byte +} + +func fileObjectConfig(namespace Namespace, name string, obj composetypes.FileObjectConfig) (swarmFileObject, error) { + data, err := ioutil.ReadFile(obj.File) + if err != nil { + return swarmFileObject{}, err + } + + return swarmFileObject{ + Annotations: swarm.Annotations{ + Name: namespace.Scope(name), + Labels: AddStackLabel(namespace, obj.Labels), + }, + Data: data, + }, nil +} diff --git a/cli/compose/convert/service.go b/cli/compose/convert/service.go index 75c6db8ffb..d69244895b 100644 --- a/cli/compose/convert/service.go +++ b/cli/compose/convert/service.go @@ -255,43 +255,24 @@ func convertServiceSecrets( secretSpecs map[string]composetypes.SecretConfig, ) ([]*swarm.SecretReference, error) { refs := []*swarm.SecretReference{} - for _, secret := range secrets { - target := secret.Target - if target == "" { - target = secret.Source - } - secretSpec, exists := secretSpecs[secret.Source] + lookup := func(key string) (composetypes.FileObjectConfig, error) { + configSpec, exists := secretSpecs[key] if !exists { - return nil, errors.Errorf("undefined secret %q", secret.Source) - } - - source := namespace.Scope(secret.Source) - if secretSpec.External.External { - source = secretSpec.External.Name - } - - uid := secret.UID - gid := secret.GID - if uid == "" { - uid = "0" - } - if gid == "" { - gid = "0" - } - mode := secret.Mode - if mode == nil { - mode = uint32Ptr(0444) + return composetypes.FileObjectConfig{}, errors.Errorf("undefined secret %q", key) + } + return composetypes.FileObjectConfig(configSpec), nil + } + for _, config := range secrets { + obj, err := convertFileObject(namespace, composetypes.FileReferenceConfig(config), lookup) + if err != nil { + return nil, err } + file := swarm.SecretReferenceFileTarget(obj.File) refs = append(refs, &swarm.SecretReference{ - File: &swarm.SecretReferenceFileTarget{ - Name: target, - UID: uid, - GID: gid, - Mode: os.FileMode(*mode), - }, - SecretName: source, + File: &file, + SecretName: obj.Name, }) } @@ -312,43 +293,24 @@ func convertServiceConfigObjs( configSpecs map[string]composetypes.ConfigObjConfig, ) ([]*swarm.ConfigReference, error) { refs := []*swarm.ConfigReference{} - for _, config := range configs { - target := config.Target - if target == "" { - target = config.Source - } - configSpec, exists := configSpecs[config.Source] + lookup := func(key string) (composetypes.FileObjectConfig, error) { + configSpec, exists := configSpecs[key] if !exists { - return nil, errors.Errorf("undefined config %q", config.Source) - } - - source := namespace.Scope(config.Source) - if configSpec.External.External { - source = configSpec.External.Name - } - - uid := config.UID - gid := config.GID - if uid == "" { - uid = "0" - } - if gid == "" { - gid = "0" - } - mode := config.Mode - if mode == nil { - mode = uint32Ptr(0444) + return composetypes.FileObjectConfig{}, errors.Errorf("undefined config %q", key) + } + return composetypes.FileObjectConfig(configSpec), nil + } + for _, config := range configs { + obj, err := convertFileObject(namespace, composetypes.FileReferenceConfig(config), lookup) + if err != nil { + return nil, err } + file := swarm.ConfigReferenceFileTarget(obj.File) refs = append(refs, &swarm.ConfigReference{ - File: &swarm.ConfigReferenceFileTarget{ - Name: target, - UID: uid, - GID: gid, - Mode: os.FileMode(*mode), - }, - ConfigName: source, + File: &file, + ConfigName: obj.Name, }) } @@ -361,6 +323,63 @@ func convertServiceConfigObjs( return confs, err } +type swarmReferenceTarget struct { + Name string + UID string + GID string + Mode os.FileMode +} + +type swarmReferenceObject struct { + File swarmReferenceTarget + ID string + Name string +} + +func convertFileObject( + namespace Namespace, + config composetypes.FileReferenceConfig, + lookup func(key string) (composetypes.FileObjectConfig, error), +) (swarmReferenceObject, error) { + target := config.Target + if target == "" { + target = config.Source + } + + obj, err := lookup(config.Source) + if err != nil { + return swarmReferenceObject{}, err + } + + source := namespace.Scope(config.Source) + if obj.External.External { + source = obj.External.Name + } + + uid := config.UID + gid := config.GID + if uid == "" { + uid = "0" + } + if gid == "" { + gid = "0" + } + mode := config.Mode + if mode == nil { + mode = uint32Ptr(0444) + } + + return swarmReferenceObject{ + File: swarmReferenceTarget{ + Name: target, + UID: uid, + GID: gid, + Mode: os.FileMode(*mode), + }, + Name: source, + }, nil +} + func uint32Ptr(value uint32) *uint32 { return &value } diff --git a/cli/compose/types/types.go b/cli/compose/types/types.go index 4e68da08ae..99c536887a 100644 --- a/cli/compose/types/types.go +++ b/cli/compose/types/types.go @@ -278,7 +278,8 @@ type ServiceVolumeVolume struct { NoCopy bool `mapstructure:"nocopy"` } -type fileReferenceConfig struct { +// FileReferenceConfig for a reference to a swarm file object +type FileReferenceConfig struct { Source string Target string UID string @@ -287,10 +288,10 @@ type fileReferenceConfig struct { } // ServiceConfigObjConfig is the config obj configuration for a service -type ServiceConfigObjConfig fileReferenceConfig +type ServiceConfigObjConfig FileReferenceConfig // ServiceSecretConfig is the secret configuration for a service -type ServiceSecretConfig fileReferenceConfig +type ServiceSecretConfig FileReferenceConfig // UlimitsConfig the ulimit configuration type UlimitsConfig struct {