jkube icon indicating copy to clipboard operation
jkube copied to clipboard

imagePullPolicy: IfNotPresent should be Always for -SNAPSHOTs

Open robinroos opened this issue 4 years ago • 9 comments

Description

Upon oc:resource oc:deploy of a Spring Boot application, the resulting Deployment has imagePullPolicy: IfNotPresent. This causes OpenShift not to fetch the new version. If the built version is a SNAPSHOT then imagePullPolicy should have value Always.

Info

  • Eclipse JKube version : 1.5.1
  • Maven version (mvn -v) : 3.6.3
  • Kubernetes / Red Hat OpenShift setup and version : OpenShift 4.7

robinroos avatar Nov 22 '21 18:11 robinroos

https://kubernetes.io/docs/concepts/containers/images/#imagepullpolicy-defaulting

Another option would be to leave it blank and let the cluster decide. But yes, this seems like something we should fix.

manusa avatar Nov 23 '21 11:11 manusa

As agreed during triage session, we should set the value to Always (not leave it blank), and provide a warning.

manusa avatar Nov 29 '21 13:11 manusa

I'm running into some problems with this scenario.

Running on Minikube and imagePullPolicy is set to Always. The scenario where the image is built in the Minikube's Docker daemon eval $(minikube docker-env) fails, since the cluster will still try to fetch the image from the remote repo.

manusa avatar Dec 13 '21 14:12 manusa

Another option that we might consider is using the hash to reference the image in the generated resources when the project version is a SNAPSHOT.

manusa avatar Dec 14 '21 09:12 manusa

I understand this was tricky and could not be included in 1.6.0, but thanks for trying. I will continue to manually set this property on new deploymentsof snapshot container versions.

robinroos avatar Feb 03 '22 11:02 robinroos

The problem is that the change may break some inner-loop scenarios (such as the mentioned issue with Minikube). We need to further evaluate this so that the fix is valid for all use cases.

manusa avatar Feb 03 '22 11:02 manusa

This is the best workaround I can find, and for the reasons stated in my opinion the workaround is not good enough.


<plugin>
    <!-- jumping through hoops here to set jkube.imagePullPolicy in a generic manner -->
    <!-- taken from: https://stackoverflow.com/questions/44002807/conditionally-set-property-in-maven -->
    <!-- in order to work around: https://github.com/eclipse/jkube/issues/1138 -->
    <!-- Personally I (Robin Roos) do not like this solution.  It is too "imperative" and very hard to read. -->
    <!-- I would prefer that JKube address the core issue, or that Apache provide a Maven capability to activate a profile if the build version is snapshot. -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>build-helper-regex-is-snapshot-used</id>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>jkube.imagePullPolicy</name>
                <value>${project.version} Always IfNotPresent</value>
                <regex>(?=.*SNAPSHOT).*(Always).*|.*(IfNotPresent).*</regex>
                <replacement>$1$2</replacement>
                <failIfNoMatch>true</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

robinroos avatar May 03 '23 10:05 robinroos

I have this approach working now. It is still a "workaround" and I look forward to moving away from it to something less brittle and more understandable.

Note that build-helper-maven-plugin is bound to the validate phase. If you run maven with other phases, then surely validate will execute. But if your maven command is instead a list of goals (goals are prefixed by plugin:) then you will have to add validate explicitly.

e.g. mvn -Pmyopenshiftprofile validate resources:resources oc:resource oc:helm oc:helm-push

robinroos avatar May 03 '23 15:05 robinroos

Apparently this is an actual bug since it contravenes what's stated in the docs:

The pull policy imagePullPolicy is set according to the given configuration. If no configuration is set, the default is IfNotPresent for release versions, and Always for snapshot versions.

manusa avatar Mar 17 '24 18:03 manusa