generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logcheck: support banning non-contextual functions and methods
Sometimes there are APIs with different variants where one variant is fine in code which doesn't support contextual logging but should be replaced with some other variant if that is the goal. For example, k8s.io/apimachinery/pkg/util/runtime.HandleError is the old-style function. We cannot deprecate it because it works fine in most existing code. But the upcoming HandleErrorWithContext is what code in Kubernetes that has (or will be) converted to contextual logging should use. We also cannot forbid it through forbidigo, because then the logcheck.conf with its definition of which code is "contextual" would be ignored. Besides, maintaining a config for forbidigo would be continual effort that would need to be replicated by other projects consuming the package with these APIs. A better solution is to: - mark these non-contextual APIs through a `//logcheck:context // <why>` comment - detect those comments in logcheck - warn about using them in contextual code
- Loading branch information
Showing
6 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
logcheck/testdata/src/k8s.io/apimachinery/pkg/util/runtime/runtime.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// This fake package is created as package golang.org/x/tools/go/analysis/analysistest | ||
// expects test data dependency to be testdata/src | ||
package runtime | ||
|
||
import "context" | ||
|
||
//logcheck:context // Use HandleErrorWithContext instead in code which supports contextual logging. | ||
func HandleError(err error) { // want HandleError:"Use HandleErrorWithContext instead in code which supports contextual logging." | ||
} | ||
|
||
func HandleErrorWithContext(ctx context.Context, err error) { | ||
} | ||
|
||
//logcheck:xyz // want "unknown logcheck keyword in comment" | ||
func Foo() { | ||
} | ||
|
||
//logcheck:context | ||
func Bar() { // want Bar:"Bar should not be used in code which supports contextual logging." | ||
} | ||
|
||
type Handler interface { | ||
//logcheck:context//Use HandleErrorWithContext instead in code which supports contextual logging. | ||
HandleError(err error) // want HandleError:"Use HandleErrorWithContext instead in code which supports contextual logging." | ||
|
||
HandleErrorWithContext(ctx context.Context, err error) | ||
} | ||
|
||
func foo(ctx context.Context, err error) { | ||
HandleError(nil) // want "Use HandleErrorWithContext instead in code which supports contextual logging." | ||
HandleErrorWithContext(ctx, err) | ||
var h Handler | ||
h.HandleError(err) // want "Use HandleErrorWithContext instead in code which supports contextual logging." | ||
h.HandleErrorWithContext(ctx, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters