Skip to content

Commit

Permalink
update build logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap committed Dec 30, 2024
1 parent c837d2a commit 89372c5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .project
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ kind: ClusterSecretOperator
kubernetesVersion: v0.27.2
mutatingWebhookEnabled: false
operatorName: clustersecret-operator-cop.cs.sap.com
owner: SAP SE
owner: SAP SE or an SAP affiliate company and clustersecret-operator-cop contributors
resource: clustersecretoperators
spdxLicenseHeaders: true
validatingWebhookEnabled: false
version: v0.1.4
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.23 as builder
FROM --platform=$BUILDPLATFORM golang:1.23.4 AS builder
ARG TARGETOS
ARG TARGETARCH

Expand Down
71 changes: 27 additions & 44 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,65 @@ all: build
##@ General

.PHONY: help
help: ## Display this help.
@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)
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate CustomResourceDefinition objects.
manifests: controller-gen ## Generate CustomResourceDefinition objects
$(CONTROLLER_GEN) crd paths="./api/..." output:crd:artifacts:config=crds ;\
test ! -d chart || test -e chart/crds || ln -s ../crds chart/crds

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: generate-deepcopy ## Generate required code pieces

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

.PHONY: generate-client
generate-client: client-gen informer-gen lister-gen ## Generate typed client.
generate-client: ## Generate typed client
./hack/genclient.sh

.PHONY: fmt
fmt: ## Run go fmt against code.
fmt: ## Run go fmt against code
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
vet: ## Run go vet against code
go vet ./...

##@ Testing

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
test: manifests generate-deepcopy fmt vet envtest ## Run tests
KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out

##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
build: generate-deepcopy fmt vet ## Build manager binary
go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
run: manifests generate-deepcopy fmt vet ## Run a controller from your host
go run ./main.go

# Build docker image in current architecture and tag it as ${IMG}.
# Build docker image in current architecture and tag it as ${IMG}
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
docker-build: ## Build docker image with the manager
docker build -t ${IMG} .

# Push docker image to the target specified in ${IMG}.
# Push docker image to the target specified in ${IMG}
.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker-push: ## Push docker image with the manager
docker push ${IMG}

# Build and push docker image for all given platforms.
# Build and push docker image for all given platforms
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: ## Build and push docker image for the manager for cross-platform support.
docker-buildx: ## Build and push docker image for the manager for cross-platform support
- docker buildx create --name project-v3-builder
docker buildx use project-v3-builder
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
Expand All @@ -83,49 +86,29 @@ $(LOCALBIN):

## Tool Binaries
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
CLIENT_GEN ?= $(LOCALBIN)/client-gen
INFORMER_GEN ?= $(LOCALBIN)/informer-gen
LISTER_GEN ?= $(LOCALBIN)/lister-gen
SETUP_ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
CONTROLLER_TOOLS_VERSION ?= v0.16.1
CODE_GENERATOR_VERSION ?= v0.29.8
SETUP_ENVTEST_VERSION ?= latest

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
controller-gen: $(CONTROLLER_GEN) ## Install controller-gen
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: client-gen
client-gen: $(CLIENT_GEN) ## Download client-gen locally if necessary.
$(CLIENT_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION)

.PHONY: informer-gen
informer-gen: $(INFORMER_GEN) ## Download informer-gen locally if necessary.
$(INFORMER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION)

.PHONY: lister-gen
lister-gen: $(LISTER_GEN) ## Download lister-gen locally if necessary.
$(LISTER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION)
go mod download sigs.k8s.io/controller-tools
GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-tools)/cmd/controller-gen

.PHONY: setup-envtest
setup-envtest: $(SETUP_ENVTEST) ## Download setup-envtest locally if necessary.
setup-envtest: $(SETUP_ENVTEST) ## Install setup-envtest
$(SETUP_ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VERSION)
go mod download sigs.k8s.io/controller-runtime
GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-runtime/tools/setup-envtest)

.PHONY: envtest
envtest: setup-envtest
envtest: setup-envtest ## Install envtest binaries
ENVTESTDIR=$$($(SETUP_ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\
chmod -R u+w $$ENVTESTDIR ;\
rm -f $(LOCALBIN)/k8s/current ;\
ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current

##@ Manage Content

.PHONY: update-content
update-content: ## Update external resources used by this repository.
rm -rf pkg/operator/data/charts
Expand Down
43 changes: 25 additions & 18 deletions hack/genclient.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,55 @@

set -eo pipefail

export GOROOT=$(go env GOROOT)
BASEDIR=$(realpath "$(dirname "$0")"/..)

export GOBIN=$BASEDIR/bin
mkdir -p "$GOBIN"

go mod download k8s.io/code-generator
CODEGEN_DIR=$(go list -m -f '{{.Dir}}' k8s.io/code-generator)
go install "$CODEGEN_DIR"/cmd/*

BASEDIR=$(realpath $(dirname "$0")/..)
TEMPDIR=$BASEDIR/tmp/gen
trap 'rm -rf "$TEMPDIR"' EXIT
mkdir -p "$TEMPDIR"

mkdir -p "$TEMPDIR"/apis/operator.kyma-project.io
ln -s "$BASEDIR"/api/v1alpha1 "$TEMPDIR"/apis/operator.kyma-project.io/v1alpha1

"$BASEDIR"/bin/client-gen \
"$GOBIN"/client-gen \
--clientset-name versioned \
--input-base "" \
--input github.com/sap/clustersecret-operator-cop/tmp/gen/apis/operator.kyma-project.io/v1alpha1 \
--input-base "$TEMPDIR"/apis \
--input operator.kyma-project.io/v1alpha1 \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package github.com/sap/clustersecret-operator-cop/pkg/client/clientset \
--output-base "$TEMPDIR"/pkg/client \
--output-pkg github.com/sap/clustersecret-operator-cop/pkg/client/clientset \
--output-dir "$TEMPDIR"/pkg/client/clientset \
--plural-exceptions ClusterSecretOperator:clustersecretoperators

"$BASEDIR"/bin/lister-gen \
--input-dirs github.com/sap/clustersecret-operator-cop/tmp/gen/apis/operator.kyma-project.io/v1alpha1 \
"$GOBIN"/lister-gen \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package github.com/sap/clustersecret-operator-cop/pkg/client/listers \
--output-base "$TEMPDIR"/pkg/client \
--plural-exceptions ClusterSecretOperator:clustersecretoperators
--output-pkg github.com/sap/clustersecret-operator-cop/pkg/client/listers \
--output-dir "$TEMPDIR"/pkg/client/listers \
--plural-exceptions ClusterSecretOperator:clustersecretoperators \
github.com/sap/clustersecret-operator-cop/tmp/gen/apis/operator.kyma-project.io/v1alpha1

"$BASEDIR"/bin/informer-gen \
--input-dirs github.com/sap/clustersecret-operator-cop/tmp/gen/apis/operator.kyma-project.io/v1alpha1 \
"$GOBIN"/informer-gen \
--versioned-clientset-package github.com/sap/clustersecret-operator-cop/pkg/client/clientset/versioned \
--listers-package github.com/sap/clustersecret-operator-cop/pkg/client/listers \
--go-header-file "$BASEDIR"/hack/boilerplate.go.txt \
--output-package github.com/sap/clustersecret-operator-cop/pkg/client/informers \
--output-base "$TEMPDIR"/pkg/client \
--plural-exceptions ClusterSecretOperator:clustersecretoperators
--output-pkg github.com/sap/clustersecret-operator-cop/pkg/client/informers \
--output-dir "$TEMPDIR"/pkg/client/informers \
--plural-exceptions ClusterSecretOperator:clustersecretoperators \
github.com/sap/clustersecret-operator-cop/tmp/gen/apis/operator.kyma-project.io/v1alpha1

find "$TEMPDIR"/pkg/client -name "*.go" -exec \
perl -pi -e "s#github\.com/sap/clustersecret-operator-cop/tmp/gen/apis/operator\.kyma-project\.io/v1alpha1#github.com/sap/clustersecret-operator-cop/api/v1alpha1#g" \
{} +

rm -rf "$BASEDIR"/pkg/client
mv "$TEMPDIR"/pkg/client/github.com/sap/clustersecret-operator-cop/pkg/client "$BASEDIR"/pkg
mv "$TEMPDIR"/pkg/client "$BASEDIR"/pkg

cd "$BASEDIR"
go mod tidy
go fmt ./pkg/client/...
go vet ./pkg/client/...
15 changes: 15 additions & 0 deletions hack/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build tools
// +build tools

/*
SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and clustersecret-operator-cop contributors
SPDX-License-Identifier: Apache-2.0
*/

package tools

import (
_ "k8s.io/code-generator"
_ "sigs.k8s.io/controller-runtime/tools/setup-envtest"
_ "sigs.k8s.io/controller-tools/cmd/controller-gen"
)

0 comments on commit 89372c5

Please sign in to comment.