Skip to content

Commit

Permalink
K0-340: Migrate to go/v4 project layout (#310)
Browse files Browse the repository at this point in the history
* Refactored cluster code

* Migrated to go/v4 project layout

* Bumped golangci-lint and fixed go-lint

* Fixed go-lint

* Changed backup service module to official one

* Bumped golang version in Jenkins

* Fixed nits

* Fixed operator-sdk path for script
  • Loading branch information
abhishekdwivedi3060 authored Sep 9, 2024
1 parent fbd9d18 commit ed8ba37
Show file tree
Hide file tree
Showing 80 changed files with 28,225 additions and 34,572 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ jobs:
- name: Setup-go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: 1.22
- name: Checkout sources
uses: actions/checkout@v3
with:
submodules: true
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
version: v1.59.1
args: --timeout=5m
12 changes: 10 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

linters-settings:
revive:
rules:
- name: dot-imports
arguments:
- allowedPackages: # Allow dot imports for testing packages
- "github.com/onsi/ginkgo/v2"
- "github.com/onsi/gomega"
goconst:
min-len: 2
min-occurrences: 3
Expand All @@ -11,9 +18,10 @@ linters-settings:
- performance
- style
govet:
check-shadowing: true
enable:
- fieldalignment
- shadow

nolintlint:
require-explanation: true
require-specific: true
Expand Down Expand Up @@ -56,7 +64,7 @@ linters:

run:
issues-exit-code: 1
go: '1.21'
go: '1.22'
# skip-dirs:
# - sample
# skip-files:
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.21 AS builder
FROM --platform=$BUILDPLATFORM golang:1.22 AS builder

# OS and Arch args
ARG TARGETOS
Expand All @@ -14,14 +14,14 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY controllers/ controllers/
COPY internal/controller/ internal/controller/
COPY pkg/ pkg/
COPY errors/ errors/

# Build
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o manager cmd/main.go

# Base image
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest
Expand Down
6 changes: 2 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
pipeline {
agent any
tools {
go 'go-1.21'
go 'go-1.22'
}

environment {
GOPATH="/var/lib/jenkins/go"
// Operator sdk command "operator-sdk" should be present in PATH or at
// /usr/local/operator-sdk-1.28.0/
PATH="/usr/local/operator-sdk-1.28.0/:${GOPATH}/bin:/usr/local/bin:${env.PATH}"
GO_REPO_ROOT="${env.GOPATH}/src/github.com"
GO_REPO="${env.GO_REPO_ROOT}/aerospike-kubernetes-operator"
PATH="${GOPATH}/bin:/usr/local/bin:${env.PATH}:${GO_REPO}/bin"
DOCKER_REGISTRY="docker.io"
DOCKER_ACCOUNT="aerospike"
OPERATOR_NAME = "aerospike-kubernetes-operator"
Expand Down
91 changes: 65 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ endif
IMG ?= controller:latest

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.26
ENVTEST_K8S_VERSION = 1.29.0

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -129,17 +129,14 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.54.0

.PHONY: golanci-lint
golanci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) $(GOLANGCI_LINT_VERSION)

go-lint: golanci-lint ## Run golangci-lint against code.
.PHONY: go-lint
go-lint: golangci-lint ## Run golangci-lint against code.
$(GOLANGCI_LINT) run

.PHONY: go-lint-fix
go-lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix

.PHONY: all-test
all-test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" cd $(shell pwd)/test; go run github.com/onsi/ginkgo/v2/ginkgo -coverprofile cover.out -r --keep-going -show-node-events -v -timeout=12h0m0s --junit-report="junit.xml" -- ${ARGS}
Expand All @@ -166,11 +163,11 @@ restore-test: manifests generate fmt vet envtest ## Run tests.

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

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

.PHONY: docker-buildx
docker-buildx: ## Build and push docker image for the manager for cross-platform support
Expand All @@ -189,6 +186,7 @@ docker-buildx-openshift: ## Build and push docker image for the manager for open
.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}

##@ Deployment

ifndef ignore-not-found
Expand Down Expand Up @@ -237,30 +235,71 @@ $(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
KUBECTL ?= kubectl
KUSTOMIZE ?= $(LOCALBIN)/kustomize-$(KUSTOMIZE_VERSION)
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen-$(CONTROLLER_TOOLS_VERSION)
ENVTEST ?= $(LOCALBIN)/setup-envtest-$(ENVTEST_VERSION)
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)

## Tool Versions
KUSTOMIZE_VERSION ?= v4.5.7
CONTROLLER_TOOLS_VERSION ?= v0.12.1
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= release-0.17
GOLANGCI_LINT_VERSION ?= v1.59.1
# Set the Operator SDK version to use. By default, what is installed on the system is used.
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
OPERATOR_SDK_VERSION ?= v1.36.0

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

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))

.PHONY: envtest
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
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))

.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})

# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f $(1) ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
}
endef

.PHONY: operator-sdk
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
operator-sdk: ## Download operator-sdk locally if necessary.
ifeq (,$(wildcard $(OPERATOR_SDK)))
ifeq (, $(shell which operator-sdk 2>/dev/null))
@{ \
set -e ;\
mkdir -p $(dir $(OPERATOR_SDK)) ;\
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
curl -sSLo $(OPERATOR_SDK) https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_$${OS}_$${ARCH} ;\
chmod +x $(OPERATOR_SDK) ;\
}
else
OPERATOR_SDK = $(shell which operator-sdk)
endif
endif

.PHONY: enable-pre-commit
enable-pre-commit:
Expand All @@ -276,13 +315,13 @@ submodules: ## Pull and update git submodules recursively
# For OpenShift bundles run
# CHANNELS=stable DEFAULT_CHANNEL=stable OPENSHIFT_VERSION=v4.6 IMG=docker.io/aerospike/aerospike-kubernetes-operator-nightly:3.3.1 make bundle
.PHONY: bundle
bundle: manifests kustomize
bundle: manifests kustomize operator-sdk
rm -rf $(ROOT_DIR)/bundle.Dockerfile $(BUNDLE_DIR)
operator-sdk generate kustomize manifests -q
$(OPERATOR_SDK) generate kustomize manifests -q
cd $(ROOT_DIR)/config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
cd $(ROOT_DIR)/config/manifests/bases && $(KUSTOMIZE) edit set annotation createdAt:$(DATE)
cd $(ROOT_DIR) && $(KUSTOMIZE) build $(OVERLAYS_DIR) | operator-sdk generate bundle $(BUNDLE_GEN_FLAGS) --output-dir $(BUNDLE_DIR)
operator-sdk bundle validate $(BUNDLE_DIR)
cd $(ROOT_DIR) && $(KUSTOMIZE) build $(OVERLAYS_DIR) | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS) --output-dir $(BUNDLE_DIR)
$(OPERATOR_SDK) bundle validate $(BUNDLE_DIR)
sed -i "s@containerImage: controller:latest@containerImage: $(IMG)@g" $(BUNDLE_DIR)/manifests/aerospike-kubernetes-operator.clusterserviceversion.yaml
sed -i "/^FROM.*/a LABEL com.redhat.openshift.versions="$(OPENSHIFT_VERSION)"" $(ROOT_DIR)/bundle.Dockerfile; \
sed -i "/^FROM.*/a LABEL com.redhat.delivery.operator.bundle=true" $(ROOT_DIR)/bundle.Dockerfile; \
Expand Down
2 changes: 1 addition & 1 deletion PROJECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
domain: aerospike.com
layout:
- go.kubebuilder.io/v3
- go.kubebuilder.io/v4
plugins:
manifests.sdk.operatorframework.io/v2: {}
scorecard.sdk.operatorframework.io/v2: {}
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/aerospikebackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"github.com/aerospike/aerospike-backup-service/pkg/model"
asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
"github.com/aerospike/aerospike-kubernetes-operator/controllers/common"
"github.com/aerospike/aerospike-kubernetes-operator/internal/controller/common"
)

func (r *AerospikeBackup) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/aerospikebackupservice_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"github.com/aerospike/aerospike-backup-service/pkg/model"
)

func (r *AerospikeBackupService) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/aerospikerestore_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"github.com/aerospike/aerospike-kubernetes-operator/controllers/common"
"github.com/aerospike/aerospike-backup-service/pkg/model"
"github.com/aerospike/aerospike-kubernetes-operator/internal/controller/common"
)

const defaultPollingPeriod time.Duration = 60 * time.Second
Expand Down
34 changes: 19 additions & 15 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
crClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
asdbv1beta1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1beta1"
"github.com/aerospike/aerospike-kubernetes-operator/internal/controller/backup"
backupservice "github.com/aerospike/aerospike-kubernetes-operator/internal/controller/backup-service"
"github.com/aerospike/aerospike-management-lib/asconfig"

asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
// +kubebuilder:scaffold:imports
asdbv1beta1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1beta1"
aerospikecluster "github.com/aerospike/aerospike-kubernetes-operator/controllers"
"github.com/aerospike/aerospike-kubernetes-operator/controllers/backup"
backupservice "github.com/aerospike/aerospike-kubernetes-operator/controllers/backup-service"
"github.com/aerospike/aerospike-kubernetes-operator/controllers/restore"
"github.com/aerospike/aerospike-kubernetes-operator/internal/controller/cluster"
"github.com/aerospike/aerospike-kubernetes-operator/internal/controller/restore"
"github.com/aerospike/aerospike-kubernetes-operator/pkg/configschema"
)

Expand Down Expand Up @@ -85,14 +86,17 @@ func main() {
if strings.Contains(watchNs, ",") {
nsList := strings.Split(watchNs, ",")

var newNsList []string
namespaces := make(map[string]cache.Config)

for _, ns := range nsList {
newNsList = append(newNsList, strings.TrimSpace(ns))
namespaces[strings.TrimSpace(ns)] = cache.Config{}
}

options.Cache.Namespaces = newNsList
options.Cache.DefaultNamespaces = namespaces
} else {
options.Cache.Namespaces = []string{watchNs}
options.Cache.DefaultNamespaces = map[string]cache.Config{
watchNs: {},
}
}

kubeConfig := ctrl.GetConfigOrDie()
Expand Down Expand Up @@ -137,11 +141,11 @@ func main() {
eventBroadcaster.StartStructuredLogging(0)
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})

if err = (&aerospikecluster.AerospikeClusterReconciler{
if err = (&cluster.AerospikeClusterReconciler{
Client: client,
KubeClient: kubeClient,
KubeConfig: kubeConfig,
Log: ctrl.Log.WithName("controllers").WithName("AerospikeCluster"),
Log: ctrl.Log.WithName("controller").WithName("AerospikeCluster"),
Scheme: mgr.GetScheme(),
Recorder: eventBroadcaster.NewRecorder(
mgr.GetScheme(), v1.EventSource{Component: "aerospikeCluster-controller"},
Expand All @@ -162,7 +166,7 @@ func main() {
if err = (&backupservice.AerospikeBackupServiceReconciler{
Client: client,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("AerospikeBackupService"),
Log: ctrl.Log.WithName("controller").WithName("AerospikeBackupService"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AerospikeBackupService")
os.Exit(1)
Expand All @@ -176,7 +180,7 @@ func main() {
if err = (&backup.AerospikeBackupReconciler{
Client: client,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("AerospikeBackup"),
Log: ctrl.Log.WithName("controller").WithName("AerospikeBackup"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AerospikeBackup")
os.Exit(1)
Expand All @@ -190,7 +194,7 @@ func main() {
if err = (&restore.AerospikeRestoreReconciler{
Client: client,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("AerospikeRestore"),
Log: ctrl.Log.WithName("controller").WithName("AerospikeRestore"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AerospikeRestore")
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions config/certmanager/certificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ metadata:
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
namespace: system
spec:
# $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
# SERVICE_NAME_PLACEHOLDER and SERVICE_NAMESPACE_PLACEHOLDER will be substituted by kustomize
dnsNames:
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
- SERVICE_NAME_PLACEHOLDER.SERVICE_NAMESPACE_PLACEHOLDER.svc
- SERVICE_NAME_PLACEHOLDER.SERVICE_NAMESPACE_PLACEHOLDER.svc.cluster.local
issuerRef:
kind: Issuer
name: selfsigned-issuer
Expand Down
Loading

0 comments on commit ed8ba37

Please sign in to comment.