diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c14a0f34..d002e2f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/.golangci.yml b/.golangci.yml index d6df958f..bdf604f0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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$ diff --git a/internal/store/clusterrole.go b/internal/store/clusterrole.go index 127d513a..374a48a1 100644 --- a/internal/store/clusterrole.go +++ b/internal/store/clusterrole.go @@ -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), } }), ), diff --git a/internal/store/clusterrolebinding.go b/internal/store/clusterrolebinding.go index 49409522..302a6b40 100644 --- a/internal/store/clusterrolebinding.go +++ b/internal/store/clusterrolebinding.go @@ -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), } }), ), diff --git a/internal/store/configmap.go b/internal/store/configmap.go index dff11ea5..7fc7f246 100644 --- a/internal/store/configmap.go +++ b/internal/store/configmap.go @@ -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), } }), ), diff --git a/internal/store/cronjob.go b/internal/store/cronjob.go index 1b335b9d..eae698fa 100644 --- a/internal/store/cronjob.go +++ b/internal/store/cronjob.go @@ -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 diff --git a/internal/store/daemonset.go b/internal/store/daemonset.go index 31d2911a..2ef2bb0c 100644 --- a/internal/store/daemonset.go +++ b/internal/store/daemonset.go @@ -217,7 +217,7 @@ func daemonSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []g { LabelKeys: []string{}, LabelValues: []string{}, - Value: float64(d.ObjectMeta.Generation), + Value: float64(d.Generation), }, }, } diff --git a/internal/store/deployment.go b/internal/store/deployment.go index b204cdb3..7f902b0c 100644 --- a/internal/store/deployment.go +++ b/internal/store/deployment.go @@ -277,7 +277,7 @@ func deploymentMetricFamilies(allowAnnotationsList, allowLabelsList []string) [] return &metric.Family{ Metrics: []*metric.Metric{ { - Value: float64(d.ObjectMeta.Generation), + Value: float64(d.Generation), }, }, } diff --git a/internal/store/horizontalpodautoscaler.go b/internal/store/horizontalpodautoscaler.go index 3994f14c..d0bc3f28 100644 --- a/internal/store/horizontalpodautoscaler.go +++ b/internal/store/horizontalpodautoscaler.go @@ -134,7 +134,7 @@ func createHPAMetaDataGeneration() generator.FamilyGenerator { return &metric.Family{ Metrics: []*metric.Metric{ { - Value: float64(a.ObjectMeta.Generation), + Value: float64(a.Generation), }, }, } diff --git a/internal/store/ingress.go b/internal/store/ingress.go index ef046616..99347596 100644 --- a/internal/store/ingress.go +++ b/internal/store/ingress.go @@ -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), } }), ), diff --git a/internal/store/mutatingwebhookconfiguration.go b/internal/store/mutatingwebhookconfiguration.go index 394fd848..8380346a 100644 --- a/internal/store/mutatingwebhookconfiguration.go +++ b/internal/store/mutatingwebhookconfiguration.go @@ -78,7 +78,7 @@ var ( "", wrapMutatingWebhookConfigurationFunc(func(mwc *admissionregistrationv1.MutatingWebhookConfiguration) *metric.Family { return &metric.Family{ - Metrics: resourceVersionMetric(mwc.ObjectMeta.ResourceVersion), + Metrics: resourceVersionMetric(mwc.ResourceVersion), } }), ), diff --git a/internal/store/persistentvolume.go b/internal/store/persistentvolume.go index 13e916cf..c24ea76d 100644 --- a/internal/store/persistentvolume.go +++ b/internal/store/persistentvolume.go @@ -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 } } diff --git a/internal/store/replicaset.go b/internal/store/replicaset.go index 76706aae..192299ff 100644 --- a/internal/store/replicaset.go +++ b/internal/store/replicaset.go @@ -158,7 +158,7 @@ func replicaSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) [] return &metric.Family{ Metrics: []*metric.Metric{ { - Value: float64(r.ObjectMeta.Generation), + Value: float64(r.Generation), }, }, } diff --git a/internal/store/replicationcontroller.go b/internal/store/replicationcontroller.go index fee30188..f29b1a92 100644 --- a/internal/store/replicationcontroller.go +++ b/internal/store/replicationcontroller.go @@ -166,7 +166,7 @@ var ( return &metric.Family{ Metrics: []*metric.Metric{ { - Value: float64(r.ObjectMeta.Generation), + Value: float64(r.Generation), }, }, } diff --git a/internal/store/role.go b/internal/store/role.go index 02bfb0d3..1ba5707c 100644 --- a/internal/store/role.go +++ b/internal/store/role.go @@ -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), } }), ), diff --git a/internal/store/rolebinding.go b/internal/store/rolebinding.go index b3bd6146..f371581d 100644 --- a/internal/store/rolebinding.go +++ b/internal/store/rolebinding.go @@ -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), } }), ), diff --git a/internal/store/secret.go b/internal/store/secret.go index 36d115c0..644cbb77 100644 --- a/internal/store/secret.go +++ b/internal/store/secret.go @@ -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), } }), ), diff --git a/internal/store/statefulset.go b/internal/store/statefulset.go index 87f7e36e..2cb6329d 100644 --- a/internal/store/statefulset.go +++ b/internal/store/statefulset.go @@ -208,7 +208,7 @@ func statefulSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) [ return &metric.Family{ Metrics: []*metric.Metric{ { - Value: float64(s.ObjectMeta.Generation), + Value: float64(s.Generation), }, }, } diff --git a/internal/store/utils_test.go b/internal/store/utils_test.go index c91256e7..6356b1ee 100644 --- a/internal/store/utils_test.go +++ b/internal/store/utils_test.go @@ -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]) } } diff --git a/internal/store/validatingwebhookconfiguration.go b/internal/store/validatingwebhookconfiguration.go index c740598c..070daba5 100644 --- a/internal/store/validatingwebhookconfiguration.go +++ b/internal/store/validatingwebhookconfiguration.go @@ -78,7 +78,7 @@ var ( "", wrapValidatingWebhookConfigurationFunc(func(vwc *admissionregistrationv1.ValidatingWebhookConfiguration) *metric.Family { return &metric.Family{ - Metrics: resourceVersionMetric(vwc.ObjectMeta.ResourceVersion), + Metrics: resourceVersionMetric(vwc.ResourceVersion), } }), ), diff --git a/pkg/app/server.go b/pkg/app/server.go index 8ab3ad54..34450729 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -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 } diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 5d2f22b0..0cd183df 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -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] diff --git a/pkg/options/types_test.go b/pkg/options/types_test.go index a1b43a2c..94f64fba 100644 --- a/pkg/options/types_test.go +++ b/pkg/options/types_test.go @@ -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) } } diff --git a/pkg/sharding/listwatch.go b/pkg/sharding/listwatch.go index 9f83c345..99ca091a 100644 --- a/pkg/sharding/listwatch.go +++ b/pkg/sharding/listwatch.go @@ -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 } diff --git a/pkg/sharding/listwatch_test.go b/pkg/sharding/listwatch_test.go index b2466420..90c0312b 100644 --- a/pkg/sharding/listwatch_test.go +++ b/pkg/sharding/listwatch_test.go @@ -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.") } diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 539743d2..430cddfe 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -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, "/") diff --git a/tests/e2e/framework/framework.go b/tests/e2e/framework/framework.go index 65e37463..9f36993a 100644 --- a/tests/e2e/framework/framework.go +++ b/tests/e2e/framework/framework.go @@ -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{} diff --git a/tests/lib/lib_test.go b/tests/lib/lib_test.go index 25c0cd42..0926fc9e 100644 --- a/tests/lib/lib_test.go +++ b/tests/lib/lib_test.go @@ -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") } }