Skip to content

Commit

Permalink
Add golangci-lint on GitHub Actions with fixes (#1179)
Browse files Browse the repository at this point in the history
- source code lint with golangci-lint
- ignore low-priority errchecks (for now)
- add GitHub actions for linting CI

Signed-off-by: Wilson E. Husin <whusin@vmware.com>
  • Loading branch information
wilsonehusin authored Oct 27, 2020
1 parent 883fc90 commit 7419e1d
Show file tree
Hide file tree
Showing 47 changed files with 116 additions and 127 deletions.
8 changes: 8 additions & 0 deletions .errcheck.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/imdario/mergo.Merge
github.com/spf13/viper.BindEnv
(github.com/spf13/pflag.Value).Set
(*github.com/briandowns/spinner.Spinner).Color
(*github.com/spf13/cobra.Command).MarkFlagRequired
(*github.com/spf13/cobra.Command).Help
(*github.com/spf13/pflag.FlagSet).MarkHidden
(*github.com/spf13/pflag.FlagSet).Set
12 changes: 12 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: golangci-lint
on: [push, pull_request]
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint run
uses: golangci/golangci-lint-action@v2
with:
version: v1.31
5 changes: 5 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
tests: false
linters-settings:
errcheck:
exclude: ./.errcheck.list
10 changes: 5 additions & 5 deletions cmd/sonobuoy/app/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func AddSystemdLogsImage(image *string, flags *pflag.FlagSet) {
// AddKubeConformanceImageVersion initialises an image version flag.
func AddKubeConformanceImageVersion(imageVersion *image.ConformanceImageVersion, flags *pflag.FlagSet) {
help := "Use default Conformance image, but override the version. "
help += fmt.Sprintf("Default is 'auto', which will be set to your cluster's version if detected, erroring otherwise.")
help += "Default is 'auto', which will be set to your cluster's version if detected, erroring otherwise."

*imageVersion = image.ConformanceImageVersionAuto
flags.Var(imageVersion, "kube-conformance-image-version", help)
Expand Down Expand Up @@ -309,7 +309,7 @@ func AddShowDefaultPodSpecFlag(flag *bool, flags *pflag.FlagSet) {
func AddWaitOutputFlag(mode *WaitOutputMode, flags *pflag.FlagSet, defaultMode WaitOutputMode) {
*mode = defaultMode
flags.Var(
mode, "wait-output",
mode, waitOutputFlag,
"Whether to enable spinner on Sonobuoy. Valid modes are Silent and Spinner")
}

Expand All @@ -328,7 +328,7 @@ func AddImagePullPolicyFlag(policy *ImagePullPolicy, flags *pflag.FlagSet) {
func AddSSHKeyPathFlag(path *string, flags *pflag.FlagSet) {
flags.StringVar(
path, "ssh-key", "",
fmt.Sprintf("Path to the private key enabling SSH to cluster nodes."),
"Path to the private key enabling SSH to cluster nodes.",
)
}

Expand All @@ -337,7 +337,7 @@ func AddSSHKeyPathFlag(path *string, flags *pflag.FlagSet) {
func AddSSHUserFlag(user *string, flags *pflag.FlagSet) {
flags.StringVar(
user, "ssh-user", "",
fmt.Sprintf("SSH user for ssh-key."),
"SSH user for ssh-key.",
)
}

Expand All @@ -363,7 +363,7 @@ func AddPluginListFlag(p *[]string, flags *pflag.FlagSet) {
func AddKubernetesVersionFlag(version *string, flags *pflag.FlagSet) {
flags.StringVar(
version, "kubernetes-version", "",
fmt.Sprintf("Version to assume for Kubernetes. If empty, the cluster will be queried for its version"),
"Version to assume for Kubernetes. If empty, the cluster will be queried for its version",
)
}

Expand Down
7 changes: 0 additions & 7 deletions cmd/sonobuoy/app/common.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package app

import (
"time"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
sonodynamic "github.com/vmware-tanzu/sonobuoy/pkg/dynamic"

"github.com/pkg/errors"
"k8s.io/client-go/rest"
)

var (
spinnerType int = 14
spinnerDuration time.Duration = 2000 * time.Millisecond
spinnerColor = "red"
)

func getSonobuoyClient(cfg *rest.Config) (*client.SonobuoyClient, error) {
var skc *sonodynamic.APIHelper
var err error
Expand Down
4 changes: 2 additions & 2 deletions cmd/sonobuoy/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"os"
"time"

"github.com/vmware-tanzu/sonobuoy/pkg/client"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
)

var deleteFlags struct {
Expand Down
1 change: 0 additions & 1 deletion cmd/sonobuoy/app/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type genFlags struct {
sshUser string
kubeConformanceImageVersion imagepkg.ConformanceImageVersion
imagePullPolicy ImagePullPolicy
e2eRepoList string
timeoutSeconds int
showDefaultPodSpec bool

Expand Down
2 changes: 1 addition & 1 deletion cmd/sonobuoy/app/gen_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"os"

"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
)

// NewCmdGenConfig creates the `config` command which will print out
Expand Down
1 change: 0 additions & 1 deletion cmd/sonobuoy/app/pluginList.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (p *pluginList) String() string {
pluginNames := make(
[]string,
len(p.DynamicPlugins)+len(p.StaticPlugins),
len(p.DynamicPlugins)+len(p.StaticPlugins),
)
for i := range p.StaticPlugins {
pluginNames[i] = p.StaticPlugins[i].SonobuoyConfig.PluginName
Expand Down
3 changes: 3 additions & 0 deletions cmd/sonobuoy/app/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func result(input resultsInput) error {
// capabilities.
r, cleanup, err := getReader(input.archive)
defer cleanup()
if err != nil {
lastErr = err
}

err = printSinglePlugin(input, r)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/sonobuoy/app/sonobuoyconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"encoding/json"
"io/ioutil"

"github.com/vmware-tanzu/sonobuoy/pkg/config"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/vmware-tanzu/sonobuoy/pkg/config"
)

// SonobuoyConfig is a config.Config that implements pflag.Value from a file path
Expand Down
12 changes: 6 additions & 6 deletions cmd/sonobuoy/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package app
import (
"fmt"

"github.com/vmware-tanzu/sonobuoy/pkg/buildinfo"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/sonobuoy/pkg/buildinfo"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
)

type versionFlags struct {
Expand Down Expand Up @@ -52,10 +52,10 @@ func runVersion(cmd *cobra.Command, args []string) {
return
}

fmt.Println(fmt.Sprintf("Sonobuoy Version: %s", buildinfo.Version))
fmt.Println(fmt.Sprintf("MinimumKubeVersion: %s", buildinfo.MinimumKubeVersion))
fmt.Println(fmt.Sprintf("MaximumKubeVersion: %s", buildinfo.MaximumKubeVersion))
fmt.Println(fmt.Sprintf("GitSHA: %s", buildinfo.GitSHA))
fmt.Printf("Sonobuoy Version: %s\n", buildinfo.Version)
fmt.Printf("MinimumKubeVersion: %s\n", buildinfo.MinimumKubeVersion)
fmt.Printf("MaximumKubeVersion: %s\n", buildinfo.MaximumKubeVersion)
fmt.Printf("GitSHA: %s\n", buildinfo.GitSHA)

// Get Kubernetes version, this is last so that the regular version information
// will be shown even if the API server cannot be contacted and throws an error
Expand Down
7 changes: 4 additions & 3 deletions cmd/sonobuoy/app/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ func runGatherGlobal(cmd *cobra.Command, args []string) error {
// for the specified amount of seconds.
func runGatherSingleNode(sleep *int64) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
err := runGather(false)

if err := runGather(false); err != nil {
return err
}
switch {
case sleep == nil || *sleep == 0:
// No sleep.
Expand All @@ -148,7 +149,7 @@ func runGatherSingleNode(sleep *int64) func(cmd *cobra.Command, args []string) e
logrus.Infof("Results transmitted to aggregator. Sleeping for %v seconds", *sleep)
time.Sleep(time.Duration(*sleep) * time.Second)
}
return err
return nil
}
}

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down
1 change: 0 additions & 1 deletion pkg/backplane/ca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
)

const (
rsaBits = 2048
validFor = 48 * time.Hour
caName = "sonobuoy-ca"
)
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func (c *SonobuoyClient) Delete(cfg *DeleteConfig) error {
}

if strings.Compare(cfg.WaitOutput, spinnerMode) == 0 {
var s *spinner.Spinner
s = getSpinnerInstance()
var s *spinner.Spinner = getSpinnerInstance()
s.Start()
defer s.Stop()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"sort"
"strings"

"github.com/pkg/errors"
"github.com/vmware-tanzu/sonobuoy/pkg/client/results"
"github.com/vmware-tanzu/sonobuoy/pkg/client/results/e2e"
"github.com/pkg/errors"
)

// GetTests extracts the junit results from a sonobuoy archive and returns the requested tests.
Expand Down
9 changes: 0 additions & 9 deletions pkg/client/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,6 @@ func checkPluginsUnique(plugins []*manifest.Manifest) error {
return nil
}

func includes(set []plugin.Selection, s string) bool {
for _, v := range set {
if s == v.Name {
return true
}
}
return false
}

// mergeEnv will combine the values from two env var sets with priority being
// given to values in the first set in case of collision. Afterwards, any env
// var with a name in the removal set will be removed.
Expand Down
5 changes: 0 additions & 5 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ import (
"k8s.io/client-go/rest"
)

// ConfigValidator allows the command configurations to be validated.
type validator interface {
Validate() error
}

// LogConfig are the input options for viewing a Sonobuoy run's logs.
type LogConfig struct {
// Follow determines if the logs should be followed or not (tail -f).
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"testing"
)

// ConfigValidator allows the command configurations to be validated.
type validator interface {
Validate() error
}

func TestConfigValidation(t *testing.T) {
testcases := []struct {
desc string
Expand Down
14 changes: 6 additions & 8 deletions pkg/client/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,12 @@ func watchPodsToStreamLogs(client kubernetes.Interface, cfg *LogConfig, podCh ch

go func() {
for {
select {
case v := <-ch:
if v.Type == watch.Added && v.Object != nil {
switch t := v.Object.(type) {
case *v1.Pod:
podCh <- t
default:
}
v := <-ch
if v.Type == watch.Added && v.Object != nil {
switch t := v.Object.(type) {
case *v1.Pod:
podCh <- t
default:
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions pkg/client/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,10 @@ import (
"k8s.io/client-go/kubernetes"
)

const (
kubeSystemNamespace = "kube-system"
kubeDNSLabelKey = "k8s-app"
kubeDNSLabelValue = "kube-dns"
coreDNSLabelValue = "coredns"
)

var (
minimumKubeVersion = version.Must(version.NewVersion(buildinfo.MinimumKubeVersion))
maximumKubeVersion = version.Must(version.NewVersion(buildinfo.MaximumKubeVersion))

expectedDNSLabels = []string{
kubeDNSLabelValue,
coreDNSLabelValue,
}

preflightChecks = []func(kubernetes.Interface, *PreflightConfig) error{
preflightDNSCheck,
preflightVersionCheck,
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/results/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func manualResultsAggregation(items ...Item) string {
parts = append(parts, fmt.Sprintf("%v: %v", k, results[k]))
}

return fmt.Sprintf(strings.Join(parts, ", "))
return strings.Join(parts, ", ")
}

// aggregateStatus defines the aggregation rules for status. Failures bubble
Expand Down Expand Up @@ -232,7 +232,7 @@ func isFailureStatus(s string) bool {
// passed/failed (if junit) or record what files were produced (if raw) and return
// that information in an Item object. All errors encountered are returned.
func PostProcessPlugin(p plugin.Interface, dir string) (Item, []error) {
i := Item{}
var i Item
var errs []error

switch p.GetResultFormat() {
Expand Down
5 changes: 1 addition & 4 deletions pkg/client/results/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ const (
// Is written into the ErrorsDir directory.
DefaultErrFile = "error.json"

hostsDir = "hosts/"
namespacedResourcesDir = "resources/ns/"
nonNamespacedResourcesDir = "resources/cluster/"
podLogs = "podlogs/"
metadataDir = "meta/"
defaultServicesFile = "Services.json"
defaultNodesFile = "Nodes.json"
defaultServerVersionFile = "serverversion.json"
defaultServerGroupsFile = "servergroups.json"
Expand Down Expand Up @@ -375,7 +372,7 @@ func (r *Reader) FileReader(filename string) (io.Reader, error) {
found := false
err := r.WalkFiles(
func(path string, info os.FileInfo, err error) error {
if err != nil || found == true {
if err != nil || found {
return err
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error {
seenStatus = true
}

// if nil below was added for coverage on staticcheck
// TODO: ensure this is the desired behavior
if status == nil {
return false, nil
}

switch {
case status.Status == aggregation.CompleteStatus:
return true, nil
Expand All @@ -131,8 +137,7 @@ func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error {
}

if strings.Compare(cfg.WaitOutput, spinnerMode) == 0 {
var s *spinner.Spinner
s = getSpinnerInstance()
var s *spinner.Spinner = getSpinnerInstance()
s.Start()
defer s.Stop()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package client
import (
"os"

"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
"github.com/pkg/errors"
"github.com/vmware-tanzu/sonobuoy/pkg/errlog"
)

// Version gets the Kubernetes API version
Expand Down
Loading

0 comments on commit 7419e1d

Please sign in to comment.