Skip to content

Commit

Permalink
Merge pull request #641 from alauda/feat/add-lint
Browse files Browse the repository at this point in the history
add golangci-lint to github actions
  • Loading branch information
halfcrazy authored Jan 13, 2021
2 parents 8dd9e33 + 87aa15c commit 4acc0e2
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 178 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.29

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
args: --timeout 180s --disable ineffassign

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go
# skip-go-installation: true
90 changes: 13 additions & 77 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (c *Controller) startWorkers(stopCh <-chan struct{}) {
break
}
}
if ready == true {
if ready {
break
}
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/controller/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ func (c *Controller) isLeader() bool {
return c.elector.IsLeader()
}

func (c *Controller) hasLeader() bool {
return c.elector.GetLeader() != ""
}

func (c *Controller) leaderElection() {
elector := setupLeaderElection(&leaderElectionConfig{
Client: c.config.KubeClient,
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *Controller) markAndCleanLSP() error {
noPodLSP := map[string]bool{}
for _, lsp := range lsps {
if !util.IsStringIn(lsp, ipNames) {
if lastNoPodLSP[lsp] == false {
if !lastNoPodLSP[lsp] {
noPodLSP[lsp] = true
} else {
klog.Infof("gc logical switch port %s", lsp)
Expand Down Expand Up @@ -296,7 +296,7 @@ func (c *Controller) gcLoadBalancer() error {
return err
}
for vip := range vips {
if !util.IsStringIn(vip, udpVips) {
if !util.IsStringIn(vip, udpSessionVips) {
err := c.ovnClient.DeleteLoadBalancerVip(vip, c.config.ClusterUdpSessionLoadBalancer)
if err != nil {
klog.Errorf("failed to delete vip %s from udp session lb, %v", vip, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Controller) InitDefaultVpc() error {
if err != nil {
return err
}
vpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Patch(context.Background(), vpc.Name, types.MergePatchType, bytes, v1.PatchOptions{}, "status")
_, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Patch(context.Background(), vpc.Name, types.MergePatchType, bytes, v1.PatchOptions{}, "status")
if err != nil {
klog.Errorf("init default vpc failed %v", err)
return err
Expand Down
10 changes: 2 additions & 8 deletions pkg/controller/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,7 @@ func hasIngressRule(np *netv1.NetworkPolicy) bool {
return true
}
}
if np.Spec.Ingress != nil {
return true
}
return false
return np.Spec.Ingress != nil
}

func hasEgressRule(np *netv1.NetworkPolicy) bool {
Expand All @@ -504,10 +501,7 @@ func hasEgressRule(np *netv1.NetworkPolicy) bool {
return true
}
}
if np.Spec.Egress != nil {
return true
}
return false
return np.Spec.Egress != nil
}

func (c *Controller) fetchPolicySelectedAddresses(namespace, protocol string, npp netv1.NetworkPolicyPeer) ([]string, []string, error) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ func (c *Controller) handleDeletePod(key string) error {
return nil
}
pod, err := c.podsLister.Pods(namespace).Get(name)
if err != nil {
return nil
}
if pod != nil && pod.DeletionTimestamp == nil && isPodAlive(pod) {
// Pod with same name exists, just return here
return nil
Expand Down Expand Up @@ -699,7 +702,7 @@ func (c *Controller) getPodDefaultSubnet(pod *v1.Pod) (*kubeovnv1.Subnet, error)
return nil, err
}

subnetName, _ = ns.Annotations[util.LogicalSwitchAnnotation]
subnetName = ns.Annotations[util.LogicalSwitchAnnotation]
if subnetName == "" {
err = fmt.Errorf("namespace default logical switch is not found")
klog.Error(err)
Expand Down
8 changes: 3 additions & 5 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,7 @@ func checkAndUpdateGateway(subnet *kubeovnv1.Subnet) (bool, error) {
func checkAndUpdateExcludeIps(subnet *kubeovnv1.Subnet) bool {
changed := false
var excludeIps []string
for _, gw := range strings.Split(subnet.Spec.Gateway, ",") {
excludeIps = append(excludeIps, gw)
}
excludeIps = append(excludeIps, strings.Split(subnet.Spec.Gateway, ",")...)
if len(subnet.Spec.ExcludeIps) == 0 {
subnet.Spec.ExcludeIps = excludeIps
changed = true
Expand Down Expand Up @@ -1053,7 +1051,7 @@ func calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
if err != nil {
return err
}
subnet, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(context.Background(), subnet.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status")
_, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(context.Background(), subnet.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status")
return err
}

Expand Down Expand Up @@ -1092,7 +1090,7 @@ func calcSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
if err != nil {
return err
}
subnet, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(context.Background(), subnet.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status")
_, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Patch(context.Background(), subnet.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status")
return err
}

Expand Down
34 changes: 5 additions & 29 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import (
"errors"
"flag"
"fmt"
"net"
"os"
"os/exec"
"regexp"
"strings"
"syscall"

clientset "github.com/alauda/kube-ovn/pkg/client/clientset/versioned"
"github.com/alauda/kube-ovn/pkg/util"
"github.com/sirupsen/logrus"
Expand All @@ -20,6 +13,11 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"net"
"os"
"os/exec"
"regexp"
"strings"
)

// Configuration is the daemon conf
Expand Down Expand Up @@ -232,28 +230,6 @@ func (config *Configuration) initKubeClient() error {
return nil
}

func getDefaultGatewayIface() (string, error) {
routes, err := netlink.RouteList(nil, syscall.AF_INET)
if err != nil {
return "", err
}

for _, route := range routes {
if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
if route.LinkIndex <= 0 {
return "", errors.New("found default route but could not determine interface")
}
iface, err := net.InterfaceByIndex(route.LinkIndex)
if err != nil {
return "", fmt.Errorf("failed to get iface %v", err)
}
return iface.Name, nil
}
}

return "", errors.New("unable to find default route")
}

func getIfaceOwnPodIP(podIP string) (*net.Interface, error) {
links, err := netlink.LinkList()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *Controller) setIptables() error {
if c.iptable[protocol] == nil {
continue
}
iptableRules := make([]util.IPTableRule, len(v4Rules))
var iptableRules []util.IPTableRule
if protocol == kubeovnv1.ProtocolIPv4 {
iptableRules = v4Rules
} else {
Expand Down Expand Up @@ -266,7 +266,7 @@ func (c *Controller) getLocalPodIPsNeedNAT(protocol string) ([]string, error) {
return nil, err
}
for _, pod := range allPods {
if pod.Spec.HostNetwork == true ||
if pod.Spec.HostNetwork ||
pod.Status.PodIP == "" ||
pod.Annotations[util.LogicalSwitchAnnotation] == "" {
continue
Expand Down
4 changes: 1 addition & 3 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ func (csh cniServerHandler) createOrUpdateIPCr(podRequest request.CniRequest, su
return errMsg
}
} else {
for _, ipStr := range strings.Split(ip, ",") {
ipCr.Spec.AttachIPs = append(ipCr.Spec.AttachIPs, ipStr)
}
ipCr.Spec.AttachIPs = append(ipCr.Spec.AttachIPs, strings.Split(ip, ",")...)
ipCr.Labels[subnet] = ""
ipCr.Spec.AttachSubnets = append(ipCr.Spec.AttachSubnets, subnet)
ipCr.Spec.AttachMacs = append(ipCr.Spec.AttachMacs, macAddr)
Expand Down
2 changes: 0 additions & 2 deletions pkg/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"strings"
"time"

"github.com/alauda/kube-ovn/pkg/util"
Expand Down Expand Up @@ -37,7 +36,6 @@ func InitNodeGateway(config *Configuration) error {
cidr = node.Annotations[util.CidrAnnotation]
portName = node.Annotations[util.PortNameAnnotation]
gw = node.Annotations[util.GatewayAnnotation]
ipAddr = fmt.Sprintf("%s/%s", ip, strings.Split(cidr, "/")[1])
break
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (ipam *IPAM) ReleaseAddressByPod(podName string) {
for _, subnet := range ipam.Subnets {
subnet.ReleaseAddress(podName)
}
return
}

func (ipam *IPAM) AddOrUpdateSubnet(name, cidrStr string, excludeIps []string) error {
Expand Down
9 changes: 4 additions & 5 deletions pkg/ipam/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force
} else {
v6 = true
}
if v4 == true && !subnet.V4CIDR.Contains(net.ParseIP(string(ip))) {
if v4 && !subnet.V4CIDR.Contains(net.ParseIP(string(ip))) {
return ip, mac, OutOfRangeError
}
if v6 == true && !subnet.V6CIDR.Contains(net.ParseIP(string(ip))) {
if v6 && !subnet.V6CIDR.Contains(net.ParseIP(string(ip))) {
return ip, mac, OutOfRangeError
}

Expand All @@ -292,7 +292,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force
}
}

if v4 == true {
if v4 {
if existPod, ok := subnet.V4IPToPod[ip]; ok {
if existPod != podName {
return ip, mac, ConflictError
Expand Down Expand Up @@ -321,7 +321,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force
return ip, mac, nil
}
}
} else if v6 == true {
} else if v6 {
if existPod, ok := subnet.V6IPToPod[ip]; ok {
if existPod != podName {
return ip, mac, ConflictError
Expand Down Expand Up @@ -408,7 +408,6 @@ func (subnet *Subnet) ReleaseAddress(podName string) {
klog.Infof("release v6 %s mac %s for %s, add ip to released list", ip, mac, podName)
}
}
return
}

func (subnet *Subnet) ContainAddress(address IP) bool {
Expand Down
18 changes: 0 additions & 18 deletions pkg/ovnmonitor/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ type Exporter struct {
errorsLocker sync.RWMutex
}

// ClusterPeer contains information about a cluster peer.
type ClusterPeer struct {
ID string
Address string
NextIndex uint64
MatchIndex uint64
Connection struct {
Inbound int
Outbound int
}
}

// OVNDBClusterStatus contains information about a cluster.
type OVNDBClusterStatus struct {
cid string
Expand All @@ -60,12 +48,6 @@ type OVNDBClusterStatus struct {
connOut float64
connInErr float64
connOutErr float64

peers map[string]*ClusterPeer
connections struct {
inbound int
outbound int
}
}

// NewExporter returns an initialized Exporter.
Expand Down
2 changes: 1 addition & 1 deletion pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ func (c Client) GetLogicalSwitchPortAddress(port string) ([]string, error) {
klog.Errorf("get port %s addresses failed %v", port, err)
return nil, err
}
if strings.Index(output, "dynamic") != -1 {
if strings.Contains(output, "dynamic") {
// [dynamic]
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ovs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func trimCommandOutput(raw []byte) string {
func ExpandExcludeIPs(excludeIPs []string, cidr string) []string {
rv := []string{}
for _, excludeIP := range excludeIPs {
if strings.Index(excludeIP, "..") != -1 {
if strings.Contains(excludeIP, "..") {
for _, cidrBlock := range strings.Split(cidr, ",") {
subnetNum := util.SubnetNumber(cidrBlock)
broadcast := util.SubnetBroadCast(cidrBlock)
Expand Down
9 changes: 4 additions & 5 deletions pkg/pinger/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (e *Exporter) setOvsDpIfMetric(datapathName string) error {
}

func (e *Exporter) ovsDatapathLookupsMetrics(line, datapath string) {
s := strings.Trim(line, "lookups:")
s := strings.TrimPrefix(line, "lookups:")
for _, field := range strings.Fields(s) {
elem := strings.Split(field, ":")
value, err := strconv.ParseFloat(elem[1], 64)
Expand All @@ -119,7 +119,7 @@ func (e *Exporter) ovsDatapathLookupsMetrics(line, datapath string) {
}

func (e *Exporter) ovsDatapathMasksMetrics(line, datapath string) {
s := strings.Trim(line, "masks:")
s := strings.TrimPrefix(line, "masks:")
for _, field := range strings.Fields(s) {
elem := strings.Split(field, ":")
value, err := strconv.ParseFloat(elem[1], 64)
Expand Down Expand Up @@ -151,9 +151,8 @@ func (e *Exporter) ovsDatapathPortMetrics(line, datapath string) {
}

func (e *Exporter) getInterfaceInfo() ([]*ovsdb.OvsInterface, error) {
intfs := []*ovsdb.OvsInterface{}
var err error
if intfs, err = e.Client.GetDbInterfaces(); err != nil {
intfs, err := e.Client.GetDbInterfaces()
if err != nil {
klog.Errorf("GetDbInterfaces error: %v", err)
e.IncrementErrorCounter()
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/speaker/bgpapiutil/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net"

// nolint:staticcheck
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
Expand Down
3 changes: 2 additions & 1 deletion pkg/speaker/bgpapiutil/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package apiutil
import (
"fmt"

proto "github.com/golang/protobuf/proto"
// nolint:staticcheck
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
api "github.com/osrg/gobgp/api"
Expand Down
Loading

0 comments on commit 4acc0e2

Please sign in to comment.