Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏃 Update linter and lint fixes #773

Merged
merged 1 commit into from Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions .golangci.yml
@@ -1,5 +1,27 @@
linters:
disable-all: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to flip these. Enable all linters and explicitly disable the others, so we know which ones we are disabling and folks can pick up the task of removing a disabled linter from this list if they wanted to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well, it seems a bit counter intuitive to me, but nothing major

enable:
- misspell
- structcheck
- golint
- deadcode
- errcheck
- varcheck
- goconst
- unparam
- ineffassign
- nakedret
- interfacer
- gocyclo
- lll
- dupl
- goimports

linters-settings:
lll:
line-length: 170
dupl:
threshold: 400

run:
timeout: 5m
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 cool

2 changes: 1 addition & 1 deletion hack/check-everything.sh
Expand Up @@ -78,7 +78,7 @@ function golangci_lint_has_version {

function fetch_go_tools {
header_text "Checking for golangci-lint"
local golangci_lint_version="v1.21.0"
local golangci_lint_version="v1.23.6"
if ! is_installed golangci-lint || ! golangci_lint_has_version "${golangci_lint_version}"; then
header_text "Installing golangci-lint"
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin "${golangci_lint_version}"
Expand Down
26 changes: 1 addition & 25 deletions hack/verify.sh
Expand Up @@ -24,30 +24,6 @@ go vet ${MOD_OPT} ./...

header_text "running golangci-lint"

golangci-lint run --disable-all \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 cool

--deadline 5m \
--enable=misspell \
--enable=structcheck \
--enable=golint \
--enable=deadcode \
--enable=errcheck \
--enable=varcheck \
--enable=goconst \
--enable=unparam \
--enable=ineffassign \
--enable=nakedret \
--enable=interfacer \
--enable=misspell \
--enable=gocyclo \
--enable=lll \
--enable=dupl \
--enable=goimports \
--enable=bodyclose \
./pkg/... ./examples/... .

# TODO: Enable these as we fix them to make them pass
# --enable=gosec \
# --enable=maligned \
# --enable=safesql \
golangci-lint run ./pkg/... ./examples/... .
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following linters have been removed and are NOT enabled by default

--enable=misspell \	   
   --enable=golint \	
   --enable=goconst \	
   --enable=unparam \	
   --enable=nakedret \	
   --enable=interfacer \	
   --enable=gocyclo \	
   --enable=lll \	
   --enable=dupl \	
   --enable=goimports \

Is this intended? If we plan to keep them... it would be great to add them to the .golangci.yml file.

https://github.com/golangci/golangci-lint#enabled-by-default-linters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this was not my intention. I misinterpreted how dropping the --disable-all flag worked. I'll make sure to explicitly enable the same ones in the config file


GO111MODULES=on go list -mod=readonly ./...
6 changes: 2 additions & 4 deletions pkg/client/client_cache.go
Expand Up @@ -124,10 +124,8 @@ type resourceMeta struct {

// isNamespaced returns true if the type is namespaced
func (r *resourceMeta) isNamespaced() bool {
if r.mapping.Scope.Name() == meta.RESTScopeNameRoot {
return false
}
return true
return r.mapping.Scope.Name() != meta.RESTScopeNameRoot

}

// resource returns the resource name of the type
Expand Down
39 changes: 20 additions & 19 deletions pkg/client/fake/client_test.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package fake

import (
"context"
"encoding/json"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -85,7 +86,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns1",
}
obj := &appsv1.Deployment{}
err := cl.Get(nil, namespacedName, obj)
err := cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
Expect(obj).To(Equal(dep))
})
Expand All @@ -99,14 +100,14 @@ var _ = Describe("Fake client", func() {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion("apps/v1")
obj.SetKind("Deployment")
err := cl.Get(nil, namespacedName, obj)
err := cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
})

It("should be able to List", func() {
By("Listing all deployments in a namespace")
list := &appsv1.DeploymentList{}
err := cl.List(nil, list, client.InNamespace("ns1"))
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(2))
Expect(list.Items).To(ConsistOf(*dep, *dep2))
Expand All @@ -117,15 +118,15 @@ var _ = Describe("Fake client", func() {
list := &unstructured.UnstructuredList{}
list.SetAPIVersion("apps/v1")
list.SetKind("DeploymentList")
err := cl.List(nil, list, client.InNamespace("ns1"))
err := cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(2))
})

It("should support filtering by labels and their values", func() {
By("Listing deployments with a particular label and value")
list := &appsv1.DeploymentList{}
err := cl.List(nil, list, client.InNamespace("ns1"),
err := cl.List(context.Background(), list, client.InNamespace("ns1"),
client.MatchingLabels(map[string]string{
"test-label": "label-value",
}))
Expand Down Expand Up @@ -156,7 +157,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
},
}
err := cl.Create(nil, newcm)
err := cl.Create(context.Background(), newcm)
Expect(err).To(BeNil())

By("Getting the new configmap")
Expand All @@ -165,7 +166,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
}
obj := &corev1.ConfigMap{}
err = cl.Get(nil, namespacedName, obj)
err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
Expect(obj).To(Equal(newcm))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("1"))
Expand All @@ -187,7 +188,7 @@ var _ = Describe("Fake client", func() {
"test-key": "new-value",
},
}
err := cl.Update(nil, newcm)
err := cl.Update(context.Background(), newcm)
Expect(err).To(BeNil())

By("Getting the new configmap")
Expand All @@ -196,33 +197,33 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
}
obj := &corev1.ConfigMap{}
err = cl.Get(nil, namespacedName, obj)
err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
Expect(obj).To(Equal(newcm))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("2"))
})

It("should be able to Delete", func() {
By("Deleting a deployment")
err := cl.Delete(nil, dep)
err := cl.Delete(context.Background(), dep)
Expect(err).To(BeNil())

By("Listing all deployments in the namespace")
list := &appsv1.DeploymentList{}
err = cl.List(nil, list, client.InNamespace("ns1"))
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(HaveLen(1))
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should be able to Delete a Collection", func() {
By("Deleting a deploymentList")
err := cl.DeleteAllOf(nil, &appsv1.Deployment{}, client.InNamespace("ns1"))
err := cl.DeleteAllOf(context.Background(), &appsv1.Deployment{}, client.InNamespace("ns1"))
Expect(err).To(BeNil())

By("Listing all deployments in the namespace")
list := &appsv1.DeploymentList{}
err = cl.List(nil, list, client.InNamespace("ns1"))
err = cl.List(context.Background(), list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(BeEmpty())
})
Expand All @@ -236,7 +237,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
},
}
err := cl.Create(nil, newcm, client.CreateDryRunAll)
err := cl.Create(context.Background(), newcm, client.DryRunAll)
Expect(err).To(BeNil())

By("Getting the new configmap")
Expand All @@ -245,7 +246,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
}
obj := &corev1.ConfigMap{}
err = cl.Get(nil, namespacedName, obj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running the linter with staticcheck enabled (not enforced by this PR) it fails with

SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use (staticcheck)

https://staticcheck.io/docs/checks#SA1012

err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
Expect(obj).NotTo(Equal(newcm))
Expand All @@ -263,7 +264,7 @@ var _ = Describe("Fake client", func() {
"test-key": "new-value",
},
}
err := cl.Update(nil, newcm, client.UpdateDryRunAll)
err := cl.Update(context.Background(), newcm, client.DryRunAll)
Expect(err).To(BeNil())

By("Getting the new configmap")
Expand All @@ -272,7 +273,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns2",
}
obj := &corev1.ConfigMap{}
err = cl.Get(nil, namespacedName, obj)
err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
Expect(obj).To(Equal(cm))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(""))
Expand All @@ -289,7 +290,7 @@ var _ = Describe("Fake client", func() {
},
})
Expect(err).NotTo(HaveOccurred())
err = cl.Patch(nil, dep, client.RawPatch(types.StrategicMergePatchType, mergePatch))
err = cl.Patch(context.Background(), dep, client.RawPatch(types.StrategicMergePatchType, mergePatch))
Expect(err).NotTo(HaveOccurred())

By("Getting the patched deployment")
Expand All @@ -298,7 +299,7 @@ var _ = Describe("Fake client", func() {
Namespace: "ns1",
}
obj := &appsv1.Deployment{}
err = cl.Get(nil, namespacedName, obj)
err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).NotTo(HaveOccurred())
Expect(obj.Annotations["foo"]).To(Equal("bar"))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal("1"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controller_integration_test.go
Expand Up @@ -122,7 +122,7 @@ var _ = Describe("controller", func() {
By("Invoking Reconciling for Update")
newDeployment := deployment.DeepCopy()
newDeployment.Labels = map[string]string{"foo": "bar"}
newDeployment, err = clientset.AppsV1().Deployments("default").Update(newDeployment)
_, err = clientset.AppsV1().Deployments("default").Update(newDeployment)
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))

Expand Down Expand Up @@ -152,7 +152,7 @@ var _ = Describe("controller", func() {
By("Invoking Reconciling for an OwnedObject when it is updated")
newReplicaset := replicaset.DeepCopy()
newReplicaset.Labels = map[string]string{"foo": "bar"}
newReplicaset, err = clientset.AppsV1().ReplicaSets("default").Update(newReplicaset)
_, err = clientset.AppsV1().ReplicaSets("default").Update(newReplicaset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 cool

Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))

Expand Down
1 change: 0 additions & 1 deletion pkg/handler/eventhandler_test.go
Expand Up @@ -42,7 +42,6 @@ var _ = Describe("Eventhandler", func() {
t := true
BeforeEach(func() {
q = controllertest.Queue{Interface: workqueue.New()}
instance = handler.EnqueueRequestForObject{}
pod = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Namespace: "biz", Name: "baz"},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/controller/controller.go
Expand Up @@ -236,7 +236,7 @@ func (c *Controller) reconcileHandler(obj interface{}) bool {
// Update metrics after processing each item
reconcileStartTS := time.Now()
defer func() {
c.updateMetrics(time.Now().Sub(reconcileStartTS))
c.updateMetrics(time.Since(reconcileStartTS))
}()

var req reconcile.Request
Expand Down
2 changes: 1 addition & 1 deletion pkg/leaderelection/fake/leader_election.go
Expand Up @@ -81,7 +81,7 @@ func (f *ResourceLock) Update(ler resourcelock.LeaderElectionRecord) error {

// RecordEvent implements the ResourceLockInterface.
func (f *ResourceLock) RecordEvent(s string) {
return

}

// Identity implements the ResourceLockInterface.
Expand Down
2 changes: 2 additions & 0 deletions pkg/log/zap/zap_test.go
Expand Up @@ -58,6 +58,8 @@ type fakeLoggerRoot struct {
messages []logInfo
}

var _ logr.Logger = &fakeLogger{}

// fakeLogger is a fake implementation of logr.Logger that records
// messages, tags, and names,
// just records the name.
Expand Down
16 changes: 6 additions & 10 deletions pkg/manager/internal.go
Expand Up @@ -360,11 +360,9 @@ func (cm *controllerManager) serveMetrics(stop <-chan struct{}) {
}()

// Shutdown the server when stop is closed
select {
case <-stop:
if err := server.Shutdown(context.Background()); err != nil {
cm.errSignal.SignalError(err)
}
<-stop
if err := server.Shutdown(context.Background()); err != nil {
cm.errSignal.SignalError(err)
}
}

Expand Down Expand Up @@ -392,11 +390,9 @@ func (cm *controllerManager) serveHealthProbes(stop <-chan struct{}) {
cm.mu.Unlock()

// Shutdown the server when stop is closed
select {
case <-stop:
if err := server.Shutdown(context.Background()); err != nil {
cm.errSignal.SignalError(err)
}
<-stop
if err := server.Shutdown(context.Background()); err != nil {
cm.errSignal.SignalError(err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/signals/signal_test.go
Expand Up @@ -36,7 +36,7 @@ var _ = Describe("runtime signal", func() {
task := &Task{
ticker: time.NewTicker(time.Second * 2),
}
c := make(chan os.Signal)
c := make(chan os.Signal, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context on this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SA1017: the channel used with signal.Notify should be buffered
https://staticcheck.io/docs/checks#SA1017

You can see elsewhere in the codebase

c := make(chan os.Signal, 2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, prevents missing signals, ok.

signal.Notify(c, os.Interrupt)
task.wg.Add(1)
go func(c chan os.Signal) {
Expand Down
1 change: 0 additions & 1 deletion pkg/reconcile/example_test.go
Expand Up @@ -26,7 +26,6 @@ import (

// This example implements a simple no-op reconcile function that prints the object to be Reconciled.
func ExampleFunc() {
type Reconciler struct{}

r := reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
// Create your business logic to create, update, delete objects here.
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/server.go
Expand Up @@ -119,7 +119,7 @@ func (s *Server) Register(path string, hook http.Handler) {
func instrumentedHook(path string, hookRaw http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
startTS := time.Now()
defer func() { metrics.RequestLatency.WithLabelValues(path).Observe(time.Now().Sub(startTS).Seconds()) }()
defer func() { metrics.RequestLatency.WithLabelValues(path).Observe(time.Since(startTS).Seconds()) }()
hookRaw.ServeHTTP(resp, req)

// TODO(directxman12): add back in metric about total requests broken down by result?
Expand Down