Skip to content

Commit f19927d

Browse files
phiscogabriele-wolfoxmnencia
authored
Store build info in the images and add version subcommands
Co-authored-by: Gabriele Quaresima <gabriele.quaresima@enterprisedb.com> Co-authored-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
1 parent 5a522c5 commit f19927d

File tree

13 files changed

+117
-20
lines changed

13 files changed

+117
-20
lines changed

.github/workflows/continuous-delivery.yml

+15
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ jobs:
156156
-
157157
name: Checkout
158158
uses: actions/checkout@v2.3.4
159+
with:
160+
# To identify the commit we need the history and all teh tags.
161+
fetch-depth: 0
159162
-
160163
name: Build meta
161164
id: build-meta
@@ -179,9 +182,17 @@ jobs:
179182
else
180183
labels='quay.expires-after=7d'
181184
fi
185+
commit_sha=${{ github.event.pull_request.head.sha || github.sha }}
186+
commit_date=$(git log -1 --pretty=format:'%ad' --date short "${commit_sha}")
187+
# use git describe to get the nearest tag and use that to build the version (e.g. 1.4.0+dev24 or 1.4.0)
188+
commit_version=$(git describe --tags --match 'v*' "${commit_sha}"| sed -e 's/^v//; s/-g[0-9a-f]\+$//; s/-\([0-9]\+\)$/+dev\1/')
189+
commit_short=$(git rev-parse --short "${commit_sha}")
182190
echo "::set-output name=images::${images}"
183191
echo "::set-output name=tags::${tags}"
184192
echo "::set-output name=labels::${labels}"
193+
echo "::set-output name=date::${commit_date}"
194+
echo "::set-output name=version::${commit_version}"
195+
echo "::set-output name=commit::${commit_short}"
185196
-
186197
name: Docker meta
187198
id: docker-meta
@@ -219,6 +230,10 @@ jobs:
219230
# Available architecture on UBI8 are: linux/amd64, linux/arm64, linux/ppc64le
220231
platforms: linux/amd64
221232
push: true
233+
build-args: |
234+
DATE=${{ steps.build-meta.outputs.date }}
235+
COMMIT=${{ steps.build-meta.outputs.commit }}
236+
VERSION=${{ steps.build-meta.outputs.version }}
222237
tags: ${{ steps.docker-meta.outputs.tags }}
223238
labels: ${{ steps.build-meta.outputs.labels }}
224239
-

.goreleaser.yml

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ builds:
1717
- id: kubectl-cnp
1818
binary: kubectl-cnp
1919
main: cmd/kubectl-cnp/main.go
20+
ldflags:
21+
- -s
22+
- -w
23+
- -X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildVersion={{.Version}}
24+
- -X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildCommit={{.ShortCommit}}
25+
- -X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildDate={{.CommitDate}}
2026
goos:
2127
- darwin
2228
- linux

Dockerfile

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Build the manager binary
22
FROM registry.access.redhat.com/ubi8/go-toolset:1.14.7-15 as builder
3+
ARG VERSION="dev"
4+
ARG COMMIT="none"
5+
ARG DATE="unknown"
36

47
# We do not use root
58
USER 1001
@@ -19,10 +22,16 @@ RUN go mod download
1922
COPY --chown=1001 . /workspace
2023

2124
# Build
22-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager ./cmd/manager
25+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager -ldflags \
26+
"-s -w \
27+
-X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildVersion=\"$VERSION\" \
28+
-X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildCommit=\"$COMMIT\" \
29+
-X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildDate=\"$DATE\"" \
30+
./cmd/manager
2331

2432
# Use UBI Minimal image as base https://developers.redhat.com/products/rhel/ubi
2533
FROM registry.access.redhat.com/ubi8/ubi-minimal
34+
ARG VERSION="dev"
2635

2736
ENV SUMMARY="Cloud Native PostgreSQL Operator Container Image." \
2837
DESCRIPTION="This Docker image contains Cloud Native PostgreSQL Operator \
@@ -40,7 +49,7 @@ LABEL summary="$SUMMARY" \
4049
name="Cloud Native PostgreSQL Operator" \
4150
vendor="EnterpriseDB Corporation" \
4251
url="https://www.enterprisedb.com/" \
43-
version="1.4.0" \
52+
version="$VERSION" \
4453
release="1"
4554

4655
COPY licenses /licenses

Makefile

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ CONTROLLER_IMG = quay.io/enterprisedb/cloud-native-postgresql-testing:${IMAGE_TA
1313
endif
1414
endif
1515

16+
COMMIT := $(shell git rev-parse --short HEAD || echo unknown)
17+
DATE := $(shell git log -1 --pretty=format:'%ad' --date short)
18+
VERSION := $(shell git describe --tags --match 'v*' | sed -e 's/^v//; s/-g[0-9a-f]\+$$//; s/-\([0-9]\+\)$$/+dev\1/')
19+
LDFLAGS= "-X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildVersion=${VERSION} $\
20+
-X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildCommit=${COMMIT} $\
21+
-X github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions.buildDate=${DATE}"
22+
1623
BUILD_IMAGE ?= true
1724
POSTGRES_IMAGE_NAME ?= quay.io/enterprisedb/postgresql:13
1825
KUSTOMIZE_VERSION ?= v3.5.4
@@ -48,8 +55,8 @@ e2e-test-k3d:
4855

4956
# Build binaries
5057
build: generate fmt vet
51-
go build -o bin/manager ./cmd/manager
52-
go build -o bin/kubectl-cnp ./cmd/kubectl-cnp
58+
go build -o bin/manager -ldflags ${LDFLAGS} ./cmd/manager
59+
go build -o bin/kubectl-cnp -ldflags ${LDFLAGS} ./cmd/kubectl-cnp
5360

5461
# Run against the configured Kubernetes cluster in ~/.kube/config
5562
run: generate fmt vet manifests
@@ -101,8 +108,9 @@ generate: controller-gen
101108
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
102109

103110
# Build the docker image
104-
docker-build: test
105-
docker build . -t ${CONTROLLER_IMG}
111+
docker-build: #test
112+
docker build . -t ${CONTROLLER_IMG} --build-arg VERSION=${VERSION} --build-arg COMMIT=${COMMIT} \
113+
--build-arg DATE=${DATE}
106114

107115
# Push the docker image
108116
docker-push:

cmd/kubectl-cnp/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/plugin/promote"
1919
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/plugin/restart"
2020
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/plugin/status"
21+
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/versions"
2122
)
2223

2324
func main() {
@@ -38,6 +39,7 @@ func main() {
3839
rootCmd.AddCommand(promote.NewCmd())
3940
rootCmd.AddCommand(certificate.NewCmd())
4041
rootCmd.AddCommand(restart.NewCmd())
42+
rootCmd.AddCommand(versions.NewCmd())
4143

4244
if err := rootCmd.Execute(); err != nil {
4345
os.Exit(1)

cmd/manager/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/manager/instance"
1919
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/manager/walarchive"
2020
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/manager/walrestore"
21+
"github.com/EnterpriseDB/cloud-native-postgresql/internal/cmd/versions"
2122

2223
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2324
)
@@ -41,6 +42,7 @@ func main() {
4142
cmd.AddCommand(instance.NewCmd())
4243
cmd.AddCommand(walarchive.NewCmd())
4344
cmd.AddCommand(walrestore.NewCmd())
45+
cmd.AddCommand(versions.NewCmd())
4446

4547
if err := cmd.Execute(); err != nil {
4648
os.Exit(1)

hack/release.sh

-4
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ sed -i -e "/Version *= *.*/Is/\".*\"/\"${release_version}\"/" \
7474
-e "/DefaultOperatorImageName *= *.*/Is/\"\(.*\):.*\"/\"\1:${release_version}\"/" \
7575
pkg/versions/versions.go
7676

77-
sed -i -e "s/version=\".*\"/version=\"${release_version}\"/" \
78-
Dockerfile
79-
8077
sed -i "s/postgresql-operator-[0-9.]*.yaml/postgresql-operator-${release_version}.yaml/g" \
8178
docs/src/installation_upgrade.md
8279

@@ -93,7 +90,6 @@ rm -fr "${CONFIG_TMP_DIR}"
9390
git checkout -b "release/v${release_version}"
9491
git add \
9592
pkg/versions/versions.go \
96-
Dockerfile \
9793
docs/src/installation_upgrade.md \
9894
"${release_manifest}"
9995
git commit -sm "Version tag to ${release_version}"

hack/setup-cluster.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ install_kind() {
9595
}
9696

9797
build_and_load_operator_k3d() {
98-
docker build -t "${CONTROLLER_IMG}" "${ROOT_DIR}"
98+
make -C "${ROOT_DIR}" CONTROLLER_IMG="${CONTROLLER_IMG}" docker-build
9999
k3d image import "${CONTROLLER_IMG}" -c "${CLUSTER_NAME}"
100100
}
101101

102102
build_and_load_operator_kind() {
103-
docker build -t "${CONTROLLER_IMG}" "${ROOT_DIR}"
103+
make -C "${ROOT_DIR}" CONTROLLER_IMG="${CONTROLLER_IMG}" docker-build
104104
kind load -v 1 docker-image --name "${CLUSTER_NAME}" "${CONTROLLER_IMG}"
105105
}
106106

internal/cmd/manager/bootstrap/cmd.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/fileutils"
1616
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/management/log"
17+
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions"
1718
)
1819

1920
// NewCmd create a new cobra command
@@ -24,7 +25,10 @@ func NewCmd() *cobra.Command {
2425
RunE: func(cmd *cobra.Command, args []string) error {
2526
dest := args[0]
2627

27-
log.Log.Info("Installing the manager executable", "destination", dest)
28+
log.Log.Info("Installing the manager executable",
29+
"destination", dest,
30+
"version", versions.Version,
31+
"build", versions.Info)
2832
err := fileutils.CopyFile(cmd.Root().Name(), dest)
2933
if err != nil {
3034
panic(err)

internal/cmd/manager/controller/controller.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func init() {
8484
func RunController(metricsAddr, configMapName string, enableLeaderElection bool) error {
8585
ctx := context.Background()
8686

87-
setupLog.Info("Starting Cloud Native PostgreSQL Operator", "version", versions.Version)
87+
setupLog.Info("Starting Cloud Native PostgreSQL Operator",
88+
"version", versions.Version,
89+
"build", versions.Info)
8890
setupLog.Info("Listening for changes", "watchNamespace", configuration.Current.WatchNamespace)
8991

9092
managerOptions := ctrl.Options{

internal/cmd/manager/instance/run/cmd.go

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/management/postgres/logpipe"
2424
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/management/postgres/metricsserver"
2525
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/management/postgres/webserver"
26+
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions"
2627
)
2728

2829
// NewCmd creates the "instance run" subcommand
@@ -64,6 +65,10 @@ func NewCmd() *cobra.Command {
6465
func runSubCommand(ctx context.Context, instance *postgres.Instance) error {
6566
var err error
6667

68+
log.Log.Info("Starting Cloud Native PostgreSQL Instance Manager",
69+
"version", versions.Version,
70+
"build", versions.Info)
71+
6772
reconciler, err := controller.NewInstanceReconciler(instance)
6873
if err != nil {
6974
log.Log.Error(err, "Error while starting reconciler")

internal/cmd/versions/cmd.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
This file is part of Cloud Native PostgreSQL.
3+
4+
Copyright (C) 2019-2021 EnterpriseDB Corporation.
5+
*/
6+
7+
// Package versions builds the version subcommand for both manager and plugins
8+
package versions
9+
10+
import (
11+
"fmt"
12+
13+
"github.com/spf13/cobra"
14+
15+
"github.com/EnterpriseDB/cloud-native-postgresql/pkg/versions"
16+
)
17+
18+
// NewCmd is a cobra command printing build information
19+
func NewCmd() *cobra.Command {
20+
return &cobra.Command{
21+
Use: "version",
22+
Short: "Prints version, commit sha and date of the build",
23+
Run: func(cmd *cobra.Command, args []string) {
24+
fmt.Printf("Build: %+v", versions.Info)
25+
},
26+
}
27+
}

pkg/versions/versions.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,40 @@ This file is part of Cloud Native PostgreSQL.
44
Copyright (C) 2019-2021 EnterpriseDB Corporation.
55
*/
66

7-
// Package versions contains the version the Cloud Native PostgreSQL
8-
// operator and the software used by it
7+
// Package versions contains the version of the Cloud Native PostgreSQL operator and the software
8+
// that is used by it
99
package versions
1010

1111
const (
1212
// Version is the version of the operator
1313
Version = "1.4.0"
1414

15-
// DefaultImageName is the image used by default by the operator to create
16-
// pods.
15+
// DefaultImageName is the default image used by the operator to create pods
1716
DefaultImageName = "quay.io/enterprisedb/postgresql:13.2"
1817

19-
// DefaultOperatorImageName used to bootstrap the controller in the Pods running
20-
// PostgreSQL
18+
// DefaultOperatorImageName is the default operator image used by the controller in the pods running PostgreSQL
2119
DefaultOperatorImageName = "quay.io/enterprisedb/cloud-native-postgresql:1.4.0"
2220
)
21+
22+
// BuildInfo is a struct containing all the info about the build
23+
type BuildInfo struct {
24+
Version, Commit, Date string
25+
}
26+
27+
var (
28+
// buildVersion injected during the build
29+
buildVersion = "dev"
30+
31+
// buildCommit injected during the build
32+
buildCommit = "none"
33+
34+
// buildDate injected during the build
35+
buildDate = "unknown"
36+
37+
// Info contains the build info
38+
Info = BuildInfo{
39+
Version: buildVersion,
40+
Commit: buildCommit,
41+
Date: buildDate,
42+
}
43+
)

0 commit comments

Comments
 (0)