Skip to content

Commit

Permalink
Merge branch 'master' into active-rack
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmayja committed Sep 13, 2024
2 parents 0c6a705 + 457c7c1 commit b5c6c81
Show file tree
Hide file tree
Showing 87 changed files with 28,413 additions and 34,621 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
10 changes: 5 additions & 5 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 All @@ -25,6 +23,8 @@ pipeline {
BUNDLE_IMG="${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME}"

AEROSPIKE_CUSTOM_INIT_REGISTRY="568976754000.dkr.ecr.ap-south-1.amazonaws.com"
AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE="aerospike"
AEROSPIKE_CUSTOM_INIT_NAME_TAG="aerospike-kubernetes-init:2.2.1"
}

stages {
Expand Down Expand Up @@ -92,7 +92,7 @@ pipeline {
dir("${env.GO_REPO}") {
sh "rsync -aK ${env.WORKSPACE}/../../aerospike-kubernetes-operator-resources/secrets/ config/samples/secrets"
sh "set +x; docker login --username AWS 568976754000.dkr.ecr.ap-south-1.amazonaws.com -p \$(aws ecr get-login-password --region ap-south-1); set -x"
sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY}"
sh "./test/test.sh -b ${OPERATOR_BUNDLE_IMAGE_CANDIDATE_NAME} -c ${OPERATOR_CATALOG_IMAGE_CANDIDATE_NAME} -r ${AEROSPIKE_CUSTOM_INIT_REGISTRY} -n ${AEROSPIKE_CUSTOM_INIT_REGISTRY_NAMESPACE} -t ${AEROSPIKE_CUSTOM_INIT_NAME_TAG}"

}
}
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: 4 additions & 0 deletions api/v1/aerospikecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ type AerospikeInitContainerSpec struct { //nolint:govet // for readability
// ImageRegistry is the name of image registry for aerospike-init container image
// ImageRegistry, e.g. docker.io, redhat.access.com
ImageRegistry string `json:"imageRegistry,omitempty"`
// ImageRegistryNamespace is the name of namespace in registry for aerospike-init container image
ImageRegistryNamespace *string `json:"imageRegistryNamespace,omitempty"`
// ImageNameAndTag is the name:tag of aerospike-init container image
ImageNameAndTag string `json:"imageNameAndTag,omitempty"`
// SecurityContext that will be added to aerospike-init container created by operator.
SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"`
// Define resources requests and limits for Aerospike init Container.
Expand Down
70 changes: 56 additions & 14 deletions api/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ const (
AerospikeServerContainerName = "aerospike-server"
AerospikeInitContainerName = "aerospike-init"
AerospikeInitContainerRegistryEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY"
AerospikeInitContainerRegistryNamespaceEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY_NAMESPACE"
AerospikeInitContainerNameTagEnvVar = "AEROSPIKE_KUBERNETES_INIT_NAME_TAG"
AerospikeInitContainerDefaultRegistry = "docker.io"
AerospikeInitContainerDefaultRegistryNamespace = "aerospike"
AerospikeInitContainerDefaultRepoAndTag = "aerospike-kubernetes-init:2.2.1"
AerospikeInitContainerDefaultNameAndTag = "aerospike-kubernetes-init:2.2.1"
AerospikeAppLabel = "app"
AerospikeAppLabelValue = "aerospike-cluster"
AerospikeCustomResourceLabel = "aerospike.com/cr"
Expand Down Expand Up @@ -121,33 +123,73 @@ func GetWorkDirectory(aerospikeConfigSpec AerospikeConfigSpec) string {
return DefaultWorkDirectory
}

func getInitContainerImage(registry string) string {
func getInitContainerImage(registry, namespace, repoAndTag string) string {
return fmt.Sprintf(
"%s/%s/%s", strings.TrimSuffix(registry, "/"),
strings.TrimSuffix(AerospikeInitContainerDefaultRegistryNamespace, "/"),
AerospikeInitContainerDefaultRepoAndTag,
strings.TrimSuffix(namespace, "/"),
repoAndTag,
)
}

func GetAerospikeInitContainerImage(aeroCluster *AerospikeCluster) string {
registry := getInitContainerImageValue(aeroCluster, AerospikeInitContainerRegistryEnvVar,
AerospikeInitContainerDefaultRegistry)
namespace := getInitContainerImageRegistryNamespace(aeroCluster)
repoAndTag := getInitContainerImageValue(aeroCluster, AerospikeInitContainerNameTagEnvVar,
AerospikeInitContainerDefaultNameAndTag)

return getInitContainerImage(registry, namespace, repoAndTag)
}

func getInitContainerImageRegistryNamespace(aeroCluster *AerospikeCluster) string {
// Given in CR
registry := ""
var namespace *string
if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil {
namespace = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistryNamespace
}

if namespace == nil {
// Given in EnvVar
envRegistryNamespace, found := os.LookupEnv(AerospikeInitContainerRegistryNamespaceEnvVar)
if found {
namespace = &envRegistryNamespace
}
}

if namespace == nil {
return AerospikeInitContainerDefaultRegistryNamespace
}

return *namespace
}

func getInitContainerImageValue(aeroCluster *AerospikeCluster, envVar, defaultValue string) string {
var value string

// Check in CR based on the valueType
if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil {
registry = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry
switch envVar {
case AerospikeInitContainerRegistryEnvVar:
value = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry
case AerospikeInitContainerNameTagEnvVar:
value = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageNameAndTag
}
}

if registry != "" {
return getInitContainerImage(registry)
// Check in EnvVar if not found in CR
if value == "" {
envVal, found := os.LookupEnv(envVar)
if found {
value = envVal
}
}

// Given in EnvVar
registry, found := os.LookupEnv(AerospikeInitContainerRegistryEnvVar)
if found {
return getInitContainerImage(registry)
// Return default values if still not found
if value == "" {
return defaultValue
}

// Use default
return getInitContainerImage(AerospikeInitContainerDefaultRegistry)
return value
}

func ClusterNamespacedName(aeroCluster *AerospikeCluster) string {
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5c6c81

Please sign in to comment.