Skip to content

Commit

Permalink
QA: update code for golangci-lint and run go fmt. (#87)
Browse files Browse the repository at this point in the history
* QA: update code for golangci-lint.

Signed-off-by: Nashwan Azhari <[email protected]>

* CI: Make linting failures stop test pipeline.

Signed-off-by: Nashwan Azhari <[email protected]>

---------

Signed-off-by: Nashwan Azhari <[email protected]>
  • Loading branch information
aznashwan authored Sep 13, 2023
1 parent baacbec commit 3a5374c
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 144 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ jobs:
go-version: ${{ env.GO_VERSION }}

- name: Lint
# TODO(aznashwan): codebase currently has linter issues to be solved in a later PR.
continue-on-error: true
uses: golangci/golangci-lint-action@v3
with:
working-directory: src/github.com/Microsoft/windows-container-networking
Expand Down
16 changes: 8 additions & 8 deletions cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ type K8SPodEnvArgs struct {
}

type OptionalFlags struct {
LocalRoutePortMapping bool `json:"localRoutedPortMapping"`
AllowAclPortMapping bool `json:"allowAclPortMapping"`
ForceBridgeGateway bool `json:"forceBridgeGateway"` // Intended to be temporary workaround
EnableDualStack bool `json:"enableDualStack"`
LoopbackDSR bool `json:"loopbackDSR"`
GatewayFromAdditionalRoutes bool `json:"gatewayFromAdditionalRoutes"`
LocalRoutePortMapping bool `json:"localRoutedPortMapping"`
AllowAclPortMapping bool `json:"allowAclPortMapping"`
ForceBridgeGateway bool `json:"forceBridgeGateway"` // Intended to be temporary workaround
EnableDualStack bool `json:"enableDualStack"`
LoopbackDSR bool `json:"loopbackDSR"`
GatewayFromAdditionalRoutes bool `json:"gatewayFromAdditionalRoutes"`
}

func (r *Result) Print() {
fmt.Printf(r.String())
fmt.Printf("%s", r.String())
}

func (r *Result) String() string {
Expand Down Expand Up @@ -369,7 +369,7 @@ func GetCurrResult(network *network.NetworkInfo, endpoint *network.EndpointInfo,

var iFace = GetInterface(endpoint)

if cniConfig.OptionalFlags.EnableDualStack == false {
if !cniConfig.OptionalFlags.EnableDualStack {

var ip = GetIP(network, endpoint)
ip.InterfaceIndex = 0
Expand Down
3 changes: 1 addition & 2 deletions cni/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func NewPlugin(name, version string) (*Plugin, error) {
// Initialize initializes the plugin.
func (plugin *Plugin) Initialize(config *common.PluginConfig) error {
// Initialize the base plugin.
plugin.Plugin.Initialize(config)
return nil
return plugin.Plugin.Initialize(config)
}

// Uninitialize uninitializes the plugin.
Expand Down
20 changes: 9 additions & 11 deletions common/core/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) {

logrus.Debugf("[cni-net] Read network configuration %+v.", cniConfig)

if cniConfig.OptionalFlags.EnableDualStack == false {
if !cniConfig.OptionalFlags.EnableDualStack {
logrus.Infof("[cni-net] Dual stack is disabled")
} else {
logrus.Infof("[cni-net] Dual stack is enabled")
Expand All @@ -141,7 +141,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) {
return errors.New("cannot create endpoint without a namespace")
}

if cniConfig.OptionalFlags.EnableDualStack == false {
if !cniConfig.OptionalFlags.EnableDualStack {
nwConfig, err = getOrCreateNetwork(plugin, networkInfo, cniConfig)
} else {
// The network must be created beforehand
Expand Down Expand Up @@ -265,8 +265,7 @@ func addEndpointGatewaysFromConfig(
m1, _ := addr.Dst.Mask.Size()
m2, _ := defaultDestipv4Network.Mask.Size()

if m1 == m2 &&
addr.Dst.IP.Equal(defaultDestipv4) {
if m1 == m2 && addr.Dst.IP.Equal(defaultDestipv4) {
endpointInfo.Gateway = addr.GW
logrus.Debugf("[cni-net] Assigned %v as ipv4 gateway", endpointInfo.Gateway.String())
}
Expand All @@ -279,8 +278,7 @@ func addEndpointGatewaysFromConfig(
m1, _ := addr.Dst.Mask.Size()
m2, _ := defaultDestipv6Network.Mask.Size()

if m1 == m2 &&
addr.Dst.IP.Equal(defaultDestipv6) {
if m1 == m2 && addr.Dst.IP.Equal(defaultDestipv6) {
endpointInfo.Gateway6 = addr.GW
logrus.Debugf("[cni-net] Assigned %v as ipv6 gateway", endpointInfo.Gateway6.String())
}
Expand All @@ -304,7 +302,7 @@ func allocateIpam(
var resultImpl *cniTypesImpl.Result
var err error

if cniConfig.OptionalFlags.EnableDualStack == false {
if !cniConfig.OptionalFlags.EnableDualStack {
// It seems the right thing would be to pass the original byte stream instead of the one
// which cni parsed into NetworkConfig. However to preserve compatibility continue
// the current behavior when dual stack is not enabled
Expand All @@ -325,7 +323,7 @@ func allocateIpam(
}

logrus.Debugf("[cni-net] IPAM plugin returned result %v.", resultImpl)
if cniConfig.OptionalFlags.EnableDualStack == false {
if !cniConfig.OptionalFlags.EnableDualStack {
// Derive the subnet from allocated IP address.
if resultImpl.IP4 != nil {
var subnetInfo = network.SubnetInfo{
Expand All @@ -337,7 +335,7 @@ func allocateIpam(
endpointInfo.IPAddress = resultImpl.IP4.IP.IP
endpointInfo.Gateway = resultImpl.IP4.Gateway

if forceBridgeGateway == true {
if forceBridgeGateway {
endpointInfo.Gateway = resultImpl.IP4.IP.IP.Mask(resultImpl.IP4.IP.Mask)
endpointInfo.Gateway[3] = 2
}
Expand All @@ -356,7 +354,7 @@ func allocateIpam(
endpointInfo.IP4Mask = resultImpl.IP4.IP.Mask
endpointInfo.Gateway = resultImpl.IP4.Gateway

if forceBridgeGateway == true {
if forceBridgeGateway {
endpointInfo.Gateway = resultImpl.IP4.IP.IP.Mask(resultImpl.IP4.IP.Mask)
endpointInfo.Gateway[3] = 2
}
Expand Down Expand Up @@ -385,7 +383,7 @@ func allocateIpam(
// deallocateIpam performs the cleanup necessary for removing an ipam
func deallocateIpam(cniConfig *cni.NetworkConfig, networkConfByteStream []byte) error {

if cniConfig.OptionalFlags.EnableDualStack == false {
if !cniConfig.OptionalFlags.EnableDualStack {
logrus.Infof("[cni-net] Delete from ipam when dual stack is disabled")
return invoke.DelegateDel(context.TODO(), cniConfig.Ipam.Type, cniConfig.Serialize(), nil)
} else {
Expand Down
2 changes: 1 addition & 1 deletion common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func LogNetworkInterfaces() {

func GetAddressAsCidr(ip string, prefix uint8) string {

return ip + string('/') + strconv.FormatUint(uint64(prefix), 10)
return ip + string('/') + strconv.FormatUint(uint64(prefix), 10)

}
58 changes: 28 additions & 30 deletions network/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package network

import (
"encoding/json"
"net"
"github.com/Microsoft/windows-container-networking/common"
"net"

"github.com/Microsoft/hcsshim/hcn"
)
Expand Down Expand Up @@ -43,7 +43,7 @@ func (endpoint *EndpointInfo) GetHostComputeEndpoint() *hcn.HostComputeEndpoint
// Check for nil on address objects.
ipAddr := ""
ipConfig := []hcn.IpConfig{}
routes := []hcn.Route{}
routes := []hcn.Route{}

if endpoint.IPAddress != nil {
ipAddr = endpoint.IPAddress.String()
Expand Down Expand Up @@ -93,8 +93,8 @@ func (endpoint *EndpointInfo) GetHostComputeEndpoint() *hcn.HostComputeEndpoint
ServerList: endpoint.DNS.Nameservers,
Options: endpoint.DNS.Options,
},
MacAddress: macAddr,
Routes: routes,
MacAddress: macAddr,
Routes: routes,
IpConfigurations: ipConfig,
SchemaVersion: hcn.SchemaVersion{
Major: 2,
Expand All @@ -109,36 +109,35 @@ func GetEndpointInfoFromHostComputeEndpoint(hcnEndpoint *hcn.HostComputeEndpoint
// Ignore empty MAC, GW, and IP.
macAddr, _ := net.ParseMAC(hcnEndpoint.MacAddress)
var gwAddr net.IP
var gwAddr6 net.IP
var gwAddr6 net.IP
var ipAddr4 net.IPNet
var ipAddr6 net.IPNet

if withIpv6 == false {
if !withIpv6 {

if len(hcnEndpoint.Routes) > 0 {
gwAddr = net.ParseIP(hcnEndpoint.Routes[0].NextHop)
}
if len(hcnEndpoint.IpConfigurations) > 0 {
ipAddr4.IP = net.ParseIP(hcnEndpoint.IpConfigurations[0].IpAddress)
}
} else {

if len(hcnEndpoint.IpConfigurations) > 0 {
ipAddr4.IP = net.ParseIP(hcnEndpoint.IpConfigurations[0].IpAddress)
}
} else {
var ip4found bool
var ip6found bool

for _, addr := range hcnEndpoint.IpConfigurations {
if net.ParseIP(addr.IpAddress).To4() == nil &&
ip6found == false {
if net.ParseIP(addr.IpAddress).To4() == nil && !ip6found {
ip, mask, _ := net.ParseCIDR(common.GetAddressAsCidr(addr.IpAddress, addr.PrefixLength))
ipAddr6.IP = ip
ipAddr6.Mask = mask.Mask
ip6found = true
} else {
if ip4found == false {
ip, mask, _ := net.ParseCIDR(common.GetAddressAsCidr(addr.IpAddress, addr.PrefixLength))
ipAddr4.IP = ip
ipAddr4.Mask = mask.Mask
ip4found = true
if !ip4found {
ip, mask, _ := net.ParseCIDR(common.GetAddressAsCidr(addr.IpAddress, addr.PrefixLength))
ipAddr4.IP = ip
ipAddr4.Mask = mask.Mask
ip4found = true
}
}

Expand All @@ -152,14 +151,13 @@ func GetEndpointInfoFromHostComputeEndpoint(hcnEndpoint *hcn.HostComputeEndpoint

for _, r := range hcnEndpoint.Routes {

if net.ParseIP(r.NextHop).To4() == nil &&
ip6found == false {
if net.ParseIP(r.NextHop).To4() == nil && !ip6found {
gwAddr6 = net.ParseIP(r.NextHop)
ip6found = true
} else {
if ip4found == false {
gwAddr = net.ParseIP(r.NextHop)
ip4found = true
if !ip4found {
gwAddr = net.ParseIP(r.NextHop)
ip4found = true
}
}

Expand All @@ -181,13 +179,13 @@ func GetEndpointInfoFromHostComputeEndpoint(hcnEndpoint *hcn.HostComputeEndpoint
Nameservers: hcnEndpoint.Dns.ServerList,
Options: hcnEndpoint.Dns.Options,
},
MacAddress: macAddr,
Gateway: gwAddr,
IPAddress: ipAddr4.IP,
IP4Mask: ipAddr4.Mask,
Gateway6: gwAddr6,
IPAddress6: ipAddr6,
Policies: GetEndpointPoliciesFromHostComputePolicies(hcnEndpoint.Policies),
MacAddress: macAddr,
Gateway: gwAddr,
IPAddress: ipAddr4.IP,
IP4Mask: ipAddr4.Mask,
Gateway6: gwAddr6,
IPAddress6: ipAddr6,
Policies: GetEndpointPoliciesFromHostComputePolicies(hcnEndpoint.Policies),
}

}
Expand Down
5 changes: 0 additions & 5 deletions network/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ func GetPortEnumValue(protocol string) (uint32, error) {
switch strings.ToLower(protocol) {
case "tcp":
protocolInt = 6
break
case "udp":
protocolInt = 17
break
case "icmpv4":
protocolInt = 1
break
case "icmpv6":
protocolInt = 58
break
case "igmp":
protocolInt = 2
break
default:
return 0, errors.New("invalid protocol supplied to port mapping policy")
}
Expand Down
8 changes: 0 additions & 8 deletions plugins/nat/nat_windows.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package main

import (
// "github.com/Microsoft/windows-container-networking/cni"
"github.com/Microsoft/windows-container-networking/common/core"
)

/*
// NetPlugin represents the CNI network plugin.
type netPlugin struct {
*cni.Plugin
nm network.Manager
}
*/
func main() {
core.Core()
}
8 changes: 0 additions & 8 deletions plugins/sdnbridge/sdnbridge_windows.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package main

import (
// "github.com/Microsoft/windows-container-networking/cni"
"github.com/Microsoft/windows-container-networking/common/core"
)

/*
// NetPlugin represents the CNI network plugin.
type netPlugin struct {
*cni.Plugin
nm network.Manager
}
*/
func main() {
core.Core()
}
8 changes: 0 additions & 8 deletions plugins/sdnoverlay/sdnoverlay_windows.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package main

import (
// "github.com/Microsoft/windows-container-networking/cni"
"github.com/Microsoft/windows-container-networking/common/core"
)

/*
// NetPlugin represents the CNI network plugin.
type netPlugin struct {
*cni.Plugin
nm network.Manager
}
*/
func main() {
core.Core()
}
Loading

0 comments on commit 3a5374c

Please sign in to comment.