mirror of https://github.com/docker/cli.git
Remove duplication in compose/convert
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
7bd26ed690
commit
a68c940f1a
|
@ -101,18 +101,11 @@ func Secrets(namespace Namespace, secrets map[string]composetypes.SecretConfig)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(secret.File)
|
obj, err := fileObjectConfig(namespace, name, composetypes.FileObjectConfig(secret))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
result = append(result, swarm.SecretSpec{Annotations: obj.Annotations, Data: obj.Data})
|
||||||
result = append(result, swarm.SecretSpec{
|
|
||||||
Annotations: swarm.Annotations{
|
|
||||||
Name: namespace.Scope(name),
|
|
||||||
Labels: AddStackLabel(namespace, secret.Labels),
|
|
||||||
},
|
|
||||||
Data: data,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -125,18 +118,31 @@ func Configs(namespace Namespace, configs map[string]composetypes.ConfigObjConfi
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(config.File)
|
obj, err := fileObjectConfig(namespace, name, composetypes.FileObjectConfig(config))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
result = append(result, swarm.ConfigSpec{Annotations: obj.Annotations, Data: obj.Data})
|
||||||
result = append(result, swarm.ConfigSpec{
|
|
||||||
Annotations: swarm.Annotations{
|
|
||||||
Name: namespace.Scope(name),
|
|
||||||
Labels: AddStackLabel(namespace, config.Labels),
|
|
||||||
},
|
|
||||||
Data: data,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return result, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -255,43 +255,24 @@ func convertServiceSecrets(
|
||||||
secretSpecs map[string]composetypes.SecretConfig,
|
secretSpecs map[string]composetypes.SecretConfig,
|
||||||
) ([]*swarm.SecretReference, error) {
|
) ([]*swarm.SecretReference, error) {
|
||||||
refs := []*swarm.SecretReference{}
|
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 {
|
if !exists {
|
||||||
return nil, errors.Errorf("undefined secret %q", secret.Source)
|
return composetypes.FileObjectConfig{}, errors.Errorf("undefined secret %q", key)
|
||||||
}
|
}
|
||||||
|
return composetypes.FileObjectConfig(configSpec), nil
|
||||||
source := namespace.Scope(secret.Source)
|
}
|
||||||
if secretSpec.External.External {
|
for _, config := range secrets {
|
||||||
source = secretSpec.External.Name
|
obj, err := convertFileObject(namespace, composetypes.FileReferenceConfig(config), lookup)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
uid := secret.UID
|
|
||||||
gid := secret.GID
|
|
||||||
if uid == "" {
|
|
||||||
uid = "0"
|
|
||||||
}
|
|
||||||
if gid == "" {
|
|
||||||
gid = "0"
|
|
||||||
}
|
|
||||||
mode := secret.Mode
|
|
||||||
if mode == nil {
|
|
||||||
mode = uint32Ptr(0444)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file := swarm.SecretReferenceFileTarget(obj.File)
|
||||||
refs = append(refs, &swarm.SecretReference{
|
refs = append(refs, &swarm.SecretReference{
|
||||||
File: &swarm.SecretReferenceFileTarget{
|
File: &file,
|
||||||
Name: target,
|
SecretName: obj.Name,
|
||||||
UID: uid,
|
|
||||||
GID: gid,
|
|
||||||
Mode: os.FileMode(*mode),
|
|
||||||
},
|
|
||||||
SecretName: source,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,43 +293,24 @@ func convertServiceConfigObjs(
|
||||||
configSpecs map[string]composetypes.ConfigObjConfig,
|
configSpecs map[string]composetypes.ConfigObjConfig,
|
||||||
) ([]*swarm.ConfigReference, error) {
|
) ([]*swarm.ConfigReference, error) {
|
||||||
refs := []*swarm.ConfigReference{}
|
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 {
|
if !exists {
|
||||||
return nil, errors.Errorf("undefined config %q", config.Source)
|
return composetypes.FileObjectConfig{}, errors.Errorf("undefined config %q", key)
|
||||||
}
|
}
|
||||||
|
return composetypes.FileObjectConfig(configSpec), nil
|
||||||
source := namespace.Scope(config.Source)
|
}
|
||||||
if configSpec.External.External {
|
for _, config := range configs {
|
||||||
source = configSpec.External.Name
|
obj, err := convertFileObject(namespace, composetypes.FileReferenceConfig(config), lookup)
|
||||||
}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
uid := config.UID
|
|
||||||
gid := config.GID
|
|
||||||
if uid == "" {
|
|
||||||
uid = "0"
|
|
||||||
}
|
|
||||||
if gid == "" {
|
|
||||||
gid = "0"
|
|
||||||
}
|
|
||||||
mode := config.Mode
|
|
||||||
if mode == nil {
|
|
||||||
mode = uint32Ptr(0444)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file := swarm.ConfigReferenceFileTarget(obj.File)
|
||||||
refs = append(refs, &swarm.ConfigReference{
|
refs = append(refs, &swarm.ConfigReference{
|
||||||
File: &swarm.ConfigReferenceFileTarget{
|
File: &file,
|
||||||
Name: target,
|
ConfigName: obj.Name,
|
||||||
UID: uid,
|
|
||||||
GID: gid,
|
|
||||||
Mode: os.FileMode(*mode),
|
|
||||||
},
|
|
||||||
ConfigName: source,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,6 +323,63 @@ func convertServiceConfigObjs(
|
||||||
return confs, err
|
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 {
|
func uint32Ptr(value uint32) *uint32 {
|
||||||
return &value
|
return &value
|
||||||
}
|
}
|
||||||
|
|
|
@ -278,7 +278,8 @@ type ServiceVolumeVolume struct {
|
||||||
NoCopy bool `mapstructure:"nocopy"`
|
NoCopy bool `mapstructure:"nocopy"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileReferenceConfig struct {
|
// FileReferenceConfig for a reference to a swarm file object
|
||||||
|
type FileReferenceConfig struct {
|
||||||
Source string
|
Source string
|
||||||
Target string
|
Target string
|
||||||
UID string
|
UID string
|
||||||
|
@ -287,10 +288,10 @@ type fileReferenceConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceConfigObjConfig is the config obj configuration for a service
|
// ServiceConfigObjConfig is the config obj configuration for a service
|
||||||
type ServiceConfigObjConfig fileReferenceConfig
|
type ServiceConfigObjConfig FileReferenceConfig
|
||||||
|
|
||||||
// ServiceSecretConfig is the secret configuration for a service
|
// ServiceSecretConfig is the secret configuration for a service
|
||||||
type ServiceSecretConfig fileReferenceConfig
|
type ServiceSecretConfig FileReferenceConfig
|
||||||
|
|
||||||
// UlimitsConfig the ulimit configuration
|
// UlimitsConfig the ulimit configuration
|
||||||
type UlimitsConfig struct {
|
type UlimitsConfig struct {
|
||||||
|
|
Loading…
Reference in New Issue