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,20 +293,67 @@ func convertServiceConfigObjs(
|
||||||
configSpecs map[string]composetypes.ConfigObjConfig,
|
configSpecs map[string]composetypes.ConfigObjConfig,
|
||||||
) ([]*swarm.ConfigReference, error) {
|
) ([]*swarm.ConfigReference, error) {
|
||||||
refs := []*swarm.ConfigReference{}
|
refs := []*swarm.ConfigReference{}
|
||||||
|
|
||||||
|
lookup := func(key string) (composetypes.FileObjectConfig, error) {
|
||||||
|
configSpec, exists := configSpecs[key]
|
||||||
|
if !exists {
|
||||||
|
return composetypes.FileObjectConfig{}, errors.Errorf("undefined config %q", key)
|
||||||
|
}
|
||||||
|
return composetypes.FileObjectConfig(configSpec), nil
|
||||||
|
}
|
||||||
for _, config := range configs {
|
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: &file,
|
||||||
|
ConfigName: obj.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
confs, err := servicecli.ParseConfigs(client, refs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// sort to ensure idempotence (don't restart services just because the entries are in different order)
|
||||||
|
sort.SliceStable(confs, func(i, j int) bool { return confs[i].ConfigName < confs[j].ConfigName })
|
||||||
|
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
|
target := config.Target
|
||||||
if target == "" {
|
if target == "" {
|
||||||
target = config.Source
|
target = config.Source
|
||||||
}
|
}
|
||||||
|
|
||||||
configSpec, exists := configSpecs[config.Source]
|
obj, err := lookup(config.Source)
|
||||||
if !exists {
|
if err != nil {
|
||||||
return nil, errors.Errorf("undefined config %q", config.Source)
|
return swarmReferenceObject{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
source := namespace.Scope(config.Source)
|
source := namespace.Scope(config.Source)
|
||||||
if configSpec.External.External {
|
if obj.External.External {
|
||||||
source = configSpec.External.Name
|
source = obj.External.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
uid := config.UID
|
uid := config.UID
|
||||||
|
@ -341,24 +369,15 @@ func convertServiceConfigObjs(
|
||||||
mode = uint32Ptr(0444)
|
mode = uint32Ptr(0444)
|
||||||
}
|
}
|
||||||
|
|
||||||
refs = append(refs, &swarm.ConfigReference{
|
return swarmReferenceObject{
|
||||||
File: &swarm.ConfigReferenceFileTarget{
|
File: swarmReferenceTarget{
|
||||||
Name: target,
|
Name: target,
|
||||||
UID: uid,
|
UID: uid,
|
||||||
GID: gid,
|
GID: gid,
|
||||||
Mode: os.FileMode(*mode),
|
Mode: os.FileMode(*mode),
|
||||||
},
|
},
|
||||||
ConfigName: source,
|
Name: source,
|
||||||
})
|
}, nil
|
||||||
}
|
|
||||||
|
|
||||||
confs, err := servicecli.ParseConfigs(client, refs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// sort to ensure idempotence (don't restart services just because the entries are in different order)
|
|
||||||
sort.SliceStable(confs, func(i, j int) bool { return confs[i].ConfigName < confs[j].ConfigName })
|
|
||||||
return confs, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func uint32Ptr(value uint32) *uint32 {
|
func uint32Ptr(value uint32) *uint32 {
|
||||||
|
|
|
@ -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