From 7dee95546ee6c8ba3acd7a348bf40d7ef90e30a2 Mon Sep 17 00:00:00 2001 From: James Blair Date: Fri, 22 Mar 2024 03:50:06 +1300 Subject: [PATCH 1/2] Add new workflow for static analysis checks. Signed-off-by: James Blair --- .github/workflows/static-analysis.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/workflows/static-analysis.yaml diff --git a/.github/workflows/static-analysis.yaml b/.github/workflows/static-analysis.yaml new file mode 100644 index 0000000..7e0e813 --- /dev/null +++ b/.github/workflows/static-analysis.yaml @@ -0,0 +1,16 @@ +--- +name: Static analysis +on: [push, pull_request] +permissions: read-all +jobs: + run: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: 1.21 + - name: golangci-lint + uses: golangci/golangci-lint-action@v4 + with: + version: v1.57.1 From 1ea9209a40ce9274b0eae90e36b783826c648c3c Mon Sep 17 00:00:00 2001 From: James Blair Date: Fri, 22 Mar 2024 19:22:58 +1300 Subject: [PATCH 2/2] Correct lint errors reported by golangci-lint. Signed-off-by: James Blair --- cmd/decode.go | 5 ++--- cmd/decode_test.go | 4 ++-- cmd/extract.go | 6 +++--- cmd/root.go | 2 -- pkg/data/data.go | 22 +++------------------- pkg/encoding/encoding.go | 8 ++++++-- pkg/encoding/encoding_test.go | 2 +- pkg/encoding/scheme.go | 1 + 8 files changed, 18 insertions(+), 32 deletions(-) diff --git a/cmd/decode.go b/cmd/decode.go index e139235..3c67749 100644 --- a/cmd/decode.go +++ b/cmd/decode.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" "io" - "io/ioutil" "os" "bufio" @@ -181,7 +180,7 @@ func run(metaOnly bool, outMediaType string, in []byte, out io.Writer) error { // Readinput reads command line input, either from a provided input file or from stdin. func readInput(inputFilename string) ([]byte, error) { if inputFilename != "" { - data, err := ioutil.ReadFile(inputFilename) + data, err := os.ReadFile(inputFilename) if err != nil { return nil, fmt.Errorf("error reading input file %s: %v", inputFilename, err) } @@ -197,7 +196,7 @@ func readInput(inputFilename string) ([]byte, error) { fmt.Fprintln(os.Stderr, "warn: waiting on stdin from tty") } - stdin, err := ioutil.ReadAll(os.Stdin) + stdin, err := io.ReadAll(os.Stdin) if err != nil { return nil, fmt.Errorf("unable to read data from stdin: %v", err) } diff --git a/cmd/decode_test.go b/cmd/decode_test.go index cc3619c..9a15e42 100644 --- a/cmd/decode_test.go +++ b/cmd/decode_test.go @@ -18,7 +18,7 @@ package cmd import ( "bytes" - "io/ioutil" + "io" "os" "regexp" "strings" @@ -87,7 +87,7 @@ func readTestFile(t *testing.T, filename string) []byte { if err != nil { t.Fatalf("failed to open test data file: %v", err) } - in, err := ioutil.ReadAll(f) + in, err := io.ReadAll(f) if err != nil { t.Fatalf("failed to read test data file, %v", err) } diff --git a/cmd/extract.go b/cmd/extract.go index 7cc3bd1..fc1bedc 100644 --- a/cmd/extract.go +++ b/cmd/extract.go @@ -107,7 +107,7 @@ func init() { extractCmd.Flags().BoolVar(&opts.raw, "raw", false, "Don't attempt to decode the etcd value") extractCmd.Flags().StringVar(&opts.fields, "fields", Key, fmt.Sprintf("Fields to include when listing entries, comma separated list of: %v", SummaryFields)) extractCmd.Flags().StringVar(&opts.template, "template", "", fmt.Sprintf("golang template to use when listing entries, see https://golang.org/pkg/text/template, template is provided an object with the fields: %v. The Value field contains the entire kubernetes resource object which also may be dereferenced using a dot seperated path.", templateFields())) - extractCmd.Flags().StringVar(&opts.filter, "filter", "", fmt.Sprintf("Filter entries using a comma separated list of '=value' constraints. Fields used in filters use the same naming as --template fields, e.g. .Value.metadata.namespace")) + extractCmd.Flags().StringVar(&opts.filter, "filter", "", "Filter entries using a comma separated list of '=value' constraints. Fields used in filters use the same naming as --template fields, e.g. .Value.metadata.namespace") } const ( @@ -319,7 +319,7 @@ func summarize(s *data.KeySummary, fields []string) (string, error) { for i, field := range fields { switch field { case Key: - values[i] = fmt.Sprintf("%s", s.Key) + values[i] = s.Key case ValueSize: values[i] = fmt.Sprintf("%d", s.Stats.ValueSize) case AllVersionsValueSize: @@ -327,7 +327,7 @@ func summarize(s *data.KeySummary, fields []string) (string, error) { case VersionCount: values[i] = fmt.Sprintf("%d", s.Stats.VersionCount) case Value: - values[i] = fmt.Sprintf("%s", s.ValueJson()) + values[i] = s.ValueJson() default: return "", fmt.Errorf("unrecognized field: %s", field) } diff --git a/cmd/root.go b/cmd/root.go index 5913250..0f02d7b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,8 +23,6 @@ import ( "github.com/spf13/cobra" ) -var cfgFile string - var RootCmd = &cobra.Command{ Use: "auger", Short: "Inspect and analyze kubernetes storage data.", diff --git a/pkg/data/data.go b/pkg/data/data.go index b46c418..bfc6473 100644 --- a/pkg/data/data.go +++ b/pkg/data/data.go @@ -37,8 +37,6 @@ var ( keyBucket = []byte("key") metaBucket = []byte("meta") - consistentIndexKeyName = []byte("consistent_index") - scheduledCompactKeyName = []byte("scheduledCompactRev") finishedCompactKeyName = []byte("finishedCompactRev") ) @@ -201,22 +199,6 @@ func getCompactRevision(db *bolt.DB) (int64, error) { return compactRev, nil } -func getConsistentIndex(db *bolt.DB) (int64, error) { - consistentIndex := int64(0) - err := db.View(func(tx *bolt.Tx) error { - b := tx.Bucket(metaBucket) - consistentIndexBytes := b.Get(consistentIndexKeyName) - if len(consistentIndexBytes) != 0 { - consistentIndex = int64(binary.BigEndian.Uint64(consistentIndexBytes[0:8])) - } - return nil - }) - if err != nil { - return 0, err - } - return consistentIndex, nil -} - // ListKeySummaries returns a result set with all the provided filters and projections applied. func ListKeySummaries(filename string, filters []Filter, proj *KeySummaryProjection, revision int64) ([]*KeySummary, error) { var err error @@ -409,7 +391,9 @@ func walkRevision(db *bolt.DB, revision int64, f func(r revKey, kv *mvccpb.KeyVa for _, s := range sorted { if !s.rev.tombstone { - f(s.rev, s.kv) + if _, err := f(s.rev, s.kv); err != nil { + return err + } } } return nil diff --git a/pkg/encoding/encoding.go b/pkg/encoding/encoding.go index 1b8a081..68283e4 100644 --- a/pkg/encoding/encoding.go +++ b/pkg/encoding/encoding.go @@ -250,7 +250,9 @@ func decodeTypeMeta(inMediaType string, in []byte) (*runtime.TypeMeta, error) { func typeMetaFromJson(in []byte) (*runtime.TypeMeta, error) { var meta runtime.TypeMeta - json.Unmarshal(in, &meta) + if err := json.Unmarshal(in, &meta); err != nil { + return nil, err + } return &meta, nil } @@ -264,6 +266,8 @@ func typeMetaFromBinaryStorage(in []byte) (*runtime.TypeMeta, error) { func typeMetaFromYaml(in []byte) (*runtime.TypeMeta, error) { var meta runtime.TypeMeta - yaml.Unmarshal(in, &meta) + if err := yaml.Unmarshal(in, &meta); err != nil { + return nil, err + } return &meta, nil } diff --git a/pkg/encoding/encoding_test.go b/pkg/encoding/encoding_test.go index 2f102dc..f62738e 100644 --- a/pkg/encoding/encoding_test.go +++ b/pkg/encoding/encoding_test.go @@ -28,7 +28,7 @@ var findProtoTests = []struct { }{ {string(ProtoEncodingPrefix), true, string(ProtoEncodingPrefix)}, {fmt.Sprintf("xxxxxx%s...end", ProtoEncodingPrefix), true, fmt.Sprintf("%s...end", ProtoEncodingPrefix)}, - {fmt.Sprintf("xxxxxx{}"), false, ""}, + {"xxxxxx{}", false, ""}, } func TestTryFindProto(t *testing.T) { diff --git a/pkg/encoding/scheme.go b/pkg/encoding/scheme.go index 99bf104..6d546c7 100644 --- a/pkg/encoding/scheme.go +++ b/pkg/encoding/scheme.go @@ -98,6 +98,7 @@ func init() { // // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types // correctly. +//nolint:errcheck func AddToScheme(scheme *runtime.Scheme) { admissionv1beta1.AddToScheme(scheme)