Skip to content

Commit

Permalink
Merge pull request #1540 from manuelbuil/golangci
Browse files Browse the repository at this point in the history
Add golangci-lint github action and fix all errors
  • Loading branch information
manuelbuil authored Feb 21, 2022
2 parents 4faee72 + 9dfcc87 commit 33c9753
Show file tree
Hide file tree
Showing 28 changed files with 249 additions and 182 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: build flannel

on: [push, pull_request]

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.5.2
with:
args: "--out-${NO_FUTURE}format colored-line-number --skip-dirs='backend/udp' --timeout=5m"
4 changes: 2 additions & 2 deletions backend/awsvpc/awsvpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (be *AwsVpcBackend) cleanupBlackholeRoutes(routeTableID string, network ip.
if *route.State == "blackhole" && route.DestinationCidrBlock != nil {
_, subnet, err := net.ParseCIDR(*route.DestinationCidrBlock)
if err == nil && network.Contains(ip.FromIP(subnet.IP)) {
log.Infof("Removing blackhole route: ", *route.DestinationCidrBlock)
log.Infof("Removing blackhole route: %v", *route.DestinationCidrBlock)
deleteRouteInput := &ec2.DeleteRouteInput{RouteTableId: &routeTableID, DestinationCidrBlock: route.DestinationCidrBlock}
if _, err := ec2c.DeleteRoute(deleteRouteInput); err != nil {
if ec2err, ok := err.(awserr.Error); !ok || ec2err.Code() != "InvalidRoute.NotFound" {
Expand Down Expand Up @@ -299,7 +299,7 @@ func (be *AwsVpcBackend) detectRouteTableID(eni *ec2.InstanceNetworkInterface, e

res, err = ec2c.DescribeRouteTables(routeTablesInput)
if err != nil {
log.Infof("error describing route tables: ", err)
log.Infof("error describing route tables: %v", err)
}

if len(res.RouteTables) == 0 {
Expand Down
11 changes: 9 additions & 2 deletions backend/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ func runCmd(env []string, stdin string, name string, arg ...string) (string, err
return "", err
}

io.WriteString(stdinpipe, stdin)
io.WriteString(stdinpipe, "\n")
_, err = io.WriteString(stdinpipe, stdin)
if err != nil {
return "", err
}

_, err = io.WriteString(stdinpipe, "\n")
if err != nil {
return "", err
}
stdinpipe.Close()

output, err := cmd.CombinedOutput()
Expand Down
13 changes: 5 additions & 8 deletions backend/extension/extension_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
)

type network struct {
name string
extIface *backend.ExternalInterface
lease *subnet.Lease
sm subnet.Manager
Expand Down Expand Up @@ -60,14 +59,12 @@ func (n *network) Run(ctx context.Context) {
defer wg.Wait()

for {
select {
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)
evtBatch, ok := <-evts
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)
}
}

Expand Down
7 changes: 4 additions & 3 deletions backend/gce/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package gce

import (
"context"
"fmt"
"time"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
log "k8s.io/klog"
)

Expand All @@ -34,12 +35,12 @@ type gceAPI struct {
}

func newAPI() (*gceAPI, error) {
client, err := google.DefaultClient(oauth2.NoContext)
client, err := google.DefaultClient(context.Background())
if err != nil {
return nil, fmt.Errorf("error creating client: %v", err)
}

cs, err := compute.New(client)
cs, err := compute.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("error creating compute service: %v", err)
}
Expand Down
16 changes: 10 additions & 6 deletions backend/ipsec/handle_charon.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ func NewCharonIKEDaemon(ctx context.Context, wg *sync.WaitGroup, espProposal str
}
wg.Add(1)
go func() {
select {
case <-ctx.Done():
cmd.Process.Signal(syscall.SIGTERM)
cmd.Wait()
log.Infof("Stopped charon daemon")
wg.Done()
<-ctx.Done()
err := cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Errorf("Error while processing the signal: %v", err)
}
err = cmd.Wait()
if err != nil {
log.Errorf("Error while waiting for process to exit: %v", err)
}
log.Infof("Stopped charon daemon")
wg.Done()
}()
return charon, nil
}
Expand Down
14 changes: 6 additions & 8 deletions backend/ipsec/ipsec_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,13 @@ func (n *network) Run(ctx context.Context) {
}()

for {
select {
case evtsBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
log.Info("Handling event")
n.handleSubnetEvents(evtsBatch)
evtsBatch, ok := <-evts
if !ok {
log.Infof("evts chan closed")
return
}
log.Info("Handling event")
n.handleSubnetEvents(evtsBatch)
}
}

Expand Down
14 changes: 6 additions & 8 deletions backend/route_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ func (n *RouteNetwork) Run(ctx context.Context) {
defer wg.Wait()

for {
select {
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)
evtBatch, ok := <-evts
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)
}
}

Expand Down Expand Up @@ -169,7 +167,7 @@ func routeAdd(route *netlink.Route, ipFamily int, addToRouteList, removeFromRout
log.Errorf("Error adding route to %v", route)
return
}
routeList, err = netlink.RouteListFiltered(ipFamily, &netlink.Route{Dst: route.Dst}, netlink.RT_FILTER_DST)
_, err = netlink.RouteListFiltered(ipFamily, &netlink.Route{Dst: route.Dst}, netlink.RT_FILTER_DST)
if err != nil {
log.Warningf("Unable to list routes: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion backend/udp/cproxy_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func writeCommand(f *os.File, cmd *C.command) {
}
buf := *(*[]byte)(unsafe.Pointer(&hdr))

f.Write(buf)
_, err := f.Write(buf)
if err != nil {
log.Errorf("Error while writing the command %v. Error: %v", cmd, err)
}
}

func setRoute(ctl *os.File, dst ip.IP4Net, nextHopIP ip.IP4, nextHopPort int) {
Expand Down
15 changes: 6 additions & 9 deletions backend/udp/udp_network_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (

type network struct {
backend.SimpleNetwork
name string
port int
ctl *os.File
ctl2 *os.File
Expand Down Expand Up @@ -107,15 +106,13 @@ func (n *network) Run(ctx context.Context) {
}()

for {
select {
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
stopProxy(n.ctl)
return
}
n.processSubnetEvents(evtBatch)
evtBatch, ok := <-evts
if !ok {
log.Infof("evts chan closed")
stopProxy(n.ctl)
return
}
n.processSubnetEvents(evtBatch)
}
}

Expand Down
12 changes: 5 additions & 7 deletions backend/vxlan/vxlan_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ func (nw *network) Run(ctx context.Context) {
defer wg.Wait()

for {
select {
case evtBatch, ok := <-events:
if !ok {
log.Infof("evts chan closed")
return
}
nw.handleSubnetEvents(evtBatch)
evtBatch, ok := <-events
if !ok {
log.Infof("evts chan closed")
return
}
nw.handleSubnetEvents(evtBatch)
}
}

Expand Down
16 changes: 10 additions & 6 deletions backend/wireguard/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ func newWGDevice(devAttrs *wgDeviceAttrs, ctx context.Context, wg *sync.WaitGrou
// We remove the device to undo any change we did to the system.
wg.Add(1)
go func() {
select {
case <-ctx.Done():
dev.remove()
log.Infof("Removed wireguard device %s", dev.attrs.name)
wg.Done()
<-ctx.Done()
err := dev.remove()
if err != nil {
log.Errorf("Error while removing device: %v", err)
}
log.Infof("Removed wireguard device %s", dev.attrs.name)
wg.Done()
}()

return &dev, nil
Expand Down Expand Up @@ -298,7 +299,10 @@ func (dev *wgDevice) addPeer(publicEndpoint string, peerPublicKeyRaw string, pee
}

// Remove peers from this endpoint with different public keys
dev.cleanupEndpointPeers(udpEndpoint, peerPublicKeyRaw)
err = dev.cleanupEndpointPeers(udpEndpoint, peerPublicKeyRaw)
if err != nil {
return fmt.Errorf("failed to clean up endpoint peers %w", err)
}

return nil
}
Expand Down
12 changes: 6 additions & 6 deletions backend/wireguard/wireguard_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {

if len(event.Lease.Attrs.BackendData) > 0 {
if err := json.Unmarshal(event.Lease.Attrs.BackendData, &wireguardAttrs); err != nil {
log.Errorf("failed to unmarshal BackendData: %w", err)
log.Errorf("failed to unmarshal BackendData: %v", err)
continue
}
}
Expand All @@ -131,7 +131,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {

if len(event.Lease.Attrs.BackendV6Data) > 0 {
if err := json.Unmarshal(event.Lease.Attrs.BackendV6Data, &wireguardAttrs); err != nil {
log.Errorf("failed to unmarshal BackendData: %w", err)
log.Errorf("failed to unmarshal BackendData: %v", err)
continue
}
}
Expand All @@ -141,7 +141,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
publicEndpoint,
wireguardAttrs.PublicKey,
event.Lease.IPv6Subnet.ToIPNet()); err != nil {
log.Errorf("failed to setup ipv6 peer (%s): %w", wireguardAttrs.PublicKey, err)
log.Errorf("failed to setup ipv6 peer (%s): %v", wireguardAttrs.PublicKey, err)
}
}

Expand All @@ -157,7 +157,7 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
log.Info("Subnet removed: ", event.Lease.Subnet)
if len(event.Lease.Attrs.BackendData) > 0 {
if err := json.Unmarshal(event.Lease.Attrs.BackendData, &wireguardAttrs); err != nil {
log.Errorf("failed to unmarshal BackendData: %w", err)
log.Errorf("failed to unmarshal BackendData: %v", err)
continue
}
}
Expand All @@ -173,15 +173,15 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
log.Info("Subnet removed: ", event.Lease.IPv6Subnet)
if len(event.Lease.Attrs.BackendV6Data) > 0 {
if err := json.Unmarshal(event.Lease.Attrs.BackendV6Data, &wireguardAttrs); err != nil {
log.Errorf("failed to unmarshal BackendData: %w", err)
log.Errorf("failed to unmarshal BackendData: %v", err)
continue
}
}

if err := n.v6Dev.removePeer(
wireguardAttrs.PublicKey,
); err != nil {
log.Errorf("failed to remove ipv6 peer (%s): %w", wireguardAttrs.PublicKey, err)
log.Errorf("failed to remove ipv6 peer (%s): %v", wireguardAttrs.PublicKey, err)
}
}

Expand Down
Loading

0 comments on commit 33c9753

Please sign in to comment.