Skip to content

Commit

Permalink
Merge pull request #30 from ThomasK33/main
Browse files Browse the repository at this point in the history
Added wrapper for golangci-lint's module plugin system wrapper
  • Loading branch information
k8s-ci-robot authored Jul 19, 2024
2 parents c99b003 + 06ec8e1 commit 68c2a66
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module sigs.k8s.io/logtools
go 1.22

require (
github.com/golangci/plugin-module-register v0.1.1
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/tools v0.21.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/golangci/plugin-module-register v0.1.1 h1:TCmesur25LnyJkpsVrupv1Cdzo+2f7zX0H6Jkw1Ol6c=
github.com/golangci/plugin-module-register v0.1.1/go.mod h1:TTpqoB6KkwOJMV8u7+NyXMrkwwESJLOkfl9TxR1DGFc=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
Expand Down
60 changes: 60 additions & 0 deletions logcheck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Format strings are not allowed where plain strings are expected.
### structured logging calls

Key/value parameters for logging calls are checked:

- For each key there must be a value.
- Keys must be constant strings.

Expand Down Expand Up @@ -113,3 +114,62 @@ This check flags check whether name arguments are valid keys according to the

This checks detects the usage of deprecated `klog` helper functions such as `KObjs` and suggests
a suitable alternative to replace them with.

# Golangci-lint

Logcheck needs to be built as a plugin to golangci-lint to be executed as a
private linter. There are two plugin systems in golangci-lint, and the following
instructions apply to the [Module Plugin System](https://golangci-lint.run/plugins/module-plugins/)
(introduced since v1.57.0), which is a supported approach to run Logcheck in golangci-lint.

One will have to build a custom golangci-lint binary by doing:

(1) Create a `.custom-gcl.yml` file at the root of the repository if you have not
done so, add the following content:

```yaml
version: v1.59.1
plugins:
- module: "sigs.k8s.io/logtools"
import: "sigs.k8s.io/logtools/logcheck/gclplugin"
version: latest
```
(2) Add logcheck to the linter configuration file `.golangci.yaml`:

```yaml
# This config disables all other linters and only runs logcheck
# Configure the linters as all other linters. This is mostly
# for brevity.
linters:
disable-all: true
enable:
- logcheck
linters-settings:
custom:
logcheck:
type: "module"
description: structured logging checker
original-url: sigs.k8s.io/logtools/logcheck
settings:
check:
contextual: true
config: |
structured .*
contextual .*
```

(3) Build a custom golangci-lint binary with logcheck included:

```sh
golangci-lint custom
```

(4) Run the custom binary instead of golangci-lint:

```sh
# Arguments are the same as `golangci-lint`.
./custom-gcl run ./...
```
88 changes: 88 additions & 0 deletions logcheck/gclplugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2024 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.
*/

// Package gclplugin implements the golangci-lint's module plugin interface for logcheck to be used
// as a private linter in golangci-lint. See more details at
// https://golangci-lint.run/plugins/module-plugins/.
package gclplugin

import (
"bytes"
"encoding/json"
"fmt"

"github.com/golangci/plugin-module-register/register"
"golang.org/x/tools/go/analysis"
"sigs.k8s.io/logtools/logcheck/pkg"
)

func init() {
register.Plugin("logcheck", New)
}

type settings struct {
Check map[string]bool `json:"check"`
Config string `json:"config"`
}

// New Module Plugin System, see https://golangci-lint.run/plugins/module-plugins/.
func New(pluginSettings interface{}) (register.LinterPlugin, error) {
// We could manually parse the settings. This would involve several
// type assertions. Encoding as JSON and then decoding into our
// settings struct is easier.
//
// The downside is that format errors are less user-friendly.
var buffer bytes.Buffer
if err := json.NewEncoder(&buffer).Encode(pluginSettings); err != nil {
return nil, fmt.Errorf("encoding settings as internal JSON buffer: %v", err)
}
var s settings
decoder := json.NewDecoder(&buffer)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&s); err != nil {
return nil, fmt.Errorf("decoding settings from internal JSON buffer: %v", err)
}

return &LogcheckPlugin{settings: s}, nil
}

var _ register.LinterPlugin = (*LogcheckPlugin)(nil)

type LogcheckPlugin struct {
settings settings
}

// BuildAnalyzers implements register.LinterPlugin.
func (l *LogcheckPlugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
// Now create an analyzer and configure it.
analyzer, config := pkg.Analyser()

for check, enabled := range l.settings.Check {
if err := config.SetEnabled(check, enabled); err != nil {
// No need to wrap, the error is informative.
return nil, err
}
}

if err := config.ParseConfig(l.settings.Config); err != nil {
return nil, fmt.Errorf("parsing config: %v", err)
}

return []*analysis.Analyzer{analyzer}, nil
}

// GetLoadMode implements register.LinterPlugin.
func (l *LogcheckPlugin) GetLoadMode() string { return register.LoadModeSyntax }

0 comments on commit 68c2a66

Please sign in to comment.