Skip to content

Commit

Permalink
lint: Enable interfacer
Browse files Browse the repository at this point in the history
interfacer suggests narrower interface types:
https://github.com/mvdan/interfacer

Part of #217

Signed-off-by: Andrew Seigner <siggy@buoyant.io>
  • Loading branch information
siggy committed Feb 26, 2019
1 parent 6ef33e8 commit 3fc0894
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ linters:
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- scopelint
Expand All @@ -33,7 +34,6 @@ linters:
# - gochecknoinits
# - gocyclo
# - gosec
# - interfacer
# - lll
# - maligned
# - prealloc
Expand Down
12 changes: 6 additions & 6 deletions cli/cmd/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"

"github.com/linkerd/linkerd2/controller/api/public"
pb "github.com/linkerd/linkerd2/controller/gen/controller/discovery"
"github.com/linkerd/linkerd2/pkg/addr"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -92,7 +92,7 @@ requests.`,
return cmd
}

func requestEndpointsFromAPI(client public.APIClient) (*pb.EndpointsResponse, error) {
func requestEndpointsFromAPI(client pb.DiscoveryClient) (*pb.EndpointsResponse, error) {
return client.Endpoints(context.Background(), &pb.EndpointsParams{})
}

Expand All @@ -114,7 +114,7 @@ type rowEndpoint struct {
Service string `json:"service"`
}

func writeEndpointsToBuffer(endpoints *pb.EndpointsResponse, w *tabwriter.Writer, options *endpointsOptions) {
func writeEndpointsToBuffer(endpoints *pb.EndpointsResponse, w io.Writer, options *endpointsOptions) {
maxPodLength := len(podHeader)
maxNamespaceLength := len(namespaceHeader)
endpointsTables := map[string][]rowEndpoint{}
Expand Down Expand Up @@ -175,7 +175,7 @@ func writeEndpointsToBuffer(endpoints *pb.EndpointsResponse, w *tabwriter.Writer
}
}

func printEndpointsTables(endpointsTables map[string][]rowEndpoint, w *tabwriter.Writer, options *endpointsOptions, maxPodLength int, maxNamespaceLength int) {
func printEndpointsTables(endpointsTables map[string][]rowEndpoint, w io.Writer, options *endpointsOptions, maxPodLength int, maxNamespaceLength int) {
firstTable := true // don't print a newline before the first table

for _, ns := range sortNamespaceKeys(endpointsTables) {
Expand All @@ -187,7 +187,7 @@ func printEndpointsTables(endpointsTables map[string][]rowEndpoint, w *tabwriter
}
}

func printEndpointsTable(namespace string, rows []rowEndpoint, w *tabwriter.Writer, options *endpointsOptions, maxPodLength int, maxNamespaceLength int) {
func printEndpointsTable(namespace string, rows []rowEndpoint, w io.Writer, options *endpointsOptions, maxPodLength int, maxNamespaceLength int) {
headers := make([]string, 0)
templateString := "%s\t%d\t%s\t%s\t%s\n"

Expand Down Expand Up @@ -224,7 +224,7 @@ func printEndpointsTable(namespace string, rows []rowEndpoint, w *tabwriter.Writ
}
}

func printEndpointsJSON(endpointsTables map[string][]rowEndpoint, w *tabwriter.Writer) {
func printEndpointsJSON(endpointsTables map[string][]rowEndpoint, w io.Writer) {
entries := []rowEndpoint{}

for _, ns := range sortNamespaceKeys(endpointsTables) {
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -101,7 +102,7 @@ func renderRouteStats(resp *pb.TopRoutesResponse, options *routesOptions) string
return renderStats(buffer, &options.statOptionsBase)
}

func writeRouteStatsToBuffer(resp *pb.TopRoutesResponse, w *tabwriter.Writer, options *routesOptions) {
func writeRouteStatsToBuffer(resp *pb.TopRoutesResponse, w io.Writer, options *routesOptions) {

tables := make(map[string][]*routeRowStats)

Expand Down Expand Up @@ -155,7 +156,7 @@ func writeRouteStatsToBuffer(resp *pb.TopRoutesResponse, w *tabwriter.Writer, op
}
}

func printRouteTable(stats []*routeRowStats, w *tabwriter.Writer, options *routesOptions) {
func printRouteTable(stats []*routeRowStats, w io.Writer, options *routesOptions) {
// template for left-aligning the route column
routeTemplate := fmt.Sprintf("%%-%ds", routeWidth(stats))

Expand Down Expand Up @@ -239,7 +240,7 @@ type jsonRouteStats struct {
LatencyMSp99 *uint64 `json:"latency_ms_p99"`
}

func printRouteJSON(tables map[string][]*routeRowStats, w *tabwriter.Writer, options *routesOptions) {
func printRouteJSON(tables map[string][]*routeRowStats, w io.Writer, options *routesOptions) {
// avoid nil initialization so that if there are not stats it gets marshalled as an empty array vs null
entries := map[string][]*jsonRouteStats{}
for resource, table := range tables {
Expand Down
9 changes: 5 additions & 4 deletions cli/cmd/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -221,7 +222,7 @@ var (
namespaceHeader = "NAMESPACE"
)

func writeStatsToBuffer(rows []*pb.StatTable_PodGroup_Row, w *tabwriter.Writer, options *statOptions) {
func writeStatsToBuffer(rows []*pb.StatTable_PodGroup_Row, w io.Writer, options *statOptions) {
maxNameLength := len(nameHeader)
maxNamespaceLength := len(namespaceHeader)
statTables := make(map[string]map[string]*row)
Expand Down Expand Up @@ -290,7 +291,7 @@ func writeStatsToBuffer(rows []*pb.StatTable_PodGroup_Row, w *tabwriter.Writer,
}
}

func printStatTables(statTables map[string]map[string]*row, w *tabwriter.Writer, maxNameLength int, maxNamespaceLength int, options *statOptions) {
func printStatTables(statTables map[string]map[string]*row, w io.Writer, maxNameLength int, maxNamespaceLength int, options *statOptions) {
usePrefix := false
if len(statTables) > 1 {
usePrefix = true
Expand All @@ -312,7 +313,7 @@ func printStatTables(statTables map[string]map[string]*row, w *tabwriter.Writer,
}
}

func printSingleStatTable(stats map[string]*row, resourceType string, w *tabwriter.Writer, maxNameLength int, maxNamespaceLength int, options *statOptions) {
func printSingleStatTable(stats map[string]*row, resourceType string, w io.Writer, maxNameLength int, maxNamespaceLength int, options *statOptions) {
headers := make([]string, 0)
if options.allNamespaces {
headers = append(headers,
Expand Down Expand Up @@ -392,7 +393,7 @@ type jsonStats struct {
TLS *float64 `json:"tls"`
}

func printStatJSON(statTables map[string]map[string]*row, w *tabwriter.Writer) {
func printStatJSON(statTables map[string]map[string]*row, w io.Writer) {
// avoid nil initialization so that if there are not stats it gets marshalled as an empty array vs null
entries := []*jsonStats{}
for _, resourceType := range k8s.AllResources {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func renderTap(w io.Writer, tapClient pb.Api_TapByResourceClient, resource strin
return nil
}

func writeTapEventsToBuffer(tapClient pb.Api_TapByResourceClient, w *tabwriter.Writer, resource string) error {
func writeTapEventsToBuffer(tapClient pb.Api_TapByResourceClient, w io.Writer, resource string) error {
for {
log.Debug("Waiting for data...")
event, err := tapClient.Recv()
Expand Down
12 changes: 6 additions & 6 deletions controller/api/public/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *grpcOverHTTPClient) Tap(ctx context.Context, req *pb.TapRequest, _ ...g

func (c *grpcOverHTTPClient) TapByResource(ctx context.Context, req *pb.TapByResourceRequest, _ ...grpc.CallOption) (pb.Api_TapByResourceClient, error) {
url := c.endpointNameToPublicAPIURL("TapByResource")
httpRsp, err := c.post(ctx, url, req)
httpRsp, err := c.post(ctx, url.String(), req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c *grpcOverHTTPClient) apiRequest(ctx context.Context, endpoint string, re
url := c.endpointNameToPublicAPIURL(endpoint)

log.Debugf("Making gRPC-over-HTTP call to [%s] [%+v]", url.String(), req)
httpRsp, err := c.post(ctx, url, req)
httpRsp, err := c.post(ctx, url.String(), req)
if err != nil {
return err
}
Expand All @@ -128,15 +128,15 @@ func (c *grpcOverHTTPClient) apiRequest(ctx context.Context, endpoint string, re
return fromByteStreamToProtocolBuffers(reader, protoResponse)
}

func (c *grpcOverHTTPClient) post(ctx context.Context, url *url.URL, req proto.Message) (*http.Response, error) {
func (c *grpcOverHTTPClient) post(ctx context.Context, url string, req proto.Message) (*http.Response, error) {
reqBytes, err := proto.Marshal(req)
if err != nil {
return nil, err
}

httpReq, err := http.NewRequest(
http.MethodPost,
url.String(),
url,
bytes.NewReader(reqBytes),
)
if err != nil {
Expand All @@ -145,9 +145,9 @@ func (c *grpcOverHTTPClient) post(ctx context.Context, url *url.URL, req proto.M

rsp, err := c.httpClient.Do(httpReq.WithContext(ctx))
if err != nil {
log.Debugf("Error invoking [%s]: %v", url.String(), err)
log.Debugf("Error invoking [%s]: %v", url, err)
} else {
log.Debugf("Response from [%s] had headers: %v", url.String(), rsp.Header)
log.Debugf("Response from [%s] had headers: %v", url, rsp.Header)
}

return rsp, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (hc *HealthChecker) allCategories() []category {
description: "is running the minimum Kubernetes API version",
hintAnchor: "k8s-version",
check: func(context.Context) error {
return hc.kubeAPI.CheckVersion(hc.kubeVersion)
return hc.kubeAPI.CheckVersion(hc.kubeVersion.String())
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/k8s/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (kubeAPI *KubernetesAPI) NewClient() (*http.Client, error) {
}

// GetVersionInfo returns version.Info for the Kubernetes cluster.
func (kubeAPI *KubernetesAPI) GetVersionInfo(ctx context.Context, client *http.Client) (*version.Info, error) {
func (kubeAPI *KubernetesAPI) GetVersionInfo(ctx context.Context, client rest.HTTPClient) (*version.Info, error) {
rsp, err := kubeAPI.getRequest(ctx, client, "/version")
if err != nil {
return nil, err
Expand All @@ -60,8 +60,8 @@ func (kubeAPI *KubernetesAPI) GetVersionInfo(ctx context.Context, client *http.C

// CheckVersion validates whether the configured Kubernetes cluster's version is
// running a minimum Kubernetes API version.
func (kubeAPI *KubernetesAPI) CheckVersion(versionInfo *version.Info) error {
apiVersion, err := getK8sVersion(versionInfo.String())
func (kubeAPI *KubernetesAPI) CheckVersion(versionInfo string) error {
apiVersion, err := getK8sVersion(versionInfo)
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func (kubeAPI *KubernetesAPI) CheckVersion(versionInfo *version.Info) error {
}

// NamespaceExists validates whether a given namespace exists.
func (kubeAPI *KubernetesAPI) NamespaceExists(ctx context.Context, client *http.Client, namespace string) (bool, error) {
func (kubeAPI *KubernetesAPI) NamespaceExists(ctx context.Context, client rest.HTTPClient, namespace string) (bool, error) {
rsp, err := kubeAPI.getRequest(ctx, client, "/api/v1/namespaces/"+namespace)
if err != nil {
return false, err
Expand All @@ -91,11 +91,11 @@ func (kubeAPI *KubernetesAPI) NamespaceExists(ctx context.Context, client *http.
}

// GetPodsByNamespace returns all pods in a given namespace
func (kubeAPI *KubernetesAPI) GetPodsByNamespace(ctx context.Context, client *http.Client, namespace string) ([]corev1.Pod, error) {
func (kubeAPI *KubernetesAPI) GetPodsByNamespace(ctx context.Context, client rest.HTTPClient, namespace string) ([]corev1.Pod, error) {
return kubeAPI.getPods(ctx, client, "/api/v1/namespaces/"+namespace+"/pods")
}

func (kubeAPI *KubernetesAPI) getPods(ctx context.Context, client *http.Client, path string) ([]corev1.Pod, error) {
func (kubeAPI *KubernetesAPI) getPods(ctx context.Context, client rest.HTTPClient, path string) ([]corev1.Pod, error) {
rsp, err := kubeAPI.getRequest(ctx, client, path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -125,7 +125,7 @@ func (kubeAPI *KubernetesAPI) URLFor(namespace, path string) (*url.URL, error) {
return generateKubernetesAPIURLFor(kubeAPI.Host, namespace, path)
}

func (kubeAPI *KubernetesAPI) getRequest(ctx context.Context, client *http.Client, path string) (*http.Response, error) {
func (kubeAPI *KubernetesAPI) getRequest(ctx context.Context, client rest.HTTPClient, path string) (*http.Response, error) {
endpoint, err := generateKubernetesURL(kubeAPI.Host, path)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/tls/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"io"
)

// === ENCODE ===
Expand All @@ -31,7 +32,7 @@ func EncodePrivateKeyPEM(k *ecdsa.PrivateKey) ([]byte, error) {
return pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: der}), nil
}

func encode(buf *bytes.Buffer, blk *pem.Block) {
func encode(buf io.Writer, blk *pem.Block) {
if err := pem.Encode(buf, blk); err != nil {
panic("encoding to memory must not fail")
}
Expand Down

0 comments on commit 3fc0894

Please sign in to comment.