Skip to content

Commit 787e274

Browse files
committed
Add Prow jobs, image builds and verify make targets
Signed-off-by: Marvin Beckers <[email protected]>
1 parent 26d0bc9 commit 787e274

File tree

13 files changed

+348
-31
lines changed

13 files changed

+348
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ go.work.sum
2626

2727
# Downloaded and built binaries
2828
bin/
29+
tools

.prow.yaml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
presubmits:
2+
- name: pull-kcp-operator-verify
3+
always_run: true
4+
decorate: true
5+
clone_uri: "https://github.com/kcp-dev/kcp-operator"
6+
labels:
7+
preset-goproxy: "true"
8+
spec:
9+
containers:
10+
- image: ghcr.io/kcp-dev/infra/build:1.22.7-1
11+
command:
12+
- make
13+
- verify
14+
resources:
15+
requests:
16+
memory: 1Gi
17+
cpu: 1
18+
19+
- name: pull-kcp-operator-lint
20+
always_run: true
21+
decorate: true
22+
clone_uri: "https://github.com/kcp-dev/kcp-operator"
23+
labels:
24+
preset-goproxy: "true"
25+
spec:
26+
containers:
27+
- image: ghcr.io/kcp-dev/infra/build:1.22.7-1
28+
command:
29+
- make
30+
- lint
31+
resources:
32+
requests:
33+
memory: 4Gi
34+
cpu: 2
35+
36+
- name: pull-kcp-operator-build-image
37+
always_run: true
38+
decorate: true
39+
clone_uri: "https://github.com/kcp-dev/kcp-operator"
40+
labels:
41+
preset-goproxy: "true"
42+
spec:
43+
containers:
44+
- image: quay.io/containers/buildah:v1.30.0
45+
command:
46+
- hack/ci/build-image.sh
47+
env:
48+
- name: DRY_RUN
49+
value: '1'
50+
# docker-in-docker needs privileged mode
51+
securityContext:
52+
privileged: true
53+
resources:
54+
requests:
55+
memory: 1Gi
56+
cpu: 1
57+
58+
- name: pull-kcp-operator-test
59+
always_run: true
60+
decorate: true
61+
clone_uri: "https://github.com/kcp-dev/kcp-operator"
62+
labels:
63+
preset-goproxy: "true"
64+
spec:
65+
containers:
66+
- image: ghcr.io/kcp-dev/infra/build:1.22.7-1
67+
command:
68+
- make
69+
- test
70+
env:
71+
- name: USE_GOTESTSUM
72+
value: '1'
73+
resources:
74+
requests:
75+
memory: 4Gi
76+
cpu: 2
77+
78+
- name: pull-kcp-operator-test-e2e
79+
always_run: true
80+
decorate: true
81+
clone_uri: "https://github.com/kcp-dev/kcp-operator"
82+
labels:
83+
preset-goproxy: "true"
84+
spec:
85+
containers:
86+
- image: ghcr.io/kcp-dev/infra/build:1.22.7-1
87+
command:
88+
- make
89+
- test-e2e
90+
env:
91+
- name: USE_GOTESTSUM
92+
value: '1'
93+
resources:
94+
requests:
95+
memory: 4Gi
96+
cpu: 2

Makefile

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ IMG ?= controller:latest
33
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
44
ENVTEST_K8S_VERSION = 1.31.0
55

6+
GO_INSTALL = ./hack/go-install.sh
7+
TOOLS_DIR=hack/tools
8+
ROOT_DIR=$(abspath .)
9+
TOOLS_GOBIN_DIR := $(abspath $(TOOLS_DIR))
10+
611
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
712
ifeq (,$(shell go env GOBIN))
813
GOBIN=$(shell go env GOPATH)/bin
@@ -16,6 +21,13 @@ endif
1621
# tools. (i.e. podman)
1722
CONTAINER_TOOL ?= docker
1823

24+
# OPENSHIFT_GIMPORTS_VER defines which version of openshift-goimports to use
25+
# for checking import statements.
26+
OPENSHIFT_GOIMPORTS_VER := c72f1dc2e3aacfa00aece3391d938c9bc734e791
27+
OPENSHIFT_GOIMPORTS_BIN := openshift-goimports
28+
OPENSHIFT_GOIMPORTS := $(TOOLS_DIR)/$(OPENSHIFT_GOIMPORTS_BIN)-$(OPENSHIFT_GOIMPORTS_VER)
29+
export OPENSHIFT_GOIMPORTS # so hack scripts can use it
30+
1931
# Setting SHELL to bash allows bash commands to be executed by recipes.
2032
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
2133
SHELL = /usr/bin/env bash -o pipefail
@@ -76,6 +88,21 @@ lint: golangci-lint ## Run golangci-lint linter
7688
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
7789
$(GOLANGCI_LINT) run --fix
7890

91+
.PHONY: modules
92+
modules: ## Run go mod tidy to ensure modules are up to date
93+
hack/update-go-modules.sh
94+
95+
$(OPENSHIFT_GOIMPORTS):
96+
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/openshift-eng/openshift-goimports $(OPENSHIFT_GOIMPORTS_BIN) $(OPENSHIFT_GOIMPORTS_VER)
97+
98+
.PHONY: imports
99+
imports: $(OPENSHIFT_GOIMPORTS)
100+
$(OPENSHIFT_GOIMPORTS) -m github.com/kcp-dev/kcp
101+
102+
.PHONY: verify
103+
verify: manifests generate fmt vet modules imports ## Run all codegen and formatting targets and check if files have changed
104+
if ! git diff --quiet --exit-code ; then echo "ERROR: Found unexpected changes to git repository"; git diff; exit 1; fi
105+
79106
##@ Build
80107

81108
.PHONY: build
@@ -97,23 +124,6 @@ docker-build: ## Build docker image with the manager.
97124
docker-push: ## Push docker image with the manager.
98125
$(CONTAINER_TOOL) push ${IMG}
99126

100-
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
101-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
102-
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
103-
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
104-
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
105-
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
106-
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
107-
.PHONY: docker-buildx
108-
docker-buildx: ## Build and push docker image for the manager for cross-platform support
109-
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
110-
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
111-
- $(CONTAINER_TOOL) buildx create --name kcp-operator-builder
112-
$(CONTAINER_TOOL) buildx use kcp-operator-builder
113-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
114-
- $(CONTAINER_TOOL) buildx rm kcp-operator-builder
115-
rm Dockerfile.cross
116-
117127
.PHONY: build-installer
118128
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
119129
mkdir -p dist

cmd/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
"flag"
2222
"os"
2323

24+
"k8s.io/apimachinery/pkg/runtime"
25+
2426
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2527
// to ensure that exec-entrypoint and run can make use of them.
26-
_ "k8s.io/client-go/plugin/pkg/client/auth"
27-
28-
"k8s.io/apimachinery/pkg/runtime"
2928
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3029
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
_ "k8s.io/client-go/plugin/pkg/client/auth"
3131
ctrl "sigs.k8s.io/controller-runtime"
3232
"sigs.k8s.io/controller-runtime/pkg/healthz"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"

hack/ci/build-image.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 The KCP Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -euo pipefail
18+
19+
# make git available
20+
if ! [ -x "$(command -v git)" ]; then
21+
echo "Installing git ..."
22+
dnf install -y git
23+
fi
24+
25+
# in CI, make use of the registry mirror to avoid getting rate limited
26+
if [ -n "${DOCKER_REGISTRY_MIRROR_ADDR:-}" ]; then
27+
# remove "http://" or "https://" prefix
28+
mirror="$(echo "$DOCKER_REGISTRY_MIRROR_ADDR" | awk -F// '{print $NF}')"
29+
30+
echo "Configuring registry mirror for docker.io ..."
31+
32+
cat <<EOF > /etc/containers/registries.conf.d/mirror.conf
33+
[[registry]]
34+
prefix = "docker.io"
35+
insecure = true
36+
location = "$mirror"
37+
EOF
38+
fi
39+
40+
repository=ghcr.io/kcp-dev/kcp-operator
41+
architectures="amd64 arm64"
42+
43+
# when building locally, just tag with the current HEAD hash
44+
version="$(git rev-parse --short HEAD)"
45+
branchName=""
46+
47+
# deduce the tag from the Prow job metadata
48+
if [ -n "${PULL_BASE_REF:-}" ]; then
49+
version="$(git tag --list "$PULL_BASE_REF")"
50+
51+
if [ -z "$version" ]; then
52+
# if the base ref did not point to a tag, it's a branch name
53+
version="$(git rev-parse --short "$PULL_BASE_REF")"
54+
branchName="$PULL_BASE_REF"
55+
else
56+
# If PULL_BASE_REF is a tag, there is no branch available locally, plus
57+
# there is no guarantee that vX.Y.Z is tagged _only_ in the release-X.Y
58+
# branch; because of this we have to deduce the branch name from the tag
59+
branchName="$(echo "$version" | sed -E 's/^v([0-9]+)\.([0-9]+)\..*/release-\1.\2/')"
60+
fi
61+
fi
62+
63+
image="$repository:$version"
64+
echo "Building container image $image ..."
65+
66+
# build image for all architectures
67+
for arch in $architectures; do
68+
fullTag="$image-$arch"
69+
70+
echo "Building $version-$arch ..."
71+
buildah build-using-dockerfile \
72+
--file Dockerfile \
73+
--tag "$fullTag" \
74+
--arch "$arch" \
75+
--override-arch "$arch" \
76+
--build-arg "TARGETOS=linux" \
77+
--build-arg "TARGETARCH=$arch" \
78+
--format=docker \
79+
.
80+
done
81+
82+
echo "Creating manifest $image ..."
83+
buildah manifest create "$image"
84+
for arch in $architectures; do
85+
buildah manifest add "$image" "$image-$arch"
86+
done
87+
88+
# Additionally to an image tagged with the Git tag, we also
89+
# release images tagged with the current branch name, which
90+
# is somewhere between a blanket "latest" tag and a specific
91+
# tag.
92+
if [ -n "$branchName" ]; then
93+
branchImage="$repository:$branchName"
94+
95+
echo "Creating manifest $branchImage ..."
96+
buildah manifest create "$branchImage"
97+
for arch in $architectures; do
98+
buildah manifest add "$branchImage" "$image-$arch"
99+
done
100+
fi
101+
102+
# push manifest, except in presubmits
103+
if [ -z "${DRY_RUN:-}" ]; then
104+
echo "Logging into GHCR ..."
105+
buildah login --username "$KCP_GHCR_USERNAME" --password "$KCP_GHCR_PASSWORD" ghcr.io
106+
107+
echo "Pushing manifest and images ..."
108+
buildah manifest push --all "$image" "docker://$image"
109+
110+
if [ -n "${branchImage:-}" ]; then
111+
buildah manifest push --all "$branchImage" "docker://$branchImage"
112+
fi
113+
else
114+
echo "Not pushing images because \$DRY_RUN is set."
115+
fi
116+
117+
echo "Done."

hack/go-install.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The KCP Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Originally copied from
18+
# https://github.com/kubernetes-sigs/cluster-api-provider-gcp/blob/c26a68b23e9317323d5d37660fe9d29b3d2ff40c/scripts/go_install.sh
19+
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
if [[ -z "${1:-}" ]]; then
25+
echo "must provide module as first parameter"
26+
exit 1
27+
fi
28+
29+
if [[ -z "${2:-}" ]]; then
30+
echo "must provide binary name as second parameter"
31+
exit 1
32+
fi
33+
34+
if [[ -z "${3:-}" ]]; then
35+
echo "must provide version as third parameter"
36+
exit 1
37+
fi
38+
39+
if [[ -z "${GOBIN:-}" ]]; then
40+
echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory."
41+
exit 1
42+
fi
43+
44+
mkdir -p "${GOBIN}"
45+
46+
tmp_dir=$(mktemp -d -t goinstall_XXXXXXXXXX)
47+
function clean {
48+
rm -rf "${tmp_dir}"
49+
}
50+
trap clean EXIT
51+
52+
rm "${GOBIN}/${2}"* > /dev/null 2>&1 || true
53+
54+
cd "${tmp_dir}"
55+
56+
# create a new module in the tmp directory
57+
go mod init fake/mod
58+
59+
# install the golang module specified as the first argument
60+
go install -tags kcptools "${1}@${3}"
61+
mv "${GOBIN}/${2}" "${GOBIN}/${2}-${3}"
62+
ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}"

0 commit comments

Comments
 (0)