Skip to content

Commit

Permalink
lint: Enable unparam
Browse files Browse the repository at this point in the history
unparam reports unused function parameters:
https://github.com/mvdan/unparam

Part of #217

Signed-off-by: Andrew Seigner <siggy@buoyant.io>
  • Loading branch information
siggy committed Feb 26, 2019
1 parent ec5a0ca commit f79bd75
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ linters:
- scopelint
- typecheck
- unconvert
- unparam
- unused
- varcheck
# TODO: enable more linters!
Expand All @@ -38,7 +39,6 @@ linters:
# - maligned
# - prealloc
# - stylecheck
# - unparam
disable:
- errcheck
- staticcheck
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/install-cni-plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestRenderCNIPlugin(t *testing.T) {
for i, tc := range testCases {
tc := tc // pin
t.Run(fmt.Sprintf("%d: %s", i, tc.goldenFileName), func(t *testing.T) {
defer teardown(defaultControlPlaneNamespace, t)
defer teardown(defaultControlPlaneNamespace)
controlPlaneNamespace = tc.namespace

var buf bytes.Buffer
Expand All @@ -84,6 +84,6 @@ func TestRenderCNIPlugin(t *testing.T) {
controlPlaneNamespace = defaultControlPlaneNamespace
}

func teardown(originalNamespace string, t *testing.T) {
func teardown(originalNamespace string) {
controlPlaneNamespace = originalNamespace
}
4 changes: 2 additions & 2 deletions cli/cmd/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ If no resource name is specified, displays stats about all resources of the spec
c := make(chan indexedResults, len(reqs))
for num, req := range reqs {
go func(num int, req *pb.StatSummaryRequest) {
resp, err := requestStatsFromAPI(client, req, options)
resp, err := requestStatsFromAPI(client, req)
rows := respToRows(resp)
c <- indexedResults{num, rows, err}
}(num, req)
Expand Down Expand Up @@ -177,7 +177,7 @@ func respToRows(resp *pb.StatSummaryResponse) []*pb.StatTable_PodGroup_Row {
return rows
}

func requestStatsFromAPI(client pb.ApiClient, req *pb.StatSummaryRequest, options *statOptions) (*pb.StatSummaryResponse, error) {
func requestStatsFromAPI(client pb.ApiClient, req *pb.StatSummaryRequest) (*pb.StatSummaryResponse, error) {
resp, err := client.StatSummary(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("StatSummary API error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func testStatCall(exp paramsExp, t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}

resp, err := requestStatsFromAPI(mockClient, reqs[0], exp.options)
resp, err := requestStatsFromAPI(mockClient, reqs[0])
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cli/cmd/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -337,7 +336,7 @@ func newCmdTop() *cobra.Command {
return err
}

return getTrafficByResourceFromAPI(os.Stdout, cliPublicAPIClient(), req, table)
return getTrafficByResourceFromAPI(cliPublicAPIClient(), req, table)
},
}

Expand All @@ -363,7 +362,7 @@ func newCmdTop() *cobra.Command {
return cmd
}

func getTrafficByResourceFromAPI(w io.Writer, client pb.ApiClient, req *pb.TapByResourceRequest, table *topTable) error {
func getTrafficByResourceFromAPI(client pb.ApiClient, req *pb.TapByResourceRequest, table *topTable) error {
rsp, err := client.TapByResource(context.Background(), req)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions controller/api/destination/endpoints_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ status:
serviceID{namespace: "ns", name: "name1"}: map[uint32]*servicePort{
8989: {
addresses: []*updateAddress{
makeUpdateAddress("172.17.0.12", 8989, "ns", "name1-1"),
makeUpdateAddress("172.17.0.19", 8989, "ns", "name1-2"),
makeUpdateAddress("172.17.0.20", 8989, "ns", "name1-3"),
makeUpdateAddress("172.17.0.12", 8989, "name1-1"),
makeUpdateAddress("172.17.0.19", 8989, "name1-2"),
makeUpdateAddress("172.17.0.20", 8989, "name1-3"),
},
targetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 8989},
endpoints: &corev1.Endpoints{
Expand Down Expand Up @@ -201,8 +201,8 @@ status:
serviceID{namespace: "ns", name: "name1"}: map[uint32]*servicePort{
8989: {
addresses: []*updateAddress{
makeUpdateAddress("10.233.66.239", 8990, "ns", "name1-f748fb6b4-hpwpw"),
makeUpdateAddress("10.233.88.244", 8990, "ns", "name1-f748fb6b4-6vcmw"),
makeUpdateAddress("10.233.66.239", 8990, "name1-f748fb6b4-hpwpw"),
makeUpdateAddress("10.233.88.244", 8990, "name1-f748fb6b4-6vcmw"),
},
targetPort: intstr.IntOrString{Type: intstr.String, StrVal: ""},
endpoints: &corev1.Endpoints{
Expand Down Expand Up @@ -283,7 +283,7 @@ status:
serviceID{namespace: "ns", name: "world"}: map[uint32]*servicePort{
7778: {
addresses: []*updateAddress{
makeUpdateAddress("10.1.30.135", 7779, "ns", "world-575bf846b4-tp4hw"),
makeUpdateAddress("10.1.30.135", 7779, "world-575bf846b4-tp4hw"),
},
targetPort: intstr.IntOrString{Type: intstr.String, StrVal: "app"},
endpoints: &corev1.Endpoints{
Expand Down Expand Up @@ -365,7 +365,7 @@ status:
serviceID{namespace: "ns", name: "name1"}: map[uint32]*servicePort{
8989: {
addresses: []*updateAddress{
makeUpdateAddress("172.17.0.25", 8989, "ns", "name1-3"),
makeUpdateAddress("172.17.0.25", 8989, "name1-3"),
},
targetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 8989},
endpoints: &corev1.Endpoints{
Expand Down
2 changes: 1 addition & 1 deletion controller/api/destination/k8s_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestGetState(t *testing.T) {
serviceID{namespace: "ns", name: "name"}: map[uint32]*servicePort{
8888: {
addresses: []*updateAddress{
makeUpdateAddress("10.1.30.135", 7779, "ns", "world-575bf846b4-tp4hw"),
makeUpdateAddress("10.1.30.135", 7779, "world-575bf846b4-tp4hw"),
},
endpoints: &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Expand Down
4 changes: 2 additions & 2 deletions controller/api/destination/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ func equalServicePorts(servicePorts1, servicePorts2 servicePorts) error {
return nil
}

func makeUpdateAddress(ipStr string, portNum uint32, ns string, name string) *updateAddress {
func makeUpdateAddress(ipStr string, portNum uint32, name string) *updateAddress {
ip, _ := addr.ParseProxyIPV4(ipStr)
return &updateAddress{
address: &proxyNet.TcpAddress{Ip: ip, Port: portNum},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Namespace: "ns",
Name: name,
},
},
Expand Down
5 changes: 1 addition & 4 deletions controller/api/public/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ func bufferedReader(t *testing.T, msg proto.Message) *bufio.Reader {
t.Fatalf("Unexpected error: %v", err)
}

payload, err := serializeAsPayload(msgBytes)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
payload := serializeAsPayload(msgBytes)

return bufio.NewReader(bytes.NewReader(payload))
}
9 changes: 3 additions & 6 deletions controller/api/public/proto_over_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ func writeProtoToHTTPResponse(w http.ResponseWriter, msg proto.Message) error {
return err
}

fullPayload, err := serializeAsPayload(marshalledProtobufMessage)
if err != nil {
return err
}
fullPayload := serializeAsPayload(marshalledProtobufMessage)
_, err = w.Write(fullPayload)
return err
}
Expand All @@ -108,13 +105,13 @@ func newStreamingWriter(w http.ResponseWriter) (flushableResponseWriter, error)
return flushableWriter, nil
}

func serializeAsPayload(messageContentsInBytes []byte) ([]byte, error) {
func serializeAsPayload(messageContentsInBytes []byte) []byte {
lengthOfThePayload := uint32(len(messageContentsInBytes))

messageLengthInBytes := make([]byte, numBytesForMessageLength)
binary.LittleEndian.PutUint32(messageLengthInBytes, lengthOfThePayload)

return append(messageLengthInBytes, messageContentsInBytes...), nil
return append(messageLengthInBytes, messageContentsInBytes...)
}

func deserializePayloadFromReader(reader *bufio.Reader) ([]byte, error) {
Expand Down
47 changes: 9 additions & 38 deletions controller/api/public/proto_over_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,7 @@ func TestDeserializePayloadFromReader(t *testing.T) {
t.Run("Can read message correctly based on payload size correct payload size to message", func(t *testing.T) {
expectedMessage := "this is the message"

messageWithSize, err := serializeAsPayload([]byte(expectedMessage))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

messageWithSize := serializeAsPayload([]byte(expectedMessage))
messageWithSomeNoise := append(messageWithSize, []byte("this is noise and should not be read")...)

actualMessage, err := deserializePayloadFromReader(bufio.NewReader(bytes.NewReader(messageWithSomeNoise)))
Expand All @@ -276,15 +272,8 @@ func TestDeserializePayloadFromReader(t *testing.T) {
expectedMessage2 = expectedMessage2 + fmt.Sprintf("tum (%d), ", i)
}

messageWithSize1, err := serializeAsPayload([]byte(expectedMessage1))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

messageWithSize2, err := serializeAsPayload([]byte(expectedMessage2))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
messageWithSize1 := serializeAsPayload([]byte(expectedMessage1))
messageWithSize2 := serializeAsPayload([]byte(expectedMessage2))

streamWithManyMessages := append(messageWithSize1, messageWithSize2...)
reader := bufio.NewReader(bytes.NewReader(streamWithManyMessages))
Expand Down Expand Up @@ -320,10 +309,7 @@ func TestDeserializePayloadFromReader(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}

serialized, err := serializeAsPayload(expectedReadArray)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
serialized := serializeAsPayload(expectedReadArray)

reader := bufio.NewReader(bytes.NewReader(serialized))
if err != nil {
Expand Down Expand Up @@ -369,11 +355,7 @@ func TestDeserializePayloadFromReader(t *testing.T) {
t.Fatalf("Test needs data larger than [%d] bytes, currently only [%d] bytes", goDefaultChunkSize, lengthOfInputData)
}

payload, err := serializeAsPayload(expectedMessageAsBytes)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

payload := serializeAsPayload(expectedMessageAsBytes)
actualMessage, err := deserializePayloadFromReader(bufio.NewReader(bytes.NewReader(payload)))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
Expand All @@ -387,13 +369,9 @@ func TestDeserializePayloadFromReader(t *testing.T) {
t.Run("Returns error when message has fewer bytes than declared message size", func(t *testing.T) {
expectedMessage := "this is the message"

messageWithSize, err := serializeAsPayload([]byte(expectedMessage))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

messageWithSize := serializeAsPayload([]byte(expectedMessage))
messageMissingOneCharacter := messageWithSize[:len(expectedMessage)-1]
_, err = deserializePayloadFromReader(bufio.NewReader(bytes.NewReader(messageMissingOneCharacter)))
_, err := deserializePayloadFromReader(bufio.NewReader(bytes.NewReader(messageMissingOneCharacter)))
if err == nil {
t.Fatalf("Expecting error, got nothing")
}
Expand Down Expand Up @@ -454,11 +432,7 @@ func TestCheckIfResponseHasError(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}

message, err := serializeAsPayload(protoInBytes)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

message := serializeAsPayload(protoInBytes)
response := &http.Response{
Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewReader(message)),
Expand All @@ -483,10 +457,7 @@ func TestCheckIfResponseHasError(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}

message, err := serializeAsPayload(protoInBytes)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
message := serializeAsPayload(protoInBytes)

response := &http.Response{
Header: make(http.Header),
Expand Down
6 changes: 3 additions & 3 deletions controller/api/public/stat_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ func (s *grpcServer) getPodStats(obj runtime.Object) (*podStats, error) {
}
}

errors := checkContainerErrors(pod.Status.ContainerStatuses, k8s.ProxyContainerName)
errors = append(errors, checkContainerErrors(pod.Status.InitContainerStatuses, k8s.InitContainerName)...)
errors := checkContainerErrors(pod.Status.ContainerStatuses)
errors = append(errors, checkContainerErrors(pod.Status.InitContainerStatuses)...)

if len(errors) > 0 {
podErrors[pod.Name] = &pb.PodErrors{Errors: errors}
Expand All @@ -466,7 +466,7 @@ func toPodError(container, image, reason, message string) *pb.PodErrors_PodError
}
}

func checkContainerErrors(containerStatuses []corev1.ContainerStatus, containerName string) []*pb.PodErrors_PodError {
func checkContainerErrors(containerStatuses []corev1.ContainerStatus) []*pb.PodErrors_PodError {
errors := []*pb.PodErrors_PodError{}
for _, st := range containerStatuses {
if !st.Ready {
Expand Down
Loading

0 comments on commit f79bd75

Please sign in to comment.