This is an automated email from the ASF dual-hosted git repository.
astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push:
new 7debc8f feat(trait): container image pull policy
7debc8f is described below
commit 7debc8f261539ecc4219b32b3d742e1a62d7d61c
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Thu Aug 19 10:30:07 2021 +0200
feat(trait): container image pull policy
Closes #2503
---
deploy/traits.yaml | 3 +++
docs/modules/traits/pages/container.adoc | 4 ++++
pkg/resources/resources.go | 4 ++--
pkg/trait/container.go | 16 ++++++++++++++--
pkg/trait/container_test.go | 25 ++++++++++++++++++++++++-
5 files changed, 47 insertions(+), 5 deletions(-)
diff --git a/deploy/traits.yaml b/deploy/traits.yaml
index e1b7d62..62f63c0 100755
--- a/deploy/traits.yaml
+++ b/deploy/traits.yaml
@@ -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`)
diff --git a/docs/modules/traits/pages/container.adoc
b/docs/modules/traits/pages/container.adoc
index 2a4dc45..53ed92e 100755
--- a/docs/modules/traits/pages/container.adoc
+++ b/docs/modules/traits/pages/container.adoc
@@ -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`)
diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go
index b2abbd2..fd3e3c2 100644
--- a/pkg/resources/resources.go
+++ b/pkg/resources/resources.go
@@ -467,9 +467,9 @@ var assets = func() http.FileSystem {
"/traits.yaml": &vfsgen۰CompressedFileInfo{
name: "traits.yaml",
modTime: time.Time{},
- uncompressedSize: 39017,
+ uncompressedSize: 39124,
- compressedContent:
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x7b\x73\x1c\x37\xf2\xd8\xff\xfa\x14\x28\xfe\x52\xc5\x47\xed\x0c\x29\x5f\xee\xce\x61\xa2\x5c\xd1\x92\x7c\xa6\xad\x07\x23\xf2\x7c\x95\x52\x54\xb7\xd8\x99\xde\x5d\x88\x33\xc0\x1c\x80\x21\xb5\x4e\xe5\xbb\xa7\xd0\x8d\xd7\xec\x0e\x97\x4b\xd9\x74\x99\xa9\xdc\xfd\x61\x91\x9c\x01\x1a\x8d\x7e\xbf\xc6\x6a\x2e\xac\x39\x7d\x56\x30\xc9\x5b\x38\x65\x7c\x3e\x17\x52\xd8\xd5\x33\xc6\xba\x86\xdb\xb9\xd2\xed\x29\x9b\xf3\xc6\x80\x
[...]
+ compressedContent:
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\x7b\x73\x24\xb7\xf1\xd8\xff\xf7\x29\x50\xfc\xa5\xea\x48\xd6\xee\xf0\x24\xc7\xb6\xc2\x44\x71\x51\x77\x27\x8b\xd2\x3d\x98\x23\x2d\x57\x4a\x51\x79\xb1\x33\xbd\xbb\x38\x62\x80\x31\x80\x21\x6f\x1d\xe7\xbb\xa7\xd0\x8d\xd7\xec\x0e\x97\xcb\x93\xa8\x12\x53\xb1\xff\xd0\x91\x9c\x01\x1a\x8d\x7e\xbf\xc6\x19\x2e\x9c\x3d\x7d\x36\x65\x8a\xb7\x70\xca\xf8\x62\x21\x94\x70\xeb\x67\x8c\x75\x92\xbb\x85\x36\xed\x29\x5b\x70\x69\xc1\x
[...]
},
}
fs["/"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
diff --git a/pkg/trait/container.go b/pkg/trait/container.go
index c8df0fe..b6c5caf 100644
--- a/pkg/trait/container.go
+++ b/pkg/trait/container.go
@@ -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.
@@ -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)
@@ -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)
diff --git a/pkg/trait/container_test.go b/pkg/trait/container_test.go
index 9952aa8..cf21d68 100644
--- a/pkg/trait/container_test.go
+++ b/pkg/trait/container_test.go
@@ -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"
@@ -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)
+}