chore: Migrate to golangci-lint v2

This commit is contained in:
Manuel Rüger 2025-03-31 13:40:29 +02:00
parent b528c28f7e
commit 0c029b690e
28 changed files with 99 additions and 88 deletions

View File

@ -21,7 +21,7 @@ env:
E2E_SETUP_KUBECTL: yes
SUDO: sudo
GO_VERSION: "^1.24"
GOLANGCI_LINT_VERSION: "v1.64.6"
GOLANGCI_LINT_VERSION: "v2.0.2"
jobs:
ci-go-lint:

View File

@ -1,15 +1,10 @@
run:
timeout: 10m
version: "2"
linters:
disable-all: true
default: none
enable:
- gocritic
- gocyclo
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- misspell
@ -18,22 +13,37 @@ linters:
- staticcheck
- unconvert
- unused
linters-settings:
goimports:
local-prefixes: k8s.io/kube-state-metrics,k8s.io/kube-state-metrics/v2
exclusions:
generated: lax
rules:
- linters:
- promlinter
path: _test\.go
- linters:
- gosec
text: 'G104:'
- linters:
- revive
text: 'package-comments:'
paths:
- third_party$
- builtin$
- examples$
issues:
exclude-use-default: false
exclude-rules:
# We don't check metrics naming in the tests.
- path: _test\.go
linters:
- promlinter
# TODO(mrueg) Improve error handling
- text: "G104:"
linters:
- gosec
- text: "package-comments:"
linters:
- revive
max-issues-per-linter: 0
max-same-issues: 0
formatters:
enable:
- gofmt
- goimports
settings:
goimports:
local-prefixes:
- k8s.io/kube-state-metrics
- k8s.io/kube-state-metrics/v2
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$

View File

@ -131,7 +131,7 @@ func clusterRoleMetricFamilies(allowAnnotationsList, allowLabelsList []string) [
"",
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(r.ResourceVersion),
}
}),
),

View File

@ -133,7 +133,7 @@ func clusterRoleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []st
"",
wrapClusterRoleBindingFunc(func(r *rbacv1.ClusterRoleBinding) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(r.ResourceVersion),
}
}),
),

View File

@ -127,7 +127,7 @@ func configMapMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g
"",
wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(c.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(c.ResourceVersion),
}
}),
),

View File

@ -273,7 +273,7 @@ func cronJobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen
"",
wrapCronJobFunc(func(j *batchv1.CronJob) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(j.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(j.ResourceVersion),
}
}),
),
@ -358,7 +358,7 @@ func getNextScheduledTime(schedule string, lastScheduleTime *metav1.Time, create
sched, err := cron.ParseStandard(schedule)
if err != nil {
return time.Time{}, fmt.Errorf("Failed to parse cron job schedule '%s': %w", schedule, err)
return time.Time{}, fmt.Errorf("failed to parse cron job schedule '%s': %w", schedule, err)
}
if !lastScheduleTime.IsZero() {
return sched.Next(lastScheduleTime.Time), nil

View File

@ -217,7 +217,7 @@ func daemonSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g
{
LabelKeys: []string{},
LabelValues: []string{},
Value: float64(d.ObjectMeta.Generation),
Value: float64(d.Generation),
},
},
}

View File

@ -277,7 +277,7 @@ func deploymentMetricFamilies(allowAnnotationsList, allowLabelsList []string) []
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(d.ObjectMeta.Generation),
Value: float64(d.Generation),
},
},
}

View File

@ -134,7 +134,7 @@ func createHPAMetaDataGeneration() generator.FamilyGenerator {
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(a.ObjectMeta.Generation),
Value: float64(a.Generation),
},
},
}

View File

@ -140,7 +140,7 @@ func ingressMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen
"",
wrapIngressFunc(func(i *networkingv1.Ingress) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(i.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(i.ResourceVersion),
}
}),
),

View File

@ -78,7 +78,7 @@ var (
"",
wrapMutatingWebhookConfigurationFunc(func(mwc *admissionregistrationv1.MutatingWebhookConfiguration) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(mwc.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(mwc.ResourceVersion),
}
}),
),

View File

@ -243,50 +243,50 @@ func createPersistentVolumeInfo() generator.FamilyGenerator {
)
switch {
case p.Spec.PersistentVolumeSource.GCEPersistentDisk != nil:
gcePDDiskName = p.Spec.PersistentVolumeSource.GCEPersistentDisk.PDName
case p.Spec.PersistentVolumeSource.AWSElasticBlockStore != nil:
ebsVolumeID = p.Spec.PersistentVolumeSource.AWSElasticBlockStore.VolumeID
case p.Spec.PersistentVolumeSource.AzureDisk != nil:
azureDiskName = p.Spec.PersistentVolumeSource.AzureDisk.DiskName
case p.Spec.PersistentVolumeSource.FC != nil:
if p.Spec.PersistentVolumeSource.FC.Lun != nil {
fcLun = strconv.FormatInt(int64(*p.Spec.PersistentVolumeSource.FC.Lun), 10)
case p.Spec.GCEPersistentDisk != nil:
gcePDDiskName = p.Spec.GCEPersistentDisk.PDName
case p.Spec.AWSElasticBlockStore != nil:
ebsVolumeID = p.Spec.AWSElasticBlockStore.VolumeID
case p.Spec.AzureDisk != nil:
azureDiskName = p.Spec.AzureDisk.DiskName
case p.Spec.FC != nil:
if p.Spec.FC.Lun != nil {
fcLun = strconv.FormatInt(int64(*p.Spec.FC.Lun), 10)
}
for _, wwn := range p.Spec.PersistentVolumeSource.FC.TargetWWNs {
for _, wwn := range p.Spec.FC.TargetWWNs {
if len(fcTargetWWNs) != 0 {
fcTargetWWNs += ","
}
fcTargetWWNs += wwn
}
for _, wwid := range p.Spec.PersistentVolumeSource.FC.WWIDs {
for _, wwid := range p.Spec.FC.WWIDs {
if len(fcWWIDs) != 0 {
fcWWIDs += ","
}
fcWWIDs += wwid
}
case p.Spec.PersistentVolumeSource.ISCSI != nil:
iscsiTargetPortal = p.Spec.PersistentVolumeSource.ISCSI.TargetPortal
iscsiIQN = p.Spec.PersistentVolumeSource.ISCSI.IQN
iscsiLun = strconv.FormatInt(int64(p.Spec.PersistentVolumeSource.ISCSI.Lun), 10)
if p.Spec.PersistentVolumeSource.ISCSI.InitiatorName != nil {
iscsiInitiatorName = *p.Spec.PersistentVolumeSource.ISCSI.InitiatorName
case p.Spec.ISCSI != nil:
iscsiTargetPortal = p.Spec.ISCSI.TargetPortal
iscsiIQN = p.Spec.ISCSI.IQN
iscsiLun = strconv.FormatInt(int64(p.Spec.ISCSI.Lun), 10)
if p.Spec.ISCSI.InitiatorName != nil {
iscsiInitiatorName = *p.Spec.ISCSI.InitiatorName
}
case p.Spec.PersistentVolumeSource.NFS != nil:
nfsServer = p.Spec.PersistentVolumeSource.NFS.Server
nfsPath = p.Spec.PersistentVolumeSource.NFS.Path
case p.Spec.PersistentVolumeSource.CSI != nil:
csiDriver = p.Spec.PersistentVolumeSource.CSI.Driver
csiVolumeHandle = p.Spec.PersistentVolumeSource.CSI.VolumeHandle
case p.Spec.PersistentVolumeSource.Local != nil:
localPath = p.Spec.PersistentVolumeSource.Local.Path
if p.Spec.PersistentVolumeSource.Local.FSType != nil {
localFS = *p.Spec.PersistentVolumeSource.Local.FSType
case p.Spec.NFS != nil:
nfsServer = p.Spec.NFS.Server
nfsPath = p.Spec.NFS.Path
case p.Spec.CSI != nil:
csiDriver = p.Spec.CSI.Driver
csiVolumeHandle = p.Spec.CSI.VolumeHandle
case p.Spec.Local != nil:
localPath = p.Spec.Local.Path
if p.Spec.Local.FSType != nil {
localFS = *p.Spec.Local.FSType
}
case p.Spec.PersistentVolumeSource.HostPath != nil:
hostPath = p.Spec.PersistentVolumeSource.HostPath.Path
if p.Spec.PersistentVolumeSource.HostPath.Type != nil {
hostPathType = string(*p.Spec.PersistentVolumeSource.HostPath.Type)
case p.Spec.HostPath != nil:
hostPath = p.Spec.HostPath.Path
if p.Spec.HostPath.Type != nil {
hostPathType = string(*p.Spec.HostPath.Type)
}
}
@ -430,11 +430,12 @@ func createPersistentVolumeCSIAttributes() generator.FamilyGenerator {
}
var csiMounter, csiMapOptions string
for k, v := range p.Spec.PersistentVolumeSource.CSI.VolumeAttributes {
for k, v := range p.Spec.CSI.VolumeAttributes {
// storage attributes handled by external CEPH CSI driver
if k == "mapOptions" {
switch k {
case "mapOptions":
csiMapOptions = v
} else if k == "mounter" {
case "mounter":
csiMounter = v
}
}

View File

@ -158,7 +158,7 @@ func replicaSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(r.ObjectMeta.Generation),
Value: float64(r.Generation),
},
},
}

View File

@ -166,7 +166,7 @@ var (
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(r.ObjectMeta.Generation),
Value: float64(r.Generation),
},
},
}

View File

@ -131,7 +131,7 @@ func roleMetricFamilies(allowAnnotationsList, allowLabelsList []string) []genera
"",
wrapRoleFunc(func(r *rbacv1.Role) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(r.ResourceVersion),
}
}),
),

View File

@ -133,7 +133,7 @@ func roleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []string) [
"",
wrapRoleBindingFunc(func(r *rbacv1.RoleBinding) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(r.ResourceVersion),
}
}),
),

View File

@ -150,7 +150,7 @@ func secretMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gene
"",
wrapSecretFunc(func(s *v1.Secret) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(s.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(s.ResourceVersion),
}
}),
),

View File

@ -208,7 +208,7 @@ func statefulSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) [
return &metric.Family{
Metrics: []*metric.Metric{
{
Value: float64(s.ObjectMeta.Generation),
Value: float64(s.Generation),
},
},
}

View File

@ -257,7 +257,7 @@ func TestKubeLabelsToPrometheusLabels(t *testing.T) {
}
for i := range tc.expectKeys {
if !(tc.expectKeys[i] == labelKeys[i] && tc.expectValues[i] == labelValues[i]) {
if tc.expectKeys[i] != labelKeys[i] || tc.expectValues[i] != labelValues[i] {
t.Errorf("Got Prometheus label %q: %q but expected %q: %q", labelKeys[i], labelValues[i], tc.expectKeys[i], tc.expectValues[i])
}
}

View File

@ -78,7 +78,7 @@ var (
"",
wrapValidatingWebhookConfigurationFunc(func(vwc *admissionregistrationv1.ValidatingWebhookConfiguration) *metric.Family {
return &metric.Family{
Metrics: resourceVersionMetric(vwc.ObjectMeta.ResourceVersion),
Metrics: resourceVersionMetric(vwc.ResourceVersion),
}
}),
),

View File

@ -497,7 +497,7 @@ func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.Con
if file := opts.CustomResourceConfigFile; file != "" {
f, err := os.Open(filepath.Clean(file))
if err != nil {
return nil, fmt.Errorf("Custom Resource State Metrics file could not be opened: %v", err)
return nil, fmt.Errorf("unable to open Custom Resource State Metrics file: %v", err)
}
return yaml.NewDecoder(f), nil
}

View File

@ -70,7 +70,7 @@ func compileCommon(c MetricMeta) (*compiledCommon, error) {
}
func compileFamily(f Generator, resource Resource) (*compiledFamily, error) {
labels := resource.Labels.Merge(f.Labels)
labels := resource.Merge(f.Labels)
if f.Each.Type == metric.Info && !strings.HasSuffix(f.Name, "_info") {
klog.InfoS("Info metric does not have _info suffix", "gvk", resource.GroupVersionKind.String(), "name", f.Name)
@ -652,7 +652,7 @@ func compilePath(path []string) (out valuePath, _ error) {
// negative index
i += len(s)
}
if !(0 <= i && i < len(s)) {
if i < 0 || i > len(s) {
return fmt.Errorf("list index out of range: %s", part)
}
return s[i]

View File

@ -50,7 +50,7 @@ func TestResourceSetSet(t *testing.T) {
for _, test := range tests {
cs := &ResourceSet{}
gotError := cs.Set(test.Value)
if !(((gotError == nil && !test.WantedError) || (gotError != nil && test.WantedError)) && reflect.DeepEqual(*cs, test.Wanted)) {
if ((gotError != nil || test.WantedError) && (gotError == nil || !test.WantedError)) || !reflect.DeepEqual(*cs, test.Wanted) {
t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v. Wanted Error: %v, Got Error: %v", test.Desc, test.Wanted, *cs, test.WantedError, gotError)
}
}

View File

@ -69,7 +69,7 @@ func (s *shardedListWatch) List(options metav1.ListOptions) (runtime.Object, err
res.Items = append(res.Items, runtime.RawExtension{Object: item})
}
}
res.ListMeta.ResourceVersion = metaObj.GetResourceVersion()
res.ResourceVersion = metaObj.GetResourceVersion()
return res, nil
}

View File

@ -42,7 +42,7 @@ func TestSharding(t *testing.T) {
totalShards: 2,
}
if !(s1.keep(cm) || s2.keep(cm)) {
if !s1.keep(cm) && !s2.keep(cm) {
t.Fatal("One shard must pick up the object.")
}

View File

@ -135,7 +135,7 @@ func GVRFromType(resourceName string, expectedType interface{}) (*schema.GroupVe
}
t, err := meta.TypeAccessor(expectedType)
if err != nil {
return nil, fmt.Errorf("Failed to get type accessor for %T: %w", expectedType, err)
return nil, fmt.Errorf("failed to get type accessor for %T: %w", expectedType, err)
}
apiVersion := t.GetAPIVersion()
g, v, found := strings.Cut(apiVersion, "/")

View File

@ -118,7 +118,7 @@ func (k *KSMClient) IsHealthz() (bool, error) {
func (k *KSMClient) writeMetrics(endpoint *url.URL, w io.Writer) error {
if endpoint == nil {
return errors.New("Endpoint is nil")
return errors.New("endpoint is nil")
}
u := *endpoint
@ -165,7 +165,7 @@ func (f *Framework) ParseMetrics(metrics func(io.Writer) error) (map[string]*dto
buf := &bytes.Buffer{}
err := metrics(buf)
if err != nil {
return nil, fmt.Errorf("Failed to get metrics: %w", err)
return nil, fmt.Errorf("failed to get metrics: %w", err)
}
parser := &expfmt.TextParser{}

View File

@ -62,7 +62,7 @@ func TestAsLibrary(t *testing.T) {
}
m := w.String()
if !strings.Contains(m, service.ObjectMeta.Name) {
if !strings.Contains(m, service.Name) {
t.Fatal("expected string to contain service name")
}
}