Commit Graph

35 Commits

Author SHA1 Message Date
Jordan Liggitt
43509b63d7 Bump hack/tools to github.com/golangci/golangci-lint 1.64.5 for go 1.24 2025-02-26 11:27:14 +01:00
Patrick Ohly
949385731f golangci-lint: remove "strict" checking
The corresponding "pull-kubernetes-verify-lint" job was already removed
earlier. Manual strict checking was still possible, but doesn't really make
sense for the same reasons why the job was removed (e.g. the decisions which
checks should be "strict" were too arbitrary).

The explanations for "hints" no longer end with "In general please prefer to
fix the error, ..." because that was misleading and only really applied to the
checks for existing code. For those checks we prefer to fix errors instead of
suppressing them, but not for hints.
2025-02-02 18:50:27 +01:00
Ed Bartosh
71b9114840 kubelet: Migrate pkg/kubelet/sysctl to contextual logging 2025-01-30 10:31:58 +02:00
Kubernetes Prow Robot
547654a8a1 Merge pull request #129813 from yongruilin/golangci-featuregate-add
feat: add a lint rule to prevent Add unversioned featuregate
2025-01-29 16:41:22 -08:00
Kubernetes Prow Robot
8294abc599 Merge pull request #128998 from bart0sh/PR165-migrate-oom-to-contextual-logging
kubelet: Migrate pkg/kubelet/oom to contextual logging
2025-01-28 13:33:22 -08:00
yongruilin
8a0937c034 feat: add a lint rule to prevent Add unversioned featuregate 2025-01-28 09:37:43 -08:00
Davanum Srinivas
4e05bc20db Linter to ensure go-cmp/cmp is used ONLY in tests
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
2025-01-24 20:49:14 -05:00
Patrick Ohly
7821abf2ae client-go/rest: finish conversion to contextual logging
The remaining calls can be converted without API changes.
2025-01-22 07:58:18 +01:00
Patrick Ohly
4638ba9716 client-go/tools/cache: add APIs with context parameter
The context is used for cancellation and to support contextual logging.

In most cases, alternative *WithContext APIs get added, except for
NewIntegerResourceVersionMutationCache where code searches indicate that the
API is not used downstream.

An API break around SharedInformer couldn't be avoided because the
alternative (keeping the interface unchanged and adding a second one with
the new method) would have been worse. controller-runtime needs to be updated
because it implements that interface in a test package. Downstream consumers of
controller-runtime will work unless they use those test package.

Converting Kubernetes to use the other new alternatives will follow. In the
meantime, usage of the new alternatives cannot be enforced via logcheck
yet (see https://github.com/kubernetes/kubernetes/issues/126379 for the
process).

Passing context through and checking it for cancellation is tricky for event
handlers. A better approach is to map the context cancellation to the normal
removal of an event handler via a helper goroutine. Thanks to the new
HandleErrorWithLogr and HandleCrashWithLogr, remembering the logger is
sufficient for handling problems at runtime.
2024-12-18 18:45:02 +01:00
Ed Bartosh
f622be0333 kubelet: Migrate pkg/kubelet/oom to contextual logging 2024-11-28 17:47:02 +02:00
Ed Bartosh
e5cb071c2e kubelet: Migrate CAdvisor to contextual logging 2024-11-06 01:49:20 +02:00
Oksana Baranova
49b88f1d8a kubelet: migrate clustertrustbundle, token to contextual logging
Signed-off-by: Oksana Baranova <oksana.baranova@intel.com>
2024-10-30 17:31:11 +02:00
Kubernetes Prow Robot
3d7c95f241 Merge pull request #127769 from mmorel-35/golangci-lint/usestdlibvars
fix: apply usestdlibvars linter with golangci strict and hints
2024-10-23 02:21:42 +01:00
Antonin Bas
7dc4af6c8a Ignore false positives from unused linter
This is a workaround for a false positive in the staticcheck linter,
which has not been addressed yet.
See https://github.com/dominikh/go-tools/issues/1294.
Once this is fixed in staticcheck, we can just remove the exclude rule
from the golangci config.

Signed-off-by: Antonin Bas <antonin.bas@broadcom.com>
2024-10-03 14:44:08 -07:00
Matthieu MOREL
25222cb812 fix: apply usestdlibvars linter with golangci strict and hints
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2024-10-01 08:36:50 +02:00
Matthieu MOREL
0d2ad0ee43 fix: clean up testifylint configuration in golangci-lint
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2024-09-29 11:45:21 +02:00
Matthieu MOREL
64e9fd50ed chore: disable require-error rule from testifylint
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2024-09-24 07:17:52 +02:00
Oksana Baranova
2474369227 kubelet: migrate pleg to contextual logging
Signed-off-by: Oksana Baranova <oksana.baranova@intel.com>
2024-09-13 12:13:26 +03:00
Ed Bartosh
e1bc8defac kubelet: Migrate DRA Manager to contextual logging
Co-authored-by: Patrick Ohly <patrick.ohly@intel.com>
2024-08-22 11:12:41 +03:00
bells17
1298c8a5fe csi-translation-lib: Support structured and contextual logging 2024-07-18 14:01:27 +09:00
Matthieu MOREL
0cde5f1e28 fix: enable bool-compare rule from testifylint linter (#125135)
* fix: enable bool-compare rule from testifylint linter

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

* Update hack/golangci.yaml.in

Co-authored-by: Patrick Ohly <patrick.ohly@intel.com>

* Update golangci.yaml.in

* Update golangci-strict.yaml

* Update golangci.yaml.in

* Update golangci.yaml.in

* Update golangci.yaml.in

* Update golangci.yaml.in

* Update golangci.yaml

* Update golangci-hints.yaml

* Update golangci-strict.yaml

* Update golangci.yaml.in

* Update golangci.yaml

* Update mux_test.go

---------

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Co-authored-by: Patrick Ohly <patrick.ohly@intel.com>
2024-06-28 10:58:05 -07:00
carlory
7149cb9f5a Revert "Revert "remove legacycloudproviders from staging""
This reverts commit d9a0be3b01.
2024-05-15 20:10:09 +08:00
carlory
d9a0be3b01 Revert "remove legacycloudproviders from staging"
This reverts commit 07c8d35681.
2024-05-14 13:39:13 +08:00
carlory
07c8d35681 remove legacycloudproviders from staging 2024-05-09 11:59:43 +08:00
bells17
318ea033d5 Include k8s.io components with contextual logging in logcheck.conf 2024-05-07 20:35:33 +09:00
bells17
1c917aa463 component-helpers: Support structured and contextual logging (#120637) 2024-04-24 03:06:15 -07:00
Patrick Ohly
5a130d2b71 apimachinery runtime: support contextual logging
In contrast to the original HandleError and HandleCrash, the new
HandleErrorWithContext and HandleCrashWithContext functions properly do contextual
logging, so if a problem occurs while e.g. dealing with a certain request and
WithValues was used for that request, then the error log entry will also
contain information about it.

The output changes from unstructured to structured, which might be a breaking
change for users who grep for panics. Care was taken to format panics
as similar as possible to the original output.

For errors, a message string gets added. There was none before, which made it
impossible to find all error output coming from HandleError.

Keeping HandleError and HandleCrash around without deprecating while changing
the signature of callbacks is a compromise between not breaking existing code
and not adding too many special cases that need to be supported. There is some
code which uses PanicHandlers or ErrorHandlers, but less than code that uses
the Handle* calls.

In Kubernetes, we want to replace the calls. logcheck warns about them in code
which is supposed to be contextual. The steps towards that are:
- add TODO remarks as reminder (this commit)
- locally remove " TODO(pohly): " to enable the check with `//logcheck:context`,
  merge fixes for linter warnings
- once there are none, remove the TODO to enable the check permanently
2024-03-26 17:28:45 +01:00
Patrick Ohly
8876b68a60 golangci-lint: add hints for error wrapping
Wrapping errors may or may not be the right thing to do (see
https://go.dev/blog/go1.13-errors#whether-to-wrap and the discussion in
https://github.com/kubernetes/kubernetes/issues/123234). But developers should
at least think about it, so let's emit linter hints for it: the golangci-lint
config by default enables it for go-errorlint, just not the linter itself, so
we just need to add it for the "hints" config.

Direct error comparisons and assertions also get checked. Those are typically
something that should be replaced by errors.Is and errors.As, but as the
existing code often doesn't do that, let's also treat those as just hints.
2024-02-13 14:12:04 +01:00
Ziqi Zhao
6b5e973e5f Migrate cmd/kube-proxy to contextual logging (#122197)
* cmd/kube-proxy support contextual logging

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>

* use ktesting.NewTestContext(t) in unit test

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>

* use ktesting.NewTestContext(t) in unit test

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>

* remove unnecessary blank line & add cmd/kube-proxy to contextual section in logcheck.conf

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>

* add more contextual logging

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>

* new lint yaml

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>

---------

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
2024-01-08 17:30:18 +01:00
Patrick Ohly
36cceff0a3 e2e: forbid usage of gomega.BeTrue/False
`BeTrue` without explanation in `Should` just prints "false is not true", which
isn't a useful failure message. Even with an explanation that message is still
printed, which is just noise. The new BeTrue/FalseBecause avoid that by
requiring that an explanation is provided and using that instead of "false is
not true".

In Kubernetes, we can enforce the usage of those better alternatives or the
if+Fail combination via forbidigo. Because we have existing code which still
needs to be updated, this can only be a hint for now.

Some examples found with this:

ERROR: test/e2e/apimachinery/protocol.go:75:20: use of `o.BeTrue` forbidden because "it does not produce a good failure message, use BeTrueBecause with an explicit printf-style failure message instead or plain Go: if ... { ginkgo.Fail(...) }" (forbidigo)
ERROR: 			o.Expect(ok).To(o.BeTrue())
ERROR: 			                ^

ERROR: test/e2e_node/unknown_pods_test.go:163:52: use of `gomega.BeTrue` forbidden because "it does not produce a good failure message, use BeTrueBecause with an explicit printf-style failure message instead or plain Go: if ... { ginkgo.Fail(...) }" (forbidigo)
ERROR: 			}, f.Timeouts.PodStart, f.Timeouts.Poll).Should(gomega.BeTrue())
ERROR: 			                                                ^

The solution might also be to write a custom matcher, but that is a bit hard to
explain in the forbidigo message.
2023-12-20 10:11:56 +01:00
Patrick Ohly
ced99383d5 logcheck: remove redundant entry for pkg/scheduler
Contextual implies structured, so listing pkg/scheduler as contextual is
enough.
2023-12-14 20:22:04 +01:00
Patrick Ohly
b450224c12 golangci-lint: inline logcheck configuration
This has the advantage that the golangci-lint cache gets invalidated
automatically each time the logcheck config changes.
2023-12-14 20:21:58 +01:00
Patrick Ohly
248100ce6d golangci-lint: tone down comment checking
39df946c06 was meant to enable stricter comment checking only for cmd/kubeadm
because the maintainers of that want that. However, the exclude rule didn't
capture all possible error texts and therefore some checks were enabled across
the entire code base.

The extended pattern is now based on the golangci-lint source code.

Also, the hint config didn't suppress any of these checks.
2023-11-01 14:59:28 +01:00
Patrick Ohly
39df946c06 golangci-lint: enable doc comment checking for cmd/kubeadm
Some code owners might want this for specific packages, like cmd/kubeadm.

This cannot be enabled for everything because:
- a lot of existing code doesn't pass (-> can't be in base config)
- a lot of packages don't need it (-> shouldn't even be a hint)
2023-10-25 12:30:28 +02:00
Patrick Ohly
ce9e668a93 golangci-lint: suppress one issue, demote others to "hints"
The voting in https://github.com/kubernetes/kubernetes/issues/117288 led to
one check that got rejected ("ifElseChain: rewrite if-else to switch
statement") and several that are "nice to know".

golangci-lint's support for issue "severity" is too limited to identify "nice
to know" issues in the output (filtering is only by linter without considering
the issue text; not part of text output). Therefore a third configuration gets
added which emits all issues (must fix and nits). The intention is to use
the "strict" configuration in pull-kubernetes-verify and the "hints"
configuration in a new non-blocking pull-kubernetes-linter-hints.

That way, "must fix" issues will block merging while issues that may be useful
will show up in a failed optional job. However, that job then also contains
"must fix" issues, partly because filtering out those would make the
configuration a lot larger and is likely to be unreliably (all "must fix"
issues would need to be identified and listed), partly because it may be useful
to have all issues in one place.

The previous approach of manually keeping two configs in sync with special
comments didn't scale to three configs. Now a single golangci.yaml.in with
text/template constructs contains the source for all three configs. A new
simple CLI frontend for text/template (cmd/gotemplate) is used by
hack/update-golangci-lint-config.sh to generate the three flavors.
2023-08-22 20:39:23 +02:00