Merge pull request #71 from thaJeztah/add-credspec-to-compose

add credential-spec to compose
This commit is contained in:
Daniel Nephin 2017-05-16 16:45:06 -04:00 committed by GitHub
commit 90809f8fd9
6 changed files with 78 additions and 3 deletions

View File

@ -98,6 +98,12 @@ func convertService(
return swarm.ServiceSpec{}, err return swarm.ServiceSpec{}, err
} }
var privileges swarm.Privileges
privileges.CredentialSpec, err = convertCredentialSpec(service.CredentialSpec)
if err != nil {
return swarm.ServiceSpec{}, err
}
var logDriver *swarm.Driver var logDriver *swarm.Driver
if service.Logging != nil { if service.Logging != nil {
logDriver = &swarm.Driver{ logDriver = &swarm.Driver{
@ -130,6 +136,7 @@ func convertService(
OpenStdin: service.StdinOpen, OpenStdin: service.StdinOpen,
Secrets: secrets, Secrets: secrets,
ReadOnly: service.ReadOnly, ReadOnly: service.ReadOnly,
Privileges: &privileges,
}, },
LogDriver: logDriver, LogDriver: logDriver,
Resources: resources, Resources: resources,
@ -477,3 +484,17 @@ func convertDNSConfig(DNS []string, DNSSearch []string) (*swarm.DNSConfig, error
} }
return nil, nil return nil, nil
} }
func convertCredentialSpec(spec composetypes.CredentialSpecConfig) (*swarm.CredentialSpec, error) {
if spec.File == "" && spec.Registry == "" {
return nil, nil
}
if spec.File != "" && spec.Registry != "" {
return nil, errors.New("Invalid credential spec - must provide one of `File` or `Registry`")
}
return &swarm.CredentialSpec{
File: spec.File,
Registry: spec.Registry,
}, nil
}

View File

@ -316,3 +316,30 @@ func TestConvertDNSConfigSearch(t *testing.T) {
Search: search, Search: search,
}, dnsConfig) }, dnsConfig)
} }
func TestConvertCredentialSpec(t *testing.T) {
swarmSpec, err := convertCredentialSpec(composetypes.CredentialSpecConfig{})
assert.NoError(t, err)
assert.Nil(t, swarmSpec)
swarmSpec, err = convertCredentialSpec(composetypes.CredentialSpecConfig{
File: "/foo",
})
assert.NoError(t, err)
assert.Equal(t, swarmSpec.File, "/foo")
assert.Equal(t, swarmSpec.Registry, "")
swarmSpec, err = convertCredentialSpec(composetypes.CredentialSpecConfig{
Registry: "foo",
})
assert.NoError(t, err)
assert.Equal(t, swarmSpec.File, "")
assert.Equal(t, swarmSpec.Registry, "foo")
swarmSpec, err = convertCredentialSpec(composetypes.CredentialSpecConfig{
File: "/asdf",
Registry: "foo",
})
assert.Error(t, err)
assert.Nil(t, swarmSpec)
}

View File

@ -198,6 +198,22 @@ secrets:
assert.Equal(t, len(actual.Secrets), 1) assert.Equal(t, len(actual.Secrets), 1)
} }
func TestLoadV33(t *testing.T) {
actual, err := loadYAML(`
version: "3.3"
services:
foo:
image: busybox
credential_spec:
File: "/foo"
`)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, len(actual.Services), 1)
assert.Equal(t, actual.Services[0].CredentialSpec.File, "/foo")
}
func TestParseAndLoad(t *testing.T) { func TestParseAndLoad(t *testing.T) {
actual, err := loadYAML(sampleYAML) actual, err := loadYAML(sampleYAML)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {

File diff suppressed because one or more lines are too long

View File

@ -89,6 +89,10 @@
] ]
}, },
"container_name": {"type": "string"}, "container_name": {"type": "string"},
"credential_spec": {"type": "object", "properties": {
"file": {"type": "string"},
"registry": {"type": "string"}
}},
"depends_on": {"$ref": "#/definitions/list_of_strings"}, "depends_on": {"$ref": "#/definitions/list_of_strings"},
"devices": {"type": "array", "items": {"type": "string"}, "uniqueItems": true}, "devices": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"dns": {"$ref": "#/definitions/string_or_list"}, "dns": {"$ref": "#/definitions/string_or_list"},

View File

@ -77,6 +77,7 @@ type ServiceConfig struct {
CgroupParent string `mapstructure:"cgroup_parent"` CgroupParent string `mapstructure:"cgroup_parent"`
Command ShellCommand Command ShellCommand
ContainerName string `mapstructure:"container_name"` ContainerName string `mapstructure:"container_name"`
CredentialSpec CredentialSpecConfig `mapstructure:"credential_spec"`
DependsOn []string `mapstructure:"depends_on"` DependsOn []string `mapstructure:"depends_on"`
Deploy DeployConfig Deploy DeployConfig
Devices []string Devices []string
@ -310,3 +311,9 @@ type SecretConfig struct {
External External External External
Labels Labels Labels Labels
} }
// CredentialSpecConfig for credential spec on Windows
type CredentialSpecConfig struct {
File string
Registry string
}