From abb1ebd203aeed23bb9d5173dccb0b8ef9659b77 Mon Sep 17 00:00:00 2001 From: Michael Nairn Date: Fri, 2 Feb 2024 13:12:13 +0000 Subject: [PATCH] Add local-setup and CI GH Workflow * Adds kind dependency and make targets * Add config/deploy/local to patch the manager imagePullPolicy to "IfNotPresent" to allow it to work with locally loaded images in kind clusters. * Add CI workflow * Add verify make targets * Add lint make target and golangci config --- .github/workflows/ci.yaml | 111 ++++++++++++++++++ .golangci.yaml | 5 + Makefile | 28 ++++- config/deploy/local/kustomization.yaml | 10 ++ config/deploy/local/manager_config_patch.yaml | 11 ++ hack/kind-cluster.yaml | 6 + make/kind.mk | 18 +++ make/verify.mk | 22 ++++ 8 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .golangci.yaml create mode 100644 config/deploy/local/kustomization.yaml create mode 100644 config/deploy/local/manager_config_patch.yaml create mode 100644 hack/kind-cluster.yaml create mode 100644 make/kind.mk create mode 100644 make/verify.mk diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 00000000..ef79f423 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,111 @@ +name: CI + +on: + push: + branches: + - main + - "release-*" + tags: + - "v[0-9]+.[0-9]+.[0-9]+" + paths-ignore: + - '**.adoc' + - '**.md' + - 'samples/**' + - 'LICENSE' + pull_request: + branches: + - main + - "release-*" + paths-ignore: + - '**.adoc' + - '**.md' + - 'samples/**' + - 'LICENSE' + +jobs: + lint: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: v1.21.x + cache: false + - uses: golangci/golangci-lint-action@v3 + with: + version: v1.55.2 + only-new-issues: true + args: --timeout=5m + verify-code: + name: Verify code + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.21.x + uses: actions/setup-go@v4 + with: + go-version: 1.21.x + cache: false + id: go + - name: Check out code + uses: actions/checkout@v4 + - name: Run make verify-code + run: | + make verify-code + verify-manifests: + name: Verify manifests + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.21.x + uses: actions/setup-go@v4 + with: + go-version: 1.21.x + cache: false + id: go + - name: Check out code + uses: actions/checkout@v4 + - name: Run make verify-manifests + run: | + make verify-manifests + verify-bundle: + name: Verify bundle + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.21.x + uses: actions/setup-go@v4 + with: + go-version: 1.21.x + cache: false + id: go + - name: Check out code + uses: actions/checkout@v4 + - name: Run make verify-bundle + run: | + make verify-bundle + verify-imports: + name: Verify imports + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.21.x + uses: actions/setup-go@v4 + with: + go-version: 1.21.x + cache: false + id: go + - name: Check out code + uses: actions/checkout@v4 + - name: Run make verify-manifests + run: | + make verify-imports + unit_test_suite: + name: Unit Test Suite + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: v1.21 + cache: false + - name: Run suite + run: | + make test diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..bc373962 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,5 @@ +# https://golangci-lint.run/usage/linters/ +linters: + enable: + - gofmt + - misspell diff --git a/Makefile b/Makefile index 88f4b4db..11c649e2 100644 --- a/Makefile +++ b/Makefile @@ -111,6 +111,10 @@ fmt: ## Run go fmt against code. vet: ## Run go vet against code. go vet ./... +.PHONY: lint +lint: ## Run golangci-lint against code. + golangci-lint run ./... + .PHONY: imports imports: openshift-goimports ## Run openshift goimports against code. $(OPENSHIFT_GOIMPORTS) -m github.com/kuadrant/kuadrant-dns-operator -i github.com/kuadrant/kuadrant-operator @@ -119,6 +123,14 @@ imports: openshift-goimports ## Run openshift goimports against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +.PHONY: local-setup +local-setup: $(KIND) ## Setup local development kind cluster and dependencies + $(MAKE) kind-delete-cluster + $(MAKE) kind-create-cluster + +.PHONY: local-deploy +local-deploy: docker-build kind-load-image deploy ## Deploy the dns operator into local kind cluster from the current code + ##@ Build .PHONY: build @@ -174,11 +186,11 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified .PHONY: deploy deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} - $(KUSTOMIZE) build config/default | $(KUBECTL) apply -f - + $(KUSTOMIZE) build config/deploy/local | $(KUBECTL) apply -f - .PHONY: undeploy 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 - + $(KUSTOMIZE) build config/deploy/local | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f - ##@ Build Dependencies @@ -193,11 +205,13 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen ENVTEST ?= $(LOCALBIN)/setup-envtest OPENSHIFT_GOIMPORTS ?= $(LOCALBIN)/openshift-goimports +KIND = $(LOCALBIN)/kind ## Tool Versions KUSTOMIZE_VERSION ?= v5.0.1 CONTROLLER_TOOLS_VERSION ?= v0.12.0 OPENSHIFT_GOIMPORTS_VERSION ?= c70783e636f2213cac683f6865d88c5edace3157 +KIND_VERSION = v0.20.0 .PHONY: kustomize kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading. @@ -238,9 +252,14 @@ endif .PHONY: openshift-goimports openshift-goimports: $(OPENSHIFT_GOIMPORTS) ## Download openshift-goimports locally if necessary -$(OPENSHIFT_GOIMPORTS): +$(OPENSHIFT_GOIMPORTS): $(LOCALBIN) GOBIN=$(LOCALBIN) go install github.com/openshift-eng/openshift-goimports@$(OPENSHIFT_GOIMPORTS_VERSION) +.PHONY: kind +kind: $(KIND) ## Download kind locally if necessary. +$(KIND): $(LOCALBIN) + GOBIN=$(LOCALBIN) go install sigs.k8s.io/kind@$(KIND_VERSION) + .PHONY: bundle bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files. $(OPERATOR_SDK) generate kustomize manifests -q @@ -296,3 +315,6 @@ catalog-build: opm ## Build a catalog image. .PHONY: catalog-push catalog-push: ## Push a catalog image. $(MAKE) docker-push IMG=$(CATALOG_IMG) + +# Include last to avoid changing MAKEFILE_LIST used above +include ./make/*.mk diff --git a/config/deploy/local/kustomization.yaml b/config/deploy/local/kustomization.yaml new file mode 100644 index 00000000..ed0c7bce --- /dev/null +++ b/config/deploy/local/kustomization.yaml @@ -0,0 +1,10 @@ +# Local deployment overlay. +# +# Set the deployment imagePullPolicy to IfNotPresent. This is required if you are using a local image loaded into kind i.e. make kind-load-image +# + +resources: + - ../../default + +patchesStrategicMerge: +- manager_config_patch.yaml diff --git a/config/deploy/local/manager_config_patch.yaml b/config/deploy/local/manager_config_patch.yaml new file mode 100644 index 00000000..cd7ae12c --- /dev/null +++ b/config/deploy/local/manager_config_patch.yaml @@ -0,0 +1,11 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + containers: + - name: manager + imagePullPolicy: IfNotPresent diff --git a/hack/kind-cluster.yaml b/hack/kind-cluster.yaml new file mode 100644 index 00000000..4697238e --- /dev/null +++ b/hack/kind-cluster.yaml @@ -0,0 +1,6 @@ +--- +kind: Cluster +apiVersion: kind.x-k8s.io/v1alpha4 +nodes: +- role: control-plane + image: kindest/node:v1.27.3 diff --git a/make/kind.mk b/make/kind.mk new file mode 100644 index 00000000..8d8e07f8 --- /dev/null +++ b/make/kind.mk @@ -0,0 +1,18 @@ + +##@ Kind + +## Targets to help install and use kind for development https://kind.sigs.k8s.io + +KIND_CLUSTER_NAME ?= kuadrant-dns-local + +.PHONY: kind-create-cluster +kind-create-cluster: kind ## Create the "kuadrant-dns-local" kind cluster. + $(KIND) create cluster --name $(KIND_CLUSTER_NAME) --config hack/kind-cluster.yaml + +.PHONY: kind-delete-cluster +kind-delete-cluster: kind ## Delete the "kuadrant-dns-local" kind cluster. + - $(KIND) delete cluster --name $(KIND_CLUSTER_NAME) + +.PHONY: kind-load-image +kind-load-image: kind ## Load image to "kuadrant-dns-local" kind cluster. + $(KIND) load docker-image $(IMG) --name $(KIND_CLUSTER_NAME) diff --git a/make/verify.mk b/make/verify.mk new file mode 100644 index 00000000..08d78bc3 --- /dev/null +++ b/make/verify.mk @@ -0,0 +1,22 @@ + +##@ Verify + +## Targets to verify actions that generate/modify code have been executed and output committed + +.PHONY: verify-code +verify-code: vet ## Verify code formatting + @diff -u <(echo -n) <(gofmt -s -d `find . -type f -name '*.go' -not -path "./vendor/*"`) + +.PHONY: verify-manifests +verify-manifests: manifests ## Verify manifests update. + git diff --exit-code ./config + [ -z "$$(git ls-files --other --exclude-standard --directory --no-empty-directory ./config)" ] + +.PHONY: verify-bundle +verify-bundle: bundle ## Verify bundle update. + git diff --exit-code ./bundle + [ -z "$$(git ls-files --other --exclude-standard --directory --no-empty-directory ./bundle)" ] + +.PHONY: verify-imports +verify-imports: ## Verify go imports are sorted and grouped correctly. + hack/verify-imports.sh