eclipse-jkube / jkube

Build and Deploy java applications on Kubernetes
https://www.eclipse.dev/jkube/
Eclipse Public License 2.0
739 stars 482 forks source link

Build args not applied as ARG (external dockerfile openshift-maven) #2860

Closed Snijder closed 5 months ago

Snijder commented 6 months ago

Describe the bug

I am trying to specify the image version from the pom which I can then use in an ARG in my dockerfile when building on openshift. (related docs)

Eclipse JKube version

1.16.2

Component

Kubernetes Maven Plugin

Apache Maven version

other (please specify in additional context)

Gradle version

None

Steps to reproduce

  1. Configure a build arg like so: pom.xml.txt
    <plugin>
    <groupId>org.eclipse.jkube</groupId>
    <artifactId>openshift-maven-plugin</artifactId>
    <version>1.16.2</version>
    <executions>
        <execution>
            <id>build-main-container</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
            <configuration>
                <buildStrategy>docker</buildStrategy>
                <images>
                    <image>
                        <name>${openshift.registry}/helloworld:%l</name>
                        <build>
                            <contextDir>${project.basedir}/src/main/docker/</contextDir>
                            <dockerFile>Dockerfile</dockerFile>
                            <args>
                                <IMAGEVERSION>latest</IMAGEVERSION>
                            </args>
                        </build>
                    </image>
                </images>
            </configuration>
        </execution>
    </executions>
    </plugin>
  2. Specify the dockerfile under src/main/docker as follows:
    
    ARG IMAGEVERSION
    FROM docker.io/alpine:${IMAGEVERSION} as builder

RUN touch test.txt

FROM docker.io/alpine:${IMAGEVERSION} COPY --from=builder test.txt test.txt

ENTRYPOINT ["ls"]


3. run `mvn clean verify -Dopenshift.namespace=yournamespacehere`

### Expected behavior

I expect the build arg to be applied and the ARG in the Dockerfile to be filled at build time.

### Runtime

OpenShift

### Kubernetes API Server version

1.25.3

### Environment

Linux, other (please specify in additional context)

### Eclipse JKube Logs

```shell
[INFO] --- oc:1.16.2:build (build-main-container) @ test ---
[INFO] oc: Using OpenShift build with strategy Docker
[INFO] oc: Using Dockerfile: /home/asdf/Documents/code/frb-common/helloworld/src/main/docker/Dockerfile
[INFO] oc: Using Docker Context Directory: /home/asdf/Documents/code/frb-common/helloworld/src/main/docker
[WARNING] oc: Dockerfile /home/asdf/Documents/code/frb-common/helloworld/src/main/docker/Dockerfile does not contain an ADD or COPY directive to include assembly created at maven. Ignoring assembly.
[INFO] oc: [image-registry.openshift-image-registry.svc:5000/bl-asdfsnijder/helloworld:latest]: Created docker source tar /home/asdf/Documents/code/frb-common/helloworld/target/docker/image-registry.openshift-image-registry.svc/5000/bl-asdfsnijder/helloworld/latest/tmp/docker-build.tar
[INFO] oc: Adding to Secret pullsecret-jkube
[INFO] oc: Using Secret pullsecret-jkube
[INFO] oc: Updating BuildServiceConfig helloworld-s2i for Docker strategy
[INFO] oc: Adding to ImageStream helloworld
[INFO] oc: Starting Build helloworld-s2i
[INFO] oc: Waiting for build helloworld-s2i-11 to complete...
[INFO] oc: Adding cluster TLS certificate authority to trust store
[INFO] oc: time="2024-03-28T12:48:54Z" level=info msg="Not using native diff for overlay, this may cause degraded performance for building images: kernel has CONFIG_OVERLAY_FS_REDIRECT_DIR enabled"
[INFO] oc: I0328 12:48:54.750934       1 defaults.go:112] Defaulting to storage driver "overlay" with options [mountopt=metacopy=on].
[INFO] oc: Caching blobs under "/var/cache/blobs".
[INFO] oc: 
[INFO] oc: Pulling image docker.io/alpine ...
[INFO] oc: Trying to pull docker.io/library/alpine:latest...
[INFO] oc: Getting image source signatures
[INFO] oc: Copying blob sha256:4abcf20661432fb2d719aaf90656f55c287f8ca915dc1c92ec14ff61e67fbaf8
[INFO] oc: Copying config sha256:05455a08881ea9cf0e752bc48e61bbd71a34c029bb13df01e40e3e70e0d007bd
[INFO] oc: Writing manifest to image destination
[INFO] oc: Storing signatures
[INFO] oc: Adding transient rw bind mount for /run/secrets/rhsm
[INFO] oc: time="2024-03-28T12:48:57Z" level=warning msg="missing \"IMAGEVERSION\" build argument. Try adding \"--build-arg IMAGEVERSION=<VALUE>\" to the command line"
[INFO] oc: [1/2] STEP 1/3: FROM docker.io/alpine: AS builder

Sample Reproducer Project

No response

Additional context

Ran on Openshift 4 generatedbuildconfig.yaml.txt The build config exported from openshift. Notice the args are available as ENV. https://github.com/eclipse/jkube/pull/1395 seems related.

manusa commented 6 months ago

I'm assuming that what you want is to replace the ${IMAGEVERSION} with whatever you define in the pom.xml and not really use an ARG.

For that, then you need to use a Maven property instead:

<properties>
   <IMAGEVERSION>latest</IMAGEVERSION>
</properties>
Snijder commented 6 months ago

That works, and I am currently using it to work around it.

However, if I understand the documentation correctly (https://eclipse.dev/jkube/docs/openshift-maven-plugin/#_build_args) we should be able to set a build-arg using the configuration in the pom, but that sets an environment variable instead. Which is not what I was expecting.

manusa commented 6 months ago

We'll have to dig deeper into this. Not sure if this was accounted for while absorbing the Docker Maven Plugin related code.