Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
chore: various updates w/ go1.19 (#25)
Browse files Browse the repository at this point in the history
- go@1.19
- hermit
- linter (and configuration so things pass)
- kubebuilder Makefile (so it's consistent with changes)
- fixed tests kubernetes-sigs/controller-runtime#1571
- controller-runtime@0.11.1
  • Loading branch information
Stan Rozenraukh committed Jan 5, 2023
1 parent 0fc0abf commit 0e040d2
Show file tree
Hide file tree
Showing 20 changed files with 377 additions and 148 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Hermit
run: ./bin/hermit env -r >> $GITHUB_ENV
run: ./bin/hermit env --raw >> $GITHUB_ENV
- name: Build & Check for changes
run: make diff-check
test:
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Hermit
run: ./bin/hermit env -r >> $GITHUB_ENV
run: ./bin/hermit env --raw >> $GITHUB_ENV
- name: Tests
run: make test
lint:
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Hermit
run: ./bin/hermit env -r >> $GITHUB_ENV
run: ./bin/hermit env --raw >> $GITHUB_ENV
- name: Lint
run: golangci-lint run --timeout=5m
docker:
Expand All @@ -47,7 +47,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Hermit
run: ./bin/hermit env -r >> $GITHUB_ENV
run: ./bin/hermit env --raw >> $GITHUB_ENV
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
Expand Down
7 changes: 7 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ linters:
enable-all: true
disable:
- exhaustivestruct
- exhaustruct
- gci
- gochecknoglobals
- gochecknoinits
- gofmt
- golint
- ifshort
- interfacer
- ireturn
- lll
- maligned
- nlreturn
- nonamedreturns
- paralleltest
- scopelint
- structcheck
- testpackage
- varcheck
- varnamelen
- wsl
- deadcode
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.17 as builder
FROM golang:1.19 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
92 changes: 56 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

# Image URL to use all building/pushing image targets
IMG ?= cmmc:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
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))
GOBIN=$(shell go env GOPATH)/bin
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
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

.PHONY: all
all: build

##@ General
Expand All @@ -32,51 +32,60 @@ all: build
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
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)

##@ Development

.PHONY: manifests
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

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
go vet ./...

.PHONY: lint
lint: ## Run golangci-lint against the code.
golangci-lint run

mkdocs-serve: ## Run mkdocs-material dev server in docker.
docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material

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.8.3/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
.PHONY: test
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

##@ Build

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

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

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

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}

##@ CI

.PHONY: diff-check
diff-check: manifests ## Checks to see if there are any changes in git
@if [ `git status --porcelain=v1 | wc -l` -ne "0" ]; then \
echo "::error ::There are likely changed manifests that are not committed in code, run 'make manifests'"; \
Expand All @@ -85,40 +94,51 @@ diff-check: manifests ## Checks to see if there are any changes in git

##@ Deployment

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -

.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 -

.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -


CONTROLLER_GEN = $(shell pwd)/.hermit/go/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)

KUSTOMIZE = $(shell pwd)/.hermit/go/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)

# 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)" ;\
go get $(2) ;\
ls $(PROJECT_BIN) ;\
rm -rf $$TMP_DIR ;\
}
endef
##@ Build Dependencies

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

## Tool binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.4.1

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 -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)

.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
4 changes: 2 additions & 2 deletions api/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ limitations under the License.
*/

// Package v1beta1 contains API Schema definitions for the cmmc v1beta1 API group
//+kubebuilder:object:generate=true
//+groupName=config.cmmc.k8s.cash.app
// +kubebuilder:object:generate=true
// +groupName=config.cmmc.k8s.cash.app
package v1beta1

import (
Expand Down
2 changes: 0 additions & 2 deletions api/v1beta1/mergesource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type MergeSourceSourceSpec struct {

// MergeSourceTargetSpec describes the MergeTarget a MergeSource will target.
type MergeSourceTargetSpec struct {

// Name specifies the MergeTarget we will attempt to write to (if it exists).
Name string `json:"name,omitempty"`

Expand All @@ -48,7 +47,6 @@ type MergeSourceTargetSpec struct {
// Manily, which ConfigMap resources to watch, which key it will be
// aggregating data from, and which MergeTarget it will be writing to.
type MergeSourceSpec struct {

// Selector specifies what labels on a source ConfigMap the controller will be watching.
Selector map[string]string `json:"selector,omitempty"`

Expand Down
6 changes: 3 additions & 3 deletions api/v1beta1/mergetarget_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func (m *MergeTarget) UpdateDataStatus(configMapData map[string]string) {
}
}

// ReduceDataState mutates configMapData, accumulating the MergeSourceList into the
// respective keys.
// nolint:cyclop
// ReduceDataState mutates configMapData, accumulating the MergeSourceList into the respective keys.
//
//nolint:cyclop
func (m *MergeTarget) ReduceDataState(
mergeSources MergeSourceList, configMapData *map[string]string,
) (statusKeysToRemove []string, updatedKeys int, fieldsErrors []string) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions bin/activate-hermit
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
# This file must be used with "source bin/activate-hermit" from bash or zsh.
# You cannot run it directly
#
# THIS FILE IS GENERATED; DO NOT MODIFY

if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
Expand Down
2 changes: 1 addition & 1 deletion bin/go
2 changes: 1 addition & 1 deletion bin/gofmt
2 changes: 1 addition & 1 deletion bin/gofumpt
2 changes: 1 addition & 1 deletion bin/golangci-lint
23 changes: 20 additions & 3 deletions bin/hermit
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/bin/bash
#
# THIS FILE IS GENERATED; DO NOT MODIFY

set -eo pipefail

export HERMIT_USER_HOME=~

if [ -z "${HERMIT_STATE_DIR}" ]; then
case "$(uname -s)" in
Darwin)
export HERMIT_STATE_DIR="${HOME}/Library/Caches/hermit"
export HERMIT_STATE_DIR="${HERMIT_USER_HOME}/Library/Caches/hermit"
;;
Linux)
export HERMIT_STATE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/hermit"
export HERMIT_STATE_DIR="${XDG_CACHE_HOME:-${HERMIT_USER_HOME}/.cache}/hermit"
;;
esac
fi
Expand All @@ -20,7 +24,20 @@ export HERMIT_EXE=${HERMIT_EXE:-${HERMIT_STATE_DIR}/pkg/hermit@${HERMIT_CHANNEL}

if [ ! -x "${HERMIT_EXE}" ]; then
echo "Bootstrapping ${HERMIT_EXE} from ${HERMIT_DIST_URL}" 1>&2
curl -fsSL "${HERMIT_DIST_URL}/install.sh" | /bin/bash 1>&2
INSTALL_SCRIPT="$(mktemp)"
# This value must match that of the install script
INSTALL_SCRIPT_SHA256="180e997dd837f839a3072a5e2f558619b6d12555cd5452d3ab19d87720704e38"
if [ "${INSTALL_SCRIPT_SHA256}" = "BYPASS" ]; then
curl -fsSL "${HERMIT_DIST_URL}/install.sh" -o "${INSTALL_SCRIPT}"
else
# Install script is versioned by its sha256sum value
curl -fsSL "${HERMIT_DIST_URL}/install-${INSTALL_SCRIPT_SHA256}.sh" -o "${INSTALL_SCRIPT}"
# Verify install script's sha256sum
openssl dgst -sha256 "${INSTALL_SCRIPT}" | \
awk -v EXPECTED="$INSTALL_SCRIPT_SHA256" \
'$2!=EXPECTED {print "Install script sha256 " $2 " does not match " EXPECTED; exit 1}'
fi
/bin/bash "${INSTALL_SCRIPT}" 1>&2
fi

exec "${HERMIT_EXE}" --level=fatal exec "$0" -- "$@"
10 changes: 8 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment

ctx context.Context
cancel context.CancelFunc
)

func TestAPIs(t *testing.T) {
Expand Down Expand Up @@ -109,8 +112,10 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager, controller.Options{})
Expect(err).ToNot(HaveOccurred())

ctx, cancel = context.WithCancel(ctrl.SetupSignalHandler())

go func() {
err = k8sManager.Start(ctrl.SetupSignalHandler())
err = k8sManager.Start(ctx)
Expect(err).ToNot(HaveOccurred())
}()
})
Expand Down Expand Up @@ -161,7 +166,7 @@ var _ = Describe("cmmc", func() {
func() (*configMapState, error) {
var cm corev1.ConfigMap
if err := k8sClient.Get(ctx, name, &cm); err != nil {
return nil, err // nolint:wrapcheck
return nil, err //nolint:wrapcheck
}
anns := cm.GetAnnotations()
watchedBy := strings.Split(anns[string(watchedBy)], ",")
Expand Down Expand Up @@ -315,6 +320,7 @@ var _ = Describe("cmmc", func() {
})

var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion controllers/watch_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down
Loading

0 comments on commit 0e040d2

Please sign in to comment.