Skip to content

Commit

Permalink
Fix apache#359 - Change pullpolicy conditionally based on image tag (a…
Browse files Browse the repository at this point in the history
…pache#362)

* Fix apache#359 - Change pullpolicy conditionally based on image tag

Signed-off-by: Ricardo Zanini <[email protected]>

* Fix leftovers on rebase

Signed-off-by: Ricardo Zanini <[email protected]>

---------

Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini authored and rgdoliveira committed Jan 29, 2024
1 parent 7bbcc0e commit 65f1c35
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
13 changes: 7 additions & 6 deletions controllers/platform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,21 @@ func createDeployment(ctx context.Context, client client.Client, platform *opera
}
liveProbe := readyProbe.DeepCopy()
liveProbe.ProbeHandler.HTTPGet.Path = common.QuarkusHealthPathLive
imageTag := psh.GetServiceImageName(constants.PersistenceTypeEphemeral)
dataDeployContainer := &corev1.Container{
Image: psh.GetServiceImageName(constants.PersistenceTypeEphemeral),
Env: psh.GetEnvironmentVariables(),
Resources: psh.GetPodResourceRequirements(),
ReadinessProbe: readyProbe,
LivenessProbe: liveProbe,
Image: imageTag,
ImagePullPolicy: kubeutil.GetImagePullPolicy(imageTag),
Env: psh.GetEnvironmentVariables(),
Resources: psh.GetPodResourceRequirements(),
ReadinessProbe: readyProbe,
LivenessProbe: liveProbe,
Ports: []corev1.ContainerPort{
{
Name: utils.HttpScheme,
ContainerPort: int32(constants.DefaultHTTPWorkflowPortInt),
Protocol: corev1.ProtocolTCP,
},
},
ImagePullPolicy: corev1.PullAlways,
VolumeMounts: []corev1.VolumeMount{
{
Name: "application-config",
Expand Down
1 change: 1 addition & 0 deletions controllers/profiles/common/mutate_visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func ImageDeploymentMutateVisitor(workflow *operatorapi.SonataFlow, image string
deployment := object.(*appsv1.Deployment)
_, idx := kubeutil.GetContainerByName(operatorapi.DefaultContainerName, &deployment.Spec.Template.Spec)
deployment.Spec.Template.Spec.Containers[idx].Image = image
deployment.Spec.Template.Spec.Containers[idx].ImagePullPolicy = kubeutil.GetImagePullPolicy(image)
return nil
}
}
Expand Down
1 change: 0 additions & 1 deletion controllers/profiles/common/object_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func defaultContainer(workflow *operatorapi.SonataFlow) (*corev1.Container, erro
FailureThreshold: healthStartedFailureThreshold,
PeriodSeconds: healthStartedPeriodSeconds,
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: kubeutil.SecurityDefaults(),
}
// Merge with flowContainer
Expand Down
39 changes: 39 additions & 0 deletions utils/kubernetes/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Apache Software Foundation (ASF)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kubernetes

import (
"strings"

corev1 "k8s.io/api/core/v1"
)

// GetImagePullPolicy gets the default corev1.PullPolicy depending on the image tag specified.
// It follows the conventions of docker client and OpenShift. If no tag specified, it assumes latest.
// Returns PullAlways if latest tag, empty otherwise to let the cluster figure it out.
func GetImagePullPolicy(imageTag string) corev1.PullPolicy {
if len(imageTag) == 0 {
return ""
}
idx := strings.LastIndex(imageTag, ":")
if idx < 0 {
return corev1.PullAlways
}
tag := imageTag[idx+1:]
if tag == "latest" {
return corev1.PullAlways
}
return ""
}
46 changes: 46 additions & 0 deletions utils/kubernetes/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Apache Software Foundation (ASF)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kubernetes

import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
)

func TestGetImagePullPolicy(t *testing.T) {
type args struct {
imageTag string
}
tests := []struct {
name string
args args
want v1.PullPolicy
}{
{"Short name with latest", args{"ubi9-micro:latest"}, v1.PullAlways},
{"Long name with latest", args{"gcr.io/knative-releases/knative.dev/eventing/cmd/event_display:latest"}, v1.PullAlways},
{"No tag specified", args{"ubi9-micro"}, v1.PullAlways},
{"Short name with tag", args{"ubi9-micro:9.3.1-2"}, ""},
{"Long name with tag", args{"gcr.io/knative-releases/knative.dev/eventing/cmd/event_display:1.2"}, ""},
{"Empty tag", args{""}, ""},
{"Messy tag", args{":"}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetImagePullPolicy(tt.args.imageTag), "GetImagePullPolicy(%v)", tt.args.imageTag)
})
}
}

0 comments on commit 65f1c35

Please sign in to comment.