Skip to content

Commit 2bf904e

Browse files
committed
chore: Migrate to golangci-lint v2
1 parent b528c28 commit 2bf904e

25 files changed

+93
-83
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ env:
2121
E2E_SETUP_KUBECTL: yes
2222
SUDO: sudo
2323
GO_VERSION: "^1.24"
24-
GOLANGCI_LINT_VERSION: "v1.64.6"
24+
GOLANGCI_LINT_VERSION: "v2.0.2"
2525

2626
jobs:
2727
ci-go-lint:

.golangci.yml

+35-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
run:
2-
timeout: 10m
3-
1+
version: "2"
42
linters:
5-
disable-all: true
3+
default: none
64
enable:
75
- gocritic
86
- gocyclo
9-
- gofmt
10-
- goimports
117
- gosec
12-
- gosimple
138
- govet
149
- ineffassign
1510
- misspell
@@ -18,22 +13,37 @@ linters:
1813
- staticcheck
1914
- unconvert
2015
- unused
21-
22-
linters-settings:
23-
goimports:
24-
local-prefixes: k8s.io/kube-state-metrics,k8s.io/kube-state-metrics/v2
25-
16+
exclusions:
17+
generated: lax
18+
rules:
19+
- linters:
20+
- promlinter
21+
path: _test\.go
22+
- linters:
23+
- gosec
24+
text: 'G104:'
25+
- linters:
26+
- revive
27+
text: 'package-comments:'
28+
paths:
29+
- third_party$
30+
- builtin$
31+
- examples$
2632
issues:
27-
exclude-use-default: false
28-
exclude-rules:
29-
# We don't check metrics naming in the tests.
30-
- path: _test\.go
31-
linters:
32-
- promlinter
33-
# TODO(mrueg) Improve error handling
34-
- text: "G104:"
35-
linters:
36-
- gosec
37-
- text: "package-comments:"
38-
linters:
39-
- revive
33+
max-issues-per-linter: 0
34+
max-same-issues: 0
35+
formatters:
36+
enable:
37+
- gofmt
38+
- goimports
39+
settings:
40+
goimports:
41+
local-prefixes:
42+
- k8s.io/kube-state-metrics
43+
- k8s.io/kube-state-metrics/v2
44+
exclusions:
45+
generated: lax
46+
paths:
47+
- third_party$
48+
- builtin$
49+
- examples$

internal/store/clusterrole.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func clusterRoleMetricFamilies(allowAnnotationsList, allowLabelsList []string) [
131131
"",
132132
wrapClusterRoleFunc(func(r *rbacv1.ClusterRole) *metric.Family {
133133
return &metric.Family{
134-
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
134+
Metrics: resourceVersionMetric(r.ResourceVersion),
135135
}
136136
}),
137137
),

internal/store/clusterrolebinding.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func clusterRoleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []st
133133
"",
134134
wrapClusterRoleBindingFunc(func(r *rbacv1.ClusterRoleBinding) *metric.Family {
135135
return &metric.Family{
136-
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
136+
Metrics: resourceVersionMetric(r.ResourceVersion),
137137
}
138138
}),
139139
),

internal/store/configmap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func configMapMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g
127127
"",
128128
wrapConfigMapFunc(func(c *v1.ConfigMap) *metric.Family {
129129
return &metric.Family{
130-
Metrics: resourceVersionMetric(c.ObjectMeta.ResourceVersion),
130+
Metrics: resourceVersionMetric(c.ResourceVersion),
131131
}
132132
}),
133133
),

internal/store/cronjob.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func cronJobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen
273273
"",
274274
wrapCronJobFunc(func(j *batchv1.CronJob) *metric.Family {
275275
return &metric.Family{
276-
Metrics: resourceVersionMetric(j.ObjectMeta.ResourceVersion),
276+
Metrics: resourceVersionMetric(j.ResourceVersion),
277277
}
278278
}),
279279
),
@@ -358,7 +358,7 @@ func getNextScheduledTime(schedule string, lastScheduleTime *metav1.Time, create
358358

359359
sched, err := cron.ParseStandard(schedule)
360360
if err != nil {
361-
return time.Time{}, fmt.Errorf("Failed to parse cron job schedule '%s': %w", schedule, err)
361+
return time.Time{}, fmt.Errorf("failed to parse cron job schedule '%s': %w", schedule, err)
362362
}
363363
if !lastScheduleTime.IsZero() {
364364
return sched.Next(lastScheduleTime.Time), nil

internal/store/daemonset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func daemonSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g
217217
{
218218
LabelKeys: []string{},
219219
LabelValues: []string{},
220-
Value: float64(d.ObjectMeta.Generation),
220+
Value: float64(d.Generation),
221221
},
222222
},
223223
}

internal/store/deployment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func deploymentMetricFamilies(allowAnnotationsList, allowLabelsList []string) []
277277
return &metric.Family{
278278
Metrics: []*metric.Metric{
279279
{
280-
Value: float64(d.ObjectMeta.Generation),
280+
Value: float64(d.Generation),
281281
},
282282
},
283283
}

internal/store/horizontalpodautoscaler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func createHPAMetaDataGeneration() generator.FamilyGenerator {
134134
return &metric.Family{
135135
Metrics: []*metric.Metric{
136136
{
137-
Value: float64(a.ObjectMeta.Generation),
137+
Value: float64(a.Generation),
138138
},
139139
},
140140
}

internal/store/ingress.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func ingressMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen
140140
"",
141141
wrapIngressFunc(func(i *networkingv1.Ingress) *metric.Family {
142142
return &metric.Family{
143-
Metrics: resourceVersionMetric(i.ObjectMeta.ResourceVersion),
143+
Metrics: resourceVersionMetric(i.ResourceVersion),
144144
}
145145
}),
146146
),

internal/store/mutatingwebhookconfiguration.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var (
7878
"",
7979
wrapMutatingWebhookConfigurationFunc(func(mwc *admissionregistrationv1.MutatingWebhookConfiguration) *metric.Family {
8080
return &metric.Family{
81-
Metrics: resourceVersionMetric(mwc.ObjectMeta.ResourceVersion),
81+
Metrics: resourceVersionMetric(mwc.ResourceVersion),
8282
}
8383
}),
8484
),

internal/store/persistentvolume.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -243,50 +243,50 @@ func createPersistentVolumeInfo() generator.FamilyGenerator {
243243
)
244244

245245
switch {
246-
case p.Spec.PersistentVolumeSource.GCEPersistentDisk != nil:
247-
gcePDDiskName = p.Spec.PersistentVolumeSource.GCEPersistentDisk.PDName
248-
case p.Spec.PersistentVolumeSource.AWSElasticBlockStore != nil:
249-
ebsVolumeID = p.Spec.PersistentVolumeSource.AWSElasticBlockStore.VolumeID
250-
case p.Spec.PersistentVolumeSource.AzureDisk != nil:
251-
azureDiskName = p.Spec.PersistentVolumeSource.AzureDisk.DiskName
252-
case p.Spec.PersistentVolumeSource.FC != nil:
253-
if p.Spec.PersistentVolumeSource.FC.Lun != nil {
254-
fcLun = strconv.FormatInt(int64(*p.Spec.PersistentVolumeSource.FC.Lun), 10)
246+
case p.Spec.GCEPersistentDisk != nil:
247+
gcePDDiskName = p.Spec.GCEPersistentDisk.PDName
248+
case p.Spec.AWSElasticBlockStore != nil:
249+
ebsVolumeID = p.Spec.AWSElasticBlockStore.VolumeID
250+
case p.Spec.AzureDisk != nil:
251+
azureDiskName = p.Spec.AzureDisk.DiskName
252+
case p.Spec.FC != nil:
253+
if p.Spec.FC.Lun != nil {
254+
fcLun = strconv.FormatInt(int64(*p.Spec.FC.Lun), 10)
255255
}
256-
for _, wwn := range p.Spec.PersistentVolumeSource.FC.TargetWWNs {
256+
for _, wwn := range p.Spec.FC.TargetWWNs {
257257
if len(fcTargetWWNs) != 0 {
258258
fcTargetWWNs += ","
259259
}
260260
fcTargetWWNs += wwn
261261
}
262-
for _, wwid := range p.Spec.PersistentVolumeSource.FC.WWIDs {
262+
for _, wwid := range p.Spec.FC.WWIDs {
263263
if len(fcWWIDs) != 0 {
264264
fcWWIDs += ","
265265
}
266266
fcWWIDs += wwid
267267
}
268-
case p.Spec.PersistentVolumeSource.ISCSI != nil:
269-
iscsiTargetPortal = p.Spec.PersistentVolumeSource.ISCSI.TargetPortal
270-
iscsiIQN = p.Spec.PersistentVolumeSource.ISCSI.IQN
271-
iscsiLun = strconv.FormatInt(int64(p.Spec.PersistentVolumeSource.ISCSI.Lun), 10)
272-
if p.Spec.PersistentVolumeSource.ISCSI.InitiatorName != nil {
273-
iscsiInitiatorName = *p.Spec.PersistentVolumeSource.ISCSI.InitiatorName
268+
case p.Spec.ISCSI != nil:
269+
iscsiTargetPortal = p.Spec.ISCSI.TargetPortal
270+
iscsiIQN = p.Spec.ISCSI.IQN
271+
iscsiLun = strconv.FormatInt(int64(p.Spec.ISCSI.Lun), 10)
272+
if p.Spec.ISCSI.InitiatorName != nil {
273+
iscsiInitiatorName = *p.Spec.ISCSI.InitiatorName
274274
}
275-
case p.Spec.PersistentVolumeSource.NFS != nil:
276-
nfsServer = p.Spec.PersistentVolumeSource.NFS.Server
277-
nfsPath = p.Spec.PersistentVolumeSource.NFS.Path
278-
case p.Spec.PersistentVolumeSource.CSI != nil:
279-
csiDriver = p.Spec.PersistentVolumeSource.CSI.Driver
280-
csiVolumeHandle = p.Spec.PersistentVolumeSource.CSI.VolumeHandle
281-
case p.Spec.PersistentVolumeSource.Local != nil:
282-
localPath = p.Spec.PersistentVolumeSource.Local.Path
283-
if p.Spec.PersistentVolumeSource.Local.FSType != nil {
284-
localFS = *p.Spec.PersistentVolumeSource.Local.FSType
275+
case p.Spec.NFS != nil:
276+
nfsServer = p.Spec.NFS.Server
277+
nfsPath = p.Spec.NFS.Path
278+
case p.Spec.CSI != nil:
279+
csiDriver = p.Spec.CSI.Driver
280+
csiVolumeHandle = p.Spec.CSI.VolumeHandle
281+
case p.Spec.Local != nil:
282+
localPath = p.Spec.Local.Path
283+
if p.Spec.Local.FSType != nil {
284+
localFS = *p.Spec.Local.FSType
285285
}
286-
case p.Spec.PersistentVolumeSource.HostPath != nil:
287-
hostPath = p.Spec.PersistentVolumeSource.HostPath.Path
288-
if p.Spec.PersistentVolumeSource.HostPath.Type != nil {
289-
hostPathType = string(*p.Spec.PersistentVolumeSource.HostPath.Type)
286+
case p.Spec.HostPath != nil:
287+
hostPath = p.Spec.HostPath.Path
288+
if p.Spec.HostPath.Type != nil {
289+
hostPathType = string(*p.Spec.HostPath.Type)
290290
}
291291
}
292292

@@ -430,7 +430,7 @@ func createPersistentVolumeCSIAttributes() generator.FamilyGenerator {
430430
}
431431

432432
var csiMounter, csiMapOptions string
433-
for k, v := range p.Spec.PersistentVolumeSource.CSI.VolumeAttributes {
433+
for k, v := range p.Spec.CSI.VolumeAttributes {
434434
// storage attributes handled by external CEPH CSI driver
435435
if k == "mapOptions" {
436436
csiMapOptions = v

internal/store/replicaset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func replicaSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []
158158
return &metric.Family{
159159
Metrics: []*metric.Metric{
160160
{
161-
Value: float64(r.ObjectMeta.Generation),
161+
Value: float64(r.Generation),
162162
},
163163
},
164164
}

internal/store/replicationcontroller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ var (
166166
return &metric.Family{
167167
Metrics: []*metric.Metric{
168168
{
169-
Value: float64(r.ObjectMeta.Generation),
169+
Value: float64(r.Generation),
170170
},
171171
},
172172
}

internal/store/role.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func roleMetricFamilies(allowAnnotationsList, allowLabelsList []string) []genera
131131
"",
132132
wrapRoleFunc(func(r *rbacv1.Role) *metric.Family {
133133
return &metric.Family{
134-
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
134+
Metrics: resourceVersionMetric(r.ResourceVersion),
135135
}
136136
}),
137137
),

internal/store/rolebinding.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func roleBindingMetricFamilies(allowAnnotationsList, allowLabelsList []string) [
133133
"",
134134
wrapRoleBindingFunc(func(r *rbacv1.RoleBinding) *metric.Family {
135135
return &metric.Family{
136-
Metrics: resourceVersionMetric(r.ObjectMeta.ResourceVersion),
136+
Metrics: resourceVersionMetric(r.ResourceVersion),
137137
}
138138
}),
139139
),

internal/store/secret.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func secretMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gene
150150
"",
151151
wrapSecretFunc(func(s *v1.Secret) *metric.Family {
152152
return &metric.Family{
153-
Metrics: resourceVersionMetric(s.ObjectMeta.ResourceVersion),
153+
Metrics: resourceVersionMetric(s.ResourceVersion),
154154
}
155155
}),
156156
),

internal/store/statefulset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func statefulSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) [
208208
return &metric.Family{
209209
Metrics: []*metric.Metric{
210210
{
211-
Value: float64(s.ObjectMeta.Generation),
211+
Value: float64(s.Generation),
212212
},
213213
},
214214
}

internal/store/validatingwebhookconfiguration.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var (
7878
"",
7979
wrapValidatingWebhookConfigurationFunc(func(vwc *admissionregistrationv1.ValidatingWebhookConfiguration) *metric.Family {
8080
return &metric.Family{
81-
Metrics: resourceVersionMetric(vwc.ObjectMeta.ResourceVersion),
81+
Metrics: resourceVersionMetric(vwc.ResourceVersion),
8282
}
8383
}),
8484
),

pkg/app/server.go

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

pkg/customresourcestate/registry_factory.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func compileCommon(c MetricMeta) (*compiledCommon, error) {
7070
}
7171

7272
func compileFamily(f Generator, resource Resource) (*compiledFamily, error) {
73-
labels := resource.Labels.Merge(f.Labels)
73+
labels := resource.Merge(f.Labels)
7474

7575
if f.Each.Type == metric.Info && !strings.HasSuffix(f.Name, "_info") {
7676
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) {
652652
// negative index
653653
i += len(s)
654654
}
655-
if !(0 <= i && i < len(s)) {
655+
if i < 0 || i > len(s) {
656656
return fmt.Errorf("list index out of range: %s", part)
657657
}
658658
return s[i]

pkg/sharding/listwatch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestSharding(t *testing.T) {
4242
totalShards: 2,
4343
}
4444

45-
if !(s1.keep(cm) || s2.keep(cm)) {
45+
if !s1.keep(cm) && !s2.keep(cm) {
4646
t.Fatal("One shard must pick up the object.")
4747
}
4848

pkg/util/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func GVRFromType(resourceName string, expectedType interface{}) (*schema.GroupVe
135135
}
136136
t, err := meta.TypeAccessor(expectedType)
137137
if err != nil {
138-
return nil, fmt.Errorf("Failed to get type accessor for %T: %w", expectedType, err)
138+
return nil, fmt.Errorf("failed to get type accessor for %T: %w", expectedType, err)
139139
}
140140
apiVersion := t.GetAPIVersion()
141141
g, v, found := strings.Cut(apiVersion, "/")

tests/e2e/framework/framework.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (k *KSMClient) IsHealthz() (bool, error) {
118118

119119
func (k *KSMClient) writeMetrics(endpoint *url.URL, w io.Writer) error {
120120
if endpoint == nil {
121-
return errors.New("Endpoint is nil")
121+
return errors.New("endpoint is nil")
122122
}
123123

124124
u := *endpoint
@@ -165,7 +165,7 @@ func (f *Framework) ParseMetrics(metrics func(io.Writer) error) (map[string]*dto
165165
buf := &bytes.Buffer{}
166166
err := metrics(buf)
167167
if err != nil {
168-
return nil, fmt.Errorf("Failed to get metrics: %w", err)
168+
return nil, fmt.Errorf("failed to get metrics: %w", err)
169169
}
170170

171171
parser := &expfmt.TextParser{}

tests/lib/lib_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestAsLibrary(t *testing.T) {
6262
}
6363
m := w.String()
6464

65-
if !strings.Contains(m, service.ObjectMeta.Name) {
65+
if !strings.Contains(m, service.Name) {
6666
t.Fatal("expected string to contain service name")
6767
}
6868
}

0 commit comments

Comments
 (0)