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

lint: Update depguard configuration #507

Merged
merged 4 commits into from
Jan 27, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
go-version-file: 'go.mod'
- uses: golangci/golangci-lint-action@v3
with:
version: v1.51.0
version: v1.55.1
48 changes: 39 additions & 9 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,44 @@ linters-settings:
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 8
# depguard:
# list-type: blacklist
# include-go-root: false
# packages:
# - github.com/sirupsen/logrus
# packages-with-error-messages:
# # specify an error message to output when a blacklisted package is used
# github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
depguard:
# Rules to apply.
# Default: Only allow $gostd in all files.
rules:
# Name of a rule.
main:
# List of file globs that will match this list of settings to compare against.
# Default: $all
files:
- $all
# List of allowed packages.
allow:
- $gostd
- github.com/Masterminds/semver
- github.com/aryann/difflib
- github.com/databus23/helm-diff/v3
- github.com/evanphx/json-patch
- github.com/gonvenience/ytbx
- github.com/google/go-cmp/cmp
- github.com/homeport/dyff/pkg/dyff
- github.com/json-iterator/go
- github.com/mgutz/ansi
- github.com/spf13/cobra
- github.com/spf13/pflag
- github.com/stretchr/testify/require
- helm.sh/helm/v3
- k8s.io/api/core/v1
- k8s.io/apiextensions-apiserver
- k8s.io/apimachinery
- k8s.io/cli-runtime
- k8s.io/client-go
- sigs.k8s.io/yaml
# Packages that are not allowed where the value is a suggestion.
deny:
- pkg: "github.com/sirupsen/logrus"
desc: not allowed
- pkg: "github.com/pkg/errors"
desc: Should be replaced by standard lib errors package
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
Expand Down Expand Up @@ -357,4 +387,4 @@ issues:
# new-from-rev: REV

# Show only new issues created in git patch with set file path.
# new-from-patch: path/to/patch/file
# new-from-patch: path/to/patch/file
34 changes: 17 additions & 17 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand All @@ -11,7 +12,6 @@ import (

jsonpatch "github.com/evanphx/json-patch"
jsoniterator "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"helm.sh/helm/v3/pkg/action"
Expand Down Expand Up @@ -339,15 +339,15 @@ func (d *diffCmd) runHelm3() error {
}
original, err := actionConfig.KubeClient.Build(bytes.NewBuffer(releaseManifest), false)
if err != nil {
return errors.Wrap(err, "unable to build kubernetes objects from original release manifest")
return fmt.Errorf("unable to build kubernetes objects from original release manifest: %w", err)
}
target, err := actionConfig.KubeClient.Build(bytes.NewBuffer(installManifest), false)
if err != nil {
return errors.Wrap(err, "unable to build kubernetes objects from new release manifest")
return fmt.Errorf("unable to build kubernetes objects from new release manifest: %w", err)
}
releaseManifest, installManifest, err = genManifest(original, target)
if err != nil {
return errors.Wrap(err, "unable to generate manifests")
return fmt.Errorf("unable to generate manifests: %w", err)
}
}

Expand Down Expand Up @@ -415,7 +415,7 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {

toBeUpdated, err := existingResourceConflict(toBeCreated)
if err != nil {
return nil, nil, errors.Wrap(err, "rendered manifests contain a resource that already exists. Unable to continue with update")
return nil, nil, fmt.Errorf("rendered manifests contain a resource that already exists. Unable to continue with update: %w", err)
}

_ = toBeUpdated.Visit(func(r *resource.Info, err error) error {
Expand All @@ -437,7 +437,7 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {
currentObj, err := helper.Get(info.Namespace, info.Name)
if err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrap(err, "could not get information about the resource")
return fmt.Errorf("could not get information about the resource: %w", err)
}
// to be created
out, _ := yaml.Marshal(info.Object)
Expand All @@ -449,11 +449,11 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {
out, _ := jsoniterator.ConfigCompatibleWithStandardLibrary.Marshal(currentObj)
pruneObj, err := deleteStatusAndTidyMetadata(out)
if err != nil {
return errors.Wrapf(err, "prune current obj %q with kind %s", info.Name, kind)
return fmt.Errorf("prune current obj %q with kind %s: %w", info.Name, kind, err)
}
pruneOut, err := yaml.Marshal(pruneObj)
if err != nil {
return errors.Wrapf(err, "prune current out %q with kind %s", info.Name, kind)
return fmt.Errorf("prune current out %q with kind %s: %w", info.Name, kind, err)
}
releaseManifest = append(releaseManifest, yamlSeperator...)
releaseManifest = append(releaseManifest, pruneOut...)
Expand All @@ -471,16 +471,16 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {
helper.ServerDryRun = true
targetObj, err := helper.Patch(info.Namespace, info.Name, patchType, patch, nil)
if err != nil {
return errors.Wrapf(err, "cannot patch %q with kind %s", info.Name, kind)
return fmt.Errorf("cannot patch %q with kind %s: %w", info.Name, kind, err)
}
out, _ = jsoniterator.ConfigCompatibleWithStandardLibrary.Marshal(targetObj)
pruneObj, err = deleteStatusAndTidyMetadata(out)
if err != nil {
return errors.Wrapf(err, "prune current obj %q with kind %s", info.Name, kind)
return fmt.Errorf("prune current obj %q with kind %s: %w", info.Name, kind, err)
}
pruneOut, err = yaml.Marshal(pruneObj)
if err != nil {
return errors.Wrapf(err, "prune current out %q with kind %s", info.Name, kind)
return fmt.Errorf("prune current out %q with kind %s: %w", info.Name, kind, err)
}
installManifest = append(installManifest, yamlSeperator...)
installManifest = append(installManifest, pruneOut...)
Expand All @@ -493,17 +493,17 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {
func createPatch(originalObj, currentObj runtime.Object, target *resource.Info) ([]byte, types.PatchType, error) {
oldData, err := json.Marshal(originalObj)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing current configuration")
return nil, types.StrategicMergePatchType, fmt.Errorf("serializing current configuration: %w", err)
}
newData, err := json.Marshal(target.Object)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing target configuration")
return nil, types.StrategicMergePatchType, fmt.Errorf("serializing target configuration: %w", err)
}

// Even if currentObj is nil (because it was not found), it will marshal just fine
currentData, err := json.Marshal(currentObj)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "serializing live configuration")
return nil, types.StrategicMergePatchType, fmt.Errorf("serializing live configuration: %w", err)
}
// kind := target.Mapping.GroupVersionKind.Kind
// if kind == "Deployment" {
Expand Down Expand Up @@ -531,7 +531,7 @@ func createPatch(originalObj, currentObj runtime.Object, target *resource.Info)

patchMeta, err := strategicpatch.NewPatchMetaFromStruct(versionedObject)
if err != nil {
return nil, types.StrategicMergePatchType, errors.Wrap(err, "unable to create patch metadata from object")
return nil, types.StrategicMergePatchType, fmt.Errorf("unable to create patch metadata from object: %w", err)
}

patch, err := strategicpatch.CreateThreeWayMergePatch(oldData, newData, currentData, patchMeta, true)
Expand All @@ -557,7 +557,7 @@ func existingResourceConflict(resources kube.ResourceList) (kube.ResourceList, e
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrap(err, "could not get information about the resource")
return fmt.Errorf("could not get information about the resource: %w", err)
}

requireUpdate.Append(info)
Expand All @@ -571,7 +571,7 @@ func deleteStatusAndTidyMetadata(obj []byte) (map[string]interface{}, error) {
var objectMap map[string]interface{}
err := jsoniterator.Unmarshal(obj, &objectMap)
if err != nil {
return nil, errors.Wrap(err, "could not unmarshal byte sequence")
return nil, fmt.Errorf("could not unmarshal byte sequence: %w", err)
}

delete(objectMap, "status")
Expand Down
Loading