Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trait): container image pull policy #2583

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deploy/traits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ traits:
- name: image
type: string
description: The main container image
- name: image-pull-policy
type: PullPolicy
description: 'The pull policy: Always|Never|IfNotPresent'
- name: probes-enabled
type: bool
description: ProbesEnabled enable/disable probes on the container (default `false`)
Expand Down
4 changes: 4 additions & 0 deletions docs/modules/traits/pages/container.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ The following configuration options are available:
| string
| The main container image

| container.image-pull-policy
| PullPolicy
| The pull policy: Always|Never|IfNotPresent

| container.probes-enabled
| bool
| ProbesEnabled enable/disable probes on the container (default `false`)
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/resources.go

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ type containerTrait struct {
ServicePort int `property:"service-port" json:"servicePort,omitempty"`
// To configure under which service port name the container port is to be exposed (default `http`).
ServicePortName string `property:"service-port-name" json:"servicePortName,omitempty"`

// The main container name. It's named `integration` by default.
Name string `property:"name" json:"name,omitempty"`
// The main container image
Image string `property:"image" json:"image,omitempty"`

// The pull policy: Always|Never|IfNotPresent
ImagePullPolicy corev1.PullPolicy `property:"image-pull-policy" json:"imagePullPolicy,omitempty"`
// ProbesEnabled enable/disable probes on the container (default `false`)
ProbesEnabled *bool `property:"probes-enabled" json:"probesEnabled,omitempty"`
// Scheme to use when connecting. Defaults to HTTP. Applies to the liveness probe.
Expand Down Expand Up @@ -141,9 +141,17 @@ func (t *containerTrait) Configure(e *Environment) (bool, error) {
}
}

if !isValidPullPolicy(t.ImagePullPolicy) {
return false, fmt.Errorf("unsupported pull policy %s", t.ImagePullPolicy)
}

return true, nil
}

func isValidPullPolicy(policy corev1.PullPolicy) bool {
return policy == "" || policy == corev1.PullAlways || policy == corev1.PullIfNotPresent || policy == corev1.PullNever
}

func (t *containerTrait) Apply(e *Environment) error {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
return t.configureDependencies(e)
Expand Down Expand Up @@ -220,6 +228,10 @@ func (t *containerTrait) configureContainer(e *Environment) error {
Env: make([]corev1.EnvVar, 0),
}

if t.ImagePullPolicy != "" {
container.ImagePullPolicy = t.ImagePullPolicy
}

// combine Environment of integration with platform, kit, integration
for _, env := range e.collectConfigurationPairs("env") {
envvar.SetVal(&container.Env, env.Name, env.Value)
Expand Down
25 changes: 24 additions & 1 deletion pkg/trait/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package trait

import (
"context"
"testing"

"github.com/google/uuid"
"k8s.io/apimachinery/pkg/types"
"testing"

"github.com/stretchr/testify/assert"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -327,3 +329,24 @@ func TestContainerWithCustomImageAndDeprecatedIntegrationKit(t *testing.T) {
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "unsupported configuration: a container image has been set in conjunction with an IntegrationKit")
}

func TestContainerWithImagePullPolicy(t *testing.T) {
target := appsv1.Deployment{}

env := newTestProbesEnv(t, v1.RuntimeProviderQuarkus)
env.Integration.Status.Phase = v1.IntegrationPhaseDeploying
env.Resources.Add(&target)

ctr := newTestContainerTrait()
ctr.ImagePullPolicy = "Always"

err := ctr.Apply(&env)
assert.Nil(t, err)
assert.Equal(t, corev1.PullAlways, target.Spec.Template.Spec.Containers[0].ImagePullPolicy)

ctr.ImagePullPolicy = "MustFail"

ok, err := ctr.Configure(&env)
assert.False(t, ok)
assert.NotNil(t, err)
}