Skip to content

Commit

Permalink
fix some lint issues and cleanup unused code (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmdrs authored Dec 13, 2022
1 parent dff1402 commit bcedc5a
Show file tree
Hide file tree
Showing 23 changed files with 66 additions and 139 deletions.
2 changes: 1 addition & 1 deletion pkg/backends/fake/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (b *Backend) SyncStatus(ctx context.Context, _ loadTestV1.LoadTest, loadTes
// newMasterJob creates a new job which runs the Fake master pod
func (b *Backend) newMasterJob(loadTest loadTestV1.LoadTest) *batchV1.Job {
imageRef := fmt.Sprintf("%s:%s", loadTest.Spec.MasterConfig.Image, loadTest.Spec.MasterConfig.Tag)
if "" == loadTest.Spec.MasterConfig.Image || "" == loadTest.Spec.MasterConfig.Tag {
if loadTest.Spec.MasterConfig.Image == "" || loadTest.Spec.MasterConfig.Tag == "" {
imageRef = fmt.Sprintf("%s:%s", b.config.Image, b.config.Tag)
b.logger.Warn("Loadtest.Spec.MasterConfig is empty; using default master image", zap.String("imageRef", imageRef))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/backends/ghz/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *Backend) NewJob(
}

envVars := []coreV1.EnvVar{}
if "" != reportURL {
if reportURL != "" {
envVars = append(envVars, coreV1.EnvVar{
Name: "REPORT_PRESIGNED_URL",
Value: reportURL,
Expand Down
9 changes: 4 additions & 5 deletions pkg/backends/jmeter/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -112,7 +111,7 @@ func (b *Backend) NewTestdataConfigMap(loadTest loadTestV1.LoadTest) ([]*coreV1.
}
defer gz.Close()

result, err := ioutil.ReadAll(gz)
result, err := io.ReadAll(gz)
if err != nil && err != io.EOF {
logger.Error("Error on ioutil reader", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -197,7 +196,7 @@ func (b *Backend) NewPod(loadTest loadTestV1.LoadTest, i int, configMap *coreV1.
optionalVolume := true

imageRef := fmt.Sprintf("%s:%s", loadTest.Spec.WorkerConfig.Image, loadTest.Spec.WorkerConfig.Tag)
if "" == loadTest.Spec.WorkerConfig.Image || "" == loadTest.Spec.WorkerConfig.Tag {
if loadTest.Spec.WorkerConfig.Image == "" || loadTest.Spec.WorkerConfig.Tag == "" {
imageRef = fmt.Sprintf("%s:%s", b.workerConfig.Image, b.workerConfig.Tag)
logger.Debug("Loadtest.Spec.WorkerConfig is empty; using worker image from config", zap.String("imageRef", imageRef))
}
Expand Down Expand Up @@ -358,7 +357,7 @@ func (b *Backend) NewJMeterMasterJob(loadTest loadTestV1.LoadTest, reportURL str
var one int32 = 1

imageRef := fmt.Sprintf("%s:%s", loadTest.Spec.MasterConfig.Image, loadTest.Spec.MasterConfig.Tag)
if "" == loadTest.Spec.MasterConfig.Image || "" == loadTest.Spec.MasterConfig.Tag {
if loadTest.Spec.MasterConfig.Image == "" || loadTest.Spec.MasterConfig.Tag == "" {
imageRef = fmt.Sprintf("%s:%s", b.masterConfig.Image, b.masterConfig.Tag)
logger.Debug("Loadtest.Spec.MasterConfig is empty; using master image from config", zap.String("imageRef", imageRef))
}
Expand All @@ -374,7 +373,7 @@ func (b *Backend) NewJMeterMasterJob(loadTest loadTestV1.LoadTest, reportURL str
},
}

if "" != reportURL {
if reportURL != "" {
jMeterEnvVars = append(jMeterEnvVars, coreV1.EnvVar{
Name: "REPORT_PRESIGNED_URL",
Value: reportURL,
Expand Down
2 changes: 1 addition & 1 deletion pkg/backends/locust/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func newMasterJob(
{Name: "LOCUST_RUN_TIME", Value: loadTest.Spec.Duration.String()},
}

if "" != reportURL {
if reportURL != "" {
envVars = append(envVars, coreV1.EnvVar{
Name: "REPORT_PRESIGNED_URL",
Value: reportURL,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (c *Controller) processNextWorkItem() bool {
if err != nil {
status = falseString
}
c.statsClient.ReportReconcile(time.Now().Sub(startTime), key, status)
c.statsClient.ReportReconcile(time.Since(startTime), key, status)
}()

// We expect strings to come off the workQueue. These are of the
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
mPkg "github.com/hellofresh/kangal/pkg/core/middleware"
)

//RunMetricsServer starts Prometheus metrics server
// RunMetricsServer starts Prometheus metrics server
func RunMetricsServer(cfg Config, rr Runner, stopChan chan struct{}) error {
r := chi.NewRouter()
// Define Middleware
Expand Down
10 changes: 0 additions & 10 deletions pkg/controller/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controller
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"

Expand Down Expand Up @@ -75,15 +74,6 @@ func CreateLoadTest(clientSet clientSetV.Clientset, pods int32, name, testFile,
return nil
}

func readFile(filename string) (string, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
str := string(b)
return str, nil
}

// WaitLoadTest waits until Loadtest resources exists
func WaitLoadTest(clientSet clientSetV.Clientset, loadtestName string) error {
watchObj, err := clientSet.KangalV1().LoadTests().Watch(context.Background(), metaV1.ListOptions{
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/http/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/go-chi/render"
)

//LivenessHandler returns ok response for liveness probe
// LivenessHandler returns ok response for liveness probe
func LivenessHandler(service string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, &Response{
Expand Down
6 changes: 1 addition & 5 deletions pkg/core/middleware/request_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,5 @@ func (m *RequestLogger) Handler(next http.Handler) http.Handler {

// IsStatusRequest checks if the request is a health check request
func IsStatusRequest(r *http.Request) bool {
if strings.HasSuffix(r.URL.Path, "status") {
return true
}

return false
return strings.HasSuffix(r.URL.Path, "status")
}
4 changes: 2 additions & 2 deletions pkg/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
gracePeriod = int64(0)
)

//Client manages calls to Kubernetes API
// Client manages calls to Kubernetes API
type Client struct {
ltClient loadTestV1.LoadTestInterface
kubeClient kubernetes.Interface
Expand All @@ -47,7 +47,7 @@ type ListOptions struct {
Continue string
}

//NewClient creates new Kubernetes client
// NewClient creates new Kubernetes client
func NewClient(loadTestClient loadTestV1.LoadTestInterface, kubeClient kubernetes.Interface, logger *zap.Logger) *Client {
return &Client{
ltClient: loadTestClient,
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubernetes/tolerations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Tolerations []Toleration
func ParseToleration(toleration string) (Toleration, error) {
tolerationParts := strings.Split(toleration, ":")
if len(tolerationParts) != 4 {
return Toleration{}, errors.New(`Failed to parse toleration, expected pattern "key:value:Operation:Effect"`)
return Toleration{}, errors.New(`failed to parse toleration, expected pattern "key:value:Operation:Effect"`)
}

t := Toleration{
Expand Down Expand Up @@ -59,13 +59,13 @@ func (t Toleration) Validate() error {
effect := kubeCoreV1.TaintEffect(t.Effect)

if op != kubeCoreV1.TolerationOpEqual && op != kubeCoreV1.TolerationOpExists {
return fmt.Errorf("Invalid operator type %q", op)
return fmt.Errorf("invalid operator type %q", op)
}

if effect != kubeCoreV1.TaintEffectNoExecute &&
effect != kubeCoreV1.TaintEffectNoSchedule &&
effect != kubeCoreV1.TaintEffectPreferNoSchedule {
return fmt.Errorf("Invalid effect type %q", effect)
return fmt.Errorf("invalid effect type %q", effect)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/kubernetes/tolerations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ func TestParseToleration(t *testing.T) {
name: "empty toleration",
toleration: "",
expect: expected{
err: `Failed to parse toleration, expected pattern "key:value:Operation:Effect"`,
err: `failed to parse toleration, expected pattern "key:value:Operation:Effect"`,
},
},
{
name: "incomplete toleration",
toleration: "key:value:Equal",
expect: expected{
err: `Failed to parse toleration, expected pattern "key:value:Operation:Effect"`,
err: `failed to parse toleration, expected pattern "key:value:Operation:Effect"`,
},
},
{
name: "invalid operator",
toleration: "key:value:Invalid:NoSchedule",
expect: expected{
err: `Invalid operator type "Invalid"`,
err: `invalid operator type "Invalid"`,
},
},
{
name: "invalid effect",
toleration: "key:value:Equal:Invalid",
expect: expected{
err: `Invalid effect type "Invalid"`,
err: `invalid effect type "Invalid"`,
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/proxy/open_api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proxy

import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -41,7 +40,7 @@ func OpenAPISpecHandler(cfg OpenAPIConfig) func(w http.ResponseWriter, r *http.R
}
}

spec, err := ioutil.ReadFile(openAPISpec)
spec, err := os.ReadFile(openAPISpec)
if err != nil {
return func(w http.ResponseWriter, r *http.Request) {
mPkg.GetLogger(r.Context()).Error("Failed to read OpenAPI spec file", zap.Error(err), zap.String("path", openAPISpec))
Expand Down
9 changes: 5 additions & 4 deletions pkg/proxy/open_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package proxy

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,7 +16,7 @@ import (
)

func TestOpenAPISpecHandler_static(t *testing.T) {
rawSpec, err := ioutil.ReadFile("../../openapi.json")
rawSpec, err := os.ReadFile("../../openapi.json")
require.NoError(t, err)

cfg := OpenAPIConfig{
Expand All @@ -36,7 +37,7 @@ func TestOpenAPISpecHandler_static(t *testing.T) {
staticHandler(w, rq.WithContext(ctx))
rs := w.Result()

rsBody, err := ioutil.ReadAll(rs.Body)
rsBody, err := io.ReadAll(rs.Body)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, rs.StatusCode)
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestOpenAPISpecHandler_custom(t *testing.T) {
staticHandler(w, rq.WithContext(ctx))
rs := w.Result()

rsBody, err := ioutil.ReadAll(rs.Body)
rsBody, err := io.ReadAll(rs.Body)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, rs.StatusCode)
Expand Down
9 changes: 4 additions & 5 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *Proxy) List(w http.ResponseWriter, r *http.Request) {
})
}

//Create creates loadtest CR on POST request
// Create creates loadtest CR on POST request
func (p *Proxy) Create(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := mPkg.GetLogger(ctx)
Expand Down Expand Up @@ -201,7 +201,7 @@ func (p *Proxy) Create(w http.ResponseWriter, r *http.Request) {
})
}

//Delete deletes load test CR
// Delete deletes load test CR
func (p *Proxy) Delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := mPkg.GetLogger(ctx)
Expand All @@ -219,7 +219,7 @@ func (p *Proxy) Delete(w http.ResponseWriter, r *http.Request) {
render.NoContent(w, r)
}

//Get returns the loadtest CR info
// Get returns the loadtest CR info
func (p *Proxy) Get(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := mPkg.GetLogger(ctx)
Expand Down Expand Up @@ -251,7 +251,7 @@ func (p *Proxy) Get(w http.ResponseWriter, r *http.Request) {
})
}

//GetLogs returns the loadtest logs from master or worker pods
// GetLogs returns the loadtest logs from master or worker pods
func (p *Proxy) GetLogs(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := mPkg.GetLogger(ctx)
Expand Down Expand Up @@ -300,7 +300,6 @@ func (p *Proxy) GetLogs(w http.ResponseWriter, r *http.Request) {
}

io.WriteString(w, string(logs))
return
}

func doRequest(req *restClient.Request) ([]byte, error) {
Expand Down
11 changes: 5 additions & 6 deletions pkg/proxy/proxy_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -173,7 +172,7 @@ func TestIntegrationCreateLoadtestReachMaxLimit(t *testing.T) {

resp, err := http.Post(fmt.Sprintf("http://localhost:%d/load-test", httpPort), request.contentType, request.body)
require.NoError(t, err, "Could not create POST request")
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
t.Logf(string(body))
require.Equal(t, http.StatusTooManyRequests, resp.StatusCode)
})
Expand Down Expand Up @@ -259,7 +258,7 @@ func TestIntegrationCreateLoadtestEmptyTestFile(t *testing.T) {
t.Run("Expect loadtest bad request response", func(t *testing.T) {
var dat map[string]interface{}

respBody, err := ioutil.ReadAll(body)
respBody, err := io.ReadAll(body)
require.NoError(t, err, "Could not get response body")

unmarshalErr := json.Unmarshal(respBody, &dat)
Expand Down Expand Up @@ -305,7 +304,7 @@ func TestIntegrationCreateLoadtestEmptyTestDataFile(t *testing.T) {
t.Run("Check loadtest response", func(t *testing.T) {
var dat map[string]interface{}

respbody, err := ioutil.ReadAll(body)
respbody, err := io.ReadAll(body)
require.NoError(t, err, "Could not get response body")

unmarshalErr := json.Unmarshal(respbody, &dat)
Expand Down Expand Up @@ -405,7 +404,7 @@ func TestIntegrationGetLoadtest(t *testing.T) {

require.Equal(t, http.StatusOK, res.StatusCode)

httpBody, err = ioutil.ReadAll(res.Body)
httpBody, err = io.ReadAll(res.Body)
require.NoError(t, err, "Could not get response body")
})

Expand Down Expand Up @@ -481,7 +480,7 @@ func TestIntegrationGetLoadtestLogs(t *testing.T) {
assert.NoError(t, err)
}()

_, err = ioutil.ReadAll(res.Body)
_, err = io.ReadAll(res.Body)
require.NoError(t, err, "Could not get response body")
})
}
Loading

0 comments on commit bcedc5a

Please sign in to comment.