Skip to content

Commit

Permalink
chore: add errorlint and depguard linters and fix gci linter config.
Browse files Browse the repository at this point in the history
errorlint: 'check for code that could be problematic with the error wrapping scheme'
depguard: to prevent the use of the deprecated 'github.com/pkg/errors'.
  • Loading branch information
machine424 committed Jan 12, 2024
1 parent d94dbbd commit 06fa2cf
Show file tree
Hide file tree
Showing 38 changed files with 121 additions and 108 deletions.
18 changes: 14 additions & 4 deletions .golangci.yaml
Expand Up @@ -9,7 +9,9 @@ run:
linters:
disable-all: true
enable:
- depguard
- errcheck
- errorlint
- bodyclose
- exportloopref
- gosimple
Expand All @@ -27,8 +29,16 @@ linters:
- whitespace
- gci

linter-settings:
linters-settings:
depguard:
rules:
# Name of a rule.
main:
deny:
- pkg: "github.com/pkg/errors"
desc: "Use 'errors' or 'fmt' instead of github.com/pkg/errors"
gci:
- standard
- default
- prefix(github.com/openshift/cluster-monitoring-operator)
sections:
- standard
- default
- prefix(github.com/openshift/cluster-monitoring-operator)
7 changes: 4 additions & 3 deletions cmd/operator/main.go
Expand Up @@ -25,13 +25,14 @@ import (
"strings"
"syscall"

"github.com/openshift/cluster-monitoring-operator/pkg/manifests"
"github.com/openshift/cluster-monitoring-operator/pkg/metrics"
cmo "github.com/openshift/cluster-monitoring-operator/pkg/operator"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"

"github.com/openshift/cluster-monitoring-operator/pkg/manifests"
"github.com/openshift/cluster-monitoring-operator/pkg/metrics"
cmo "github.com/openshift/cluster-monitoring-operator/pkg/operator"
)

type images map[string]string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -12,7 +12,6 @@ require (
github.com/openshift/api v0.0.0-20231109185848-6cd72e415ddb
github.com/openshift/client-go v0.0.0-20230926161409-848405da69e1
github.com/openshift/library-go v0.0.0-20231128230659-785a9313da6c
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator v0.70.0
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.70.0
github.com/prometheus-operator/prometheus-operator/pkg/client v0.70.0
Expand Down Expand Up @@ -107,6 +106,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus-community/prom-label-proxy v0.7.0 // indirect
github.com/prometheus/alertmanager v0.26.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions pkg/alert/relabel_controller.go
Expand Up @@ -21,8 +21,6 @@ import (
"strings"

osmv1 "github.com/openshift/api/monitoring/v1"
"github.com/openshift/cluster-monitoring-operator/pkg/client"

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/relabel"
"gopkg.in/yaml.v3"
Expand All @@ -32,6 +30,8 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"

"github.com/openshift/cluster-monitoring-operator/pkg/client"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions pkg/alert/rule_controller.go
Expand Up @@ -22,8 +22,6 @@ import (
"time"

osmv1 "github.com/openshift/api/monitoring/v1"
"github.com/openshift/cluster-monitoring-operator/pkg/client"

monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -33,6 +31,8 @@ import (
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"

"github.com/openshift/cluster-monitoring-operator/pkg/client"
)

const (
Expand Down
21 changes: 13 additions & 8 deletions pkg/client/client.go
Expand Up @@ -38,7 +38,6 @@ import (
openshiftsecurityclientset "github.com/openshift/client-go/security/clientset/versioned"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"

monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -1102,8 +1101,8 @@ func (c *Client) CreateOrUpdateDeployment(ctx context.Context, dep *appsv1.Deplo

err = c.UpdateDeployment(ctx, required)
if err != nil {
uErr, ok := err.(*apierrors.StatusError)
if ok && uErr.ErrStatus.Code == 422 && uErr.ErrStatus.Reason == metav1.StatusReasonInvalid {
var statusErr *apierrors.StatusError
if errors.As(err, &statusErr) && statusErr.ErrStatus.Code == 422 && statusErr.ErrStatus.Reason == metav1.StatusReasonInvalid {
// try to delete Deployment
err = c.DeleteDeployment(ctx, existing)
if err != nil {
Expand Down Expand Up @@ -1379,8 +1378,8 @@ func (c *Client) CreateOrUpdateDaemonSet(ctx context.Context, ds *appsv1.DaemonS

err = c.UpdateDaemonSet(ctx, required)
if err != nil {
uErr, ok := err.(*apierrors.StatusError)
if ok && uErr.ErrStatus.Code == 422 && uErr.ErrStatus.Reason == metav1.StatusReasonInvalid {
var statusErr *apierrors.StatusError
if errors.As(err, &statusErr) && statusErr.ErrStatus.Code == 422 && statusErr.ErrStatus.Reason == metav1.StatusReasonInvalid {
// try to delete DaemonSet
err = c.DeleteDaemonSet(ctx, existing)
if err != nil {
Expand Down Expand Up @@ -1441,7 +1440,7 @@ func (c *Client) WaitForDaemonSetRollout(ctx context.Context, ds *appsv1.DaemonS
maxUnavailable, intstrErr := intstr.GetScaledValueFromIntOrPercent(&maxUnavailableIntStr, int(want), true)

if intstrErr != nil {
lastErr = fmt.Errorf("The daemonset has an invalid MaxUnavailable value: %v", intstrErr)
lastErr = fmt.Errorf("The daemonset has an invalid MaxUnavailable value: %w", intstrErr)
return false, nil
}

Expand Down Expand Up @@ -1491,7 +1490,10 @@ func (c *Client) CreateOrUpdateSecret(ctx context.Context, s *v1.Secret) error {
}
}
_, err = sClient.Update(ctx, required, metav1.UpdateOptions{})
return fmt.Errorf("updating Secret object failed: %w", err)
if err != nil {
return fmt.Errorf("updating Secret object failed: %w", err)
}
return nil
}

// maybeHasServiceCAData checks if the passed Secret s has at least one owner reference that
Expand Down Expand Up @@ -1523,7 +1525,10 @@ func (c *Client) CreateIfNotExistSecret(ctx context.Context, s *v1.Secret) error
}
return nil
}
return fmt.Errorf("retrieving Secret object failed: %w", err)
if err != nil {
return fmt.Errorf("retrieving Secret object failed: %w", err)
}
return nil
}

func (c *Client) CreateOrUpdateConfigMapList(ctx context.Context, cml *v1.ConfigMapList) error {
Expand Down
15 changes: 6 additions & 9 deletions pkg/client/client_test.go
Expand Up @@ -19,27 +19,24 @@ import (
"reflect"
"testing"

routev1 "github.com/openshift/api/route/v1"
secv1 "github.com/openshift/api/security/v1"
osrfake "github.com/openshift/client-go/route/clientset/versioned/fake"
ossfake "github.com/openshift/client-go/security/clientset/versioned/fake"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monfake "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
admissionv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

routev1 "github.com/openshift/api/route/v1"

monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes/fake"

osrfake "github.com/openshift/client-go/route/clientset/versioned/fake"
ossfake "github.com/openshift/client-go/security/clientset/versioned/fake"
monfake "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
"k8s.io/utils/ptr"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion pkg/client/status_reporter.go
Expand Up @@ -20,10 +20,11 @@ import (

v1 "github.com/openshift/api/config/v1"
clientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
cmostr "github.com/openshift/cluster-monitoring-operator/pkg/strings"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"

cmostr "github.com/openshift/cluster-monitoring-operator/pkg/strings"
)

const (
Expand Down
3 changes: 1 addition & 2 deletions pkg/client/status_reporter_test.go
Expand Up @@ -21,11 +21,10 @@ import (
"sort"
"testing"

apierrors "k8s.io/apimachinery/pkg/api/errors"

v1 "github.com/openshift/api/config/v1"
configv1 "github.com/openshift/client-go/config/applyconfigurations/config/v1"
clientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
Expand Down
1 change: 1 addition & 0 deletions pkg/manifests/apiserver_config_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

configv1 "github.com/openshift/api/config/v1"

"github.com/openshift/cluster-monitoring-operator/pkg/manifests"
)

Expand Down
16 changes: 8 additions & 8 deletions pkg/manifests/config.go
Expand Up @@ -24,14 +24,14 @@ import (
"strings"

configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/cluster-monitoring-operator/pkg/metrics"

poperator "github.com/prometheus-operator/prometheus-operator/pkg/operator"
"golang.org/x/exp/slices"
v1 "k8s.io/api/core/v1"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
auditv1 "k8s.io/apiserver/pkg/apis/audit/v1"
"k8s.io/klog/v2"

"github.com/openshift/cluster-monitoring-operator/pkg/metrics"
)

const (
Expand Down Expand Up @@ -348,7 +348,7 @@ func (c *Config) LoadClusterID(load func() (*configv1.ClusterVersion, error)) er

cv, err := load()
if err != nil {
return fmt.Errorf("error loading cluster version: %v", err)
return fmt.Errorf("error loading cluster version: %w", err)
}

c.ClusterMonitoringConfiguration.TelemeterClientConfig.ClusterID = string(cv.Spec.ClusterID)
Expand All @@ -362,7 +362,7 @@ func (c *Config) LoadToken(load func() (*v1.Secret, error)) error {

secret, err := load()
if err != nil {
return fmt.Errorf("error loading secret: %v", err)
return fmt.Errorf("error loading secret: %w", err)
}

if secret.Type != v1.SecretTypeDockerConfigJson {
Expand All @@ -378,7 +378,7 @@ func (c *Config) LoadToken(load func() (*v1.Secret, error)) error {
}{}

if err := json.Unmarshal(secret.Data[v1.DockerConfigJsonKey], &ps); err != nil {
return fmt.Errorf("unmarshaling pull secret failed: %v", err)
return fmt.Errorf("unmarshaling pull secret failed: %w", err)
}

c.ClusterMonitoringConfiguration.TelemeterClientConfig.Token = ps.Auths.COC.Auth
Expand Down Expand Up @@ -413,7 +413,7 @@ func (c *Config) LoadEnforcedBodySizeLimit(pcr PodCapacityReader, ctx context.Co
if c.ClusterMonitoringConfiguration.PrometheusK8sConfig.EnforcedBodySizeLimit == automaticBodySizeLimit {
podCapacity, err := pcr.PodCapacity(ctx)
if err != nil {
return fmt.Errorf("error fetching pod capacity: %v", err)
return fmt.Errorf("error fetching pod capacity: %w", err)
}
c.ClusterMonitoringConfiguration.PrometheusK8sConfig.EnforcedBodySizeLimit = calculateBodySizeLimit(podCapacity)
return nil
Expand All @@ -424,7 +424,7 @@ func (c *Config) LoadEnforcedBodySizeLimit(pcr PodCapacityReader, ctx context.Co

func (c *Config) Precheck() error {
if c.ClusterMonitoringConfiguration.PrometheusK8sConfig.CollectionProfile != FullCollectionProfile && !c.TechPreview {
return errors.Wrap(ErrConfigValidation, "collectionProfiles is a TechPreview feature, to be able to use a profile different from the default (\"full\") please enable TechPreview")
return fmt.Errorf("collectionProfiles is a TechPreview feature, to be able to use a profile different from the default (\"full\") please enable TechPreview: %w", ErrConfigValidation)
}

// Validate the configured collection profile iff tech preview is enabled, even if the default profile is set.
Expand All @@ -437,7 +437,7 @@ func (c *Config) Precheck() error {
metrics.CollectionProfile.WithLabelValues(string(profile)).Set(v)
}
if !slices.Contains(SupportedCollectionProfiles, c.ClusterMonitoringConfiguration.PrometheusK8sConfig.CollectionProfile) {
return errors.Wrap(ErrConfigValidation, fmt.Sprintf(`%q is not supported, supported collection profiles are: %q`, c.ClusterMonitoringConfiguration.PrometheusK8sConfig.CollectionProfile, SupportedCollectionProfiles.String()))
return fmt.Errorf(`%q is not supported, supported collection profiles are: %q: %w`, c.ClusterMonitoringConfiguration.PrometheusK8sConfig.CollectionProfile, SupportedCollectionProfiles.String(), ErrConfigValidation)
}
}

Expand Down
16 changes: 7 additions & 9 deletions pkg/manifests/manifests.go
Expand Up @@ -34,9 +34,7 @@ import (
consolev1 "github.com/openshift/api/console/v1"
routev1 "github.com/openshift/api/route/v1"
securityv1 "github.com/openshift/api/security/v1"
"github.com/openshift/cluster-monitoring-operator/pkg/promqlgen"
"github.com/openshift/library-go/pkg/crypto"

monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"golang.org/x/exp/slices"
yaml2 "gopkg.in/yaml.v2"
Expand All @@ -51,6 +49,8 @@ import (
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
"k8s.io/utils/ptr"
k8syaml "sigs.k8s.io/yaml"

"github.com/openshift/cluster-monitoring-operator/pkg/promqlgen"
)

const (
Expand Down Expand Up @@ -951,7 +951,7 @@ func (f *Factory) updateNodeExporterArgs(args []string) ([]string, error) {

pattern, err := regexListToArg(f.config.ClusterMonitoringConfiguration.NodeExporterConfig.Collectors.Systemd.Units)
if err != nil {
return nil, fmt.Errorf("systemd unit pattern valiation error: %s", err)
return nil, fmt.Errorf("systemd unit pattern valiation error: %w", err)
}
args = setArg(args, "--collector.systemd.unit-include=", pattern)
} else {
Expand Down Expand Up @@ -2585,11 +2585,9 @@ func (f *Factory) ControlPlaneKubeletMinimalServiceMonitor() (*monv1.ServiceMoni
}

func IsMissingPortInAddressError(err error) bool {
switch e := err.(type) {
case *net.AddrError:
if e.Err == "missing port in address" {
return true
}
var addrErr *net.AddrError
if errors.As(err, &addrErr) {
return addrErr.Err == "missing port in address"
}
return false
}
Expand Down Expand Up @@ -3194,7 +3192,7 @@ func (f *Factory) TelemeterClientSecret() (*v1.Secret, error) {

salt, err := GeneratePassword(32)
if err != nil {
return nil, fmt.Errorf("failed to generate Telemeter client salt: %v", err)
return nil, fmt.Errorf("failed to generate Telemeter client salt: %w", err)
}
s.Data["salt"] = []byte(salt)

Expand Down
4 changes: 1 addition & 3 deletions pkg/manifests/manifests_test.go
Expand Up @@ -26,11 +26,9 @@ import (
"testing"
"time"

configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/library-go/pkg/crypto"

monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"

configv1 "github.com/openshift/api/config/v1"
v1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down
5 changes: 2 additions & 3 deletions pkg/manifests/tls.go
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/openshift/library-go/pkg/crypto"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/authentication/user"
Expand Down Expand Up @@ -239,11 +238,11 @@ func RotateGRPCSecret(s *v1.Secret) error {
func createCertificate(template, parent *x509.Certificate, pub, priv interface{}) (*x509.Certificate, error) {
rawCert, err := x509.CreateCertificate(rand.Reader, template, parent, pub, priv)
if err != nil {
return nil, fmt.Errorf("error creating certificate: %v", err)
return nil, fmt.Errorf("error creating certificate: %w", err)
}
parsedCerts, err := x509.ParseCertificates(rawCert)
if err != nil {
return nil, fmt.Errorf("error parsing certificate: %v", err)
return nil, fmt.Errorf("error parsing certificate: %w", err)
}
return parsedCerts[0], nil
}

0 comments on commit 06fa2cf

Please sign in to comment.