Skip to content

Commit

Permalink
cleanup/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap committed Jul 10, 2023
1 parent b241aee commit d439e8b
Show file tree
Hide file tree
Showing 26 changed files with 165 additions and 139 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ jobs:
echo "Target commit: $sha"
echo "sha=$sha" >> $GITHUB_OUTPUT
- name: Wait for build to finish
uses: fountainhead/[email protected]
id: wait_for_build
with:
token: ${{ github.token }}
checkName: Build Docker image
ref: ${{ steps.get_target_commit.outputs.sha }}
timeoutSeconds: 900

- name: Check that build was successful
uses: actions/github-script@v3
with:
script: |
if ('${{ steps.wait_for_build.outputs.conclusion}}' != 'success') {
core.setFailed('Build not successful, conclusion: ${{ steps.wait_for_build.outputs.conclusion}}.')
}
- name: Create Release
uses: softprops/action-gh-release@v1
with:
Expand Down
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.dylib
bin
testbin/*
__debug_bin
Dockerfile.cross

# Test binary, build with `go test -c`
*.test
Expand All @@ -26,17 +28,13 @@ testbin/*
# vscode stuff
.vscode/*.log

# temporary folder
# temp stuff
/tmp

# local stuff
/.kubeconfig
/.local/ssl

# Debug artifacts
__debug_bin
debug.*

# Hugo artifacts
.hugo_build.lock
/website/resources
Expand Down
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"docker.commands.build": "docker build --pull --rm -f \"${dockerfile}\" -t ${tag} ${workspaceFolder}"
"go.testEnvVars": {
"KUBEBUILDER_ASSETS": "${workspaceFolder}/bin/k8s/current"
}
}
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ COPY main.go main.go
COPY api/ api/
COPY pkg/ pkg/
COPY internal/ internal/
COPY crds/ crds/
COPY Makefile Makefile

# Build
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
# Run tests and build
RUN make envtest \
&& CGO_ENABLED=0 KUBEBUILDER_ASSETS="/workspace/bin/k8s/current" go test ./... \
&& CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
129 changes: 67 additions & 62 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24
BUILDOS := $(shell go env GOOS)
BUILDARCH := $(shell go env GOARCH)
TESTOS ?= $(BUILDOS)
TESTARCH ?= $(BUILDARCH)
TARGETOS ?= $(TESTOS)
TARGETARCH ?= $(TESTARCH)
ENVTEST_K8S_VERSION = 1.25.0

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -18,7 +12,6 @@ GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
Expand Down Expand Up @@ -47,7 +40,7 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="main.go" paths="./api/..." paths="./pkg/..." paths="./internal/..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="main.go" paths="./api/..." paths="./pkg/..." paths="./internal/..." output:crd:artifacts:config=config/crd/bases
rm -rf crds && cp -r config/crd/bases crds

.PHONY: generate
Expand All @@ -65,22 +58,21 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out

.PHONY: envtest-test
envtest-test: ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out

##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build -o bin/manager main.go
go build -o bin/manager main.go

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

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
Expand All @@ -89,6 +81,23 @@ docker-build: test ## Build docker image with the manager.
docker-push: ## Push docker image with the manager.
docker push ${IMG}

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

##@ Deployment

ifndef ignore-not-found
Expand All @@ -112,60 +121,56 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
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.
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
.PHONY: controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
##@ Build Dependencies

## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

KUSTOMIZE = $(shell pwd)/bin/kustomize
## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
CLIENT_GEN ?= $(shell pwd)/bin/client-gen
INFORMER_GEN ?= $(shell pwd)/bin/informer-gen
LISTER_GEN ?= $(shell pwd)/bin/lister-gen

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.9.2
CODE_GENERATOR_VERSION ?= v0.23.4

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }

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

ENVTEST = $(shell pwd)/bin/setup-envtest
.PHONY: envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
ENVTESTDIR=$$($(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\
rm -f $(LOCALBIN)/k8s/current ;\
ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current

CLIENT_GEN = $(shell pwd)/bin/client-gen
.PHONY: client-gen
client-gen: ## Download client-gen
$(call go-install-tool,$(CLIENT_GEN),k8s.io/code-generator/cmd/[email protected])
client-gen: $(CLIENT_GEN) ## Download client-gen
$(CLIENT_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION)

INFORMER_GEN = $(shell pwd)/bin/informer-gen
.PHONY: informer-gen
informer-gen: ## Download informer-gen
$(call go-install-tool,$(INFORMER_GEN),k8s.io/code-generator/cmd/[email protected])
informer-gen: $(INFORMER_GEN) ## Download informer-gen
$(INFORMER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION)

LISTER_GEN = $(shell pwd)/bin/lister-gen
.PHONY: lister-gen
lister-gen: ## Download lister-gen
$(call go-install-tool,$(LISTER_GEN),k8s.io/code-generator/cmd/[email protected])

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef

# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
lister-gen: $(LISTER_GEN) ## Download lister-gen
$(LISTER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION)
16 changes: 8 additions & 8 deletions api/v1alpha1/clusterspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster
//+kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+genclient
//+genclient:nonNamespaced
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
// +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +genclient
// +genclient:nonNamespaced

// ClusterSpace is the Schema for the clusterspaces API
type ClusterSpace struct {
Expand All @@ -28,7 +28,7 @@ type ClusterSpace struct {
Status SpaceStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// ClusterSpaceList contains a list of ClusterSpace
type ClusterSpaceList struct {
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/clusterspace_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (r *ClusterSpace) SetupWebhookWithManager(mgr ctrl.Manager) error {
Complete()
}

//+kubebuilder:webhook:path=/mutate-cf-cs-sap-com-v1alpha1-clusterspace,mutating=true,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=clusterspaces,verbs=create;update,versions=v1alpha1,name=mclusterspace.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/mutate-cf-cs-sap-com-v1alpha1-clusterspace,mutating=true,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=clusterspaces,verbs=create;update,versions=v1alpha1,name=mclusterspace.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &ClusterSpace{}

Expand All @@ -36,7 +36,7 @@ func (r *ClusterSpace) Default() {
}
}

//+kubebuilder:webhook:path=/validate-cf-cs-sap-com-v1alpha1-clusterspace,mutating=false,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=clusterspaces,verbs=create;update,versions=v1alpha1,name=vclusterspace.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/validate-cf-cs-sap-com-v1alpha1-clusterspace,mutating=false,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=clusterspaces,verbs=create;update,versions=v1alpha1,name=vclusterspace.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &ClusterSpace{}

Expand Down
12 changes: 6 additions & 6 deletions api/v1alpha1/servicebinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+genclient
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +genclient

// ServiceBinding is the Schema for the servicebindings API
type ServiceBinding struct {
Expand All @@ -27,7 +27,7 @@ type ServiceBinding struct {
Status ServiceBindingStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// ServiceBindingList contains a list of ServiceBinding
type ServiceBindingList struct {
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/servicebinding_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (r *ServiceBinding) SetupWebhookWithManager(mgr ctrl.Manager) error {
Complete()
}

//+kubebuilder:webhook:path=/mutate-cf-cs-sap-com-v1alpha1-servicebinding,mutating=true,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=servicebindings,verbs=create;update,versions=v1alpha1,name=mservicebinding.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/mutate-cf-cs-sap-com-v1alpha1-servicebinding,mutating=true,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=servicebindings,verbs=create;update,versions=v1alpha1,name=mservicebinding.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &ServiceBinding{}

Expand All @@ -44,7 +44,7 @@ func (r *ServiceBinding) Default() {
}
}

//+kubebuilder:webhook:path=/validate-cf-cs-sap-com-v1alpha1-servicebinding,mutating=false,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=servicebindings,verbs=create;update,versions=v1alpha1,name=vservicebinding.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/validate-cf-cs-sap-com-v1alpha1-servicebinding,mutating=false,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=servicebindings,verbs=create;update,versions=v1alpha1,name=vservicebinding.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &ServiceBinding{}

Expand Down
12 changes: 6 additions & 6 deletions api/v1alpha1/serviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+genclient
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +genclient

// ServiceInstance is the Schema for the serviceinstances API
type ServiceInstance struct {
Expand All @@ -27,7 +27,7 @@ type ServiceInstance struct {
Status ServiceInstanceStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// ServiceInstanceList contains a list of ServiceInstance
type ServiceInstanceList struct {
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/serviceinstance_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (r *ServiceInstance) SetupWebhookWithManager(mgr ctrl.Manager) error {
Complete()
}

//+kubebuilder:webhook:path=/mutate-cf-cs-sap-com-v1alpha1-serviceinstance,mutating=true,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=serviceinstances,verbs=create;update,versions=v1alpha1,name=mserviceinstance.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/mutate-cf-cs-sap-com-v1alpha1-serviceinstance,mutating=true,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=serviceinstances,verbs=create;update,versions=v1alpha1,name=mserviceinstance.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &ServiceInstance{}

Expand All @@ -46,7 +46,7 @@ func (r *ServiceInstance) Default() {
}
}

//+kubebuilder:webhook:path=/validate-cf-cs-sap-com-v1alpha1-serviceinstance,mutating=false,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=serviceinstances,verbs=create;update,versions=v1alpha1,name=vserviceinstance.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/validate-cf-cs-sap-com-v1alpha1-serviceinstance,mutating=false,failurePolicy=fail,sideEffects=None,groups=cf.cs.sap.com,resources=serviceinstances,verbs=create;update,versions=v1alpha1,name=vserviceinstance.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &ServiceInstance{}

Expand Down
Loading

0 comments on commit d439e8b

Please sign in to comment.