Skip to content

Commit

Permalink
Updates for operator SDK version v1.15.0
Browse files Browse the repository at this point in the history
Update dependencies https://sdk.operatorframework.io/docs/upgrading-sdk-version/v1.14.0
Update Makefile targets to be consistent with latest version.
Add script to install a specific version of the operator SDK and use it in Makefile targets.
Update to use go version 1.16

Set IMAGE_TAG_BASE to quay.io/kuadrant/limitador-operator
Removed custom envtest setup, use ENVTEST_K8S_VERSION 1.22
  • Loading branch information
mikenairn committed Dec 22, 2021
1 parent da6afb2 commit b42bfb2
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 298 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ jobs:
name: Build Docker image
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.16.x
uses: actions/setup-go@v2
with:
go-version: 1.16.x
id: go
- uses: actions/checkout@v2
- name: Build Docker image
run: make docker-build
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.16.x
uses: actions/setup-go@v2
with:
go-version: 1.16.x
id: go
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- name: Run the tests
run: make test
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 golang:1.15 as builder
FROM golang:1.16 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
114 changes: 65 additions & 49 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# The "source" command used in the "test" target does not work with /bin/sh.
SHELL := /bin/bash

# VERSION defines the project version for the bundle.
# Update this value when you upgrade the version of your project.
# To re-generate a bundle for another specific version without changing the standard setup, you can:
Expand All @@ -9,10 +6,10 @@ SHELL := /bin/bash
VERSION ?= 0.0.1

# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
# - use environment variables to overwrite this value (e.g export CHANNELS="preview,fast,stable")
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=candidate,fast,stable)
# - use environment variables to overwrite this value (e.g export CHANNELS="candidate,fast,stable")
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
Expand All @@ -31,17 +28,17 @@ BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
# This variable is used to construct full image tags for bundle and catalog images.
#
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
# kuadrant.io/limitador-operator-bundle:$VERSION and kuadrant.io/limitador-operator-catalog:$VERSION.
IMAGE_TAG_BASE ?= https://quay.io/repository/3scale/limitador-operator
# quay.io/kuadrant/limitador-operator-bundle:$VERSION and quay.io/kuadrant/limitador-operator-catalog:$VERSION.
IMAGE_TAG_BASE ?= quay.io/kuadrant/limitador-operator

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

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
IMG ?= quay.io/kuadrant/limitador-operator:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.22

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -50,6 +47,12 @@ else
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

all: build

##@ General
Expand All @@ -66,12 +69,12 @@ all: build
# http://linuxcommand.org/lc3_adv_awk.php

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

##@ Development

manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
Expand All @@ -82,11 +85,8 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.2/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out

##@ Build

Expand All @@ -98,7 +98,7 @@ run: export LOG_MODE = development
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

docker-build: test ## Build docker image with the manager.
docker-build: ## Build docker image with the manager.
docker build -t ${IMG} .

docker-push: ## Push docker image with the manager.
Expand All @@ -122,37 +122,15 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0)

KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

KIND = $(shell pwd)/bin/kind
kind: ## Download kind locally if necessary
$(call go-get-tool,$(KIND),sigs.k8s.io/[email protected])

KIND_CLUSTER_NAME = limitador-local

.PHONY: local-setup
local-setup: local-cleanup local-setup-kind docker-build ## Deploy operator in local kind cluster
@echo "Deploying Limitador control plane"
$(KIND) load docker-image ${IMG} --name ${KIND_CLUSTER_NAME}
make deploy
kubectl -n limitador-operator-system patch deployment limitador-operator-controller-manager -p '{"spec": {"template": {"spec":{"containers":[{"name": "manager","image":"${IMG}", "imagePullPolicy":"IfNotPresent"}]}}}}'
@echo "Wait for all deployments to be up"
kubectl -n limitador-operator-system wait --timeout=300s --for=condition=Available deployments --all

.PHONY: local-dev-setup
local-dev-setup: local-cleanup local-setup-kind install run ## Run operator locally in local kind cluster

.PHONY: local-cleanup
local-cleanup: kind ## Clean up local kind cluster
$(KIND) delete cluster --name $(KIND_CLUSTER_NAME)

.PHONY: local-setup-kind
local-setup-kind: kind ## Create kind cluster
$(KIND) create cluster --name $(KIND_CLUSTER_NAME)
ENVTEST = $(shell pwd)/bin/setup-envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
Expand All @@ -168,12 +146,17 @@ rm -rf $$TMP_DIR ;\
}
endef

OPERATOR_SDK = $(shell pwd)/bin/operator-sdk
OPERATOR_SDK_VERSION = v1.15.0
operator-sdk: ## Download operator-sdk locally if necessary.
./utils/install-operator-sdk.sh $(OPERATOR_SDK) $(OPERATOR_SDK_VERSION)

.PHONY: bundle
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
operator-sdk generate kustomize manifests -q
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
$(OPERATOR_SDK) generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
$(OPERATOR_SDK) bundle validate ./bundle

.PHONY: bundle-build
bundle-build: ## Build the bundle image.
Expand Down Expand Up @@ -223,3 +206,36 @@ catalog-build: opm ## Build a catalog image.
.PHONY: catalog-push
catalog-push: ## Push a catalog image.
$(MAKE) docker-push IMG=$(CATALOG_IMG)

##@ Misc

## Miscellaneous Custom targets

KIND = $(shell pwd)/bin/kind
kind: ## Download kind locally if necessary
$(call go-get-tool,$(KIND),sigs.k8s.io/[email protected])

KIND_CLUSTER_NAME = limitador-local

.PHONY: local-setup
local-setup: local-cleanup local-setup-kind docker-build ## Deploy operator in local kind cluster
@echo "Deploying Limitador control plane"
$(KIND) load docker-image ${IMG} --name ${KIND_CLUSTER_NAME}
make deploy
kubectl -n limitador-operator-system patch deployment limitador-operator-controller-manager -p '{"spec": {"template": {"spec":{"containers":[{"name": "manager","image":"${IMG}", "imagePullPolicy":"IfNotPresent"}]}}}}'
@echo "Wait for all deployments to be up"
kubectl -n limitador-operator-system wait --timeout=300s --for=condition=Available deployments --all

.PHONY: local-dev-setup
local-dev-setup: local-cleanup local-setup-kind install run ## Run operator locally in local kind cluster

.PHONY: local-cleanup
local-cleanup: kind ## Clean up local kind cluster
$(KIND) delete cluster --name $(KIND_CLUSTER_NAME)

.PHONY: local-setup-kind
local-setup-kind: kind ## Create kind cluster
$(KIND) create cluster --name $(KIND_CLUSTER_NAME)

# Include last to avoid changing MAKEFILE_LIST used above
include ./make/*.mk
2 changes: 1 addition & 1 deletion config/crd/bases/limitador.kuadrant.io_limitadors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.7.0
creationTimestamp: null
name: limitadors.limitador.kuadrant.io
spec:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/limitador.kuadrant.io_ratelimits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.7.0
creationTimestamp: null
name: ratelimits.limitador.kuadrant.io
spec:
Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: controller
newName: quay.io/kuadrant/limitador-operator
newTag: latest
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ metadata:
spec:
apiservicedefinitions: {}
customresourcedefinitions: {}
description: The Limitador operator installs and maintains limitador instances as well as rate limit objects.
description: The Limitador operator installs and maintains limitador instances as
well as rate limit objects.
displayName: Limitador
icon:
- base64data: ""
Expand Down
12 changes: 10 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package controllers

import (
"github.com/onsi/gomega/gexec"
"net/url"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -125,14 +127,20 @@ var _ = BeforeSuite(func() {
Expect(err).ToNot(HaveOccurred())

go func() {
defer GinkgoRecover()
err = mgr.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
gexec.KillAndWait(4 * time.Second)

// Teardown the test environment once controller is finished.
// Otherwise from Kubernetes 1.21+, teardown timeouts waiting on
// kube-apiserver to return
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
}()

}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module github.com/kuadrant/limitador-operator

go 1.15
go 1.16

require (
github.com/go-logr/logr v0.3.0
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/stretchr/testify v1.5.1
go.uber.org/zap v1.15.0
github.com/go-logr/logr v0.4.0
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.15.0
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.0
gotest.tools v2.2.0+incompatible
k8s.io/api v0.19.2
k8s.io/apimachinery v0.19.2
k8s.io/client-go v0.19.2
k8s.io/klog/v2 v2.2.0
sigs.k8s.io/controller-runtime v0.7.2
k8s.io/api v0.22.1
k8s.io/apimachinery v0.22.1
k8s.io/client-go v0.22.1
k8s.io/klog/v2 v2.9.0
sigs.k8s.io/controller-runtime v0.10.0
)
Loading

0 comments on commit b42bfb2

Please sign in to comment.