Skip to content

Commit

Permalink
combine linux and win Dockerfiles using build targets
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Baker <rbtr@users.noreply.github.com>
  • Loading branch information
rbtr authored Jun 14, 2024
1 parent bebbe63 commit 23c50aa
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 148 deletions.
44 changes: 28 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,16 @@ qemu-user-static: ## Set up the host to run qemu multiplatform container builds.

container-buildah: # util target to build container images using buildah. do not invoke directly.
buildah bud \
--build-arg ARCH=$(ARCH) \
--build-arg OS=$(OS) \
--build-arg OS_VERSION=$(OS_VERSION) \
--build-arg PLATFORM=$(PLATFORM) \
--build-arg VERSION=$(TAG) \
$(EXTRA_BUILD_ARGS) \
--jobs 16 \
--platform $(PLATFORM) \
--target $(TARGET) \
-f $(DOCKERFILE) \
--build-arg VERSION=$(TAG) $(EXTRA_BUILD_ARGS) \
-t $(IMAGE_REGISTRY)/$(IMAGE):$(TAG) \
.
buildah push $(IMAGE_REGISTRY)/$(IMAGE):$(TAG)
Expand All @@ -312,19 +318,26 @@ container-docker: # util target to build container images using docker buildx. d
docker buildx create --use --platform $(PLATFORM)
docker buildx build \
$(BUILDX_ACTION) \
--build-arg ARCH=$(ARCH) \
--build-arg OS=$(OS) \
--build-arg OS_VERSION=$(OS_VERSION) \
--build-arg PLATFORM=$(PLATFORM) \
--build-arg VERSION=$(TAG) \
$(EXTRA_BUILD_ARGS) \
--platform $(PLATFORM) \
--target $(TARGET) \
-f $(DOCKERFILE) \
--build-arg VERSION=$(TAG) $(EXTRA_BUILD_ARGS) \
-t $(IMAGE_REGISTRY)/$(IMAGE):$(TAG) \
.

container: # util target to build container images. do not invoke directly.
$(MAKE) container-$(CONTAINER_BUILDER) \
ARCH=$(ARCH) \
OS=$(OS) \
OS_VERSION=$(OS_VERSION) \
PLATFORM=$(PLATFORM) \
TAG=$(TAG) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
TARGET=$(TARGET)

container-push: # util target to publish container image. do not invoke directly.
$(CONTAINER_BUILDER) push \
Expand Down Expand Up @@ -372,11 +385,11 @@ azure-ipam-image-name-and-tag: # util target to print the azure-ipam image name

azure-ipam-image: ## build azure-ipam container image.
$(MAKE) container \
DOCKERFILE=azure-ipam/$(OS).Dockerfile \
DOCKERFILE=azure-ipam/Dockerfile \
IMAGE=$(AZURE_IPAM_IMAGE) \
EXTRA_BUILD_ARGS='--build-arg OS=$(OS) --build-arg ARCH=$(ARCH) --build-arg OS_VERSION=$(OS_VERSION)' \
PLATFORM=$(PLATFORM) \
TAG=$(AZURE_IPAM_PLATFORM_TAG) \
TARGET=$(OS) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
Expand Down Expand Up @@ -430,11 +443,11 @@ cni-image-name-and-tag: # util target to print the cni image name and tag.

cni-image: ## build cni container image.
$(MAKE) container \
DOCKERFILE=cni/$(OS).Dockerfile \
DOCKERFILE=cni/Dockerfile \
IMAGE=$(CNI_IMAGE) \
EXTRA_BUILD_ARGS='--build-arg OS=$(OS) --build-arg ARCH=$(ARCH) --build-arg OS_VERSION=$(OS_VERSION)' \
PLATFORM=$(PLATFORM) \
TAG=$(CNI_PLATFORM_TAG) \
TARGET=$(OS) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
Expand All @@ -461,7 +474,6 @@ cni-dropgz-image-name-and-tag: # util target to print the CNI dropgz image name
cni-dropgz-image: ## build cni-dropgz container image.
$(MAKE) container \
DOCKERFILE=dropgz/build/$(OS).Dockerfile \
EXTRA_BUILD_ARGS='--build-arg OS=$(OS) --build-arg ARCH=$(ARCH) --build-arg OS_VERSION=$(OS_VERSION)' \
IMAGE=$(CNI_DROPGZ_IMAGE) \
TAG=$(CNI_DROPGZ_PLATFORM_TAG)

Expand All @@ -486,11 +498,12 @@ cns-image-name-and-tag: # util target to print the CNS image name and tag.

cns-image: ## build cns container image.
$(MAKE) container \
DOCKERFILE=cns/$(OS).Dockerfile \
DOCKERFILE=cns/Dockerfile \
IMAGE=$(CNS_IMAGE) \
EXTRA_BUILD_ARGS='--build-arg CNS_AI_PATH=$(CNS_AI_PATH) --build-arg CNS_AI_ID=$(CNS_AI_ID) --build-arg OS_VERSION=$(OS_VERSION)' \
EXTRA_BUILD_ARGS='--build-arg CNS_AI_PATH=$(CNS_AI_PATH) --build-arg CNS_AI_ID=$(CNS_AI_ID)' \
PLATFORM=$(PLATFORM) \
TAG=$(CNS_PLATFORM_TAG) \
TARGET=$(OS) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
Expand All @@ -517,9 +530,10 @@ npm-image: ## build the npm container image.
$(MAKE) container-$(CONTAINER_BUILDER) \
DOCKERFILE=npm/$(OS).Dockerfile \
IMAGE=$(NPM_IMAGE) \
EXTRA_BUILD_ARGS='--build-arg NPM_AI_PATH=$(NPM_AI_PATH) --build-arg NPM_AI_ID=$(NPM_AI_ID) --build-arg OS_VERSION=$(OS_VERSION)' \
EXTRA_BUILD_ARGS='--build-arg NPM_AI_PATH=$(NPM_AI_PATH) --build-arg NPM_AI_ID=$(NPM_AI_ID)' \
PLATFORM=$(PLATFORM) \
TAG=$(NPM_PLATFORM_TAG)\
TAG=$(NPM_PLATFORM_TAG) \
TARGET=$(OS) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
Expand Down Expand Up @@ -589,8 +603,6 @@ manifest-build: # util target to compose multiarch container manifests from plat
)\
)\



manifest-push: # util target to push multiarch container manifest.
$(CONTAINER_BUILDER) manifest push --all $(IMAGE_REGISTRY)/$(IMAGE):$(TAG) docker://$(IMAGE_REGISTRY)/$(IMAGE):$(TAG)

Expand Down
8 changes: 6 additions & 2 deletions azure-ipam/windows.Dockerfile → azure-ipam/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG ARCH
ARG DROPGZ_VERSION=v0.0.12
ARG OS
ARG OS_VERSION
ARG OS

FROM --platform=linux/${ARCH} mcr.microsoft.com/oss/go/microsoft/golang:1.21 AS azure-ipam
ARG OS
Expand All @@ -27,6 +27,10 @@ WORKDIR /go/pkg/mod/github.com/azure/azure-container-networking/dropgz\@$DROPGZ_
COPY --from=compressor /payload/* pkg/embed/fs/
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/dropgz -trimpath -ldflags "-X github.com/Azure/azure-container-networking/dropgz/internal/buildinfo.Version="$VERSION"" -gcflags="-dwarflocationlists=true" main.go

FROM mcr.microsoft.com/windows/nanoserver:${OS_VERSION}
FROM scratch as linux
COPY --from=dropgz /go/bin/dropgz dropgz
ENTRYPOINT [ "/dropgz" ]

FROM mcr.microsoft.com/windows/nanoserver:${OS_VERSION} as windows
COPY --from=dropgz /go/bin/dropgz dropgz.exe
ENTRYPOINT [ "/dropgz.exe" ]
32 changes: 0 additions & 32 deletions azure-ipam/linux.Dockerfile

This file was deleted.

12 changes: 10 additions & 2 deletions cni/windows.Dockerfile → cni/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG ARCH
ARG DROPGZ_VERSION=v0.0.12
ARG OS
ARG OS_VERSION
ARG OS

FROM --platform=linux/${ARCH} mcr.microsoft.com/oss/go/microsoft/golang:1.21 AS azure-vnet
ARG OS
Expand All @@ -13,12 +13,16 @@ RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-vnet-telemetry -trimpath
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-vnet-ipam -trimpath -ldflags "-X main.version="$VERSION"" -gcflags="-dwarflocationlists=true" cni/ipam/plugin/main.go
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-vnet-stateless -trimpath -ldflags "-X main.version="$VERSION"" -gcflags="-dwarflocationlists=true" cni/network/stateless/main.go

FROM scratch as bins
COPY --from=azure-vnet /go/bin/* /

FROM --platform=linux/${ARCH} mcr.microsoft.com/cbl-mariner/base/core:2.0 AS compressor
ARG OS
WORKDIR /payload
COPY --from=azure-vnet /go/bin/* /payload/
COPY --from=azure-vnet /azure-container-networking/cni/azure-$OS.conflist /payload/azure.conflist
COPY --from=azure-vnet /azure-container-networking/cni/azure-$OS-swift.conflist /payload/azure-swift.conflist
COPY --from=azure-vnet /azure-container-networking/cni/azure-$OS-multitenancy-transparent-vlan.conflist /payload/azure-multitenancy-transparent-vlan.conflist
COPY --from=azure-vnet /azure-container-networking/cni/azure-$OS-swift-overlay.conflist /payload/azure-swift-overlay.conflist
COPY --from=azure-vnet /azure-container-networking/cni/azure-$OS-swift-overlay-dualstack.conflist /payload/azure-swift-overlay-dualstack.conflist
COPY --from=azure-vnet /azure-container-networking/cni/azure-$OS-multitenancy.conflist /payload/azure-multitenancy.conflist
Expand All @@ -35,6 +39,10 @@ WORKDIR /go/pkg/mod/github.com/azure/azure-container-networking/dropgz\@$DROPGZ_
COPY --from=compressor /payload/* pkg/embed/fs/
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/dropgz -trimpath -ldflags "-X github.com/Azure/azure-container-networking/dropgz/internal/buildinfo.Version="$VERSION"" -gcflags="-dwarflocationlists=true" main.go

FROM mcr.microsoft.com/windows/nanoserver:${OS_VERSION}
FROM scratch as linux
COPY --from=dropgz /go/bin/dropgz dropgz
ENTRYPOINT [ "/dropgz" ]

FROM mcr.microsoft.com/windows/nanoserver:${OS_VERSION} as windows
COPY --from=dropgz /go/bin/dropgz dropgz.exe
ENTRYPOINT [ "/dropgz.exe" ]
43 changes: 0 additions & 43 deletions cni/linux.Dockerfile

This file was deleted.

36 changes: 36 additions & 0 deletions cns/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
ARG ARCH
ARG OS_VERSION
ARG OS

FROM --platform=linux/${ARCH} mcr.microsoft.com/oss/go/microsoft/golang:1.22-cbl-mariner2.0 AS builder
ARG OS
ARG CNS_AI_ID
ARG CNS_AI_PATH
ARG VERSION
WORKDIR /azure-container-networking
COPY . .
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-cns -ldflags "-X main.version="$VERSION" -X "$CNS_AI_PATH"="$CNS_AI_ID"" -gcflags="-dwarflocationlists=true" cns/service/*.go

FROM mcr.microsoft.com/cbl-mariner/base/core:2.0 as iptables
RUN tdnf install -y iptables

FROM mcr.microsoft.com/cbl-mariner/distroless/minimal:2.0 as linux
COPY --from=iptables /usr/sbin/*tables* /usr/sbin/
COPY --from=iptables /usr/lib /usr/lib
COPY --from=builder /go/bin/azure-cns /usr/local/bin/azure-cns
ENTRYPOINT [ "/usr/local/bin/azure-cns" ]
EXPOSE 10090


# intermediate for win-ltsc2019
FROM mcr.microsoft.com/windows/servercore@sha256:6fdf140282a2f809dae9b13fe441635867f0a27c33a438771673b8da8f3348a4 as ltsc2019

# intermediate for win-ltsc2022
FROM mcr.microsoft.com/windows/servercore@sha256:45952938708fbde6ec0b5b94de68bcdec3f8c838be018536b1e9e5bd95e6b943 as ltsc2022

FROM ${OS_VERSION} as windows
COPY --from=builder /azure-container-networking/cns/kubeconfigtemplate.yaml kubeconfigtemplate.yaml
COPY --from=builder /azure-container-networking/npm/examples/windows/setkubeconfigpath.ps1 setkubeconfigpath.ps1
COPY --from=builder /go/bin/azure-cns /azure-cns.exe
ENTRYPOINT ["azure-cns.exe"]
EXPOSE 10090
28 changes: 0 additions & 28 deletions cns/linux.Dockerfile

This file was deleted.

21 changes: 0 additions & 21 deletions cns/windows.Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion dropgz/build/linux.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ WORKDIR /dropgz
COPY --from=compressor /dropgz .
RUN CGO_ENABLED=0 go build -a -o bin/dropgz -trimpath -ldflags "-X github.com/Azure/azure-container-networking/dropgz/internal/buildinfo.Version="$VERSION"" -gcflags="-dwarflocationlists=true" main.go

FROM scratch
FROM scratch as linux
COPY --from=dropgz /dropgz/bin/dropgz /dropgz
ENTRYPOINT [ "/dropgz" ]
2 changes: 1 addition & 1 deletion dropgz/build/windows.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ WORKDIR /dropgz
COPY --from=compressor /dropgz .
RUN GOOS=windows CGO_ENABLED=0 go build -a -o bin/dropgz.exe -trimpath -ldflags "-X github.com/Azure/azure-container-networking/dropgz/internal/buildinfo.Version="$VERSION"" -gcflags="-dwarflocationlists=true" main.go

FROM mcr.microsoft.com/windows/nanoserver:${OS_VERSION}
FROM mcr.microsoft.com/windows/nanoserver:${OS_VERSION} as windows
COPY --from=dropgz /dropgz/bin/dropgz.exe dropgz.exe
ENTRYPOINT [ "dropgz.exe" ]
2 changes: 1 addition & 1 deletion npm/linux.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WORKDIR /usr/local/src
COPY . .
RUN CGO_ENABLED=0 go build -v -o /usr/local/bin/azure-npm -ldflags "-X main.version="$VERSION" -X "$NPM_AI_PATH"="$NPM_AI_ID"" -gcflags="-dwarflocationlists=true" npm/cmd/*.go

FROM mcr.microsoft.com/mirror/docker/library/ubuntu:20.04
FROM mcr.microsoft.com/mirror/docker/library/ubuntu:20.04 as linux
COPY --from=builder /usr/local/bin/azure-npm /usr/bin/azure-npm
RUN apt-get update && apt-get install -y iptables ipset ca-certificates && apt-get autoremove -y && apt-get clean
RUN chmod +x /usr/bin/azure-npm
Expand Down
2 changes: 1 addition & 1 deletion npm/windows.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN GOOS=windows CGO_ENABLED=0 go build -v -o /usr/local/bin/azure-npm.exe -ldfl
# intermediate for win-ltsc2022
FROM mcr.microsoft.com/windows/servercore@sha256:45952938708fbde6ec0b5b94de68bcdec3f8c838be018536b1e9e5bd95e6b943 as ltsc2022

FROM ${OS_VERSION}
FROM ${OS_VERSION} as windows
COPY --from=builder /usr/local/src/npm/examples/windows/kubeconfigtemplate.yaml kubeconfigtemplate.yaml
COPY --from=builder /usr/local/src/npm/examples/windows/setkubeconfigpath.ps1 setkubeconfigpath.ps1
COPY --from=builder /usr/local/src/npm/examples/windows/setkubeconfigpath-capz.ps1 setkubeconfigpath-capz.ps1
Expand Down

0 comments on commit 23c50aa

Please sign in to comment.