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 Feb 1, 2024
1 parent 8b5a956 commit cd1b1d5
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 130 deletions.
27 changes: 16 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,11 @@ 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 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 @@ -282,19 +283,21 @@ 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 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 @@ -342,11 +345,12 @@ 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 All @@ -372,11 +376,12 @@ 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 Down Expand Up @@ -428,11 +433,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)' \
PLATFORM=$(PLATFORM) \
TAG=$(CNS_PLATFORM_TAG) \
TARGET=$(OS) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
Expand Down Expand Up @@ -461,7 +467,8 @@ npm-image: ## build the npm container image.
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)' \
PLATFORM=$(PLATFORM) \
TAG=$(NPM_PLATFORM_TAG)\
TAG=$(NPM_PLATFORM_TAG) \
TARGET=$(OS) \
OS=$(OS) \
ARCH=$(ARCH) \
OS_VERSION=$(OS_VERSION)
Expand Down Expand Up @@ -531,8 +538,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.

11 changes: 8 additions & 3 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 @@ -11,14 +11,15 @@ COPY . .
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-vnet -trimpath -ldflags "-X main.version="$VERSION"" -gcflags="-dwarflocationlists=true" cni/network/plugin/main.go
RUN GOOS=$OS CGO_ENABLED=0 go build -a -o /go/bin/azure-vnet-telemetry -trimpath -ldflags "-X main.version="$VERSION"" -gcflags="-dwarflocationlists=true" cni/telemetry/service/telemetrymain.go
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/azurecni-stateless -trimpath -ldflags "-X main.version="$VERSION"" -gcflags="-dwarflocationlists=true" cni/network/stateless/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 --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/telemetry/azure-vnet-telemetry.config /payload/azure-vnet-telemetry.config
Expand All @@ -34,6 +35,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" ]
40 changes: 0 additions & 40 deletions cni/linux.Dockerfile

This file was deleted.

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

FROM --platform=linux/${ARCH} mcr.microsoft.com/oss/go/microsoft/golang:1.21 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:1.0 AS certs
RUN tdnf upgrade -y && tdnf install -y ca-certificates

FROM mcr.microsoft.com/cbl-mariner/base/core:2.0 as linux
RUN tdnf install -y iptables
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /go/bin/azure-cns /azure-cns
RUN ln -s /usr/local/bin/azure-cns /azure-cns
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT [ "/azure-cns" ]
EXPOSE 10090

FROM mcr.microsoft.com/windows/servercore:${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 /azure-cns.exe /go/bin/azure-cns
ENTRYPOINT ["azure-cns.exe"]
EXPOSE 10090
23 changes: 0 additions & 23 deletions cns/linux.Dockerfile

This file was deleted.

15 changes: 0 additions & 15 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 @@ -33,6 +33,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
COPY --from=builder /usr/local/src/npm/scripts /usr/local/npm
RUN apt-get update && apt-get install -y iptables ipset ca-certificates && apt-get autoremove -y && apt-get clean
Expand Down
2 changes: 1 addition & 1 deletion npm/windows.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WORKDIR /usr/local/src
COPY . .
RUN GOOS=windows CGO_ENABLED=0 go build -v -o /usr/local/bin/azure-npm.exe -ldflags "-X main.version="$VERSION" -X "$NPM_AI_PATH"="$NPM_AI_ID"" -gcflags="-dwarflocationlists=true" npm/cmd/*.go

FROM mcr.microsoft.com/windows/servercore:${OS_VERSION}
FROM mcr.microsoft.com/windows/servercore:${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 cd1b1d5

Please sign in to comment.