Skip to content

Commit

Permalink
Introduce support for pull-policy in v1alpha3
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ferquel <simon.ferquel@docker.com>
  • Loading branch information
simonferquel committed Jan 28, 2019
1 parent 98e85c3 commit 87d7598
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cli/command/stack/kubernetes/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
const (
// PullSecretExtraField is an extra field on ServiceConfigs usable to reference a pull secret
PullSecretExtraField = "x-pull-secret"
// PullPolicyExtraField is an extra field on ServiceConfigs usable to explicitly set the service's pull policy
PullPolicyExtraField = "x-pull-policy"
)

// NewStackConverter returns a converter from types.Config (compose) to the specified
Expand Down Expand Up @@ -242,15 +244,18 @@ func fromComposeConfigs(s map[string]composeTypes.ConfigObjConfig) map[string]la
}

func fromComposeServiceConfig(s composeTypes.ServiceConfig, caps composeCapabilities) (latest.ServiceConfig, error) {
var userID *int64
var (
userID *int64
pullSecret string
pullPolicy string
)
if s.User != "" {
numerical, err := strconv.Atoi(s.User)
if err == nil {
unixUserID := int64(numerical)
userID = &unixUserID
}
}
var pullSecret string
if psInterface, ok := s.Extras[PullSecretExtraField]; ok {
if !caps.hasPullSecrets {
return latest.ServiceConfig{}, errors.Errorf("stack API version %s does not support pull secrets, please use version v1alpha3", caps.apiVersion)
Expand All @@ -259,6 +264,14 @@ func fromComposeServiceConfig(s composeTypes.ServiceConfig, caps composeCapabili
return latest.ServiceConfig{}, errors.Errorf("pull secret %v type is %T, should be a string", psInterface, psInterface)
}
}
if ppInterface, ok := s.Extras[PullPolicyExtraField]; ok {
if !caps.hasPullPolicy {
return latest.ServiceConfig{}, errors.Errorf("stack API version %s does not support pull policies, please use version v1alpha3", caps.apiVersion)
}
if pullPolicy, ok = ppInterface.(string); !ok {
return latest.ServiceConfig{}, errors.Errorf("pull secret %v type is %T, should be a string", ppInterface, ppInterface)
}
}
return latest.ServiceConfig{
Name: s.Name,
CapAdd: s.CapAdd,
Expand Down Expand Up @@ -295,6 +308,7 @@ func fromComposeServiceConfig(s composeTypes.ServiceConfig, caps composeCapabili
Volumes: fromComposeServiceVolumeConfig(s.Volumes),
WorkingDir: s.WorkingDir,
PullSecret: pullSecret,
PullPolicy: pullPolicy,
}, nil
}

Expand Down Expand Up @@ -467,10 +481,12 @@ var (
v1alpha3Capabilities = composeCapabilities{
apiVersion: "v1alpha3",
hasPullSecrets: true,
hasPullPolicy: true,
}
)

type composeCapabilities struct {
apiVersion string
hasPullSecrets bool
hasPullPolicy bool
}
42 changes: 42 additions & 0 deletions cli/command/stack/kubernetes/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,45 @@ func TestHandlePullSecret(t *testing.T) {
})
}
}

func loadTestStackWithPullPolicy(t *testing.T) *composetypes.Config {
t.Helper()
data, err := ioutil.ReadFile("testdata/compose-with-pull-policy.yml")
assert.NilError(t, err)
yamlData, err := loader.ParseYAML(data)
assert.NilError(t, err)
cfg, err := loader.Load(composetypes.ConfigDetails{
ConfigFiles: []composetypes.ConfigFile{
{Config: yamlData, Filename: "testdata/compose-with-pull-policy.yml"},
},
})
assert.NilError(t, err)
return cfg
}

func TestHandlePullPolicy(t *testing.T) {
testData := loadTestStackWithPullPolicy(t)
cases := []struct {
version string
err string
}{
{version: "v1beta1", err: "stack API version v1beta1 does not support pull policies, please use version v1alpha3"},
{version: "v1beta2", err: "stack API version v1beta2 does not support pull policies, please use version v1alpha3"},
{version: "v1alpha3"},
}

for _, c := range cases {
t.Run(c.version, func(t *testing.T) {
conv, err := NewStackConverter(c.version)
assert.NilError(t, err)
s, err := conv.FromCompose(ioutil.Discard, "test", testData)
if c.err != "" {
assert.Error(t, err, c.err)

} else {
assert.NilError(t, err)
assert.Equal(t, s.Spec.Services[0].PullPolicy, "Never")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "3.7"
services:
test:
image: "some-image"
x-pull-policy: "Never"

0 comments on commit 87d7598

Please sign in to comment.