Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #63 from simonferquel/pull-policy
Browse files Browse the repository at this point in the history
Add Pull Policy support in v1alpha3
  • Loading branch information
simonferquel authored Jan 28, 2019
2 parents 911a015 + ae7e2e0 commit 356b291
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 10 deletions.
1 change: 1 addition & 0 deletions api/compose/v1alpha3/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type ServiceConfig struct {
Volumes []ServiceVolumeConfig `json:"volumes,omitempty"`
WorkingDir string `json:"working_dir,omitempty"`
PullSecret string `json:"pull_secret,omitempty"`
PullPolicy string `json:"pull_policy,omitempty"`
}

// ServicePortConfig is the port configuration for a service
Expand Down
22 changes: 17 additions & 5 deletions internal/convert/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func toPodTemplate(serviceConfig latest.ServiceConfig, labels map[string]string,
if err != nil {
return apiv1.PodTemplateSpec{}, err
}
pullPolicy, err := toImagePullPolicy(serviceConfig.Image, serviceConfig.PullPolicy)
if err != nil {
return apiv1.PodTemplateSpec{}, err
}
tpl.ObjectMeta = metav1.ObjectMeta{
Labels: labels,
Annotations: serviceConfig.Labels,
Expand Down Expand Up @@ -76,7 +80,7 @@ func toPodTemplate(serviceConfig latest.ServiceConfig, labels map[string]string,
}
tpl.Spec.Containers[containerIX].Name = serviceConfig.Name
tpl.Spec.Containers[containerIX].Image = serviceConfig.Image
tpl.Spec.Containers[containerIX].ImagePullPolicy = toImagePullPolicy(serviceConfig.Image)
tpl.Spec.Containers[containerIX].ImagePullPolicy = pullPolicy
tpl.Spec.Containers[containerIX].Command = serviceConfig.Entrypoint
tpl.Spec.Containers[containerIX].Args = serviceConfig.Command
tpl.Spec.Containers[containerIX].WorkingDir = serviceConfig.WorkingDir
Expand Down Expand Up @@ -104,11 +108,19 @@ func toPodTemplate(serviceConfig latest.ServiceConfig, labels map[string]string,
return tpl, nil
}

func toImagePullPolicy(image string) apiv1.PullPolicy {
if strings.HasSuffix(image, ":latest") {
return apiv1.PullAlways
func toImagePullPolicy(image string, specifiedPolicy string) (apiv1.PullPolicy, error) {
if specifiedPolicy == "" {
if strings.HasSuffix(image, ":latest") {
return apiv1.PullAlways, nil
}
return apiv1.PullIfNotPresent, nil
}
switch apiv1.PullPolicy(specifiedPolicy) {
case apiv1.PullAlways, apiv1.PullIfNotPresent, apiv1.PullNever:
return apiv1.PullPolicy(specifiedPolicy), nil
default:
return "", errors.Errorf("invalid pull policy %q, must be %q, %q or %q", specifiedPolicy, apiv1.PullAlways, apiv1.PullIfNotPresent, apiv1.PullNever)
}
return apiv1.PullIfNotPresent
}

func toHostAliases(extraHosts []string) ([]apiv1.HostAlias, error) {
Expand Down
74 changes: 69 additions & 5 deletions internal/convert/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ import (
)

func podTemplate(t *testing.T, stack *latest.Stack) apiv1.PodTemplateSpec {
s, err := StackToStack(*stack, loadBalancerServiceStrategy{}, stackresources.EmptyStackState)
res, err := podTemplateWithError(stack)
assert.NoError(t, err)
return res
}

func podTemplateWithError(stack *latest.Stack) (apiv1.PodTemplateSpec, error) {
s, err := StackToStack(*stack, loadBalancerServiceStrategy{}, stackresources.EmptyStackState)
if err != nil {
return apiv1.PodTemplateSpec{}, err
}
for _, r := range s.Deployments {
return r.Spec.Template
return r.Spec.Template, nil
}
for _, r := range s.Daemonsets {
return r.Spec.Template
return r.Spec.Template, nil
}
for _, r := range s.Statefulsets {
return r.Spec.Template
return r.Spec.Template, nil
}
return apiv1.PodTemplateSpec{}
return apiv1.PodTemplateSpec{}, nil
}

func TestToPodWithDockerSocket(t *testing.T) {
Expand Down Expand Up @@ -879,3 +887,59 @@ func TestToPodWithPullSecret(t *testing.T) {
)))
assert.Nil(t, podTemplateNoSecret.Spec.ImagePullSecrets)
}

func TestToPodWithPullPolicy(t *testing.T) {
cases := []struct {
name string
stack *latest.Stack
expectedPolicy apiv1.PullPolicy
expectedError string
}{
{
name: "specific tag",
stack: Stack("demo",
WithService("nginx",
Image("nginx:specific"),
)),
expectedPolicy: apiv1.PullIfNotPresent,
},
{
name: "latest tag",
stack: Stack("demo",
WithService("nginx",
Image("nginx:latest"),
)),
expectedPolicy: apiv1.PullAlways,
},
{
name: "explicit policy",
stack: Stack("demo",
WithService("nginx",
Image("nginx:latest"),
PullPolicy("Never"),
)),
expectedPolicy: apiv1.PullNever,
},
{
name: "invalid policy",
stack: Stack("demo",
WithService("nginx",
Image("nginx:latest"),
PullPolicy("Invalid"),
)),
expectedError: `invalid pull policy "Invalid", must be "Always", "IfNotPresent" or "Never"`,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
pod, err := podTemplateWithError(c.stack)
if c.expectedError != "" {
assert.EqualError(t, err, c.expectedError)
} else {
assert.NoError(t, err)
assert.Equal(t, pod.Spec.Containers[0].ImagePullPolicy, c.expectedPolicy)
}
})
}
}
15 changes: 15 additions & 0 deletions internal/registry/strategyvalidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,18 @@ func TestValidateStackNotNilWithoutStatus(t *testing.T) {
err := validateStackNotNil()(nil, &stack)
assert.Len(t, err, 1)
}

func TestValidateInvalidPullPolicy(t *testing.T) {
s := Stack("test",
WithService("redis",
Image("redis"),
PullPolicy("Invalid")))
stack := iv.Stack{
Spec: iv.StackSpec{
Stack: s.Spec,
},
}
err := validateDryRun()(nil, &stack)
assert.Len(t, err, 1)
assert.Contains(t, err[0].Error(), `invalid pull policy "Invalid", must be "Always", "IfNotPresent" or "Never"`)
}
7 changes: 7 additions & 0 deletions internal/test/builders/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ func PullSecret(name string) func(*latest.ServiceConfig) {
}
}

// PullPolicy specifies the pull policy used for this service
func PullPolicy(policy string) func(*latest.ServiceConfig) {
return func(c *latest.ServiceConfig) {
c.PullPolicy = policy
}
}

// StopGracePeriod specifies the stop-grace-period duration of a service
func StopGracePeriod(duration time.Duration) func(*latest.ServiceConfig) {
return func(c *latest.ServiceConfig) {
Expand Down

0 comments on commit 356b291

Please sign in to comment.