Skip to content

Commit

Permalink
Run staticcheck and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mogren committed Jun 29, 2020
1 parent 8dfc5b1 commit 8c266e9
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 104 deletions.
6 changes: 0 additions & 6 deletions cmd/routed-eni-cni-plugin/cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ func setup(t *testing.T) (*gomock.Controller,
mock_driver.NewMockNetworkAPIs(ctrl)
}

type rpcConn struct{}

func (*rpcConn) Close() error {
return nil
}

func TestCmdAdd(t *testing.T) {
ctrl, mocksTypes, mocksGRPC, mocksRPC, mocksNetwork := setup(t)
defer ctrl.Finish()
Expand Down
20 changes: 10 additions & 10 deletions cmd/routed-eni-cni-plugin/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,28 @@ func (m *testMocks) mockWithFailureAt(t *testing.T, failAt string) *createVethPa

// veth pair
if failAt == "link-add" {
call = m.netlink.EXPECT().LinkAdd(gomock.Any()).Return(errors.New("error on LinkAdd"))
m.netlink.EXPECT().LinkAdd(gomock.Any()).Return(errors.New("error on LinkAdd"))
return mockContext
}
call = m.netlink.EXPECT().LinkAdd(gomock.Any()).Return(nil)

//hostVeth
if failAt == "link-by-name" {
call = m.netlink.EXPECT().LinkByName(gomock.Any()).Return(nil, errors.New("error on LinkByName host")).After(call)
m.netlink.EXPECT().LinkByName(gomock.Any()).Return(nil, errors.New("error on LinkByName host")).After(call)
return mockContext
}
call = m.netlink.EXPECT().LinkByName(gomock.Any()).Return(mockHostVeth, nil).After(call)

//host side setup
if failAt == "link-setup" {
call = m.netlink.EXPECT().LinkSetUp(mockHostVeth).Return(errors.New("error on LinkSetup")).After(call)
m.netlink.EXPECT().LinkSetUp(mockHostVeth).Return(errors.New("error on LinkSetup")).After(call)
return mockContext
}
call = m.netlink.EXPECT().LinkSetUp(mockHostVeth).Return(nil).After(call)

//container side
if failAt == "link-byname" {
call = m.netlink.EXPECT().LinkByName(gomock.Any()).Return(mockContVeth, errors.New("error on LinkByName container")).After(call)
m.netlink.EXPECT().LinkByName(gomock.Any()).Return(mockContVeth, errors.New("error on LinkByName container")).After(call)
return mockContext
}
call = m.netlink.EXPECT().LinkByName(gomock.Any()).Return(mockContVeth, nil).After(call)
Expand All @@ -131,20 +131,20 @@ func (m *testMocks) mockWithFailureAt(t *testing.T, failAt string) *createVethPa
call = mockContVeth.EXPECT().Attrs().Return(mockLinkAttrs).After(call)

if failAt == "route-replace" {
call = m.netlink.EXPECT().RouteReplace(gomock.Any()).Return(errors.New("error on RouteReplace")).After(call)
m.netlink.EXPECT().RouteReplace(gomock.Any()).Return(errors.New("error on RouteReplace")).After(call)
return mockContext
}
call = m.netlink.EXPECT().RouteReplace(gomock.Any()).Return(nil).After(call)

if failAt == "add-defaultroute" {
call = m.ip.EXPECT().AddDefaultRoute(gomock.Any(), mockContVeth).Return(errors.New("error on AddDefaultRoute")).After(call)
m.ip.EXPECT().AddDefaultRoute(gomock.Any(), mockContVeth).Return(errors.New("error on AddDefaultRoute")).After(call)
return mockContext
}
call = m.ip.EXPECT().AddDefaultRoute(gomock.Any(), mockContVeth).Return(nil).After(call)

// container addr
if failAt == "addr-add" {
call = m.netlink.EXPECT().AddrAdd(mockContVeth, gomock.Any()).Return(errors.New("error on AddrAdd")).After(call)
m.netlink.EXPECT().AddrAdd(mockContVeth, gomock.Any()).Return(errors.New("error on AddrAdd")).After(call)
return mockContext
}
call = m.netlink.EXPECT().AddrAdd(mockContVeth, gomock.Any()).Return(nil).After(call)
Expand All @@ -154,18 +154,18 @@ func (m *testMocks) mockWithFailureAt(t *testing.T, failAt string) *createVethPa
// hostVethMAC
call = mockHostVeth.EXPECT().Attrs().Return(mockLinkAttrs).After(call)
if failAt == "neigh-add" {
call = m.netlink.EXPECT().NeighAdd(gomock.Any()).Return(errors.New("error on NeighAdd")).After(call)
m.netlink.EXPECT().NeighAdd(gomock.Any()).Return(errors.New("error on NeighAdd")).After(call)
return mockContext
}
call = m.netlink.EXPECT().NeighAdd(gomock.Any()).Return(nil).After(call)

call = m.netns.EXPECT().Fd().Return(uintptr(testFD)).After(call)
// move it host namespace
if failAt == "link-setns" {
call = m.netlink.EXPECT().LinkSetNsFd(mockHostVeth, testFD).Return(errors.New("error on LinkSetNsFd")).After(call)
m.netlink.EXPECT().LinkSetNsFd(mockHostVeth, testFD).Return(errors.New("error on LinkSetNsFd")).After(call)
return mockContext
}
call = m.netlink.EXPECT().LinkSetNsFd(mockHostVeth, testFD).Return(nil).After(call)
m.netlink.EXPECT().LinkSetNsFd(mockHostVeth, testFD).Return(nil).After(call)

return mockContext
}
Expand Down
1 change: 0 additions & 1 deletion pkg/awsutils/awsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ type EC2InstanceMetadataCache struct {
// metadata info
securityGroups StringSet
subnetID string
cidrBlock string
localIPv4 string
instanceID string
instanceType string
Expand Down
7 changes: 0 additions & 7 deletions pkg/awsutils/awsutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"os"
"sort"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -75,12 +74,6 @@ func TestInitWithEC2metadata(t *testing.T) {
defer ctrl.Finish()

metadataVPCIPv4CIDRs := "192.168.0.0/16 100.66.0.0/1"
vpcIPv4CIDRs := strings.Fields(metadataVPCIPv4CIDRs)
var cidr []*string
for _, vpcCIDR := range vpcIPv4CIDRs {
log.Debugf("Found VPC CIDR: %s", vpcCIDR)
cidr = append(cidr, aws.String(vpcCIDR))
}

mockMetadata.EXPECT().GetMetadata(metadataAZ).Return(az, nil)
mockMetadata.EXPECT().GetMetadata(metadataLocalIP).Return(localIP, nil)
Expand Down
23 changes: 11 additions & 12 deletions pkg/eniconfig/eniconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package eniconfig

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -21,7 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
sdk "github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/stretchr/testify/assert"

corev1 "k8s.io/api/core/v1"
Expand All @@ -38,11 +39,10 @@ func updateENIConfig(hdlr sdk.Handler, name string, eniConfig v1alpha1.ENIConfig
Deleted: toDelete,
}

hdlr.Handle(nil, event)
_ = hdlr.Handle(context.TODO(), event)
}

func updateNodeAnnotation(hdlr sdk.Handler, nodeName string, configName string, toDelete bool) {

node := corev1.Node{
TypeMeta: metav1.TypeMeta{APIVersion: corev1.SchemeGroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -66,7 +66,7 @@ func updateNodeAnnotation(hdlr sdk.Handler, nodeName string, configName string,
eniAnnotations[eniConfigAnnotationDef] = configName
}
accessor.SetAnnotations(eniAnnotations)
hdlr.Handle(nil, event)
_ = hdlr.Handle(context.TODO(), event)
}

func updateNodeLabel(hdlr sdk.Handler, nodeName string, configName string, toDelete bool) {
Expand Down Expand Up @@ -94,11 +94,10 @@ func updateNodeLabel(hdlr sdk.Handler, nodeName string, configName string, toDel
eniLabels[eniConfigLabelDef] = configName
}
accessor.SetLabels(eniLabels)
hdlr.Handle(nil, event)
_ = hdlr.Handle(context.TODO(), event)
}

func TestENIConfig(t *testing.T) {

testENIConfigController := NewENIConfigController()

testHandler := NewHandler(testENIConfigController)
Expand Down Expand Up @@ -136,7 +135,7 @@ func TestENIConfig(t *testing.T) {
func TestNodeENIConfig(t *testing.T) {
myNodeName := "testMyNodeWithAnnotation"
myENIConfig := "testMyENIConfig"
os.Setenv("MY_NODE_NAME", myNodeName)
_ = os.Setenv("MY_NODE_NAME", myNodeName)
testENIConfigController := NewENIConfigController()

testHandler := NewHandler(testENIConfigController)
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestNodeENIConfig(t *testing.T) {
func TestNodeENIConfigLabel(t *testing.T) {
myNodeName := "testMyNodeWithLabel"
myENIConfig := "testMyENIConfig"
os.Setenv("MY_NODE_NAME", myNodeName)
_ = os.Setenv("MY_NODE_NAME", myNodeName)
testENIConfigController := NewENIConfigController()

testHandler := NewHandler(testENIConfigController)
Expand Down Expand Up @@ -216,25 +215,25 @@ func TestNodeENIConfigLabel(t *testing.T) {
}

func TestGetEniConfigAnnotationDefDefault(t *testing.T) {
os.Unsetenv(envEniConfigAnnotationDef)
_ = os.Unsetenv(envEniConfigAnnotationDef)
eniConfigAnnotationDef := getEniConfigAnnotationDef()
assert.Equal(t, eniConfigAnnotationDef, defaultEniConfigAnnotationDef)
}

func TestGetEniConfigAnnotationlDefCustom(t *testing.T) {
os.Setenv(envEniConfigAnnotationDef, "k8s.amazonaws.com/eniConfigCustom")
_ = os.Setenv(envEniConfigAnnotationDef, "k8s.amazonaws.com/eniConfigCustom")
eniConfigAnnotationDef := getEniConfigAnnotationDef()
assert.Equal(t, eniConfigAnnotationDef, "k8s.amazonaws.com/eniConfigCustom")
}

func TestGetEniConfigLabelDefDefault(t *testing.T) {
os.Unsetenv(envEniConfigLabelDef)
_ = os.Unsetenv(envEniConfigLabelDef)
eniConfigLabelDef := getEniConfigLabelDef()
assert.Equal(t, eniConfigLabelDef, defaultEniConfigLabelDef)
}

func TestGetEniConfigLabelDefCustom(t *testing.T) {
os.Setenv(envEniConfigLabelDef, "k8s.amazonaws.com/eniConfigCustom")
_ = os.Setenv(envEniConfigLabelDef, "k8s.amazonaws.com/eniConfigCustom")
eniConfigLabelDef := getEniConfigLabelDef()
assert.Equal(t, eniConfigLabelDef, "k8s.amazonaws.com/eniConfigCustom")
}
11 changes: 3 additions & 8 deletions pkg/ipamd/datastore/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
)

const (
minLifeTime = 1 * time.Minute
// addressENICoolingPeriod is used to ensure ENI will NOT get freed back to EC2 control plane if one of
// its secondary IP addresses is used for a Pod within last addressENICoolingPeriod
addressENICoolingPeriod = 1 * time.Minute
// minENILifeTime is the shortest time before we consider deleting a newly created ENI
minENILifeTime = 1 * time.Minute

// addressCoolingPeriod is used to ensure an IP not get assigned to a Pod if this IP is used by a different Pod
// in addressCoolingPeriod
Expand Down Expand Up @@ -64,9 +62,6 @@ const BackfillNetworkIface = "unknown"
// ErrUnknownPod is an error when there is no pod in data store matching pod name, namespace, sandbox id
var ErrUnknownPod = errors.New("datastore: unknown pod")

// ErrUnknownPodIP is an error where pod's IP address is not found in data store
var ErrUnknownPodIP = errors.New("datastore: pod using unknown IP address")

var (
enis = prometheus.NewGauge(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -574,7 +569,7 @@ func (ds *DataStore) getDeletableENI(warmIPTarget int, minimumIPTarget int) *ENI

// IsTooYoung returns true if the ENI hasn't been around long enough to be deleted.
func (e *ENI) isTooYoung() bool {
return time.Since(e.createTime) < minLifeTime
return time.Since(e.createTime) < minENILifeTime
}

// HasIPInCooling returns true if an IP address was unassigned recently.
Expand Down
2 changes: 1 addition & 1 deletion pkg/ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ type UnmanagedENISet struct {

func (u *UnmanagedENISet) isUnmanaged(eniID string) bool {
val, ok := u.data[eniID]
return ok && val == true
return ok && val
}

func (u *UnmanagedENISet) reset() {
Expand Down
9 changes: 0 additions & 9 deletions pkg/ipamd/ipamd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,6 @@ func TestTryAddIPToENI(t *testing.T) {

mockContext.dataStore = testDatastore()

podENIConfig := &v1alpha1.ENIConfigSpec{
SecurityGroups: []string{"sg1-id", "sg2-id"},
Subnet: "subnet1",
}
var sg []*string
for _, sgID := range podENIConfig.SecurityGroups {
sg = append(sg, aws.String(sgID))
}

m.awsutils.EXPECT().AllocENI(false, nil, "").Return(secENIid, nil)
m.awsutils.EXPECT().AllocIPAddresses(secENIid, warmIpTarget)
m.awsutils.EXPECT().GetAttachedENIs().Return([]awsutils.ENIMetadata{
Expand Down
10 changes: 1 addition & 9 deletions pkg/k8sapi/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"

clientset "k8s.io/client-go/kubernetes"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
Expand Down Expand Up @@ -82,7 +80,7 @@ func NewController(clientset kubernetes.Interface) *Controller {
}

// CreateKubeClient creates a k8s client
func CreateKubeClient() (clientset.Interface, error) {
func CreateKubeClient() (kubernetes.Interface, error) {
kubeClient := k8sclient.GetKubeClient()
// Informers don't seem to do a good job logging error messages when it
// can't reach the server, making debugging hard. This makes it easier to
Expand Down Expand Up @@ -121,12 +119,6 @@ func (d *Controller) DiscoverCNIK8SPods() {
d.DiscoverK8SPods(cache.NewListWatchFromClient(d.kubeClient.CoreV1().RESTClient(), "pods", metav1.NamespaceSystem, fields.Everything()))
}

// DiscoverLocalK8SPods discovers local pods running on the node
func (d *Controller) DiscoverLocalK8SPods() {
// create the pod watcher
d.DiscoverK8SPods(cache.NewListWatchFromClient(d.kubeClient.CoreV1().RESTClient(), "pods", metav1.NamespaceAll, fields.OneTermEqualSelector("spec.nodeName", d.myNodeName)))
}

// DiscoverK8SPods takes a watcher and updates the Controller cache
func (d *Controller) DiscoverK8SPods(podListWatcher *cache.ListWatch) {
// create the workqueue
Expand Down
10 changes: 0 additions & 10 deletions pkg/networkutils/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"encoding/binary"
"encoding/csv"
"fmt"
"io"
"math"
"net"
"os"
Expand All @@ -43,9 +42,6 @@ import (
)

const (
// 0 - 511 can be used other higher priorities
toPodRulePriority = 512

// 513 - 1023, can be used priority lower than toPodRulePriority but higher than default nonVPC CIDR rule

// 1024 is reserved for (ip rule not to <VPC's subnet> table main)
Expand Down Expand Up @@ -134,7 +130,6 @@ type linuxNetwork struct {
typeOfSNAT snatType
nodePortSupportEnabled bool
shouldConfigureRpFilter bool
connmark uint32
mtu int

netLink netlinkwrapper.NetLink
Expand Down Expand Up @@ -186,11 +181,6 @@ func New() NetworkAPIs {
}
}

type stringWriteCloser interface {
io.Closer
WriteString(s string) (int, error)
}

// find out the primary interface name
func findPrimaryInterfaceName(primaryMAC string) (string, error) {
log.Debugf("Trying to find primary interface that has mac : %s", primaryMAC)
Expand Down
21 changes: 0 additions & 21 deletions pkg/networkutils/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,24 +670,3 @@ func (ipt *mockIptables) HasRandomFully() bool {
// TODO: Work out how to write a test case for this
return true
}

type mockFile struct {
closed bool
data string
}

func (f *mockFile) WriteString(s string) (int, error) {
if f.closed {
panic("write call on closed file")
}
f.data += s
return len(s), nil
}

func (f *mockFile) Close() error {
if f.closed {
panic("close call on closed file")
}
f.closed = true
return nil
}
6 changes: 0 additions & 6 deletions pkg/utils/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@

package logger

import (
"sync"
)

const pluginBinaryName = "aws-cni"

var once sync.Once

//Log is global variable so that log functions can be directly accessed
var log Logger

Expand Down
Loading

0 comments on commit 8c266e9

Please sign in to comment.