Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add errcheck linter with golangci-lint #1426

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
run:
timeout: 5m

linters-settings:
golint:
min-confidence: 0
misspell:
locale: US

linters:
disable-all: true
enable:
- gofmt
- golint
- errcheck
# TODO(oxddr): enable more checkers
# - deadcode
# - errcheck
# - misspell
wojtek-t marked this conversation as resolved.
Show resolved Hide resolved

service:
golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
2 changes: 1 addition & 1 deletion benchmark/pkg/comparer/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func CompareJobsUsingScheme(jobComparisonData *util.JobComparisonData, scheme st
schemes.CompareJobsUsingKSTest(jobComparisonData, matchThreshold, minMetricAvgForCompare)
return nil
default:
return fmt.Errorf("Unknown comparison scheme '%v'", scheme)
return fmt.Errorf("unknown comparison scheme '%v'", scheme)
}
}
2 changes: 1 addition & 1 deletion benchmark/pkg/metricsfetcher/runselector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func GetJobRunsUsingScheme(job string, scheme string, nValue int, utils util.Job
case LastNHours:
return schemes.GetJobRunsFromLastNHours(job, nValue, utils)
default:
return nil, fmt.Errorf("Unknown run selection scheme '%v'", scheme)
return nil, fmt.Errorf("unknown run selection scheme '%v'", scheme)
}
}
12 changes: 6 additions & 6 deletions benchmark/pkg/metricsfetcher/util/mock_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MockJobLogUtils struct {
func (utils MockJobLogUtils) GetLatestBuildNumberForJob(job string) (int, error) {
length := len(utils.MockBuildNumbers)
if length == 0 {
return 0, fmt.Errorf("Array of mock build numbers is empty")
return 0, fmt.Errorf("array of mock build numbers is empty")
}
return utils.MockBuildNumbers[length-1], nil
}
Expand All @@ -49,7 +49,7 @@ func (utils MockJobLogUtils) GetBuildNumbersForJob(job string) ([]int, error) {
func (utils MockJobLogUtils) GetJobRunStartTimestamp(job string, run int) (uint64, error) {
value, ok := utils.MockStartTimestamps[run]
if !ok {
return 0, fmt.Errorf("Run number %v not a key in the mock start timestamps map", run)
return 0, fmt.Errorf("run number %v not a key in the mock start timestamps map", run)
}
return value, nil
}
Expand All @@ -58,7 +58,7 @@ func (utils MockJobLogUtils) GetJobRunStartTimestamp(job string, run int) (uint6
func (utils MockJobLogUtils) GetJobRunFinishedStatus(job string, run int) (bool, error) {
value, ok := utils.MockFinishedStatuses[run]
if !ok {
return false, fmt.Errorf("Run number %v not a key in the mock finished statuses map", run)
return false, fmt.Errorf("run number %v not a key in the mock finished statuses map", run)
}
return value, nil
}
Expand All @@ -67,11 +67,11 @@ func (utils MockJobLogUtils) GetJobRunFinishedStatus(job string, run int) (bool,
func (utils MockJobLogUtils) GetJobRunFileContents(job string, run int, filepath string) ([]byte, error) {
files, ok := utils.MockFileContents[run]
if !ok {
return nil, fmt.Errorf("Run number %v not a 1st key in the mock file contents map", run)
return nil, fmt.Errorf("run number %v not a 1st key in the mock file contents map", run)
}
file, ok := files[filepath]
if !ok {
return nil, fmt.Errorf("Filepath %v not a 2nd key in the mock file contents map for 1st key %v", filepath, run)
return nil, fmt.Errorf("filepath %v not a 2nd key in the mock file contents map for 1st key %v", filepath, run)
}
return file, nil
}
Expand All @@ -80,7 +80,7 @@ func (utils MockJobLogUtils) GetJobRunFileContents(job string, run int, filepath
func (utils MockJobLogUtils) ListJobRunFilesWithPrefix(job string, run int, prefix string) ([]string, error) {
filesWithPrefixes, ok := utils.MockFilesWithPrefix[run]
if !ok {
return nil, fmt.Errorf("Run number %v not a 1st key in the mock files with prefix map", run)
return nil, fmt.Errorf("run number %v not a 1st key in the mock files with prefix map", run)
}
filesWithPrefix, ok := filesWithPrefixes[prefix]
return filesWithPrefix, nil
Expand Down
4 changes: 2 additions & 2 deletions benchmark/pkg/metricsfetcher/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (utils GCSLogUtils) GetJobRunFinishedStatus(job string, run int) (bool, err
func (utils GCSLogUtils) GetJobRunFileContents(job string, run int, filepath string) ([]byte, error) {
response, err := utils.googleGCSBucketUtils.GetFileFromJenkinsGoogleBucket(job, run, filepath)
if err != nil {
return nil, fmt.Errorf("Couldn't read file from GCS: %v", err)
return nil, fmt.Errorf("couldn't read file from GCS: %v", err)
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
Expand All @@ -98,6 +98,6 @@ func GetJobLogUtilsForMode(mode string) (JobLogUtils, error) {
case GCS:
return NewGCSLogUtils(), nil
default:
return nil, fmt.Errorf("Unknown source mode '%v'", mode)
return nil, fmt.Errorf("unknown source mode '%v'", mode)
}
}
10 changes: 8 additions & 2 deletions clusterloader2/cmd/clusterloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ func initClusterFlags() {
flags.StringEnvVar(&clusterLoaderConfig.ClusterConfig.EtcdKeyPath, "etcd-key", "ETCD_KEY", "/etc/srv/kubernetes/pki/etcd-apiserver-server.key", "Path to the etcd key on the master machine")
flags.IntEnvVar(&clusterLoaderConfig.ClusterConfig.EtcdInsecurePort, "etcd-insecure-port", "ETCD_INSECURE_PORT", 2382, "Inscure http port")
flags.BoolEnvVar(&clusterLoaderConfig.ClusterConfig.DeleteStaleNamespaces, "delete-stale-namespaces", "DELETE_STALE_NAMESPACES", false, "DEPRECATED: Whether to delete all stale namespaces before the test execution.")
flags.MarkDeprecated("delete-stale-namespaces", "specify deleteStaleNamespaces in testconfig file instead.")
err := flags.MarkDeprecated("delete-stale-namespaces", "specify deleteStaleNamespaces in testconfig file instead.")
if err != nil {
klog.Fatalf("unable to mark flag delete-stale-namespaces deprecated %v", err)
}
flags.BoolEnvVar(&clusterLoaderConfig.ClusterConfig.DeleteAutomanagedNamespaces, "delete-automanaged-namespaces", "DELETE_AUTOMANAGED_NAMESPACES", true, "DEPRECATED: Whether to delete all automanaged namespaces after the test execution.")
flags.MarkDeprecated("delete-automanaged-namespaces", "specify deleteAutomanagedNamespaces in testconfig file instead.")
err = flags.MarkDeprecated("delete-automanaged-namespaces", "specify deleteAutomanagedNamespaces in testconfig file instead.")
if err != nil {
klog.Fatalf("unable to mark flag delete-automanaged-namespaces deprecated %v", err)
}
flags.StringEnvVar(&clusterLoaderConfig.ClusterConfig.MasterName, "mastername", "MASTER_NAME", "", "Name of the masternode")
// TODO(#595): Change the name of the MASTER_IP and MASTER_INTERNAL_IP flags and vars to plural
flags.StringSliceEnvVar(&clusterLoaderConfig.ClusterConfig.MasterIPs, "masterip", "MASTER_IP", nil /*defaultValue*/, "Hostname/IP of the master node, supports multiple values when separated by commas")
Expand Down
5 changes: 4 additions & 1 deletion clusterloader2/pkg/config/template_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ func GetMapping(clusterLoaderConfig *ClusterLoaderConfig) (map[string]interface{
if err != nil {
return nil, errors.NewErrorList(goerrors.Errorf("mapping creation error: %v", err))
}
MergeMappings(mapping, envMapping)
err = MergeMappings(mapping, envMapping)
if err != nil {
return nil, errors.NewErrorList(goerrors.Errorf("mapping merging error: %v", err))
}
return mapping, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const (
)

func init() {
measurement.Register(serviceCreationLatencyName, createServiceCreationLatencyMeasurement)
if err := measurement.Register(serviceCreationLatencyName, createServiceCreationLatencyMeasurement); err != nil {
klog.Fatalf("cant register service %v", err)
}
}

func createServiceCreationLatencyMeasurement() measurement.Measurement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ var (
klogLogToStderr = true
)

func turnOffLoggingToStderrInKlog() {
func turnOffLoggingToStderrInKlog(t *testing.T) {
if klogLogToStderr {
klog.InitFlags(nil)
flag.Set("logtostderr", "false")
flag.Set("v", "2")
err := flag.Set("logtostderr", "false")
if err != nil {
t.Errorf("Unable to set flag %v", err)
return
}
err = flag.Set("v", "2")
if err != nil {
t.Errorf("Unable to set flag %v", err)
return
}
flag.Parse()
klogLogToStderr = false
}
Expand Down Expand Up @@ -554,7 +562,7 @@ func TestLogging(t *testing.T) {
},
}

turnOffLoggingToStderrInKlog()
turnOffLoggingToStderrInKlog(t)

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -565,7 +573,10 @@ func TestLogging(t *testing.T) {
gatherer := &apiResponsivenessGatherer{}
config := &measurement.Config{}

gatherer.Gather(executor, time.Now(), config)
_, err := gatherer.Gather(executor, time.Now(), config)
if err != nil && !errors.IsMetricViolationError(err) {
t.Errorf("error while gathering results: %v", err)
}
klog.Flush()

for _, msg := range tc.expectedMessages {
Expand Down Expand Up @@ -717,7 +728,7 @@ func TestAPIResponsivenessCustomThresholds(t *testing.T) {
},
}

turnOffLoggingToStderrInKlog()
turnOffLoggingToStderrInKlog(t)

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ const (
)

func init() {
measurement.Register(podStartupLatencyMeasurementName, createPodStartupLatencyMeasurement)
if err := measurement.Register(podStartupLatencyMeasurementName, createPodStartupLatencyMeasurement); err != nil {
klog.Fatalf("cant register service %v", err)
}
}

func createPodStartupLatencyMeasurement() measurement.Measurement {
Expand Down
2 changes: 1 addition & 1 deletion clusterloader2/pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (pc *Controller) runNodeExporter() error {
g.Go(func() error {
f, err := os.Open(os.ExpandEnv(pc.clusterLoaderConfig.PrometheusConfig.NodeExporterPod))
if err != nil {
return fmt.Errorf("Unable to open manifest file: %v", err)
return fmt.Errorf("unable to open manifest file: %v", err)
}
defer f.Close()
return pc.ssh.Exec("sudo tee /etc/kubernetes/manifests/node-exporter.yaml > /dev/null", &node, f)
Expand Down
4 changes: 2 additions & 2 deletions clusterloader2/pkg/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func runSSHCommand(cmd, host string) (string, string, int, error) {
return "", "", 0, err
}
if signer == nil {
return "", "", 0, fmt.Errorf("Cannot find sshkey")
return "", "", 0, fmt.Errorf("cannot find sshkey")
}

user := defaultSSHUser()
Expand Down Expand Up @@ -89,7 +89,7 @@ func sshSignerFromKeyFile(KeyPathEnv string, defaultKeyPath string) (ssh.Signer,
}

if keyfile == "" {
return nil, fmt.Errorf("Cannot find ssh key file")
return nil, fmt.Errorf("cannot find ssh key file")
}

return sshutil.MakePrivateKeySignerFromFile(keyfile)
Expand Down
7 changes: 6 additions & 1 deletion clusterloader2/pkg/tuningset/global_qps_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
"k8s.io/perf-tests/clusterloader2/api"

"golang.org/x/time/rate"
Expand Down Expand Up @@ -59,7 +60,11 @@ func newGlobalQPSLoad(params *api.GlobalQPSLoad) *globalQPSLoad {
func (ql *globalQPSLoad) Execute(actions []func()) {
var wg wait.Group
for i := range actions {
ql.limiter.Wait(context.TODO())
err := ql.limiter.Wait(context.TODO())
if err != nil {
klog.Errorf("Error while waiting for rate limitter: %v - skipping the action", err)
continue
}
wg.Start(actions[i])
}
wg.Wait()
Expand Down
23 changes: 19 additions & 4 deletions network/benchmarks/netperf/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,22 @@ func cleanup(c *kubernetes.Clientset) {
}
for _, svc := range svcs.Items {
fmt.Println("Deleting svc", svc.GetName())
c.Core().Services(testNamespace).Delete(
err := c.Core().Services(testNamespace).Delete(
svc.GetName(), &metav1.DeleteOptions{})
if err != nil {
fmt.Println("Failed to get service", err)
}
}
}

// createServices: Long-winded function to programmatically create our two services
func createServices(c *kubernetes.Clientset) bool {
// Create our namespace if not present
if _, err := c.Core().Namespaces().Get(testNamespace, metav1.GetOptions{}); err != nil {
c.Core().Namespaces().Create(&api.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}})
_, err := c.Core().Namespaces().Create(&api.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}})
if err != nil {
fmt.Println("Failed to create service", err)
}
}

// Create the orchestrator service that points to the coordinator pod
Expand Down Expand Up @@ -344,14 +350,23 @@ func processCsvData(csvData *string) bool {
outputFilePrefix := fmt.Sprintf("%s-%s_%s.", testNamespace, tag, t.Format("20060102150405"))
fmt.Printf("Test concluded - CSV raw data written to %s/%scsv\n", outputFileDirectory, outputFilePrefix)
if _, err := os.Stat(outputFileDirectory); os.IsNotExist(err) {
os.Mkdir(outputFileDirectory, 0766)
err := os.Mkdir(outputFileDirectory, 0766)
if err != nil {
fmt.Println("Error creating directory", err)
return false
}

}
fd, err := os.OpenFile(fmt.Sprintf("%s/%scsv", outputFileDirectory, outputFilePrefix), os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
fmt.Println("ERROR writing output CSV datafile", err)
return false
}
fd.WriteString(*csvData)
_, err = fd.WriteString(*csvData)
if err != nil {
fmt.Println("Error writing string", err)
return false
}
fd.Close()
return true
}
Expand Down
20 changes: 16 additions & 4 deletions network/benchmarks/netperf/nptest/nptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,19 @@ func (t *NetPerfRPC) ReceiveOutput(data *WorkerOutput, reply *int) error {

func serveRPCRequests(port string) {
baseObject := new(NetPerfRPC)
rpc.Register(baseObject)
err := rpc.Register(baseObject)
if err != nil {
log.Fatal("failed to register rpc", err)
}
rpc.HandleHTTP()
listener, e := net.Listen("tcp", ":"+port)
if e != nil {
log.Fatal("listen error:", e)
}
http.Serve(listener, nil)
err = http.Serve(listener, nil)
if err != nil {
log.Fatal("failed start server", err)
}
}

// Blocking RPC server start - only runs on the orchestrator
Expand Down Expand Up @@ -555,11 +561,17 @@ func handleClientWorkItem(client *rpc.Client, workItem *WorkItem) {
case workItem.ClientItem.Type == iperfTCPTest || workItem.ClientItem.Type == iperfUDPTest || workItem.ClientItem.Type == iperfSctpTest:
outputString := iperfClient(workItem.ClientItem.Host, workItem.ClientItem.Port, workItem.ClientItem.MSS, workItem.ClientItem.Type)
var reply int
client.Call("NetPerfRPC.ReceiveOutput", WorkerOutput{Output: outputString, Worker: worker, Type: workItem.ClientItem.Type}, &reply)
err := client.Call("NetPerfRPC.ReceiveOutput", WorkerOutput{Output: outputString, Worker: worker, Type: workItem.ClientItem.Type}, &reply)
if err != nil {
log.Fatal("failed to call client", err)
}
case workItem.ClientItem.Type == netperfTest:
outputString := netperfClient(workItem.ClientItem.Host, workItem.ClientItem.Port, workItem.ClientItem.Type)
var reply int
client.Call("NetPerfRPC.ReceiveOutput", WorkerOutput{Output: outputString, Worker: worker, Type: workItem.ClientItem.Type}, &reply)
err := client.Call("NetPerfRPC.ReceiveOutput", WorkerOutput{Output: outputString, Worker: worker, Type: workItem.ClientItem.Type}, &reply)
if err != nil {
log.Fatal("failed to call client", err)
}
}
// Client COOLDOWN period before asking for next work item to replenish burst allowance policers etc
time.Sleep(10 * time.Second)
Expand Down
12 changes: 9 additions & 3 deletions perfdash/metrics-downloader-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ func serveHTTPObject(res http.ResponseWriter, req *http.Request, obj interface{}
if err != nil {
res.Header().Set("Content-type", "text/html")
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(fmt.Sprintf("<h3>Internal Error</h3><p>%v", err)))
_, err = res.Write([]byte(fmt.Sprintf("<h3>Internal Error</h3><p>%v", err)))
if err != nil {
klog.Errorf("unable to write error %v", err)
}
return
}
res.Header().Set("Content-type", "application/json")
res.WriteHeader(http.StatusOK)
res.Write(data)
_, err = res.Write(data)
if err != nil {
klog.Errorf("unable to write response data %v", err)
}
}

func getURLParam(req *http.Request, name string) (string, bool) {
Expand All @@ -80,7 +86,7 @@ func (j *JobToCategoryData) ServeJobNames(res http.ResponseWriter, req *http.Req
func (j *JobToCategoryData) ServeCategoryNames(res http.ResponseWriter, req *http.Request) {
jobname, ok := getURLParam(req, "jobname")
if !ok {
klog.Warningf("Url Param 'jobname' is missing")
klog.Warningf("url Param 'jobname' is missing")
return
}

Expand Down
Loading