Skip to content

Commit 1d4c2f6

Browse files
authored
chore: update operator sdk and use go version 1.17 (#5)
1 parent 2f0e74b commit 1d4c2f6

33 files changed

+854
-419
lines changed

.dockerignore

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2-
# Ignore all files which are not go type
3-
!**/*.go
4-
!**/*.mod
5-
!**/*.sum
2+
# Ignore build and test binaries.
3+
bin/
4+
testbin/

.github/workflows/release.yml

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,29 @@ jobs:
2121
2222
- uses: actions/setup-go@v2
2323
with:
24-
go-version: '^1.16.1'
24+
go-version: 1.17.x
2525

2626
- name: Login to DockerHub
2727
uses: docker/login-action@v1
2828
with:
2929
username: ${{ secrets.DOCKERHUB_USERNAME }}
3030
password: ${{ secrets.DOCKERHUB_PASSWORD }}
3131

32-
- name: Run Makefile
32+
- name: Build & publish container images
3333
run: |
3434
export VERSION=${GITHUB_REF#refs/*/}
35+
export IMG="anbraten/external-database-operator:${VERSION}"
3536
make build
3637
make docker-build
3738
make docker-push
39+
40+
- name: Generate latest manifest file
41+
run: |
42+
export VERSION=${GITHUB_REF#refs/*/}
43+
export IMG="anbraten/external-database-operator:${VERSION}"
44+
make generate-manifests
45+
46+
- name: Release latest manifest
47+
uses: softprops/action-gh-release@v1
48+
with:
49+
files: deploy/external-database-controller.yml

.github/workflows/test.yml

+60-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ${{ matrix.os }}
1717
strategy:
1818
matrix:
19-
go-version: [1.18.x]
19+
go-version: [1.17.x]
2020
os: [ubuntu-latest]
2121
steps:
2222
- name: Set up Go ${{ matrix.go-version }} on ${{ matrix.os }}
@@ -25,6 +25,13 @@ jobs:
2525
go-version: ${{ matrix.go-version }}
2626
id: go
2727

28+
- uses: actions/cache@v2
29+
with:
30+
path: ~/go/pkg/mod
31+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
32+
restore-keys: |
33+
${{ runner.os }}-go-
34+
2835
- name: Check out code into the Go module directory
2936
uses: actions/checkout@v2
3037

@@ -40,3 +47,55 @@ jobs:
4047
- name: Stop containers
4148
if: always()
4249
run: docker-compose down
50+
51+
check-manifests:
52+
name: Check if latest manifests have been generated
53+
runs-on: ubuntu-latest
54+
steps:
55+
- name: Set up Go
56+
uses: actions/setup-go@v1
57+
with:
58+
go-version: 1.17.x
59+
id: go
60+
61+
- uses: actions/cache@v2
62+
with:
63+
path: ~/go/pkg/mod
64+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
65+
restore-keys: |
66+
${{ runner.os }}-go-
67+
68+
- name: Check out code into the Go module directory
69+
uses: actions/checkout@v2
70+
71+
- name: Test manifest for un-commited changes
72+
run: |
73+
export IMG="anbraten/external-database-operator:0.0.1"
74+
make generate-manifests
75+
! git status -u | grep deploy/external-database-controller.yml
76+
77+
test-build:
78+
name: Build container image
79+
runs-on: ubuntu-latest
80+
steps:
81+
- name: Set up Go
82+
uses: actions/setup-go@v1
83+
with:
84+
go-version: 1.17.x
85+
id: go
86+
87+
- uses: actions/cache@v2
88+
with:
89+
path: ~/go/pkg/mod
90+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
91+
restore-keys: |
92+
${{ runner.os }}-go-
93+
94+
- name: Check out code into the Go module directory
95+
uses: actions/checkout@v2
96+
97+
- name: Build container images
98+
run: |
99+
export IMG="anbraten/external-database-operator:0.0.1"
100+
make build
101+
make docker-build

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.15 as builder
2+
FROM golang:1.17 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests

Makefile

+124-39
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
# To re-generate a bundle for another specific version without changing the standard setup, you can:
44
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
55
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
6-
VERSION ?= 0.2.0
6+
VERSION ?= 0.0.1
77

88
# CHANNELS define the bundle channels used in the bundle.
9-
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
9+
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
1010
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
11-
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
12-
# - use environment variables to overwrite this value (e.g export CHANNELS="preview,fast,stable")
11+
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=candidate,fast,stable)
12+
# - use environment variables to overwrite this value (e.g export CHANNELS="candidate,fast,stable")
1313
ifneq ($(origin CHANNELS), undefined)
1414
BUNDLE_CHANNELS := --channels=$(CHANNELS)
1515
endif
@@ -24,14 +24,32 @@ BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
2424
endif
2525
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
2626

27+
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
28+
# This variable is used to construct full image tags for bundle and catalog images.
29+
#
30+
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31+
# example.com/new-bundle:$VERSION and example.com/new-catalog:$VERSION.
32+
IMAGE_TAG_BASE ?= anbraten/external-database-operator
33+
2734
# BUNDLE_IMG defines the image:tag used for the bundle.
2835
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
29-
BUNDLE_IMG ?= controller-bundle:$(VERSION)
36+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
37+
38+
# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
39+
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
40+
41+
# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
42+
# You can enable this value if you would like to use SHA Based Digests
43+
# To enable set flag to true
44+
USE_IMAGE_DIGESTS ?= false
45+
ifeq ($(USE_IMAGE_DIGESTS), true)
46+
BUNDLE_GEN_FLAGS += --use-image-digests
47+
endif
3048

3149
# Image URL to use all building/pushing image targets
32-
IMG ?= anbraten/external-database-operator:$(VERSION)
33-
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
34-
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
50+
IMG ?= controller:latest
51+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
52+
ENVTEST_K8S_VERSION = 1.23
3553

3654
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
3755
ifeq (,$(shell go env GOBIN))
@@ -40,6 +58,13 @@ else
4058
GOBIN=$(shell go env GOBIN)
4159
endif
4260

61+
# Setting SHELL to bash allows bash commands to be executed by recipes.
62+
# This is a requirement for 'setup-envtest.sh' in the test target.
63+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
64+
SHELL = /usr/bin/env bash -o pipefail
65+
.SHELLFLAGS = -ec
66+
67+
.PHONY: all
4368
all: build
4469

4570
##@ General
@@ -55,71 +80,88 @@ all: build
5580
# More info on the awk command:
5681
# http://linuxcommand.org/lc3_adv_awk.php
5782

83+
.PHONY: help
5884
help: ## Display this help.
5985
@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)
6086

6187
##@ Development
6288

89+
.PHONY: manifests
6390
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
64-
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
91+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
6592

93+
.PHONY: generate
6694
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
6795
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
6896

97+
.PHONY: fmt
6998
fmt: ## Run go fmt against code.
7099
go fmt ./...
71100

101+
.PHONY: vet
72102
vet: ## Run go vet against code.
73103
go vet ./...
74104

75-
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
76-
test: manifests generate fmt vet ## Run tests.
77-
docker-compose up -d
78-
sleep 3s
79-
go test -timeout 30s ./adapters/...
80-
docker-compose down
81-
# mkdir -p ${ENVTEST_ASSETS_DIR}
82-
# test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.2/hack/setup-envtest.sh
83-
# source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
105+
.PHONY: test
106+
test: manifests generate fmt vet envtest ## Run tests.
107+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
84108

85109
##@ Build
86110

111+
.PHONY: build
87112
build: generate fmt vet ## Build manager binary.
88113
go build -o bin/manager main.go
89114

115+
.PHONY: run
90116
run: manifests generate fmt vet ## Run a controller from your host.
91117
go run ./main.go
92118

119+
.PHONY: docker-build
93120
docker-build: ## Build docker image with the manager.
94121
docker build -t ${IMG} .
95122

123+
.PHONY: docker-push
96124
docker-push: ## Push docker image with the manager.
97125
docker push ${IMG}
98126

99127
##@ Deployment
100128

101-
# install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
102-
# $(KUSTOMIZE) build config/crd | kubectl apply -f -
129+
ifndef ignore-not-found
130+
ignore-not-found = false
131+
endif
103132

104-
# uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
105-
# $(KUSTOMIZE) build config/crd | kubectl delete -f -
133+
.PHONY: install
134+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
135+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
106136

107-
# deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
108-
# cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
109-
# $(KUSTOMIZE) build config/default | kubectl apply -f -
137+
.PHONY: uninstall
138+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
139+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
110140

111-
# undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
112-
# $(KUSTOMIZE) build config/default | kubectl delete -f -
141+
.PHONY: deploy
142+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
143+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
144+
$(KUSTOMIZE) build config/default | kubectl apply -f -
113145

146+
.PHONY: undeploy
147+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
148+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
114149

115150
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
151+
.PHONY: controller-gen
116152
controller-gen: ## Download controller-gen locally if necessary.
117-
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
153+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0)
118154

119155
KUSTOMIZE = $(shell pwd)/bin/kustomize
156+
.PHONY: kustomize
120157
kustomize: ## Download kustomize locally if necessary.
121158
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
122159

160+
ENVTEST = $(shell pwd)/bin/setup-envtest
161+
.PHONY: envtest
162+
envtest: ## Download envtest-setup locally if necessary.
163+
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
164+
123165
# go-get-tool will 'go get' any package $2 and install it to $1.
124166
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
125167
define go-get-tool
@@ -134,19 +176,62 @@ rm -rf $$TMP_DIR ;\
134176
}
135177
endef
136178

137-
# .PHONY: bundle ## Generate bundle manifests and metadata, then validate generated files.
138-
# bundle: manifests kustomize
139-
# operator-sdk generate kustomize manifests -q
140-
# cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
141-
# $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
142-
# operator-sdk bundle validate ./bundle
179+
.PHONY: bundle
180+
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
181+
operator-sdk generate kustomize manifests -q
182+
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
183+
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle $(BUNDLE_GEN_FLAGS)
184+
operator-sdk bundle validate ./bundle
185+
186+
.PHONY: bundle-build
187+
bundle-build: ## Build the bundle image.
188+
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
189+
190+
.PHONY: bundle-push
191+
bundle-push: ## Push the bundle image.
192+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
193+
194+
.PHONY: opm
195+
OPM = ./bin/opm
196+
opm: ## Download opm locally if necessary.
197+
ifeq (,$(wildcard $(OPM)))
198+
ifeq (,$(shell which opm 2>/dev/null))
199+
@{ \
200+
set -e ;\
201+
mkdir -p $(dir $(OPM)) ;\
202+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
203+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.19.1/$${OS}-$${ARCH}-opm ;\
204+
chmod +x $(OPM) ;\
205+
}
206+
else
207+
OPM = $(shell which opm)
208+
endif
209+
endif
210+
211+
# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
212+
# These images MUST exist in a registry and be pull-able.
213+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
214+
215+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
216+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
217+
218+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
219+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
220+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
221+
endif
143222

144-
# .PHONY: bundle-build ## Build the bundle image.
145-
# bundle-build:
146-
# docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
223+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
224+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
225+
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
226+
.PHONY: catalog-build
227+
catalog-build: opm ## Build a catalog image.
228+
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
147229

148-
##@ Release
230+
# Push the catalog image.
231+
.PHONY: catalog-push
232+
catalog-push: ## Push a catalog image.
233+
$(MAKE) docker-push IMG=$(CATALOG_IMG)
149234

150-
release: manifests kustomize ## Release kubectl apply -f ready file
235+
generate-manifests: manifests kustomize ## Generate kubectl apply -f ready file
151236
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
152237
$(KUSTOMIZE) build config/default > deploy/external-database-controller.yml

PROJECT

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
domain: anbraten.github.io
2-
layout: go.kubebuilder.io/v3
2+
layout:
3+
- go.kubebuilder.io/v3
34
plugins:
45
manifests.sdk.operatorframework.io/v2: {}
56
scorecard.sdk.operatorframework.io/v2: {}

0 commit comments

Comments
 (0)