Skip to content

Commit

Permalink
Automated cherry pick of #3016: Enable goimports, unused, deadcode, a…
Browse files Browse the repository at this point in the history
…nd varcheck linters in (#3039)

* Enable goimports, unused, deadcode, and varcheck linters in CI

* remove unused code

Co-authored-by: DanielZhangQD <36026334+DanielZhangQD@users.noreply.github.com>
  • Loading branch information
cofyc and DanielZhangQD authored Jul 30, 2020
1 parent e062366 commit cd8a176
Show file tree
Hide file tree
Showing 54 changed files with 72 additions and 250 deletions.
5 changes: 1 addition & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ run:

linters:
enable:
- gofmt
- goimports
disable:
# Enable once related errors are fixed or ignored explicitly
- staticcheck
- unused
- gosimple
- structcheck
- errcheck
- deadcode
- varcheck
4 changes: 4 additions & 0 deletions hack/tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export GO111MODULE=on
GOBIN ?= $(shell go env GOPATH)/bin
DEP_PROGS=\
$(GOBIN)/misspell\
$(GOBIN)/goimports\
$(GOBIN)/golangci-lint

.PHONY: all
Expand All @@ -38,3 +39,6 @@ $(GOBIN)/golangci-lint: deps

$(GOBIN)/misspell: deps
go install github.com/client9/misspell/cmd/misspell

$(GOBIN)/goimports: deps
go install golang.org/x/tools/cmd/goimports
1 change: 1 addition & 0 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.14
require (
github.com/client9/misspell v0.3.4
github.com/golangci/golangci-lint v1.29.0
golang.org/x/tools v0.0.0-20200724022722-7017fd6b1305 // indirect
)
2 changes: 2 additions & 0 deletions hack/tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ golang.org/x/tools v0.0.0-20200625211823-6506e20df31f/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200710042808-f1c4188a97a1 h1:rD1FcWVsRaMY+l8biE9jbWP5MS/CJJ/90a9TMkMgNrM=
golang.org/x/tools v0.0.0-20200710042808-f1c4188a97a1/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200724022722-7017fd6b1305 h1:yaM5S0KcY0lIoZo7Fl+oi91b/DdlU2zuWpfHrpWbCS0=
golang.org/x/tools v0.0.0-20200724022722-7017fd6b1305/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
1 change: 1 addition & 0 deletions hack/update-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ hack/update-test-codegen.sh
hack/update-openapi-spec.sh
hack/update-crd-groups.sh
hack/update-EOF.sh
hack/update-goimports.sh
29 changes: 29 additions & 0 deletions hack/update-goimports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# Copyright 2020 PingCAP, Inc.
#
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

ROOT=$(unset CDPATH && cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
cd $ROOT

pushd "${ROOT}/hack/tools" >/dev/null
GO111MODULE=on go install golang.org/x/tools/cmd/goimports
popd >/dev/null

find . -type f -name '*.go' -not \( \
-path '*/vendor/*' \
\) | xargs goimports -w
3 changes: 2 additions & 1 deletion misc/cmd/debug-launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
package main

import (
"github.com/pingcap/tidb-operator/pkg/tkctl/debug"
"log"
"os"

"github.com/pingcap/tidb-operator/pkg/tkctl/debug"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pingcap/v1alpha1/defaulting/tidbcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
package defaulting

import (
"testing"

. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"testing"
)

func TestSetTidbSpecDefault(t *testing.T) {
Expand Down
5 changes: 0 additions & 5 deletions pkg/apis/pingcap/v1alpha1/pd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ package v1alpha1

// initially copied from PD v3.0.6

const (
defaultEnableTelemetry = true
defaultDisableTelemetry = false
)

// PDConfig is the configuration of pd-server
// +k8s:openapi-gen=true
type PDConfig struct {
Expand Down
12 changes: 5 additions & 7 deletions pkg/apis/pingcap/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ import (

const (
// defaultHelperImage is default image of helper
defaultHelperImage = "busybox:1.26.2"
defaultTimeZone = "UTC"
defaultEnableTLSCluster = false
defaultEnableTLSClient = false
defaultExposeStatus = true
defaultSeparateSlowLog = true
defaultEnablePVReclaim = false
defaultHelperImage = "busybox:1.26.2"
defaultTimeZone = "UTC"
defaultExposeStatus = true
defaultSeparateSlowLog = true
defaultEnablePVReclaim = false
)

var (
Expand Down
7 changes: 0 additions & 7 deletions pkg/apiserver/storage/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

const (
Expand Down Expand Up @@ -95,12 +94,6 @@ func (o *objKey) objectMeta() metav1.ObjectMeta {
}
}

func (o *objKey) selector() (labels.Selector, error) {
return metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: o.labelMap(),
})
}

func (o *objKey) labelSelectorStr() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("%s=%s,%s=%s",
Expand Down
4 changes: 0 additions & 4 deletions pkg/apiserver/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
clientv1alpha1 "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/typed/pingcap/v1alpha1"
informerv1alpha1 "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions/pingcap/v1alpha1"
listerv1alpha1 "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"
Expand All @@ -39,8 +37,6 @@ import (

// store implements a ConfigMap backed storage.Interface
type store struct {
lister listerv1alpha1.DataResourceLister
informer informerv1alpha1.DataResourceInformer
client clientv1alpha1.DataResourceInterface
codec runtime.Codec
versioner storage.Versioner
Expand Down
2 changes: 2 additions & 0 deletions pkg/apiserver/storage/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ func (w *informerBasedWatcher) ResultChan() <-chan watch.Event {
// TODO: implementation
return w.resultChan
}

var _ = &informerBasedWatcher{}
3 changes: 2 additions & 1 deletion pkg/autoscaler/autoscaler/calculate/calculate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
package calculate

import (
. "github.com/onsi/gomega"
"testing"

. "github.com/onsi/gomega"
)

func TestCalculate(t *testing.T) {
Expand Down
14 changes: 0 additions & 14 deletions pkg/autoscaler/autoscaler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,3 @@ func genMetricsEndpoint(tac *v1alpha1.TidbClusterAutoScaler) (string, error) {
}
return fmt.Sprintf("http://%s-prometheus.%s.svc:9090", tac.Spec.Monitor.Name, tac.Spec.Monitor.Namespace), nil
}

func emptyStorageMetricsStatus(tac *v1alpha1.TidbClusterAutoScaler) {
for id, m := range tac.Status.TiKV.MetricsStatusList {
if m.Name == string(corev1.ResourceStorage) {
m.StoragePressure = nil
m.StoragePressureStartTime = nil
m.CapacityStorage = nil
m.AvailableStorage = nil
m.BaselineAvailableStorage = nil
tac.Status.TiKV.MetricsStatusList[id] = m
return
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func (tac *Controller) Run(workers int, stopCh <-chan struct{}) {

func (tac *Controller) worker() {
for tac.processNextWorkItem() {
// revive:disable:empty-block
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/controller/backup/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func (bkc *Controller) Run(workers int, stopCh <-chan struct{}) {
// worker runs a worker goroutine that invokes processNextWorkItem until the the controller's queue is closed
func (bkc *Controller) worker() {
for bkc.processNextWorkItem() {
// revive:disable:empty-block
}
}

Expand Down
20 changes: 1 addition & 19 deletions pkg/controller/backup_schedule_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ package controller

import (
"fmt"
"strings"

"k8s.io/klog"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions/pingcap/v1alpha1"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
corev1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog"
)

// BackupScheduleStatusUpdaterInterface is an interface used to update the BackupScheduleStatus associated with a BackupSchedule.
Expand Down Expand Up @@ -84,21 +81,6 @@ func (bss *realBackupScheduleStatusUpdater) UpdateBackupScheduleStatus(
return err
}

func (bss *realBackupScheduleStatusUpdater) recordBackupScheduleEvent(verb string, bs *v1alpha1.BackupSchedule, err error) {
bsName := bs.GetName()
if err == nil {
reason := fmt.Sprintf("Successful%s", strings.Title(verb))
msg := fmt.Sprintf("%s BackupSchedule %s successful",
strings.ToLower(verb), bsName)
bss.recorder.Event(bs, corev1.EventTypeNormal, reason, msg)
} else {
reason := fmt.Sprintf("Failed%s", strings.Title(verb))
msg := fmt.Sprintf("%s BackupSchedule %s failed error: %s",
strings.ToLower(verb), bsName, err)
bss.recorder.Event(bs, corev1.EventTypeWarning, reason, msg)
}
}

var _ BackupScheduleStatusUpdaterInterface = &realBackupScheduleStatusUpdater{}

// FakeBackupScheduleStatusUpdater is a fake BackupScheduleStatusUpdaterInterface
Expand Down
17 changes: 0 additions & 17 deletions pkg/controller/backup_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ package controller

import (
"fmt"
"strings"

"k8s.io/klog"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions/pingcap/v1alpha1"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
corev1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -80,21 +78,6 @@ func (bcu *realBackupConditionUpdater) Update(backup *v1alpha1.Backup, condition
return err
}

func (bcu *realBackupConditionUpdater) recordBackupEvent(verb string, backup *v1alpha1.Backup, err error) {
backupName := backup.GetName()
if err == nil {
reason := fmt.Sprintf("Successful%s", strings.Title(verb))
msg := fmt.Sprintf("%s Backup %s successful",
strings.ToLower(verb), backupName)
bcu.recorder.Event(backup, corev1.EventTypeNormal, reason, msg)
} else {
reason := fmt.Sprintf("Failed%s", strings.Title(verb))
msg := fmt.Sprintf("%s Backup %s failed error: %s",
strings.ToLower(verb), backupName, err)
bcu.recorder.Event(backup, corev1.EventTypeWarning, reason, msg)
}
}

var _ BackupConditionUpdaterInterface = &realBackupConditionUpdater{}

// FakeBackupConditionUpdater is a fake BackupConditionUpdaterInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func (bsc *Controller) Run(workers int, stopCh <-chan struct{}) {
// worker runs a worker goroutine that invokes processNextWorkItem until the the controller's queue is closed
func (bsc *Controller) worker() {
for bsc.processNextWorkItem() {
// revive:disable:empty-block
}
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/controller/configmap_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ConfigMapControlInterface manages configmaps used by TiDB clusters
Expand All @@ -43,7 +42,6 @@ type ConfigMapControlInterface interface {
}

type realConfigMapControl struct {
client client.Client
kubeCli kubernetes.Interface
recorder record.EventRecorder
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ var (
PodWebhookEnabled bool
)

const (
// defaultTiDBSlowLogImage is default image of tidb log tailer
defaultTiDBLogTailerImage = "busybox:1.26.2"
)

// RequeueError is used to requeue the item, this error type should't be considered as a real error
type RequeueError struct {
s string
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pv_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ package controller

import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"strings"

"github.com/pingcap/tidb-operator/pkg/label"
corev1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
coreinformers "k8s.io/client-go/informers/core/v1"
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/restore/restore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func (rsc *Controller) Run(workers int, stopCh <-chan struct{}) {
// worker runs a worker goroutine that invokes processNextWorkItem until the the controller's queue is closed
func (rsc *Controller) worker() {
for rsc.processNextWorkItem() {
// revive:disable:empty-block
}
}

Expand Down
Loading

0 comments on commit cd8a176

Please sign in to comment.