From 8dbe8f94678a8f5c7ec6259477d7a9e9dd711057 Mon Sep 17 00:00:00 2001 From: Mengxin Liu Date: Fri, 10 Sep 2021 11:36:35 +0800 Subject: [PATCH] lint: make staticcheck happy --- pkg/controller/pod.go | 2 +- pkg/controller/vpc_nat_gateway.go | 4 +-- pkg/ipam/ipam.go | 16 ++++----- pkg/ipam/subnet.go | 22 ++++++------ pkg/ovnmonitor/util.go | 2 +- pkg/speaker/bgpapiutil/attribute.go | 2 +- pkg/speaker/bgpapiutil/capability.go | 3 +- pkg/speaker/bgpapiutil/util.go | 1 + pkg/speaker/subnet.go | 6 ++-- test/unittest/ipam/ipam.go | 50 ++++++++++++++-------------- 10 files changed, 54 insertions(+), 54 deletions(-) diff --git a/pkg/controller/pod.go b/pkg/controller/pod.go index 182bae0716a..aed8970d40d 100644 --- a/pkg/controller/pod.go +++ b/pkg/controller/pod.go @@ -1090,7 +1090,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st } } klog.Errorf("alloc address for %s failed, return NoAvailableAddress", key) - return "", "", "", ipam.NoAvailableError + return "", "", "", ipam.ErrNoAvailable } func generatePatchPayload(annotations map[string]string, op string) []byte { diff --git a/pkg/controller/vpc_nat_gateway.go b/pkg/controller/vpc_nat_gateway.go index 181cf38937b..f7f65ff2caf 100644 --- a/pkg/controller/vpc_nat_gateway.go +++ b/pkg/controller/vpc_nat_gateway.go @@ -337,7 +337,7 @@ func (c *Controller) handleInitVpcNatGw(key string) error { if pod.Status.Phase != corev1.PodRunning { time.Sleep(5 * 1000) - return fmt.Errorf("failed to init vpc nat gateway, pod is not ready.") + return fmt.Errorf("failed to init vpc nat gateway, pod is not ready") } if _, hasInit := pod.Annotations[util.VpcNatGatewayInitAnnotation]; hasInit { @@ -768,7 +768,7 @@ func (c *Controller) getNatGwPod(name string) (*corev1.Pod, error) { if err != nil { return nil, err } else if len(pods) != 1 { - return nil, fmt.Errorf("too many pod.") + return nil, fmt.Errorf("too many pod") } else if pods[0].Status.Phase != "Running" { return nil, fmt.Errorf("pod is not active now") } diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index e2d1810ffbc..75b32c86345 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -12,10 +12,10 @@ import ( ) var ( - OutOfRangeError = errors.New("AddressOutOfRange") - ConflictError = errors.New("AddressConflict") - NoAvailableError = errors.New("NoAvailableAddress") - InvalidCIDRError = errors.New("CIDRInvalid") + ErrOutOfRange = errors.New("AddressOutOfRange") + ErrConflict = errors.New("AddressConflict") + ErrNoAvailable = errors.New("NoAvailableAddress") + ErrInvalidCIDR = errors.New("CIDRInvalid") ) type IPAM struct { @@ -42,7 +42,7 @@ func (ipam *IPAM) GetRandomAddress(podName, subnetName string, skippedAddrs []st subnet, ok := ipam.Subnets[subnetName] if !ok { - return "", "", "", NoAvailableError + return "", "", "", ErrNoAvailable } v4IP, v6IP, mac, err := subnet.GetRandomAddress(podName, skippedAddrs) @@ -54,7 +54,7 @@ func (ipam *IPAM) GetStaticAddress(podName, ip, mac, subnetName string, checkCon ipam.mutex.RLock() defer ipam.mutex.RUnlock() if subnet, ok := ipam.Subnets[subnetName]; !ok { - return "", "", "", NoAvailableError + return "", "", "", ErrNoAvailable } else { var ips []IP var err error @@ -84,7 +84,7 @@ func (ipam *IPAM) GetStaticAddress(podName, ip, mac, subnetName string, checkCon return string(ips[0]), string(ips[1]), mac, err } } - return "", "", "", NoAvailableError + return "", "", "", ErrNoAvailable } func checkAndAppendIpsForDual(ips []IP, podName string, subnet *Subnet) ([]IP, error) { @@ -127,7 +127,7 @@ func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr string, excludeIps []string) e var cidrs []*net.IPNet for _, cidrBlock := range strings.Split(cidrStr, ",") { if _, cidr, err := net.ParseCIDR(cidrBlock); err != nil { - return InvalidCIDRError + return ErrInvalidCIDR } else { cidrs = append(cidrs, cidr) } diff --git a/pkg/ipam/subnet.go b/pkg/ipam/subnet.go index f3de6d4fd35..057481b2632 100644 --- a/pkg/ipam/subnet.go +++ b/pkg/ipam/subnet.go @@ -37,7 +37,7 @@ func NewSubnet(name, cidrStr string, excludeIps []string) (*Subnet, error) { var cidrs []*net.IPNet for _, cidrBlock := range strings.Split(cidrStr, ",") { if _, cidr, err := net.ParseCIDR(cidrBlock); err != nil { - return nil, InvalidCIDRError + return nil, ErrInvalidCIDR } else { cidrs = append(cidrs, cidr) } @@ -136,7 +136,7 @@ func (subnet *Subnet) GetRandomMac(podName string) string { func (subnet *Subnet) GetStaticMac(podName, mac string, checkConflict bool) error { if checkConflict { if p, ok := subnet.MacToPod[mac]; ok && p != podName { - return ConflictError + return ErrConflict } } subnet.MacToPod[mac] = podName @@ -183,7 +183,7 @@ func (subnet *Subnet) getV4RandomAddress(podName string, skippedAddrs []string) } if len(subnet.V4FreeIPList) == 0 { if len(subnet.V4ReleasedIPList) == 0 { - return "", "", "", NoAvailableError + return "", "", "", ErrNoAvailable } subnet.V4FreeIPList = subnet.V4ReleasedIPList subnet.V4ReleasedIPList = IPRangeList{} @@ -204,7 +204,7 @@ func (subnet *Subnet) getV4RandomAddress(podName string, skippedAddrs []string) } } if ip == "" { - return "", "", "", ConflictError + return "", "", "", ErrConflict } ipr := subnet.V4FreeIPList[idx] @@ -233,7 +233,7 @@ func (subnet *Subnet) getV6RandomAddress(podName string, skippedAddrs []string) } if len(subnet.V6FreeIPList) == 0 { if len(subnet.V6ReleasedIPList) == 0 { - return "", "", "", NoAvailableError + return "", "", "", ErrNoAvailable } subnet.V6FreeIPList = subnet.V6ReleasedIPList subnet.V6ReleasedIPList = IPRangeList{} @@ -254,7 +254,7 @@ func (subnet *Subnet) getV6RandomAddress(podName string, skippedAddrs []string) } } if ip == "" { - return "", "", "", ConflictError + return "", "", "", ErrConflict } ipr := subnet.V6FreeIPList[idx] @@ -285,10 +285,10 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force v6 = subnet.V6CIDR != nil } if v4 && !subnet.V4CIDR.Contains(net.ParseIP(string(ip))) { - return ip, mac, OutOfRangeError + return ip, mac, ErrOutOfRange } if v6 && !subnet.V6CIDR.Contains(net.ParseIP(string(ip))) { - return ip, mac, OutOfRangeError + return ip, mac, ErrOutOfRange } if mac == "" { @@ -312,7 +312,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force subnet.V4IPToPod[ip] = fmt.Sprintf("%s,%s", subnet.V4IPToPod[ip], podName) return ip, mac, nil } - return ip, mac, ConflictError + return ip, mac, ErrConflict } if !force { return ip, mac, nil @@ -347,7 +347,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force subnet.V6IPToPod[ip] = fmt.Sprintf("%s,%s", subnet.V6IPToPod[ip], podName) return ip, mac, nil } - return ip, mac, ConflictError + return ip, mac, ErrConflict } if !force { return ip, mac, nil @@ -374,7 +374,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force } } } - return ip, mac, NoAvailableError + return ip, mac, ErrNoAvailable } func (subnet *Subnet) releaseAddr(podName string) { diff --git a/pkg/ovnmonitor/util.go b/pkg/ovnmonitor/util.go index 7ad1c86b939..d790e22f055 100644 --- a/pkg/ovnmonitor/util.go +++ b/pkg/ovnmonitor/util.go @@ -126,7 +126,7 @@ func getClusterInfo(direction, dbName string) (*OVNDBClusterStatus, error) { cmd := exec.Command("sh", "-c", cmdstr) output, err := cmd.CombinedOutput() if err != nil { - return nil, fmt.Errorf("Failed to retrieve cluster/status info for database %s: %v", dbName, err) + return nil, fmt.Errorf("failed to retrieve cluster/status info for database %s: %v", dbName, err) } for _, line := range strings.Split(string(output), "\n") { diff --git a/pkg/speaker/bgpapiutil/attribute.go b/pkg/speaker/bgpapiutil/attribute.go index d8c6efbc933..e3d81baf24d 100644 --- a/pkg/speaker/bgpapiutil/attribute.go +++ b/pkg/speaker/bgpapiutil/attribute.go @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//nolint:staticcheck package apiutil import ( @@ -20,7 +21,6 @@ import ( "fmt" "net" - // nolint:staticcheck "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes/any" diff --git a/pkg/speaker/bgpapiutil/capability.go b/pkg/speaker/bgpapiutil/capability.go index d3215b9920f..e4540cc90c8 100644 --- a/pkg/speaker/bgpapiutil/capability.go +++ b/pkg/speaker/bgpapiutil/capability.go @@ -13,12 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +//nolint:staticcheck package apiutil import ( "fmt" - - // nolint:staticcheck "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes/any" diff --git a/pkg/speaker/bgpapiutil/util.go b/pkg/speaker/bgpapiutil/util.go index 1dd25c79f9d..46655fecbb2 100644 --- a/pkg/speaker/bgpapiutil/util.go +++ b/pkg/speaker/bgpapiutil/util.go @@ -13,6 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//nolint:staticcheck package apiutil import ( diff --git a/pkg/speaker/subnet.go b/pkg/speaker/subnet.go index 81c5aa97134..469563c7a6e 100644 --- a/pkg/speaker/subnet.go +++ b/pkg/speaker/subnet.go @@ -1,3 +1,4 @@ +//nolint:staticcheck package speaker import ( @@ -8,7 +9,6 @@ import ( "strings" "github.com/golang/protobuf/ptypes" - "github.com/golang/protobuf/ptypes/any" anypb "github.com/golang/protobuf/ptypes/any" bgpapiutil "github.com/kubeovn/kube-ovn/pkg/speaker/bgpapiutil" "github.com/kubeovn/kube-ovn/pkg/util" @@ -174,7 +174,7 @@ func (c *Controller) addRoute(route string) error { return nil } -func (c *Controller) getNlriAndAttrs(route string) (*anypb.Any, []*any.Any, error) { +func (c *Controller) getNlriAndAttrs(route string) (*anypb.Any, []*anypb.Any, error) { prefix, prefixLen, err := parseRoute(route) if err != nil { return nil, nil, err @@ -189,7 +189,7 @@ func (c *Controller) getNlriAndAttrs(route string) (*anypb.Any, []*any.Any, erro a2, _ := ptypes.MarshalAny(&bgpapi.NextHopAttribute{ NextHop: c.config.RouterId, }) - attrs := []*any.Any{a1, a2} + attrs := []*anypb.Any{a1, a2} return nlri, attrs, err } diff --git a/test/unittest/ipam/ipam.go b/test/unittest/ipam/ipam.go index 1c34f1ffe7e..f993b365926 100644 --- a/test/unittest/ipam/ipam.go +++ b/test/unittest/ipam/ipam.go @@ -35,9 +35,9 @@ var _ = Describe("[IPAM]", func() { It("invalid subnet", func() { im := ipam.NewIPAM() err := im.AddOrUpdateSubnet(subnetName, "1.1.1.1/64", nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) err = im.AddOrUpdateSubnet(subnetName, "1.1.256.1/24", nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) }) It("normal subnet", func() { @@ -57,14 +57,14 @@ var _ = Describe("[IPAM]", func() { Expect(ip).To(Equal("10.16.0.3")) _, _, _, err = im.GetStaticAddress("pod3.ns", "10.16.0.2", "", subnetName, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) im.ReleaseAddressByPod("pod1.ns") _, _, _, err = im.GetStaticAddress("pod3.ns", "10.16.0.2", "", subnetName, true) Expect(err).ShouldNot(HaveOccurred()) _, _, _, err = im.GetRandomAddress("pod4.ns", "invalid_subnet", nil) - Expect(err).Should(MatchError(ipam.NoAvailableError)) + Expect(err).Should(MatchError(ipam.ErrNoAvailable)) err = im.AddOrUpdateSubnet(subnetName, ipv4CIDR, nil) Expect(err).ShouldNot(HaveOccurred()) @@ -115,9 +115,9 @@ var _ = Describe("[IPAM]", func() { It("invalid subnet", func() { im := ipam.NewIPAM() err := im.AddOrUpdateSubnet(subnetName, "fd00::/130", nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) err = im.AddOrUpdateSubnet(subnetName, "fd00::g/120", nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) }) It("normal subnet", func() { @@ -137,14 +137,14 @@ var _ = Describe("[IPAM]", func() { Expect(ip).To(Equal("fd00::3")) _, _, _, err = im.GetStaticAddress("pod3.ns", "fd00::2", "", subnetName, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) im.ReleaseAddressByPod("pod1.ns") _, _, _, err = im.GetStaticAddress("pod3.ns", "fd00::2", "", subnetName, true) Expect(err).ShouldNot(HaveOccurred()) _, _, _, err = im.GetRandomAddress("pod4.ns", "invalid_subnet", nil) - Expect(err).Should(MatchError(ipam.NoAvailableError)) + Expect(err).Should(MatchError(ipam.ErrNoAvailable)) err = im.AddOrUpdateSubnet(subnetName, ipv6CIDR, nil) Expect(err).ShouldNot(HaveOccurred()) @@ -195,13 +195,13 @@ var _ = Describe("[IPAM]", func() { It("invalid subnet", func() { im := ipam.NewIPAM() err := im.AddOrUpdateSubnet(subnetName, fmt.Sprintf("1.1.1.1/64,%s", ipv6CIDR), nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) err = im.AddOrUpdateSubnet(subnetName, fmt.Sprintf("1.1.256.1/24,%s", ipv6CIDR), nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) err = im.AddOrUpdateSubnet(subnetName, fmt.Sprintf("%s,fd00::/130", ipv4CIDR), nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) err = im.AddOrUpdateSubnet(subnetName, fmt.Sprintf("%s,fd00::g/120", ipv4CIDR), nil) - Expect(err).Should(MatchError(ipam.InvalidCIDRError)) + Expect(err).Should(MatchError(ipam.ErrInvalidCIDR)) }) It("normal subnet", func() { @@ -223,14 +223,14 @@ var _ = Describe("[IPAM]", func() { Expect(ipv6).To(Equal("fd00::3")) _, _, _, err = im.GetStaticAddress("pod3.ns", "10.16.0.2,fd00::2", "", subnetName, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) im.ReleaseAddressByPod("pod1.ns") _, _, _, err = im.GetStaticAddress("pod3.ns", "10.16.0.2,fd00::2", "", subnetName, true) Expect(err).ShouldNot(HaveOccurred()) _, _, _, err = im.GetRandomAddress("pod4.ns", "invalid_subnet", nil) - Expect(err).Should(MatchError(ipam.NoAvailableError)) + Expect(err).Should(MatchError(ipam.ErrNoAvailable)) err = im.AddOrUpdateSubnet(subnetName, dualCIDR, nil) Expect(err).ShouldNot(HaveOccurred()) @@ -361,9 +361,9 @@ var _ = Describe("[IPAM]", func() { Expect(subnet.V4PodToIP).To(HaveKeyWithValue("pod3.ns", ipam.IP("10.16.0.20"))) _, _, err = subnet.GetStaticAddress("pod4.ns", "10.16.0.3", "", false, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) _, _, err = subnet.GetStaticAddress("pod5.ns", "19.16.0.3", "", false, true) - Expect(err).Should(MatchError(ipam.OutOfRangeError)) + Expect(err).Should(MatchError(ipam.ErrOutOfRange)) subnet.ReleaseAddress("pod1.ns") subnet.ReleaseAddress("pod2.ns") @@ -394,7 +394,7 @@ var _ = Describe("[IPAM]", func() { Expect(ip2).To(Equal(ipam.IP("10.16.0.2"))) _, _, _, err = subnet.GetRandomAddress("pod3.ns", nil) - Expect(err).Should(MatchError(ipam.NoAvailableError)) + Expect(err).Should(MatchError(ipam.ErrNoAvailable)) Expect(subnet.V4FreeIPList).To(BeEmpty()) Expect(subnet.V4IPToPod).To(HaveKeyWithValue(ipam.IP("10.16.0.1"), "pod1.ns")) @@ -449,9 +449,9 @@ var _ = Describe("[IPAM]", func() { Expect(subnet.V6PodToIP).To(HaveKeyWithValue("pod3.ns", ipam.IP("fd00::14"))) _, _, err = subnet.GetStaticAddress("pod4.ns", "fd00::3", "", false, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) _, _, err = subnet.GetStaticAddress("pod5.ns", "fe00::3", "", false, true) - Expect(err).Should(MatchError(ipam.OutOfRangeError)) + Expect(err).Should(MatchError(ipam.ErrOutOfRange)) subnet.ReleaseAddress("pod1.ns") subnet.ReleaseAddress("pod2.ns") @@ -482,7 +482,7 @@ var _ = Describe("[IPAM]", func() { Expect(ip2).To(Equal(ipam.IP("fd00::2"))) _, _, _, err = subnet.GetRandomAddress("pod3.ns", nil) - Expect(err).Should(MatchError(ipam.NoAvailableError)) + Expect(err).Should(MatchError(ipam.ErrNoAvailable)) Expect(subnet.V6FreeIPList).To(BeEmpty()) Expect(subnet.V6IPToPod).To(HaveKeyWithValue(ipam.IP("fd00::1"), "pod1.ns")) @@ -563,13 +563,13 @@ var _ = Describe("[IPAM]", func() { Expect(subnet.V6PodToIP).To(HaveKeyWithValue("pod3.ns", ipam.IP("fd00::14"))) _, _, err = subnet.GetStaticAddress("pod4.ns", "10.16.0.3", "", false, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) _, _, err = subnet.GetStaticAddress("pod4.ns", "fd00::3", "", false, true) - Expect(err).Should(MatchError(ipam.ConflictError)) + Expect(err).Should(MatchError(ipam.ErrConflict)) _, _, err = subnet.GetStaticAddress("pod5.ns", "19.16.0.3", "", false, true) - Expect(err).Should(MatchError(ipam.OutOfRangeError)) + Expect(err).Should(MatchError(ipam.ErrOutOfRange)) _, _, err = subnet.GetStaticAddress("pod5.ns", "fe00::3", "", false, true) - Expect(err).Should(MatchError(ipam.OutOfRangeError)) + Expect(err).Should(MatchError(ipam.ErrOutOfRange)) subnet.ReleaseAddress("pod1.ns") subnet.ReleaseAddress("pod2.ns") @@ -610,7 +610,7 @@ var _ = Describe("[IPAM]", func() { Expect(ipv6).To(Equal(ipam.IP("fd00::2"))) _, _, _, err = subnet.GetRandomAddress("pod3.ns", nil) - Expect(err).Should(MatchError(ipam.NoAvailableError)) + Expect(err).Should(MatchError(ipam.ErrNoAvailable)) Expect(subnet.V4FreeIPList).To(BeEmpty()) Expect(subnet.V6FreeIPList).To(BeEmpty())