kubernetes-csi / csi-release-tools

shared build and test files used by kubernetes-csi projects
Apache License 2.0
22 stars 73 forks source link

Add support for windows multi-image tag builds #145

Closed mauriciopoppe closed 3 years ago

mauriciopoppe commented 3 years ago

When doing a multiarch build (make push-multiarch) we might need to override additional args that might be set in the Dockerfile.Windows file for a multi-image build, for example

https://github.com/kubernetes-csi/node-driver-registrar/blob/bd1ad6268a014dceaa36c71c14a3fc0ad8240b58/Dockerfile.Windows#L1-L15

ARG CORE_IMAGE=servercore
ARG CORE_IMAGE_TAG=1809
ARG BUILD_IMAGE=nanoserver
ARG BUILD_IMAGE_TAG=1809
ARG REGISTRY=mcr.microsoft.com/windows

FROM ${REGISTRY}/${CORE_IMAGE}:${CORE_IMAGE_TAG} as core
FROM ${REGISTRY}/${BUILD_IMAGE}:${BUILD_IMAGE_TAG}
LABEL description="CSI Node driver registrar"

COPY ./bin/csi-node-driver-registrar.exe /csi-node-driver-registrar.exe
COPY --from=core /Windows/System32/netapi32.dll /Windows/System32/netapi32.dll

USER ContainerAdministrator
ENTRYPOINT ["/csi-node-driver-registrar.exe"]

We can override BUILD_IMAGE and BUILD_IMAGE_TAG before building the docker image e.g.

docker build \
   --build-arg BUILD_IMAGE=X \
   --build-arg BUILD_IMAGE_TAG=Y \
   -f Dockerfile.windows .

The existing Makefile has support for https://github.com/kubernetes-csi/csi-release-tools/blob/master/prow.sh#L80 BUILD_PLATFORMS="linux amd64; windows amd64 .exe; ..., in the Makefile this string is split by ; and used in this fragment:

        echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do \
            docker buildx build --push \
                --tag $(IMAGE_NAME):$$arch-$$os-$$tag \
                --platform=$$os/$$arch \
                --file $$(eval echo \$${dockerfile_$$os}) \
                --build-arg binary=./bin/$*$$suffix \
                --build-arg ARCH=$$arch \
                --label revision=$(REV) \
                .; \
        done; \

We'd like to include additional build args when calling docker build


One approach that I've seen in PD CSI is to have multiple targets per windows build https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/blob/cfe67c8322c9094030bdf28737a50eb5b1c2a542/Makefile#L51-L78, an approach would be to split BUILD_PLATFORMS to LINUX_BUILD_PLATFORMS and WINDOWS_BUILD_PLATFORMS and then do:

LINUX_BUILD_PLATFORMS="linux ppc64le -ppc64le; linux s390x -s390x; linux arm64 -arm64 ..."
WINDOWS_BUILD_PLATFORMS="windows amd64 nanoserver:1809; windows amd64 servercore:20H2 ..."
    echo "$$linux_build_platforms" | tr ';' '\n' | while read -r os arch suffix; do \
        docker buildx build --push \
            --tag $(IMAGE_NAME):$$arch-$$os-$$tag \
            --platform=$$os/$$arch \
            --file $$(eval echo \$${dockerfile_$$os}) \
            --build-arg binary=./bin/$*$$suffix \
            --build-arg ARCH=$$arch \
            --label revision=$(REV) \
            .; \
    done; \
    echo "$$windows_build_platforms" | tr ';' '\n' | while read -r os arch base_image; do \
        docker buildx build --push \
            --tag $(IMAGE_NAME):$$arch-$$os-$$tag \
            --platform=$$os/$$arch \
            --file $$(eval echo \$${dockerfile_$$os}) \
            --build-arg binary=./bin/$*.exe \
            --build-arg BASE_IMAGE=$$base_image \
            --build-arg ARCH=$$arch \
            --label revision=$(REV) \
            .; \
    done; \

Another approach would be to add more tokens to the string (I'm not sure if at the end to make it backwards compatible) for windows e.g.

BUILD_PLATFORMS="windows amd64 .exe nanoserver:1909; ..."

        echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix base_image; do \
            docker buildx build --push \
                --tag $(IMAGE_NAME):$$arch-$$os-$$tag \
                --platform=$$os/$$arch \
                --file $$(eval echo \$${dockerfile_$$os}) \
                --build-arg binary=./bin/$*$$suffix \
                --build-arg BASE_IMAGE=$$base_image \
                --build-arg ARCH=$$arch \
                --label revision=$(REV) \
                .; \
        done; \

These are just a few approaches, any feedback would be appreciated!

Also ref https://github.com/kubernetes-csi/node-driver-registrar/issues/135

cc @jingxu97

jingxu97 commented 3 years ago

cc @pohly

pohly commented 3 years ago

I prefer extending BUILD_PLATFORMS.