Skip to content

Commit d24467f

Browse files
authored
OCPBUGS-45421: Linter fixes (#1253)
* Linter fixes Golangci-linter configuration ".ci-operator.yaml" was configured in a way which was showing a lot of false positives and also missing a lot of issues. Fix both the linter configuration and the missing issues that found its way into the codebase. * Remove misspelt word coming from openshift/kubernetes * Keep e2e-gcp-pao-updating-profile happy Tests in 7_performance_kubelet_node/kubelet.go are broken and need fixing, keep the current semantics and make the linter pass. --------- Co-authored-by: Jiri Mencak <[email protected]>
1 parent 729cd59 commit d24467f

File tree

74 files changed

+434
-359
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+434
-359
lines changed

.ci-operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
build_root_image:
22
name: release
33
namespace: openshift
4-
tag: rhel-9-release-golang-1.22-openshift-4.18
4+
tag: rhel-9-release-golang-1.23-openshift-4.19

.golangci.yaml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
run:
22
timeout: 5m
3-
tests: false
4-
skip-dirs:
5-
- vendor
6-
- test
73
modules-download-mode: vendor
4+
issues:
5+
exclude-dirs:
6+
- vendor
7+
exclude-rules:
8+
# Temporarily disable the deprecation warnings until we rewrite the code based on deprecated functionality.
9+
- linters:
10+
- staticcheck
11+
text: "SA1019: .* deprecated"
812
linters:
913
disable-all: true
1014
enable:
1115
- errcheck
1216
- bodyclose
13-
- exportloopref
1417
- gosimple
1518
- govet
1619
- ineffassign
@@ -25,8 +28,14 @@ linters:
2528
- wastedassign
2629
- whitespace
2730
linters-settings:
31+
errcheck:
32+
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
33+
# default is false: such cases aren't reported by default.
34+
check-blank: false
2835
misspell:
29-
locale: US
36+
# Do not set locale explicitly, the default is to use a neutral variety of English.
37+
# Setting locale to US will cause correcting the British spelling of 'colour' to 'color'.
38+
# locale: US
3039
ignore-words:
3140
- NTO
3241
- nto

Dockerfile.rhel9

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.22-openshift-4.18 AS builder
1+
FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.23-openshift-4.19 AS builder
22
WORKDIR /go/src/github.com/openshift/cluster-node-tuning-operator
33
COPY . .
44
RUN make build
55

6-
FROM registry.ci.openshift.org/ocp/4.18:base-rhel9
6+
FROM registry.ci.openshift.org/ocp/4.19:base-rhel9
77
COPY --from=builder /go/src/github.com/openshift/cluster-node-tuning-operator/_output/cluster-node-tuning-operator /usr/bin/
88
COPY --from=builder /go/src/github.com/openshift/cluster-node-tuning-operator/_output/performance-profile-creator /usr/bin/
99
COPY --from=builder /go/src/github.com/openshift/cluster-node-tuning-operator/_output/gather-sysinfo /usr/bin/

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ PAO_CRD_APIS :=$(addprefix ./$(API_TYPES_DIR)/performanceprofile/,v2 v1 v1alpha1
5555
PAO_E2E_SUITES := $(shell hack/list-test-bin.sh)
5656

5757
# golangci-lint variables
58-
GOLANGCI_LINT_VERSION=1.54.2
58+
GOLANGCI_LINT_VERSION=1.62.2
5959
GOLANGCI_LINT_BIN=$(OUT_DIR)/golangci-lint
6060
GOLANGCI_LINT_VERSION_TAG=v${GOLANGCI_LINT_VERSION}
6161

cmd/gather-sysinfo/gather-sysinfo_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"io/ioutil"
54
"os"
65
"strings"
76
"testing"
@@ -63,7 +62,7 @@ func TestCollectMachineInfo(t *testing.T) {
6362
t.Errorf("Collection of machine info failed: %v", err)
6463
}
6564

66-
content, err := ioutil.ReadFile(destFile)
65+
content, err := os.ReadFile(destFile)
6766
if err != nil {
6867
t.Errorf("Reading of generated output failed: %v", err)
6968
}

pkg/apis/performanceprofile/performance_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package performance
22

33
import (
4-
"io/ioutil"
4+
"os"
55
"strings"
66

77
"github.com/RHsyseng/operator-utils/pkg/validation"
@@ -53,7 +53,7 @@ var _ = Describe("PerformanceProfile CR(D) Schema", func() {
5353
// getSchema reads in & returns CRD schema file as openAPIV3Schema{} for validation usage.
5454
// See references operator-utils/validation/schema & go-openapi/spec/schema
5555
func getSchema(crdPath string) (validation.Schema, error) {
56-
bytes, err := ioutil.ReadFile(crdPath)
56+
bytes, err := os.ReadFile(crdPath)
5757
if err != nil {
5858
return nil, err
5959
}
@@ -66,7 +66,7 @@ func getSchema(crdPath string) (validation.Schema, error) {
6666

6767
// getCR unmarshals a *_cr.yaml file and returns the representing struct
6868
func getCR(crPath string) (map[string]interface{}, error) {
69-
bytes, err := ioutil.ReadFile(crPath)
69+
bytes, err := os.ReadFile(crPath)
7070
if err != nil {
7171
return nil, err
7272
}

pkg/apis/performanceprofile/v2/performanceprofile_validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ func validateNoIntersectionExists(lists *components.CPULists, allErrs field.Erro
216216
func (r *PerformanceProfile) validateSelectors() field.ErrorList {
217217
var allErrs field.ErrorList
218218

219-
if r.Spec.MachineConfigLabel != nil && len(r.Spec.MachineConfigLabel) > 1 {
219+
if len(r.Spec.MachineConfigLabel) > 1 {
220220
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.machineConfigLabel"), r.Spec.MachineConfigLabel, "you should provide only 1 MachineConfigLabel"))
221221
}
222222

223-
if r.Spec.MachineConfigPoolSelector != nil && len(r.Spec.MachineConfigPoolSelector) > 1 {
223+
if len(r.Spec.MachineConfigPoolSelector) > 1 {
224224
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.machineConfigPoolSelector"), r.Spec.MachineConfigLabel, "you should provide only 1 MachineConfigPoolSelector"))
225225
}
226226

pkg/apis/performanceprofile/v2/performanceprofile_validation_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
corev1 "k8s.io/api/core/v1"
1313
"k8s.io/apimachinery/pkg/api/resource"
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15-
"k8s.io/utils/pointer"
15+
"k8s.io/utils/ptr"
1616
)
1717

1818
const (
@@ -126,13 +126,13 @@ func NewPerformanceProfile(name string) *PerformanceProfile {
126126
},
127127
},
128128
RealTimeKernel: &RealTimeKernel{
129-
Enabled: pointer.Bool(true),
129+
Enabled: ptr.To(true),
130130
},
131131
NUMA: &NUMA{
132132
TopologyPolicy: &numaPolicy,
133133
},
134134
Net: &Net{
135-
UserLevelNetworking: pointer.Bool(true),
135+
UserLevelNetworking: ptr.To(true),
136136
Devices: []Device{
137137
{
138138
InterfaceName: &netDeviceName,
@@ -381,7 +381,7 @@ var _ = Describe("PerformanceProfile", func() {
381381
setValidNodeSelector(profile)
382382

383383
errors = profile.validateSelectors()
384-
Expect(profile.validateSelectors()).To(BeEmpty(), "should not have validation errors when machine config selector nil")
384+
Expect(errors).To(BeEmpty(), "should not have validation errors when machine config selector nil")
385385
})
386386

387387
It("should should have 0 or 1 MachineConfigPoolSelector labels", func() {
@@ -397,7 +397,7 @@ var _ = Describe("PerformanceProfile", func() {
397397
setValidNodeSelector(profile)
398398

399399
errors = profile.validateSelectors()
400-
Expect(profile.validateSelectors()).To(BeEmpty(), "should not have validation errors when machine config pool selector nil")
400+
Expect(errors).To(BeEmpty(), "should not have validation errors when machine config pool selector nil")
401401
})
402402

403403
It("should have sensible NodeSelector in case MachineConfigLabel or MachineConfigPoolSelector is empty", func() {
@@ -583,7 +583,7 @@ var _ = Describe("PerformanceProfile", func() {
583583

584584
profile.Spec.HugePages.Pages = append(profile.Spec.HugePages.Pages, HugePage{
585585
Count: 128,
586-
Node: pointer.Int32(0),
586+
Node: ptr.To(int32(0)),
587587
Size: "14M",
588588
})
589589
errors := profile.validateHugePages(nodes)
@@ -604,7 +604,7 @@ var _ = Describe("PerformanceProfile", func() {
604604

605605
profile.Spec.HugePages.Pages = append(profile.Spec.HugePages.Pages, HugePage{
606606
Count: 128,
607-
Node: pointer.Int32(0),
607+
Node: ptr.To(int32(0)),
608608
Size: "14M",
609609
})
610610
errors := profile.validateHugePages(nodes)
@@ -635,7 +635,7 @@ var _ = Describe("PerformanceProfile", func() {
635635

636636
profile.Spec.HugePages.Pages = append(profile.Spec.HugePages.Pages, HugePage{
637637
Count: 128,
638-
Node: pointer.Int32(0),
638+
Node: ptr.To(int32(0)),
639639
Size: "14M",
640640
})
641641

@@ -661,12 +661,12 @@ var _ = Describe("PerformanceProfile", func() {
661661
profile.Spec.HugePages.Pages = append(profile.Spec.HugePages.Pages, HugePage{
662662
Count: 128,
663663
Size: hugepagesSize1G,
664-
Node: pointer.Int32(0),
664+
Node: ptr.To(int32(0)),
665665
})
666666
profile.Spec.HugePages.Pages = append(profile.Spec.HugePages.Pages, HugePage{
667667
Count: 64,
668668
Size: hugepagesSize1G,
669-
Node: pointer.Int32(0),
669+
Node: ptr.To(int32(0)),
670670
})
671671
errors := profile.validateHugePages(nodes)
672672
Expect(errors).NotTo(BeEmpty())
@@ -729,33 +729,33 @@ var _ = Describe("PerformanceProfile", func() {
729729
It("should raise the validation syntax errors", func() {
730730
invalidVendor := "123"
731731
invalidDevice := "0x12345"
732-
profile.Spec.Net.Devices[0].InterfaceName = pointer.String("")
733-
profile.Spec.Net.Devices[0].VendorID = pointer.String(invalidVendor)
734-
profile.Spec.Net.Devices[0].DeviceID = pointer.String(invalidDevice)
732+
profile.Spec.Net.Devices[0].InterfaceName = ptr.To("")
733+
profile.Spec.Net.Devices[0].VendorID = ptr.To(invalidVendor)
734+
profile.Spec.Net.Devices[0].DeviceID = ptr.To(invalidDevice)
735735
errors := profile.validateNet()
736736
Expect(len(errors)).To(Equal(3))
737-
Expect(errors[0].Error()).To(ContainSubstring(fmt.Sprintf("device name cannot be empty")))
737+
Expect(errors[0].Error()).To(ContainSubstring("device name cannot be empty"))
738738
Expect(errors[1].Error()).To(ContainSubstring(fmt.Sprintf("device vendor ID %s has an invalid format. Vendor ID should be represented as 0x<4 hexadecimal digits> (16 bit representation)", invalidVendor)))
739739
Expect(errors[2].Error()).To(ContainSubstring(fmt.Sprintf("device model ID %s has an invalid format. Model ID should be represented as 0x<4 hexadecimal digits> (16 bit representation)", invalidDevice)))
740740

741741
})
742742
It("should raise the validation errors for missing fields", func() {
743743
profile.Spec.Net.Devices[0].VendorID = nil
744-
profile.Spec.Net.Devices[0].DeviceID = pointer.String("0x1")
744+
profile.Spec.Net.Devices[0].DeviceID = ptr.To("0x1")
745745
errors := profile.validateNet()
746746
Expect(errors).NotTo(BeEmpty())
747-
Expect(errors[0].Error()).To(ContainSubstring(fmt.Sprintf("device model ID can not be used without specifying the device vendor ID.")))
747+
Expect(errors[0].Error()).To(ContainSubstring("device model ID can not be used without specifying the device vendor ID."))
748748
})
749749
})
750750

751751
Describe("Workload hints validation", func() {
752752
When("realtime kernel is enabled and realtime workload hint is explicitly disabled", func() {
753753
It("should raise validation error", func() {
754754
profile.Spec.WorkloadHints = &WorkloadHints{
755-
RealTime: pointer.Bool(false),
755+
RealTime: ptr.To(false),
756756
}
757757
profile.Spec.RealTimeKernel = &RealTimeKernel{
758-
Enabled: pointer.Bool(true),
758+
Enabled: ptr.To(true),
759759
}
760760
errors := profile.validateWorkloadHints()
761761
Expect(errors).NotTo(BeEmpty())
@@ -765,8 +765,8 @@ var _ = Describe("PerformanceProfile", func() {
765765
When("HighPowerConsumption hint is enabled and PerPodPowerManagement hint is enabled", func() {
766766
It("should raise validation error", func() {
767767
profile.Spec.WorkloadHints = &WorkloadHints{
768-
HighPowerConsumption: pointer.Bool(true),
769-
PerPodPowerManagement: pointer.Bool(true),
768+
HighPowerConsumption: ptr.To(true),
769+
PerPodPowerManagement: ptr.To(true),
770770
}
771771
errors := profile.validateWorkloadHints()
772772
Expect(errors).NotTo(BeEmpty())
@@ -776,7 +776,7 @@ var _ = Describe("PerformanceProfile", func() {
776776
When("MixedCPUs hint is enabled but no shared CPUs are specified", func() {
777777
It("should raise validation error", func() {
778778
profile.Spec.WorkloadHints = &WorkloadHints{
779-
MixedCpus: pointer.Bool(true),
779+
MixedCpus: ptr.To(true),
780780
}
781781
errors := profile.validateWorkloadHints()
782782
Expect(errors).NotTo(BeEmpty())
@@ -807,17 +807,17 @@ var _ = Describe("PerformanceProfile", func() {
807807
profile.Spec.HugePages.DefaultHugePagesSize = &incorrectDefaultSize
808808

809809
profile.Spec.WorkloadHints = &WorkloadHints{
810-
RealTime: pointer.Bool(false),
810+
RealTime: ptr.To(false),
811811
}
812812
profile.Spec.RealTimeKernel = &RealTimeKernel{
813-
Enabled: pointer.Bool(true),
813+
Enabled: ptr.To(true),
814814
}
815815

816816
invalidVendor := "123"
817817
invalidDevice := "0x12345"
818-
profile.Spec.Net.Devices[0].InterfaceName = pointer.String("")
819-
profile.Spec.Net.Devices[0].VendorID = pointer.String(invalidVendor)
820-
profile.Spec.Net.Devices[0].DeviceID = pointer.String(invalidDevice)
818+
profile.Spec.Net.Devices[0].InterfaceName = ptr.To("")
819+
profile.Spec.Net.Devices[0].VendorID = ptr.To(invalidVendor)
820+
profile.Spec.Net.Devices[0].DeviceID = ptr.To(invalidDevice)
821821

822822
errors := profile.ValidateBasicFields()
823823

pkg/operator/status.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package operator
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78

89
configv1 "github.com/openshift/api/config/v1"
910
operatorv1 "github.com/openshift/api/operator/v1"
1011
operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers"
1112
corev1 "k8s.io/api/core/v1"
12-
"k8s.io/apimachinery/pkg/api/errors"
13+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
"k8s.io/apimachinery/pkg/labels"
1516
"k8s.io/klog/v2"
@@ -80,7 +81,7 @@ func (c *Controller) syncOperatorStatus(tuned *tunedv1.Tuned) error {
8081
func (c *Controller) getOrCreateOperatorStatus() (*configv1.ClusterOperator, error) {
8182
co, err := c.listers.ClusterOperators.Get(tunedv1.TunedClusterOperatorResourceName)
8283
if err != nil {
83-
if errors.IsNotFound(err) {
84+
if apierrors.IsNotFound(err) {
8485
// Cluster operator not found, create it
8586
co = &configv1.ClusterOperator{ObjectMeta: metav1.ObjectMeta{Name: tunedv1.TunedClusterOperatorResourceName}}
8687
co, err = c.clients.ConfigV1Client.ClusterOperators().Create(context.TODO(), co, metav1.CreateOptions{})
@@ -205,7 +206,7 @@ func (c *Controller) computeStatus(tuned *tunedv1.Tuned, conditions []configv1.C
205206
ds, err := c.listers.DaemonSets.Get(dsMf.Name)
206207
if err != nil {
207208
// There was a problem fetching Tuned daemonset
208-
if errors.IsNotFound(err) {
209+
if apierrors.IsNotFound(err) {
209210
// Tuned daemonset has not been created yet
210211
if len(conditions) == 0 {
211212
// This looks like a fresh install => initialize
@@ -238,7 +239,7 @@ func (c *Controller) computeStatus(tuned *tunedv1.Tuned, conditions []configv1.C
238239
} else {
239240
if ds.Status.ObservedGeneration != ds.Generation {
240241
// Do not base the conditions on stale information
241-
return conditions, "", fmt.Errorf(errGenerationMismatch)
242+
return conditions, "", errors.New(errGenerationMismatch)
242243
}
243244

244245
dsReleaseVersion := c.getDaemonSetReleaseVersion(ds)
@@ -303,14 +304,14 @@ func (c *Controller) computeStatus(tuned *tunedv1.Tuned, conditions []configv1.C
303304
}
304305

305306
if numDegradedProfiles > 0 {
306-
klog.Infof(fmt.Sprintf("%v/%v Profiles failed to be applied", numDegradedProfiles, len(profileList)))
307+
klog.Infof("%v/%v Profiles failed to be applied", numDegradedProfiles, len(profileList))
307308
availableCondition.Reason = "ProfileDegraded"
308309
availableCondition.Message = fmt.Sprintf("%v/%v Profiles failed to be applied", numDegradedProfiles, len(profileList))
309310
}
310311

311312
numConflict := c.numProfilesWithBootcmdlineConflict(profileList)
312313
if numConflict > 0 {
313-
klog.Infof(fmt.Sprintf("%v/%v Profiles with bootcmdline conflict", numConflict, len(profileList)))
314+
klog.Infof("%v/%v Profiles with bootcmdline conflict", numConflict, len(profileList))
314315
degradedCondition.Status = configv1.ConditionTrue
315316
degradedCondition.Reason = "ProfileConflict"
316317
degradedCondition.Message = fmt.Sprintf("%v/%v Profiles with bootcmdline conflict", numConflict, len(profileList))

0 commit comments

Comments
 (0)