Skip to content

Commit 1a5e332

Browse files
add a controller and custom uploader
Signed-off-by: Takeshi Yoshimura <[email protected]>
1 parent 38baa34 commit 1a5e332

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3148
-659
lines changed

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,33 @@ Cargo.lock
88

99
# These are backup files generated by rustfmt
1010
**/*.rs.bk
11+
12+
# Binaries for programs and plugins
13+
*.exe
14+
*.exe~
15+
*.dll
16+
*.so
17+
*.dylib
18+
bin
19+
testbin/*
20+
Dockerfile.cross
21+
22+
go.sum
23+
24+
# Test binary, build with `go test -c`
25+
*.test
26+
27+
# Output of the go coverage tool, specifically when used with LiteIDE
28+
*-cover.out
29+
*-cover.html
30+
31+
# Kubernetes Generated files - skip generated files, except for vendored files
32+
33+
!vendor/**/zz_generated.*
34+
35+
# editor and IDE paraphernalia
36+
.vscode
37+
.idea
38+
*.swp
39+
*.swo
40+
*~

Dockerfile

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
# Build the manager binary
2-
FROM quay.io/operator-framework/helm-operator:v1.7.2
2+
FROM golang:1.19 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
35

4-
ENV HOME=/opt/helm
5-
COPY watches.yaml ${HOME}/watches.yaml
6-
COPY helm-charts ${HOME}/helm-charts
7-
WORKDIR ${HOME}
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN go mod download
13+
14+
# Copy the go source
15+
COPY main.go main.go
16+
COPY api/ api/
17+
COPY controllers/ controllers/
18+
19+
# Build
20+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
25+
26+
# Use distroless as minimal base image to package the manager binary
27+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
28+
FROM gcr.io/distroless/static:nonroot
29+
WORKDIR /
30+
COPY --from=builder /workspace/manager .
31+
USER 65532:65532
32+
33+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 157 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Copyright 2023- IBM Inc. All rights reserved
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
# VERSION defines the project version for the bundle.
25
# Update this value when you upgrade the version of your project.
36
# To re-generate a bundle for another specific version without changing the standard setup, you can:
@@ -28,17 +31,43 @@ BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
2831
# This variable is used to construct full image tags for bundle and catalog images.
2932
#
3033
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31-
# com.ibm/core-dump-operator-bundle:$VERSION and com.ibm/core-dump-operator-catalog:$VERSION.
34+
# ibm.com/core-dump-operator-bundle:$VERSION and ibm.com/core-dump-operator-catalog:$VERSION.
3235
IMAGE_TAG_BASE ?= quay.io/number9/core-dump-operator
3336

3437
# BUNDLE_IMG defines the image:tag used for the bundle.
3538
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
3639
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
3740

41+
# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
42+
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
43+
44+
# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
45+
# You can enable this value if you would like to use SHA Based Digests
46+
# To enable set flag to true
47+
USE_IMAGE_DIGESTS ?= false
48+
ifeq ($(USE_IMAGE_DIGESTS), true)
49+
BUNDLE_GEN_FLAGS += --use-image-digests
50+
endif
51+
3852
# Image URL to use all building/pushing image targets
3953
IMG ?= $(IMAGE_TAG_BASE):$(VERSION)
54+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
55+
ENVTEST_K8S_VERSION = 1.26.0
56+
57+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
58+
ifeq (,$(shell go env GOBIN))
59+
GOBIN=$(shell go env GOPATH)/bin
60+
else
61+
GOBIN=$(shell go env GOBIN)
62+
endif
63+
64+
# Setting SHELL to bash allows bash commands to be executed by recipes.
65+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
66+
SHELL = /usr/bin/env bash -o pipefail
67+
.SHELLFLAGS = -ec
4068

41-
all: docker-build
69+
.PHONY: all
70+
all: build
4271

4372
##@ General
4473

@@ -53,75 +82,135 @@ all: docker-build
5382
# More info on the awk command:
5483
# http://linuxcommand.org/lc3_adv_awk.php
5584

85+
.PHONY: help
5686
help: ## Display this help.
5787
@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)
5888

89+
##@ Development
90+
91+
.PHONY: manifests
92+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
93+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
94+
95+
.PHONY: generate
96+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
97+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
98+
99+
.PHONY: fmt
100+
fmt: ## Run go fmt against code.
101+
go fmt ./...
102+
103+
.PHONY: vet
104+
vet: ## Run go vet against code.
105+
go vet ./...
106+
107+
.PHONY: operator-test
108+
operator-test: manifests generate fmt vet envtest ## Run tests.
109+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./controllers/... -coverprofile operator-cover.out
110+
59111
##@ Build
60112

61-
run: helm-operator ## Run against the configured Kubernetes cluster in ~/.kube/config
62-
$(HELM_OPERATOR) run
113+
.PHONY: build
114+
build: manifests generate fmt vet ## Build manager binary.
115+
go build -o bin/manager main.go
116+
117+
.PHONY: run
118+
run: manifests generate fmt vet ## Run a controller from your host.
119+
go run ./main.go
63120

64-
docker-build: ## Build docker image with the manager.
121+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
122+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
123+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
124+
.PHONY: docker-build
125+
docker-build: operator-test ## Build docker image with the manager.
65126
docker build -t ${IMG} .
66127

128+
.PHONY: docker-push
67129
docker-push: ## Push docker image with the manager.
68130
docker push ${IMG}
69131

132+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
133+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
134+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
135+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
136+
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
137+
# To properly provided solutions that supports more than one platform you should use this option.
138+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
139+
.PHONY: docker-buildx
140+
docker-buildx: operator-test ## Build and push docker image for the manager for cross-platform support
141+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
142+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
143+
- docker buildx create --name project-v3-builder
144+
docker buildx use project-v3-builder
145+
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
146+
- docker buildx rm project-v3-builder
147+
rm Dockerfile.cross
148+
70149
##@ Deployment
71150

72-
install: kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
151+
ifndef ignore-not-found
152+
ignore-not-found = false
153+
endif
154+
155+
.PHONY: install
156+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
73157
$(KUSTOMIZE) build config/crd | kubectl apply -f -
74158

75-
uninstall: kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
76-
$(KUSTOMIZE) build config/crd | kubectl delete -f -
159+
.PHONY: uninstall
160+
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.
161+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
77162

78-
deploy: kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
163+
.PHONY: deploy
164+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
79165
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
80166
$(KUSTOMIZE) build config/default | kubectl apply -f -
81167

82-
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
83-
$(KUSTOMIZE) build config/default | kubectl delete -f -
168+
.PHONY: undeploy
169+
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.
170+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
171+
172+
##@ Build Dependencies
173+
174+
## Location to install dependencies to
175+
LOCALBIN ?= $(shell pwd)/bin
176+
$(LOCALBIN):
177+
mkdir -p $(LOCALBIN)
84178

85-
OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
86-
ARCH := $(shell uname -m | sed 's/x86_64/amd64/')
179+
## Tool Binaries
180+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
181+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
182+
ENVTEST ?= $(LOCALBIN)/setup-envtest
87183

184+
## Tool Versions
185+
KUSTOMIZE_VERSION ?= v3.8.7
186+
CONTROLLER_TOOLS_VERSION ?= v0.11.1
187+
188+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
88189
.PHONY: kustomize
89-
KUSTOMIZE = $(shell pwd)/bin/kustomize
90-
kustomize: ## Download kustomize locally if necessary.
91-
ifeq (,$(wildcard $(KUSTOMIZE)))
92-
ifeq (,$(shell which kustomize 2>/dev/null))
93-
@{ \
94-
set -e ;\
95-
mkdir -p $(dir $(KUSTOMIZE)) ;\
96-
curl -sSLo - https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v3.5.4/kustomize_v3.5.4_$(OS)_$(ARCH).tar.gz | \
97-
tar xzf - -C bin/ ;\
98-
}
99-
else
100-
KUSTOMIZE = $(shell which kustomize)
101-
endif
102-
endif
190+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
191+
$(KUSTOMIZE): $(LOCALBIN)
192+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
193+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
194+
rm -rf $(LOCALBIN)/kustomize; \
195+
fi
196+
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
103197

104-
.PHONY: helm-operator
105-
HELM_OPERATOR = $(shell pwd)/bin/helm-operator
106-
helm-operator: ## Download helm-operator locally if necessary, preferring the $(pwd)/bin path over global if both exist.
107-
ifeq (,$(wildcard $(HELM_OPERATOR)))
108-
ifeq (,$(shell which helm-operator 2>/dev/null))
109-
@{ \
110-
set -e ;\
111-
mkdir -p $(dir $(HELM_OPERATOR)) ;\
112-
curl -sSLo $(HELM_OPERATOR) https://github.com/operator-framework/operator-sdk/releases/download/v1.7.2/helm-operator_$(OS)_$(ARCH) ;\
113-
chmod +x $(HELM_OPERATOR) ;\
114-
}
115-
else
116-
HELM_OPERATOR = $(shell which helm-operator)
117-
endif
118-
endif
198+
.PHONY: controller-gen
199+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
200+
$(CONTROLLER_GEN): $(LOCALBIN)
201+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
202+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
203+
204+
.PHONY: envtest
205+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
206+
$(ENVTEST): $(LOCALBIN)
207+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
119208

120209
.PHONY: bundle
121-
bundle: kustomize ## Generate bundle manifests and metadata, then validate generated files.
210+
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
122211
operator-sdk generate kustomize manifests -q
123212
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
124-
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
213+
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle $(BUNDLE_GEN_FLAGS) --extra-service-accounts core-dump-operator-uploader-sa
125214
operator-sdk bundle validate ./bundle
126215

127216
.PHONY: bundle-build
@@ -140,7 +229,8 @@ ifeq (,$(shell which opm 2>/dev/null))
140229
@{ \
141230
set -e ;\
142231
mkdir -p $(dir $(OPM)) ;\
143-
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$(OS)-$(ARCH)-opm ;\
232+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
233+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
144234
chmod +x $(OPM) ;\
145235
}
146236
else
@@ -171,3 +261,25 @@ catalog-build: opm ## Build a catalog image.
171261
.PHONY: catalog-push
172262
catalog-push: ## Push a catalog image.
173263
$(MAKE) docker-push IMG=$(CATALOG_IMG)
264+
265+
UPLOADER_IMAGE_TAG_BASE ?= ghcr.io/ibm/core-dump-operator/core-dump-uploader
266+
267+
.PHONY: uploader-build
268+
uploader-build: bin/core-dump-uploader
269+
bin/core-dump-uploader: uploader-test cmd/core-dump-uploader/uploader.go
270+
CGO_ENABLED=0 GOOS=linux go build -v -a -ldflags '-extldflags "-static"' -gcflags="all=-N -l" -o bin/core-dump-uploader ./cmd/core-dump-uploader/
271+
272+
.PHONY: uploader-cbuild
273+
uploader-cbuild: bin/core-dump-uploader
274+
cp /etc/ssl/certs/ca-certificates.crt bin/
275+
docker build bin/ -f cmd/core-dump-uploader/Dockerfile -t $(UPLOADER_IMAGE_TAG_BASE):$(VERSION)
276+
277+
.PHONY: uploader-push
278+
uploader-push: uploader-cbuild
279+
docker push $(UPLOADER_IMAGE_TAG_BASE):$(VERSION)
280+
281+
.PHONY: uploader-test
282+
uploader-test: fmt vet envtest ## Run tests.
283+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./cmd/... -coverprofile uploader-cover.out
284+
go tool cover -html=uploader-cover.out -o uploader-cover.html
285+

PROJECT

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
domain: com.ibm
1+
domain: ibm.com
22
layout:
3-
- helm.sdk.operatorframework.io/v1
3+
- go.kubebuilder.io/v3
44
plugins:
55
manifests.sdk.operatorframework.io/v2: {}
66
scorecard.sdk.operatorframework.io/v2: {}
@@ -9,8 +9,9 @@ resources:
99
- api:
1010
crdVersion: v1
1111
namespaced: true
12-
domain: com.ibm
12+
domain: ibm.com
1313
group: charts
1414
kind: CoreDumpHandler
15+
path: github.com/IBM/core-dump-operator/api/v1alpha1
1516
version: v1alpha1
1617
version: "3"

0 commit comments

Comments
 (0)