Skip to content

Commit

Permalink
Merge zegl#438
Browse files Browse the repository at this point in the history
438: Introduce golangci-lint r=zegl a=atombrella

There are some commented out linters that create a fair share of warnings, e.g., `govet`. Don't know how you feel about fixing the warnings from them, before merging. 


Co-authored-by: Mads Jensen <[email protected]>
  • Loading branch information
bors[bot] and atombrella authored Feb 22, 2022
2 parents 2d3d45f + 1e26f6f commit 4cbc569
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
- run:
name: "Enforce Go Formatted Code"
command: "! go fmt github.com/zegl/kube-score/... 2>&1 | read"
- run:
name: "Validate lint"
command: |
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.44.2
golangci-lint run
workflows:
version: 2
Expand Down
29 changes: 29 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

linters:
disable-all: true
enable:
- deadcode
# - errcheck
- gosimple
# - govet
- ineffassign
# - staticcheck
- structcheck
- typecheck
- unused
- varcheck
- bodyclose
- contextcheck
- cyclop
- durationcheck
# - errname
- errorlint
- exportloopref
- goimports
- gosec
- gocritic

linters-settings:
cyclop:
max-complexity: 35

3 changes: 2 additions & 1 deletion cmd/kube-score/help_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExecName(t *testing.T) {
Expand Down
22 changes: 12 additions & 10 deletions cmd/kube-score/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func scoreFiles(binName string, args []string) error {

err := fs.Parse(args)
if err != nil {
return fmt.Errorf("failed to parse files: %s", err)
return fmt.Errorf("failed to parse files: %w", err)
}

if *printHelp {
Expand Down Expand Up @@ -183,37 +183,39 @@ Use "-" as filename to read from STDIN.`, execName(binName))
}

var exitCode int
if scoreCard.AnyBelowOrEqualToGrade(scorecard.GradeCritical) {
switch {
case scoreCard.AnyBelowOrEqualToGrade(scorecard.GradeCritical):
exitCode = 1
} else if *exitOneOnWarning && scoreCard.AnyBelowOrEqualToGrade(scorecard.GradeWarning) {
case *exitOneOnWarning && scoreCard.AnyBelowOrEqualToGrade(scorecard.GradeWarning):
exitCode = 1
} else {
default:
exitCode = 0
}

var r io.Reader

version := getOutputVersion(*outputVersion, *outputFormat)

if *outputFormat == "json" && version == "v1" {
switch {
case *outputFormat == "json" && version == "v1":
d, _ := json.MarshalIndent(scoreCard, "", " ")
w := bytes.NewBufferString("")
w.WriteString(string(d))
r = w
} else if *outputFormat == "json" && version == "v2" {
case *outputFormat == "json" && version == "v2":
r = json_v2.Output(scoreCard)
} else if *outputFormat == "human" && version == "v1" {
case *outputFormat == "human" && version == "v1":
termWidth, _, err := terminal.GetSize(int(os.Stdin.Fd()))
// Assume a width of 80 if it can't be detected
if err != nil {
termWidth = 80
}
r = human.Human(scoreCard, *verboseOutput, termWidth)
} else if *outputFormat == "ci" && version == "v1" {
case *outputFormat == "ci" && version == "v1":
r = ci.CI(scoreCard)
} else if *outputFormat == "sarif" {
case *outputFormat == "sarif":
r = sarif.Output(scoreCard)
} else {
default:
return fmt.Errorf("error: Unknown --output-format or --output-version")
}

Expand Down
3 changes: 2 additions & 1 deletion config/semver_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSemver(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func ParseFiles(cnf config.Configuration) (ks.AllTypes, error) {
}

// Convert to unix style newlines
fullFile = bytes.Replace(fullFile, []byte("\r\n"), []byte("\n"), -1)
fullFile = bytes.ReplaceAll(fullFile, []byte("\r\n"), []byte("\n"))

offset := 1 // Line numbers are 1 indexed

Expand Down
14 changes: 8 additions & 6 deletions renderer/human/human.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func Human(scoreCard *scorecard.Scorecard, verboseOutput int, termWidth int) io.
// Adjust to termsize
fmt.Fprintf(w, safeRepeat(" ", min(80, termWidth)-writtenHeaderChars-2))

if scoredObject.AnyBelowOrEqualToGrade(scorecard.GradeCritical) {
switch {
case scoredObject.AnyBelowOrEqualToGrade(scorecard.GradeCritical):
fmt.Fprintf(w, "💥\n")
} else if scoredObject.AnyBelowOrEqualToGrade(scorecard.GradeWarning) {
case scoredObject.AnyBelowOrEqualToGrade(scorecard.GradeWarning):
fmt.Fprintf(w, "🤔\n")
} else {
default:
fmt.Fprintf(w, "✅\n")
}

Expand All @@ -66,7 +67,8 @@ func outputHumanStep(card scorecard.TestScore, verboseOutput int, termWidth int)

var col color.Attribute

if card.Skipped || card.Grade >= scorecard.GradeAllOK {
switch {
case card.Skipped || card.Grade >= scorecard.GradeAllOK:
// Higher than or equal to --threshold-ok
col = color.FgGreen

Expand All @@ -75,10 +77,10 @@ func outputHumanStep(card scorecard.TestScore, verboseOutput int, termWidth int)
return w
}

} else if card.Grade >= scorecard.GradeWarning {
case card.Grade >= scorecard.GradeWarning:
// Higher than or equal to --threshold-warning
col = color.FgYellow
} else {
default:
// All lower than both --threshold-ok and --threshold-warning are critical
col = color.FgRed
}
Expand Down
1 change: 1 addition & 0 deletions renderer/json_v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"io"

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

ks "github.com/zegl/kube-score/domain"
Expand Down
2 changes: 1 addition & 1 deletion score/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewCheck(name, targetType, comment string, optional bool) ks.Check {

func machineFriendlyName(in string) string {
in = strings.ToLower(in)
in = strings.Replace(in, " ", "-", -1)
in = strings.ReplaceAll(in, " ", "-")
return in
}

Expand Down
9 changes: 5 additions & 4 deletions score/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func containerResources(requireCPULimit bool, requireMemoryLimit bool) func(core
}
}

if len(allContainers) == 0 {
switch {
case len(allContainers) == 0:
score.Grade = scorecard.GradeCritical
score.AddComment("", "No containers defined", "")
} else if hasMissingLimit {
case hasMissingLimit:
score.Grade = scorecard.GradeCritical
} else if hasMissingRequest {
case hasMissingRequest:
score.Grade = scorecard.GradeWarning
} else {
default:
score.Grade = scorecard.GradeAllOK
}

Expand Down
3 changes: 2 additions & 1 deletion score/container/container_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package container

import (
"k8s.io/apimachinery/pkg/api/resource"
"testing"

"k8s.io/apimachinery/pkg/api/resource"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion score/disruptionbudget/disruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func hasMatching(budgets []ks.PodDisruptionBudget, namespace string, labels map[

selector, err := metav1.LabelSelectorAsSelector(budget.PodDisruptionBudgetSelector())
if err != nil {
return false, fmt.Errorf("failed to create selector: %v", err)
return false, fmt.Errorf("failed to create selector: %w", err)
}

if selector.Matches(internal.MapLables(labels)) {
Expand Down
3 changes: 2 additions & 1 deletion score/disruptionbudget/disruptionbudget_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package disruptionbudget

import (
"testing"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
"testing"

"github.com/zegl/kube-score/scorecard"
)
Expand Down
3 changes: 2 additions & 1 deletion score/filelocation_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package score

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

"github.com/zegl/kube-score/config"
ks "github.com/zegl/kube-score/domain"
)
Expand Down
3 changes: 2 additions & 1 deletion score/hpa/hpa_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hpa

import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/autoscaling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

"github.com/zegl/kube-score/domain"
"github.com/zegl/kube-score/scorecard"
Expand Down
3 changes: 2 additions & 1 deletion score/meta/labels_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package meta

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

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

"github.com/zegl/kube-score/domain"
Expand Down
9 changes: 5 additions & 4 deletions score/networkpolicy/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ func podHasNetworkPolicy(allNetpols []ks.NetworkPolicy) func(spec corev1.PodTemp
}
}

if hasMatchingEgressNetpol && hasMatchingIngressNetpol {
switch {
case hasMatchingEgressNetpol && hasMatchingIngressNetpol:
score.Grade = scorecard.GradeAllOK
} else if hasMatchingEgressNetpol && !hasMatchingIngressNetpol {
case hasMatchingEgressNetpol && !hasMatchingIngressNetpol:
score.Grade = scorecard.GradeWarning
score.AddComment("", "The pod does not have a matching ingress NetworkPolicy", "Add a ingress policy to the pods NetworkPolicy")
} else if hasMatchingIngressNetpol && !hasMatchingEgressNetpol {
case hasMatchingIngressNetpol && !hasMatchingEgressNetpol:
score.Grade = scorecard.GradeWarning
score.AddComment("", "The pod does not have a matching egress NetworkPolicy", "Add a egress policy to the pods NetworkPolicy")
} else {
default:
score.Grade = scorecard.GradeCritical
score.AddComment("", "The pod does not have a matching NetworkPolicy", "Create a NetworkPolicy that targets this pod to control who/what can communicate with this pod. Note, this feature needs to be supported by the CNI implementation used in the Kubernetes cluster to have an effect.")
}
Expand Down
3 changes: 2 additions & 1 deletion score/networkpolicy/networkpolicy_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package networkpolicy

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

"github.com/zegl/kube-score/domain"
"github.com/zegl/kube-score/scorecard"
Expand Down
9 changes: 0 additions & 9 deletions score/score_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package score

import (
"io"
"os"
"testing"

Expand Down Expand Up @@ -60,14 +59,6 @@ func testExpectedScore(t *testing.T, filename string, testcase string, expectedS
}, testcase, expectedScore)
}

type unnamedReader struct {
io.Reader
}

func (unnamedReader) Name() string {
return ""
}

func TestPodContainerNoResources(t *testing.T) {
t.Parallel()
testExpectedScore(t, "pod-test-resources-none.yaml", "Container Resources", scorecard.GradeCritical)
Expand Down
3 changes: 2 additions & 1 deletion score/stable/stable_version_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package stable

import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"

"github.com/zegl/kube-score/config"
ks "github.com/zegl/kube-score/domain"
Expand Down
3 changes: 2 additions & 1 deletion scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package scorecard

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"

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

ks "github.com/zegl/kube-score/domain"
)

Expand Down

0 comments on commit 4cbc569

Please sign in to comment.