From 7f760cffb275019f06eb5649557b9978aba8fdb7 Mon Sep 17 00:00:00 2001 From: Kristina Pathak Date: Mon, 19 Sep 2022 17:21:53 -0700 Subject: [PATCH] fix more linting issues --- .../allocation/consistent_hashing_test.go | 12 ++++++------ .../allocation/least_weighted_test.go | 18 +++++++++--------- cmd/otel-allocator/main.go | 17 +++++++++-------- cmd/otel-allocator/watcher/promOperator.go | 3 +-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/cmd/otel-allocator/allocation/consistent_hashing_test.go b/cmd/otel-allocator/allocation/consistent_hashing_test.go index e91120eb0c..31ac9c2007 100644 --- a/cmd/otel-allocator/allocation/consistent_hashing_test.go +++ b/cmd/otel-allocator/allocation/consistent_hashing_test.go @@ -21,7 +21,7 @@ import ( ) func TestCanSetSingleTarget(t *testing.T) { - cols := makeNCollectors(3, 0, 0) + cols := makeNCollectors(3, 0) c := newConsistentHashingAllocator(logger) c.SetCollectors(cols) c.SetTargets(makeNNewTargets(1, 3, 0)) @@ -35,7 +35,7 @@ func TestCanSetSingleTarget(t *testing.T) { func TestRelativelyEvenDistribution(t *testing.T) { numCols := 15 numItems := 10000 - cols := makeNCollectors(numCols, 0, 0) + cols := makeNCollectors(numCols, 0) var expectedPerCollector = float64(numItems / numCols) expectedDelta := (expectedPerCollector * 1.5) - expectedPerCollector c := newConsistentHashingAllocator(logger) @@ -52,7 +52,7 @@ func TestRelativelyEvenDistribution(t *testing.T) { } func TestFullReallocation(t *testing.T) { - cols := makeNCollectors(10, 0, 0) + cols := makeNCollectors(10, 0) c := newConsistentHashingAllocator(logger) c.SetCollectors(cols) c.SetTargets(makeNNewTargets(10000, 10, 0)) @@ -60,7 +60,7 @@ func TestFullReallocation(t *testing.T) { assert.Len(t, actualTargetItems, 10000) actualCollectors := c.Collectors() assert.Len(t, actualCollectors, 10) - newCols := makeNCollectors(10, 0, 10) + newCols := makeNCollectors(10, 10) c.SetCollectors(newCols) updatedTargetItems := c.TargetItems() assert.Len(t, updatedTargetItems, 10000) @@ -77,7 +77,7 @@ func TestNumRemapped(t *testing.T) { numInitialCols := 15 numFinalCols := 16 expectedDelta := float64((numFinalCols - numInitialCols) * (numItems / numFinalCols)) - cols := makeNCollectors(numInitialCols, 0, 0) + cols := makeNCollectors(numInitialCols, 0) c := newConsistentHashingAllocator(logger) c.SetCollectors(cols) c.SetTargets(makeNNewTargets(numItems, numInitialCols, 0)) @@ -85,7 +85,7 @@ func TestNumRemapped(t *testing.T) { assert.Len(t, actualTargetItems, numItems) actualCollectors := c.Collectors() assert.Len(t, actualCollectors, numInitialCols) - newCols := makeNCollectors(numFinalCols, 0, 0) + newCols := makeNCollectors(numFinalCols, 0) c.SetCollectors(newCols) updatedTargetItems := c.TargetItems() assert.Len(t, updatedTargetItems, numItems) diff --git a/cmd/otel-allocator/allocation/least_weighted_test.go b/cmd/otel-allocator/allocation/least_weighted_test.go index 4faae48b79..3b6209ddd5 100644 --- a/cmd/otel-allocator/allocation/least_weighted_test.go +++ b/cmd/otel-allocator/allocation/least_weighted_test.go @@ -50,13 +50,13 @@ func makeNNewTargets(n int, numCollectors int, startingIndex int) map[string]*Ta return toReturn } -func makeNCollectors(n int, targetsForEach int, startingIndex int) map[string]*Collector { +func makeNCollectors(n int, startingIndex int) map[string]*Collector { toReturn := map[string]*Collector{} for i := startingIndex; i < n+startingIndex; i++ { collector := fmt.Sprintf("collector-%d", i) toReturn[collector] = &Collector{ Name: collector, - NumTargets: targetsForEach, + NumTargets: 0, } } return toReturn @@ -65,7 +65,7 @@ func makeNCollectors(n int, targetsForEach int, startingIndex int) map[string]*C func TestSetCollectors(t *testing.T) { s, _ := New("least-weighted", logger) - cols := makeNCollectors(3, 0, 0) + cols := makeNCollectors(3, 0) s.SetCollectors(cols) expectedColLen := len(cols) @@ -81,7 +81,7 @@ func TestAddingAndRemovingTargets(t *testing.T) { // prepare allocator with initial targets and collectors s, _ := New("least-weighted", logger) - cols := makeNCollectors(3, 0, 0) + cols := makeNCollectors(3, 0) s.SetCollectors(cols) initTargets := makeNNewTargets(6, 3, 0) @@ -116,7 +116,7 @@ func TestAllocationCollision(t *testing.T) { // prepare allocator with initial targets and collectors s, _ := New("least-weighted", logger) - cols := makeNCollectors(3, 0, 0) + cols := makeNCollectors(3, 0) s.SetCollectors(cols) firstLabels := model.LabelSet{ "test": "test1", @@ -150,7 +150,7 @@ func TestAllocationCollision(t *testing.T) { func TestNoCollectorReassignment(t *testing.T) { s, _ := New("least-weighted", logger) - cols := makeNCollectors(3, 0, 0) + cols := makeNCollectors(3, 0) s.SetCollectors(cols) expectedColLen := len(cols) @@ -170,7 +170,7 @@ func TestNoCollectorReassignment(t *testing.T) { assert.Len(t, targetItems, expectedTargetLen) // assign new set of collectors with the same names - newCols := makeNCollectors(3, 0, 0) + newCols := makeNCollectors(3, 0) s.SetCollectors(newCols) newTargetItems := s.TargetItems() @@ -181,7 +181,7 @@ func TestNoCollectorReassignment(t *testing.T) { func TestSmartCollectorReassignment(t *testing.T) { s, _ := New("least-weighted", logger) - cols := makeNCollectors(4, 0, 0) + cols := makeNCollectors(4, 0) s.SetCollectors(cols) expectedColLen := len(cols) @@ -232,7 +232,7 @@ func TestCollectorBalanceWhenAddingAndRemovingAtRandom(t *testing.T) { // prepare allocator with 3 collectors and 'random' amount of targets s, _ := New("least-weighted", logger) - cols := makeNCollectors(3, 0, 0) + cols := makeNCollectors(3, 0) s.SetCollectors(cols) targets := makeNNewTargets(27, 3, 0) diff --git a/cmd/otel-allocator/main.go b/cmd/otel-allocator/main.go index 26287b3233..635b454582 100644 --- a/cmd/otel-allocator/main.go +++ b/cmd/otel-allocator/main.go @@ -22,6 +22,7 @@ import ( "os" "os/signal" "syscall" + "time" gokitlog "github.com/go-kit/log" "github.com/go-logr/logr" @@ -174,7 +175,7 @@ func newServer(log logr.Logger, allocator allocation.Allocator, discoveryManager router.HandleFunc("/jobs", s.JobHandler).Methods("GET") router.HandleFunc("/jobs/{job_id}/targets", s.TargetsHandler).Methods("GET") router.Path("/metrics").Handler(promhttp.Handler()) - s.server = &http.Server{Addr: *listenAddr, Handler: router} + s.server = &http.Server{Addr: *listenAddr, Handler: router, ReadHeaderTimeout: 90 * time.Second} return s, nil } @@ -214,7 +215,7 @@ func (s *server) JobHandler(w http.ResponseWriter, r *http.Request) { for _, v := range s.allocator.TargetItems() { displayData[v.JobName] = allocation.LinkJSON{Link: v.Link.Link} } - jsonHandler(s.logger, w, r, displayData) + jsonHandler(s.logger, w, displayData) } // PrometheusMiddleware implements mux.MiddlewareFunc. @@ -238,30 +239,30 @@ func (s *server) TargetsHandler(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) jobId, err := url.QueryUnescape(params["job_id"]) if err != nil { - errorHandler(err, w, r) + errorHandler(w) return } if len(q) == 0 { displayData := allocation.GetAllTargetsByJob(jobId, compareMap, s.allocator) - jsonHandler(s.logger, w, r, displayData) + jsonHandler(s.logger, w, displayData) } else { tgs := allocation.GetAllTargetsByCollectorAndJob(q[0], jobId, compareMap, s.allocator) // Displays empty list if nothing matches if len(tgs) == 0 { - jsonHandler(s.logger, w, r, []interface{}{}) + jsonHandler(s.logger, w, []interface{}{}) return } - jsonHandler(s.logger, w, r, tgs) + jsonHandler(s.logger, w, tgs) } } -func errorHandler(err error, w http.ResponseWriter, r *http.Request) { +func errorHandler(w http.ResponseWriter) { w.WriteHeader(500) } -func jsonHandler(logger logr.Logger, w http.ResponseWriter, r *http.Request, data interface{}) { +func jsonHandler(logger logr.Logger, w http.ResponseWriter, data interface{}) { w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(data) if err != nil { diff --git a/cmd/otel-allocator/watcher/promOperator.go b/cmd/otel-allocator/watcher/promOperator.go index 66080f648b..09eee6f50f 100644 --- a/cmd/otel-allocator/watcher/promOperator.go +++ b/cmd/otel-allocator/watcher/promOperator.go @@ -20,7 +20,6 @@ import ( allocatorconfig "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/config" "github.com/go-kit/log" - "github.com/go-logr/logr" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" "github.com/prometheus-operator/prometheus-operator/pkg/assets" monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned" @@ -34,7 +33,7 @@ import ( "k8s.io/client-go/tools/cache" ) -func newCRDMonitorWatcher(logger logr.Logger, config allocatorconfig.CLIConfig) (*PrometheusCRWatcher, error) { +func newCRDMonitorWatcher(config allocatorconfig.CLIConfig) (*PrometheusCRWatcher, error) { mClient, err := monitoringclient.NewForConfig(config.ClusterConfig) if err != nil { return nil, err