From f282274c8631df945ef5c1b70ad0c7f41a0881df Mon Sep 17 00:00:00 2001 From: Yifei Zhang Date: Fri, 24 Dec 2021 21:45:29 +0800 Subject: [PATCH] add gci linter check (#694) --- .github/workflows/ci.yaml | 2 +- .golangci.yaml | 47 +++++++++++++++++++ Makefile | 2 +- .../controller-manager.go | 2 + cmd/yurt-tunnel-agent/app/config/config.go | 4 +- cmd/yurt-tunnel-server/app/config/config.go | 6 +-- cmd/yurthub/app/config/config.go | 8 ++-- pkg/controller/certificates/csrapprover.go | 3 +- .../cmd/controller-manager/app/serve.go | 4 +- pkg/yurtctl/cmd/convert/convert.go | 1 - pkg/yurtctl/cmd/join/phases/join-edge-node.go | 2 +- pkg/yurtctl/util/kubernetes/util.go | 7 ++- pkg/yurthub/network/dummyif_linux.go | 3 +- .../handlerwrapper/localhostproxy/handler.go | 7 ++- pkg/yurttunnel/server/interceptor.go | 4 +- pkg/yurttunnel/server/serveraddr/addr.go | 21 ++++----- pkg/yurttunnel/util/util.go | 2 +- 17 files changed, 85 insertions(+), 40 deletions(-) create mode 100644 .golangci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 301d9624fbe..c8eab32520b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -45,7 +45,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: ${{ env.GOLANGCI_VERSION }} - args: --disable-all -E deadcode -E gofmt -E goimports -E ineffassign -E misspell -E vet --timeout=15m + args: -v markdownlint-misspell-shellcheck: runs-on: ubuntu-18.04 diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000000..576bdc94108 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,47 @@ +# This file contains all available configuration options +# with their default values. + +# options for analysis running +run: + # default concurrency is a available CPU number + concurrency: 4 + + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 15m + + # exit code when at least one issue was found, default is 1 + issues-exit-code: 1 + + # include test files or not, default is true + tests: true + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions + # default is "colored-line-number" + format: colored-line-number + + # print lines of code with issue, default is true + print-issued-lines: true + + # print linter name in the end of issue text, default is true + print-linter-name: true + + # make issues output unique by line, default is true + uniq-by-line: true + +# all available settings of specific linters +linters-settings: + gci: + local-prefixes: github.com/openyurtio + +linters: + disable-all: true + enable: + - deadcode + - gofmt + - goimports + - gci + - ineffassign + - misspell + - vet \ No newline at end of file diff --git a/Makefile b/Makefile index 014edf519d6..7106ddc129a 100644 --- a/Makefile +++ b/Makefile @@ -90,4 +90,4 @@ GOLINT_BIN=$(shell which golangci-lint) endif lint: install-golint ## Run go lint against code. - $(GOLINT_BIN) run --disable-all -E deadcode -E gofmt -E goimports -E ineffassign -E misspell -E vet --timeout=15m + $(GOLINT_BIN) run -v diff --git a/cmd/yurt-controller-manager/controller-manager.go b/cmd/yurt-controller-manager/controller-manager.go index 66ef80b5e79..e9405394ce8 100644 --- a/cmd/yurt-controller-manager/controller-manager.go +++ b/cmd/yurt-controller-manager/controller-manager.go @@ -27,8 +27,10 @@ import ( "time" "k8s.io/component-base/logs" + // load all the prometheus client-go plugin _ "k8s.io/component-base/metrics/prometheus/clientgo" + // for version metric registration _ "k8s.io/component-base/metrics/prometheus/version" diff --git a/cmd/yurt-tunnel-agent/app/config/config.go b/cmd/yurt-tunnel-agent/app/config/config.go index db153d49dc4..f7c96eb6dff 100644 --- a/cmd/yurt-tunnel-agent/app/config/config.go +++ b/cmd/yurt-tunnel-agent/app/config/config.go @@ -19,10 +19,10 @@ package config import ( "fmt" + "k8s.io/client-go/kubernetes" + "github.com/openyurtio/openyurt/pkg/projectinfo" "github.com/openyurtio/openyurt/pkg/yurttunnel/constants" - - "k8s.io/client-go/kubernetes" ) // Config is the main context object for yurttunel-agent diff --git a/cmd/yurt-tunnel-server/app/config/config.go b/cmd/yurt-tunnel-server/app/config/config.go index a86a0d9b224..84e7e2f298e 100644 --- a/cmd/yurt-tunnel-server/app/config/config.go +++ b/cmd/yurt-tunnel-server/app/config/config.go @@ -21,11 +21,11 @@ import ( "fmt" "net" - "github.com/openyurtio/openyurt/pkg/projectinfo" - "github.com/openyurtio/openyurt/pkg/yurttunnel/constants" - "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" + + "github.com/openyurtio/openyurt/pkg/projectinfo" + "github.com/openyurtio/openyurt/pkg/yurttunnel/constants" ) // Config is the main context object for yurttunel-server diff --git a/cmd/yurthub/app/config/config.go b/cmd/yurthub/app/config/config.go index 66cda497804..a08c1054b72 100644 --- a/cmd/yurthub/app/config/config.go +++ b/cmd/yurthub/app/config/config.go @@ -24,10 +24,6 @@ import ( "strings" "time" - yurtcorev1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/apis/apps/v1alpha1" - yurtclientset "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/clientset/versioned" - yurtinformers "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions" - yurtv1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions/apps/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" @@ -51,6 +47,10 @@ import ( "github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/serializer" "github.com/openyurtio/openyurt/pkg/yurthub/storage/factory" "github.com/openyurtio/openyurt/pkg/yurthub/util" + yurtcorev1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/apis/apps/v1alpha1" + yurtclientset "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/clientset/versioned" + yurtinformers "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions" + yurtv1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions/apps/v1alpha1" ) // YurtHubConfiguration represents configuration of yurthub diff --git a/pkg/controller/certificates/csrapprover.go b/pkg/controller/certificates/csrapprover.go index 5d22708695e..596fb819f73 100644 --- a/pkg/controller/certificates/csrapprover.go +++ b/pkg/controller/certificates/csrapprover.go @@ -28,7 +28,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/wait" - certinformer "k8s.io/client-go/informers/certificates/v1beta1" certv1beta1 "k8s.io/client-go/informers/certificates/v1beta1" "k8s.io/client-go/kubernetes" typev1beta1 "k8s.io/client-go/kubernetes/typed/certificates/v1beta1" @@ -138,7 +137,7 @@ func enqueueObj(wq workqueue.RateLimitingInterface, obj interface{}) { // NewCSRApprover creates a new YurtCSRApprover func NewCSRApprover( clientset kubernetes.Interface, - csrInformer certinformer.CertificateSigningRequestInformer) *YurtCSRApprover { + csrInformer certv1beta1.CertificateSigningRequestInformer) *YurtCSRApprover { wq := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()) csrInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ diff --git a/pkg/controller/kubernetes/cmd/controller-manager/app/serve.go b/pkg/controller/kubernetes/cmd/controller-manager/app/serve.go index 85e052fe572..4ac956533ad 100644 --- a/pkg/controller/kubernetes/cmd/controller-manager/app/serve.go +++ b/pkg/controller/kubernetes/cmd/controller-manager/app/serve.go @@ -35,7 +35,9 @@ import ( "k8s.io/apiserver/pkg/server/routes" componentbaseconfig "k8s.io/component-base/config" "k8s.io/component-base/metrics/legacyregistry" - _ "k8s.io/component-base/metrics/prometheus/workqueue" // for workqueue metric registration + + // for workqueue metric registration + _ "k8s.io/component-base/metrics/prometheus/workqueue" ) var ( diff --git a/pkg/yurtctl/cmd/convert/convert.go b/pkg/yurtctl/cmd/convert/convert.go index 1d761421cf5..79b3ead8fdf 100644 --- a/pkg/yurtctl/cmd/convert/convert.go +++ b/pkg/yurtctl/cmd/convert/convert.go @@ -26,7 +26,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" - batchv1 "k8s.io/api/batch/v1" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" diff --git a/pkg/yurtctl/cmd/join/phases/join-edge-node.go b/pkg/yurtctl/cmd/join/phases/join-edge-node.go index 6cad2dfe9b3..375aa329184 100644 --- a/pkg/yurtctl/cmd/join/phases/join-edge-node.go +++ b/pkg/yurtctl/cmd/join/phases/join-edge-node.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" + "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -45,7 +46,6 @@ import ( "github.com/openyurtio/openyurt/pkg/yurtctl/constants" "github.com/openyurtio/openyurt/pkg/yurtctl/util/edgenode" "github.com/openyurtio/openyurt/pkg/yurthub/util" - "github.com/pkg/errors" ) // NewEdgeNodePhase creates a yurtctl workflow phase that start kubelet on a edge node. diff --git a/pkg/yurtctl/util/kubernetes/util.go b/pkg/yurtctl/util/kubernetes/util.go index 59aee456844..ea920a0d1bb 100644 --- a/pkg/yurtctl/util/kubernetes/util.go +++ b/pkg/yurtctl/util/kubernetes/util.go @@ -36,7 +36,6 @@ import ( appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" @@ -154,7 +153,7 @@ func CreateConfigMapFromYaml(cliSet *kubernetes.Clientset, ns, cmTmpl string) er if err != nil { return err } - cm, ok := obj.(*v1.ConfigMap) + cm, ok := obj.(*corev1.ConfigMap) if !ok { return fmt.Errorf("fail to assert configmap: %v", err) } @@ -434,7 +433,7 @@ func YamlToObject(yamlContent []byte) (k8sruntime.Object, error) { } // LabelNode add a new label (=) to the given node -func LabelNode(cliSet *kubernetes.Clientset, node *v1.Node, key, val string) (*v1.Node, error) { +func LabelNode(cliSet *kubernetes.Clientset, node *corev1.Node, key, val string) (*corev1.Node, error) { node.Labels[key] = val newNode, err := cliSet.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}) if err != nil { @@ -444,7 +443,7 @@ func LabelNode(cliSet *kubernetes.Clientset, node *v1.Node, key, val string) (*v } // AnnotateNode add a new annotation (=) to the given node -func AnnotateNode(cliSet *kubernetes.Clientset, node *v1.Node, key, val string) (*v1.Node, error) { +func AnnotateNode(cliSet *kubernetes.Clientset, node *corev1.Node, key, val string) (*corev1.Node, error) { node.Annotations[key] = val newNode, err := cliSet.CoreV1().Nodes().Update(context.Background(), node, metav1.UpdateOptions{}) if err != nil { diff --git a/pkg/yurthub/network/dummyif_linux.go b/pkg/yurthub/network/dummyif_linux.go index 3f73a7c9997..f3306db96d8 100644 --- a/pkg/yurthub/network/dummyif_linux.go +++ b/pkg/yurthub/network/dummyif_linux.go @@ -21,9 +21,8 @@ import ( "net" "strings" - "k8s.io/klog/v2" - "github.com/vishvananda/netlink" + "k8s.io/klog/v2" ) type DummyInterfaceController interface { diff --git a/pkg/yurttunnel/handlerwrapper/localhostproxy/handler.go b/pkg/yurttunnel/handlerwrapper/localhostproxy/handler.go index ad9bd8bfe8b..7b72b5edc22 100644 --- a/pkg/yurttunnel/handlerwrapper/localhostproxy/handler.go +++ b/pkg/yurttunnel/handlerwrapper/localhostproxy/handler.go @@ -26,7 +26,6 @@ import ( "time" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/informers" @@ -125,15 +124,15 @@ func (plm *localHostProxyMiddleware) SetSharedInformerFactory(factory informers. return err } - plm.getNodesByIP = func(nodeIP string) ([]*v1.Node, error) { + plm.getNodesByIP = func(nodeIP string) ([]*corev1.Node, error) { objs, err := nodeInformer.Informer().GetIndexer().ByIndex(constants.NodeIPKeyIndex, nodeIP) if err != nil { return nil, err } - nodes := make([]*v1.Node, 0, len(objs)) + nodes := make([]*corev1.Node, 0, len(objs)) for _, obj := range objs { - if node, ok := obj.(*v1.Node); ok { + if node, ok := obj.(*corev1.Node); ok { nodes = append(nodes, node) } } diff --git a/pkg/yurttunnel/server/interceptor.go b/pkg/yurttunnel/server/interceptor.go index 7d8ff2ee01c..0fcbfa61590 100644 --- a/pkg/yurttunnel/server/interceptor.go +++ b/pkg/yurttunnel/server/interceptor.go @@ -28,12 +28,12 @@ import ( "sync" "time" - "github.com/openyurtio/openyurt/pkg/yurttunnel/constants" - "k8s.io/apimachinery/pkg/util/httpstream" "k8s.io/apiserver/pkg/util/flushwriter" "k8s.io/apiserver/pkg/util/wsstream" "k8s.io/klog/v2" + + "github.com/openyurtio/openyurt/pkg/yurttunnel/constants" ) var ( diff --git a/pkg/yurttunnel/server/serveraddr/addr.go b/pkg/yurttunnel/server/serveraddr/addr.go index 75ea2f2d524..fba2fc8fd2d 100644 --- a/pkg/yurttunnel/server/serveraddr/addr.go +++ b/pkg/yurttunnel/server/serveraddr/addr.go @@ -23,7 +23,6 @@ import ( "net" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/informers" @@ -51,7 +50,7 @@ func GetTunnelServerAddr(clientset kubernetes.Interface) (string, error) { return "", err } - dnsNames, ips, err := extractTunnelServerDNSandIPs(svc, []*v1.Endpoints{eps}, NodeListToNodes(nodeLst)) + dnsNames, ips, err := extractTunnelServerDNSandIPs(svc, []*corev1.Endpoints{eps}, NodeListToNodes(nodeLst)) if err != nil { return "", err } @@ -106,7 +105,7 @@ func GetYurttunelServerDNSandIP( return []string{}, []net.IP{}, err } - return extractTunnelServerDNSandIPs(svc, []*v1.Endpoints{eps}, NodeListToNodes(nodeLst)) + return extractTunnelServerDNSandIPs(svc, []*corev1.Endpoints{eps}, NodeListToNodes(nodeLst)) } // YurttunelServerAddrManager list the latest tunnel server resources, extract ips and dnsNames from them @@ -165,11 +164,11 @@ func YurttunnelServerAddrManager(factory informers.SharedInformerFactory) ([]str } // getTunnelServerResources get service, endpoints, and cloud nodes of tunnel server -func getTunnelServerResources(clientset kubernetes.Interface) (*v1.Service, *v1.Endpoints, *v1.NodeList, error) { +func getTunnelServerResources(clientset kubernetes.Interface) (*corev1.Service, *corev1.Endpoints, *corev1.NodeList, error) { var ( - svc *v1.Service - eps *v1.Endpoints - nodeLst *v1.NodeList + svc *corev1.Service + eps *corev1.Endpoints + nodeLst *corev1.NodeList err error ) // get x-tunnel-server-svc service @@ -202,7 +201,7 @@ func getTunnelServerResources(clientset kubernetes.Interface) (*v1.Service, *v1. } // extractTunnelServerDNSandIPs extract tunnel server dnsNames and ips from service and endpoints -func extractTunnelServerDNSandIPs(svc *v1.Service, eps []*v1.Endpoints, nodes []*v1.Node) ([]string, []net.IP, error) { +func extractTunnelServerDNSandIPs(svc *corev1.Service, eps []*corev1.Endpoints, nodes []*corev1.Node) ([]string, []net.IP, error) { var ( dnsNames = make([]string, 0) ips = make([]net.IP, 0) @@ -304,7 +303,7 @@ func getDNSandIPFromAnnotations(svc *corev1.Service) ([]string, []net.IP, error) } // getClusterIPDNSandIP gets the DNS names and IPs from the NodePort service -func getNodePortDNSandIP(nodes []*v1.Node) ([]string, []net.IP, error) { +func getNodePortDNSandIP(nodes []*corev1.Node) ([]string, []net.IP, error) { var ( dnsNames = make([]string, 0) ips = make([]net.IP, 0) @@ -350,8 +349,8 @@ func GetDefaultDomainsForSvc(ns, name string) []string { return domains } -func NodeListToNodes(nodeLst *v1.NodeList) []*v1.Node { - nodes := make([]*v1.Node, 0) +func NodeListToNodes(nodeLst *corev1.NodeList) []*corev1.Node { + nodes := make([]*corev1.Node, 0) for _, node := range nodeLst.Items { nodes = append(nodes, &node) } diff --git a/pkg/yurttunnel/util/util.go b/pkg/yurttunnel/util/util.go index e0586ba5a63..0b4db70a418 100644 --- a/pkg/yurttunnel/util/util.go +++ b/pkg/yurttunnel/util/util.go @@ -25,6 +25,7 @@ import ( "strings" "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus/promhttp" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -33,7 +34,6 @@ import ( "github.com/openyurtio/openyurt/pkg/profile" "github.com/openyurtio/openyurt/pkg/projectinfo" - "github.com/prometheus/client_golang/prometheus/promhttp" ) const (