Fix all errors from golangci-lint command

Signed-off-by: Arjun Naik <arjun.rn@gmail.com>
This commit is contained in:
Arjun Naik
2019-04-27 15:35:08 +02:00
parent 7c848a1282
commit 888e76b748
11 changed files with 43 additions and 36 deletions

View File

@ -5,13 +5,15 @@ go:
- "1.11.x"
env:
- GO111MODULE=on
- GO111MODULE=on GOLANGCI_RELEASE="v1.16.0"
before_install:
- go get github.com/mattn/goveralls
- go get github.com/lawrencewoodman/roveralls
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_RELEASE}
script:
- make check
- make test
- make build.docker
- roveralls

View File

@ -19,8 +19,7 @@ test:
go test -v $(GOPKGS)
check:
golint $(GOPKGS)
go vet -v $(GOPKGS)
golangci-lint run ./...
build.local: build/$(BINARY)
build.linux: build/linux/$(BINARY)

View File

@ -3,13 +3,15 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
)
func metricsHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf(`{"queue": {"length": %d}}`, size)))
_, err := w.Write([]byte(fmt.Sprintf(`{"queue": {"length": %d}}`, size)))
log.Fatalf("failed to write: %v", err)
}
var (
@ -29,5 +31,5 @@ func main() {
ReadTimeout: 5 * time.Second,
}
server.ListenAndServe()
log.Fatal(server.ListenAndServe())
}

View File

@ -2,9 +2,10 @@ package annotations
import (
"fmt"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
"strings"
"time"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
)
const (

View File

@ -1,9 +1,9 @@
package annotations
import (
"github.com/stretchr/testify/require"
"testing"
"github.com/stretchr/testify/require"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
)

View File

@ -10,7 +10,7 @@ import (
"time"
"github.com/oliveagle/jsonpath"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
)
// JSONPathMetricsGetter is a metrics getter which looks up pod metrics by
@ -58,7 +58,7 @@ func NewJSONPathMetricsGetter(config map[string]string) (*JSONPathMetricsGetter,
// GetMetric gets metric from pod by fetching json metrics from the pods metric
// endpoint and extracting the desired value using the specified json path
// query.
func (g *JSONPathMetricsGetter) GetMetric(pod *v1.Pod) (float64, error) {
func (g *JSONPathMetricsGetter) GetMetric(pod *corev1.Pod) (float64, error) {
data, err := getPodMetrics(pod, g.scheme, g.path, g.port)
if err != nil {
return 0, err
@ -89,7 +89,7 @@ func (g *JSONPathMetricsGetter) GetMetric(pod *v1.Pod) (float64, error) {
}
// getPodMetrics returns the content of the pods metrics endpoint.
func getPodMetrics(pod *v1.Pod, scheme, path string, port int) ([]byte, error) {
func getPodMetrics(pod *corev1.Pod, scheme, path string, port int) ([]byte, error) {
if pod.Status.PodIP == "" {
return nil, fmt.Errorf("pod %s/%s does not have a pod IP", pod.Namespace, pod.Namespace)
}

View File

@ -6,7 +6,7 @@ import (
log "github.com/sirupsen/logrus"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@ -40,7 +40,7 @@ type PodCollector struct {
}
type PodMetricsGetter interface {
GetMetric(pod *v1.Pod) (float64, error)
GetMetric(pod *corev1.Pod) (float64, error)
}
func NewPodCollector(client kubernetes.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*PodCollector, error) {

View File

@ -3,11 +3,11 @@ package collector
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/api/resource"
"strings"
"time"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/metrics/pkg/apis/custom_metrics"

View File

@ -3,20 +3,19 @@ package collector
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/metrics/pkg/apis/custom_metrics"
"testing"
"time"
"github.com/stretchr/testify/require"
"k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/metrics/pkg/apis/custom_metrics"
)
const (
@ -73,27 +72,27 @@ func newHPA(namesapce string, refName string, refKind string) *autoscalingv2.Hor
}
}
func newDeployment(client *fake.Clientset, namespace string, name string, replicas, readyReplicas int32) (*v1.Deployment, error) {
return client.AppsV1().Deployments(namespace).Create(&v1.Deployment{
func newDeployment(client *fake.Clientset, namespace string, name string, replicas, readyReplicas int32) (*appsv1.Deployment, error) {
return client.AppsV1().Deployments(namespace).Create(&appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1.DeploymentSpec{},
Status: v1.DeploymentStatus{
Spec: appsv1.DeploymentSpec{},
Status: appsv1.DeploymentStatus{
ReadyReplicas: replicas,
Replicas: readyReplicas,
},
})
}
func newStatefulSet(client *fake.Clientset, namespace string, name string) (*v1.StatefulSet, error) {
return client.AppsV1().StatefulSets(namespace).Create(&v1.StatefulSet{
func newStatefulSet(client *fake.Clientset, namespace string, name string) (*appsv1.StatefulSet, error) {
return client.AppsV1().StatefulSets(namespace).Create(&appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Status: v1.StatefulSetStatus{
Status: appsv1.StatefulSetStatus{
ReadyReplicas: 1,
Replicas: 2,
},
@ -204,17 +203,19 @@ func TestSkipperCollector(t *testing.T) {
} {
t.Run(tc.msg, func(tt *testing.T) {
client := fake.NewSimpleClientset()
makeIngress(client, tc.namespace, tc.ingressName, tc.backend, tc.backendWeights)
err := makeIngress(client, tc.namespace, tc.ingressName, tc.backend, tc.backendWeights)
require.NoError(t, err)
plugin := makePlugin(tc.metrics)
hpa := makeHPA(tc.ingressName, tc.backend)
config := makeConfig(tc.backend)
newDeployment(client, tc.namespace, tc.backend, tc.replicas, tc.readyReplicas)
_, err = newDeployment(client, tc.namespace, tc.backend, tc.replicas, tc.readyReplicas)
require.NoError(t, err)
collector, err := NewSkipperCollector(client, plugin, hpa, config, time.Minute, tc.backendAnnotations, tc.backend)
assert.NoError(tt, err, "failed to create skipper collector: %v", err)
require.NoError(tt, err, "failed to create skipper collector: %v", err)
collected, err := collector.GetMetrics()
assert.NoError(tt, err, "failed to collect metrics: %v", err)
assert.Len(t, collected, 1, "the number of metrics returned is not 1")
assert.EqualValues(t, tc.collectedMetric, collected[0].Custom.Value.Value(), "the returned metric is not expected value")
require.NoError(tt, err, "failed to collect metrics: %v", err)
require.Len(t, collected, 1, "the number of metrics returned is not 1")
require.EqualValues(t, tc.collectedMetric, collected[0].Custom.Value.Value(), "the returned metric is not expected value")
})
}
}

View File

@ -1,6 +1,9 @@
package provider
import (
"testing"
"time"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
"github.com/stretchr/testify/require"
"github.com/zalando-incubator/kube-metrics-adapter/pkg/collector"
@ -12,8 +15,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/metrics/pkg/apis/custom_metrics"
"k8s.io/metrics/pkg/apis/external_metrics"
"testing"
"time"
)
func newMetricIdentifier(metricName string) custom_metrics.MetricIdentifier {

View File

@ -131,7 +131,8 @@ func TestQuery(tt *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(ti.status)
w.Write([]byte(ti.body))
_, err := w.Write([]byte(ti.body))
assert.NoError(t, err)
}),
)
defer ts.Close()