Skip to content

Commit b53c028

Browse files
cbandybenjaminjb
andcommitted
Simplify build of development images
This is compatible with more environments. Co-authored-by: Ben Blattberg <ben.blattberg@crunchydata.com> Issue: PGO-1046
1 parent cd43e53 commit b53c028

File tree

6 files changed

+48
-159
lines changed

6 files changed

+48
-159
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://docs.docker.com/build/concepts/context/#dockerignore-files
2+
# https://docs.podman.io/en/latest/markdown/podman-build.1.html#files
3+
/.git
4+
/bin
5+
/hack
6+
!/hack/tools/queries

.github/workflows/test.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ jobs:
112112
registry.developers.crunchydata.com/crunchydata/crunchy-postgres-gis:ubi9-16.9-3.4-2520
113113
registry.developers.crunchydata.com/crunchydata/crunchy-upgrade:ubi9-17.5-2520
114114
registry.developers.crunchydata.com/crunchydata/crunchy-postgres-gis:ubi9-17.5-3.4-2520
115-
- run: go mod download
116-
- name: Build executable
117-
run: PGO_VERSION='${{ github.sha }}' make build-postgres-operator
118115
119116
- name: Get pgMonitor files.
120117
run: make get-pgmonitor
@@ -123,13 +120,14 @@ jobs:
123120
QUERIES_CONFIG_DIR: "${{ github.workspace }}/hack/tools/queries"
124121

125122
# Start a Docker container with the working directory mounted.
123+
- run: make build BUILDAH=docker
126124
- name: Start PGO
127125
run: |
128126
kubectl apply --server-side -k ./config/namespace
129127
kubectl apply --server-side -k ./config/dev
130128
hack/create-kubeconfig.sh postgres-operator pgo
131129
docker run --detach --network host --read-only \
132-
--volume "$(pwd):/mnt" --workdir '/mnt' --env 'PATH=/mnt/bin' \
130+
--volume "$(pwd):/mnt" --workdir '/mnt' \
133131
--env 'CHECK_FOR_UPGRADES=false' \
134132
--env 'QUERIES_CONFIG_DIR=/mnt/hack/tools/queries' \
135133
--env 'KUBECONFIG=hack/.kube/postgres-operator/pgo' \
@@ -145,8 +143,7 @@ jobs:
145143
--env 'RELATED_IMAGE_STANDALONE_PGADMIN=registry.developers.crunchydata.com/crunchydata/crunchy-pgadmin4:ubi9-9.2-2520' \
146144
--env 'RELATED_IMAGE_COLLECTOR=registry.developers.crunchydata.com/crunchydata/postgres-operator:ubi9-5.8.2-0' \
147145
--env 'PGO_FEATURE_GATES=TablespaceVolumes=true,OpenTelemetryLogs=true,OpenTelemetryMetrics=true' \
148-
--name 'postgres-operator' ubuntu \
149-
postgres-operator
146+
--name 'postgres-operator' localhost/postgres-operator
150147
- name: Install kuttl
151148
run: |
152149
curl -Lo /usr/local/bin/kubectl-kuttl https://github.com/kudobuilder/kuttl/releases/download/v0.13.0/kubectl-kuttl_0.13.0_linux_x86_64

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2017 - 2025 Crunchy Data Solutions, Inc.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
FROM docker.io/library/golang:bookworm AS build
6+
7+
COPY licenses /licenses
8+
COPY hack/tools/queries /opt/crunchy/conf
9+
10+
WORKDIR /usr/src/app
11+
COPY . .
12+
ENV GOCACHE=/var/cache/go
13+
RUN --mount=type=cache,target=/var/cache/go go build ./cmd/postgres-operator
14+
15+
FROM docker.io/library/debian:bookworm
16+
17+
COPY --from=build /licenses /licenses
18+
COPY --from=build /opt/crunchy/conf /opt/crunchy/conf
19+
COPY --from=build /usr/src/app/postgres-operator /usr/local/bin
20+
21+
USER 2
22+
23+
CMD ["postgres-operator"]

Makefile

Lines changed: 16 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
PGO_IMAGE_NAME ?= postgres-operator
2-
PGO_IMAGE_MAINTAINER ?= Crunchy Data
3-
PGO_IMAGE_SUMMARY ?= Crunchy PostgreSQL Operator
4-
PGO_IMAGE_DESCRIPTION ?= $(PGO_IMAGE_SUMMARY)
5-
PGO_IMAGE_URL ?= https://www.crunchydata.com/products/crunchy-postgresql-for-kubernetes
6-
PGO_IMAGE_PREFIX ?= localhost
71

82
PGMONITOR_DIR ?= hack/tools/pgmonitor
93
PGMONITOR_VERSION ?= v5.2.1
104
QUERIES_CONFIG_DIR ?= hack/tools/queries
115

12-
# Buildah's "build" used to be "bud". Use the alias to be compatible for a while.
13-
BUILDAH_BUILD ?= buildah bud
14-
6+
BUILDAH ?= buildah
157
GO ?= go
16-
GO_BUILD = $(GO) build
178
GO_TEST ?= $(GO) test
189

1910
# Ensure modules imported by `postgres-operator` and `controller-gen` are compatible
@@ -25,28 +16,12 @@ ENVTEST ?= $(GO) run sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
2516
KUTTL ?= $(GO) run github.com/kudobuilder/kuttl/pkg/kuttlctl/cmd/kubectl-kuttl@latest
2617
KUTTL_TEST ?= $(KUTTL) test
2718

28-
2919
##@ General
3020

31-
# The help target prints out all targets with their descriptions organized
32-
# beneath their categories. The categories are represented by '##@' and the
33-
# target descriptions by '##'. The awk command is responsible for reading the
34-
# entire set of makefiles included in this invocation, looking for lines of the
35-
# file as xyz: ## something, and then pretty-formatting the target and help. Then,
36-
# if there's a line with ##@ something, that gets pretty-printed as a category.
37-
# More info on the usage of ANSI control characters for terminal formatting:
38-
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
39-
# More info on the awk command:
40-
# http://linuxcommand.org/lc3_adv_awk.php
41-
4221
.PHONY: help
4322
help: ## Display this help.
4423
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
4524

46-
.PHONY: all
47-
all: ## Build all images
48-
all: build-postgres-operator-image
49-
5025
.PHONY: setup
5126
setup: ## Run Setup needed to build images
5227
setup: get-pgmonitor
@@ -103,6 +78,7 @@ clean-deprecated: ## Clean deprecated resources
10378

10479

10580
##@ Deployment
81+
10682
.PHONY: createnamespaces
10783
createnamespaces: ## Create operator and target namespaces
10884
kubectl apply -k ./config/namespace
@@ -131,7 +107,6 @@ undeploy: ## Undeploy the PostgreSQL Operator
131107
deploy-dev: ## Deploy the PostgreSQL Operator locally
132108
deploy-dev: PGO_FEATURE_GATES ?= "AllAlpha=true"
133109
deploy-dev: get-pgmonitor
134-
deploy-dev: build-postgres-operator
135110
deploy-dev: createnamespaces
136111
kubectl apply --server-side -k ./config/dev
137112
hack/create-kubeconfig.sh postgres-operator pgo
@@ -150,54 +125,22 @@ deploy-dev: createnamespaces
150125
/RELATED_IMAGE_/ { N; s,.*\(RELATED_[^[:space:]]*\).*value:[[:space:]]*\([^[:space:]]*\),\1="\2",; p; }; \
151126
}') \
152127
$(foreach v,$(filter RELATED_IMAGE_%,$(.VARIABLES)),$(v)="$($(v))") \
153-
bin/postgres-operator
154-
155-
##@ Build - Binary
156-
.PHONY: build-postgres-operator
157-
build-postgres-operator: ## Build the postgres-operator binary
158-
$(GO_BUILD) $(\
159-
) --ldflags '-X "main.versionString=$(PGO_VERSION)"' $(\
160-
) --trimpath -o bin/postgres-operator ./cmd/postgres-operator
161-
162-
##@ Build - Images
163-
.PHONY: build-postgres-operator-image
164-
build-postgres-operator-image: ## Build the postgres-operator image
165-
build-postgres-operator-image: PGO_IMAGE_REVISION := $(shell git rev-parse HEAD)
166-
build-postgres-operator-image: PGO_IMAGE_TIMESTAMP := $(shell date -u +%FT%TZ)
167-
build-postgres-operator-image: build-postgres-operator
168-
build-postgres-operator-image: build/postgres-operator/Dockerfile
169-
$(if $(shell (echo 'buildah version 1.24'; $(word 1,$(BUILDAH_BUILD)) --version) | sort -Vc 2>&1), \
170-
$(warning WARNING: old buildah does not invalidate its cache for changed labels: \
171-
https://github.com/containers/buildah/issues/3517))
172-
$(if $(IMAGE_TAG),, $(error missing IMAGE_TAG))
173-
$(strip $(BUILDAH_BUILD)) \
174-
--tag $(BUILDAH_TRANSPORT)$(PGO_IMAGE_PREFIX)/$(PGO_IMAGE_NAME):$(IMAGE_TAG) \
175-
--label name='$(PGO_IMAGE_NAME)' \
176-
--label build-date='$(PGO_IMAGE_TIMESTAMP)' \
177-
--label description='$(PGO_IMAGE_DESCRIPTION)' \
178-
--label maintainer='$(PGO_IMAGE_MAINTAINER)' \
179-
--label summary='$(PGO_IMAGE_SUMMARY)' \
180-
--label url='$(PGO_IMAGE_URL)' \
181-
--label vcs-ref='$(PGO_IMAGE_REVISION)' \
182-
--label vendor='$(PGO_IMAGE_MAINTAINER)' \
183-
--label io.k8s.display-name='$(PGO_IMAGE_NAME)' \
184-
--label io.k8s.description='$(PGO_IMAGE_DESCRIPTION)' \
185-
--label io.openshift.tags="postgresql,postgres,sql,nosql,crunchy" \
186-
--annotation org.opencontainers.image.authors='$(PGO_IMAGE_MAINTAINER)' \
187-
--annotation org.opencontainers.image.vendor='$(PGO_IMAGE_MAINTAINER)' \
188-
--annotation org.opencontainers.image.created='$(PGO_IMAGE_TIMESTAMP)' \
189-
--annotation org.opencontainers.image.description='$(PGO_IMAGE_DESCRIPTION)' \
190-
--annotation org.opencontainers.image.revision='$(PGO_IMAGE_REVISION)' \
191-
--annotation org.opencontainers.image.title='$(PGO_IMAGE_SUMMARY)' \
192-
--annotation org.opencontainers.image.url='$(PGO_IMAGE_URL)' \
193-
$(if $(PGO_VERSION),$(strip \
194-
--label release='$(PGO_VERSION)' \
195-
--label version='$(PGO_VERSION)' \
196-
--annotation org.opencontainers.image.version='$(PGO_VERSION)' \
197-
)) \
198-
--file $< --format docker --layers .
128+
$(GO) run ./cmd/postgres-operator
129+
130+
##@ Build
131+
132+
.PHONY: build
133+
build: ## Build a postgres-operator image
134+
$(BUILDAH) build --tag localhost/postgres-operator \
135+
--label org.opencontainers.image.authors='Crunchy Data' \
136+
--label org.opencontainers.image.description='Crunchy PostgreSQL Operator' \
137+
--label org.opencontainers.image.revision='$(shell git rev-parse HEAD)' \
138+
--label org.opencontainers.image.source='https://github.com/CrunchyData/postgres-operator' \
139+
--label org.opencontainers.image.title='Crunchy PostgreSQL Operator' \
140+
.
199141

200142
##@ Test
143+
201144
.PHONY: check
202145
check: ## Run basic go tests with coverage output
203146
check: get-pgmonitor
@@ -301,23 +244,3 @@ generate-rbac: ## Generate RBAC
301244
rbac:roleName='postgres-operator' \
302245
paths='./cmd/...' paths='./internal/...' \
303246
output:dir='config/rbac' # {directory}/role.yaml
304-
305-
306-
##@ Release
307-
308-
.PHONY: license licenses
309-
license: licenses
310-
licenses: ## Aggregate license files
311-
./bin/license_aggregator.sh ./cmd/...
312-
313-
.PHONY: release-postgres-operator-image release-postgres-operator-image-labels
314-
release-postgres-operator-image: ## Build the postgres-operator image and all its prerequisites
315-
release-postgres-operator-image: release-postgres-operator-image-labels
316-
release-postgres-operator-image: licenses
317-
release-postgres-operator-image: build-postgres-operator-image
318-
release-postgres-operator-image-labels:
319-
$(if $(PGO_IMAGE_DESCRIPTION),, $(error missing PGO_IMAGE_DESCRIPTION))
320-
$(if $(PGO_IMAGE_MAINTAINER),, $(error missing PGO_IMAGE_MAINTAINER))
321-
$(if $(PGO_IMAGE_NAME),, $(error missing PGO_IMAGE_NAME))
322-
$(if $(PGO_IMAGE_SUMMARY),, $(error missing PGO_IMAGE_SUMMARY))
323-
$(if $(PGO_VERSION),, $(error missing PGO_VERSION))

bin/license_aggregator.sh

Lines changed: 0 additions & 45 deletions
This file was deleted.

build/postgres-operator/Dockerfile

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)