Skip to content

Commit

Permalink
Add linting to the project (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm authored Oct 6, 2023
1 parent 1d10ce3 commit 18d0277
Show file tree
Hide file tree
Showing 52 changed files with 213 additions and 375 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/test_and_build_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ jobs:
with:
go-version-file: 'go.mod'
cache: true
- name: Create temporary directory for yq install
run: |
mkdir ${{ runner.temp }}/yq
- name: Install yq
working-directory: ${{ runner.temp }}/yq
run: |
sudo rm /bin/yq
wget https://github.com/mikefarah/yq/releases/download/v4.11.2/yq_linux_amd64.tar.gz
tar -xzf yq_linux_amd64.tar.gz
sudo mv ./yq_linux_amd64 /usr/bin/yq
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
# GHA requires longer timeout
args: --timeout=10m
# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: ${{ github.event_name == 'pull_request' }}
skip-pkg-cache: true
skip-build-cache: true
- name: Run unit and integration tests
run: |
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG/CHANGELOG-1.10.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ When cutting a new release, update the `unreleased` heading to the tag being gen
## unreleased

* [BUGFIX] [#1060](https://github.com/k8ssandra/k8ssandra-operator/issues/1060) Fix restore mapping shuffling nodes when restoring in place
* [BUGFIX] [#1061](https://github.com/k8ssandra/k8ssandra-operator/issues/1061) Point to cass-config-builder 1.0.7 for arm64 compatibility
* [BUGFIX] [#1061](https://github.com/k8ssandra/k8ssandra-operator/issues/1061) Point to cass-config-builder 1.0.7 for arm64 compatibility
* [ENHANCEMENT] [#956](https://github.com/k8ssandra/k8ssandra-operator/issues/956) Enable linting in the project
38 changes: 30 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,28 @@ help: ## Display this help.

##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=k8ssandra-operator 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: golangci-lint ## Run golangci-lint against code.
$(GOLANGCI_LINT) run ./...

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet envtest ## Run tests.
test: manifests generate fmt vet lint envtest ## Run tests.
ifdef TEST
@echo Running test $(TEST)
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(GO_FLAGS) ./apis/... ./pkg/... ./test/yq/... ./controllers/... -run="$(TEST)" -covermode=atomic -coverprofile coverage.out
Expand Down Expand Up @@ -310,11 +318,13 @@ $(LOCALBIN):
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint # TODO Add linting to the GHA also

## Tool Versions
CERT_MANAGER_VERSION ?= v1.9.1
KUSTOMIZE_VERSION ?= v4.5.7
CONTROLLER_TOOLS_VERSION ?= v0.11.4
CERT_MANAGER_VERSION ?= v1.12.2
KUSTOMIZE_VERSION ?= v5.0.3
CONTROLLER_TOOLS_VERSION ?= v0.12.0
GOLINT_VERSION ?= 1.54.2

cert-manager: ## Install cert-manager to the cluster
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/$(CERT_MANAGER_VERSION)/cert-manager.yaml
Expand Down Expand Up @@ -370,17 +380,29 @@ create-clientconfig:
--dest-context kind-k8ssandra-0; \
done


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: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
$(KUSTOMIZE): $(LOCALBIN)
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/kustomize; \
fi
test -s $(LOCALBIN)/kustomize || { curl -s $(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: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
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)

.PHONY: golangci-lint
golangci-lint:
@if test -x $(LOCALBIN)/golangci-lint && ! $(LOCALBIN)/golangci-lint version | grep -q $(GOLINT_VERSION); then \
echo "$(LOCALBIN)/golangci-lint version is not expected $(GOLINT_VERSION). Removing it before installing."; \
rm -rf $(LOCALBIN)/golangci-lint; \
fi
test -s $(LOCALBIN)/golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v$(GOLINT_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/config.k8ssandra.io_clientconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: clientconfigs.config.k8ssandra.io
spec:
group: config.k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/control.k8ssandra.io_k8ssandratasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: k8ssandratasks.control.k8ssandra.io
spec:
group: control.k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/k8ssandra.io_k8ssandraclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: k8ssandraclusters.k8ssandra.io
spec:
group: k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/medusa.k8ssandra.io_medusabackupjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: medusabackupjobs.medusa.k8ssandra.io
spec:
group: medusa.k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/medusa.k8ssandra.io_medusabackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: medusabackups.medusa.k8ssandra.io
spec:
group: medusa.k8ssandra.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: medusabackupschedules.medusa.k8ssandra.io
spec:
group: medusa.k8ssandra.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: medusarestorejobs.medusa.k8ssandra.io
spec:
group: medusa.k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/medusa.k8ssandra.io_medusatasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: medusatasks.medusa.k8ssandra.io
spec:
group: medusa.k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/reaper.k8ssandra.io_reapers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: reapers.reaper.k8ssandra.io
spec:
group: reaper.k8ssandra.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: replicatedsecrets.replication.k8ssandra.io
spec:
group: replication.k8ssandra.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/stargate.k8ssandra.io_stargates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.4
controller-gen.kubebuilder.io/version: v0.12.0
name: stargates.stargate.k8ssandra.io
spec:
group: stargate.k8ssandra.io
Expand Down
6 changes: 0 additions & 6 deletions controllers/k8ssandra/add_dc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,6 @@ func verifyReplicationOfKeyspaceUpdated(t *testing.T, mockMgmtApi *testutils.Fak
}, timeout, interval, fmt.Sprintf("failed to verify replication for keyspace %s updated", keyspace))
}

func verifyKeyspaceReplicationAltered(t *testing.T, mockMgmtApi *testutils.FakeManagementApiFacade, keyspace string, replication map[string]int) {
require.Eventually(t, func() bool {
return mockMgmtApi.GetFirstCall(testutils.AlterKeyspace, keyspace, replication) > -1
}, timeout, interval, fmt.Sprintf("failed to verify replication for keyspace %s updated", keyspace))
}

func verifyRebuildTaskCreated(ctx context.Context, t *testing.T, f *framework.Framework, targetDcKey, srcDcKey framework.ClusterKey) {
t.Log("check that rebuild task was created")
require := require.New(t)
Expand Down
8 changes: 4 additions & 4 deletions controllers/k8ssandra/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func createSingleDcClusterNoAuth(t *testing.T, ctx context.Context, f *framework
Name: "cluster1",
},
Spec: api.K8ssandraClusterSpec{
Auth: pointer.BoolPtr(false),
Auth: pointer.Bool(false),
Cassandra: &api.CassandraClusterTemplate{
Datacenters: []api.CassandraDatacenterTemplate{{
Meta: api.EmbeddedObjectMeta{Name: "dc1"},
Expand Down Expand Up @@ -141,7 +141,7 @@ func createSingleDcClusterAuth(t *testing.T, ctx context.Context, f *framework.F
Name: "cluster1",
},
Spec: api.K8ssandraClusterSpec{
Auth: pointer.BoolPtr(true),
Auth: pointer.Bool(true),
Cassandra: &api.CassandraClusterTemplate{
Datacenters: []api.CassandraDatacenterTemplate{{
Meta: api.EmbeddedObjectMeta{Name: "dc1"},
Expand Down Expand Up @@ -255,7 +255,7 @@ func createSingleDcClusterAuthExternalSecrets(t *testing.T, ctx context.Context,
Name: "cluster1",
},
Spec: api.K8ssandraClusterSpec{
Auth: pointer.BoolPtr(true),
Auth: pointer.Bool(true),
Cassandra: &api.CassandraClusterTemplate{
Datacenters: []api.CassandraDatacenterTemplate{{
Meta: api.EmbeddedObjectMeta{Name: "dc1"},
Expand Down Expand Up @@ -383,7 +383,7 @@ func createSingleDcClusterExternalInternode(t *testing.T, ctx context.Context, f
Name: "cluster1",
},
Spec: api.K8ssandraClusterSpec{
Auth: pointer.BoolPtr(true),
Auth: pointer.Bool(true),
Cassandra: &api.CassandraClusterTemplate{
Datacenters: []api.CassandraDatacenterTemplate{{
Meta: api.EmbeddedObjectMeta{Name: "dc1"},
Expand Down
1 change: 0 additions & 1 deletion controllers/k8ssandra/cassandra_metrics_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func createSingleDcClusterWithMetricsAgent(t *testing.T, ctx context.Context, f
require.Eventually(f.DatacenterExists(ctx, dcKey), timeout, interval)
// Check that we have the right volumes and volume mounts.
dc := &cassdcapi.CassandraDatacenter{}
f.Get(ctx, dcKey, dc)
if err := f.Get(ctx, dcKey, dc); err != nil {
require.Fail("could not find dc")
}
Expand Down
20 changes: 8 additions & 12 deletions controllers/k8ssandra/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
testlogr "github.com/go-logr/logr/testing"
k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1"
k8ssandralabels "github.com/k8ssandra/k8ssandra-operator/pkg/labels"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -17,7 +18,7 @@ import (
)

func TestK8ssandraClusterReconciler_DeleteServices(t *testing.T) {
k8sMock := fake.NewFakeClient()
k8sMock := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
ctx := context.Background()
logger := testlogr.NewTestLogger(t)

Expand All @@ -38,28 +39,23 @@ func TestK8ssandraClusterReconciler_DeleteServices(t *testing.T) {
},
}

k8sMock.Create(ctx, service)
require.NoError(t, k8sMock.Create(ctx, service))

res := K8ssandraClusterReconciler{
Client: k8sMock,
Scheme: scheme.Scheme,
}

hasError := res.deleteServices(ctx, kc, k8ssandraapi.CassandraDatacenterTemplate{}, namespace, k8sMock, logger)

if hasError != false {
t.Errorf("Error while deleting services")
}
require.False(t, hasError, "Error while deleting services")

err := k8sMock.Get(ctx, client.ObjectKeyFromObject(service), service)
if err == nil || !errors.IsNotFound(err) {
t.Errorf("Service was not deleted: %v", err)
}

require.Error(t, err)
require.True(t, errors.IsNotFound(err))
}

func TestK8ssandraClusterReconciler_DeleteDeployments(t *testing.T) {
k8sMock := fake.NewFakeClient()
k8sMock := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
ctx := context.Background()
logger := testlogr.NewTestLogger(t)

Expand All @@ -80,7 +76,7 @@ func TestK8ssandraClusterReconciler_DeleteDeployments(t *testing.T) {
},
}

k8sMock.Create(ctx, deployment)
require.NoError(t, k8sMock.Create(ctx, deployment))

res := K8ssandraClusterReconciler{
Client: k8sMock,
Expand Down
Loading

0 comments on commit 18d0277

Please sign in to comment.