Skip to content

Commit 5e76f7b

Browse files
update linting error
1 parent 8e673c3 commit 5e76f7b

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

cns/NetworkContainerContract.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/google/uuid"
11-
"github.com/pkg/errors"
12-
corev1 "k8s.io/api/core/v1"
13-
1410
"github.com/Azure/azure-container-networking/cns/types"
1511
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
1612
"github.com/Azure/azure-container-networking/network/policy"
13+
"github.com/google/uuid"
14+
"github.com/pkg/errors"
15+
corev1 "k8s.io/api/core/v1"
1716
)
1817

1918
// Container Network Service DNC Contract

cns/kubecontroller/nodenetworkconfig/conversion.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66
"strconv"
77
"strings"
88

9-
"github.com/pkg/errors"
10-
119
"github.com/Azure/azure-container-networking/cns"
1210
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
11+
"github.com/pkg/errors"
1312
)
1413

1514
var (
@@ -63,7 +62,7 @@ func CreateNCRequestFromDynamicNC(nc v1alpha.NetworkContainer) (*cns.CreateNetwo
6362
NetworkContainerid: nc.ID,
6463
NetworkContainerType: cns.Docker,
6564
Version: strconv.FormatInt(nc.Version, 10), //nolint:gomnd // it's decimal
66-
SwiftV2PrefixOnNic: false, // Dynamic NCs don't use SwiftV2 PrefixOnNic
65+
SwiftV2PrefixOnNic: false, // Dynamic NCs don't use SwiftV2 PrefixOnNic and should always be false
6766
IPConfiguration: cns.IPConfiguration{
6867
IPSubnet: subnet,
6968
GatewayIPAddress: nc.DefaultGateway,

cns/kubecontroller/nodenetworkconfig/conversion_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
5959
GatewayIPAddress: nc.DefaultGateway,
6060
GatewayIPv6Address: nc.DefaultGatewayV6,
6161
},
62-
NCStatus: nc.Status,
62+
NCStatus: nc.Status,
6363
SwiftV2PrefixOnNic: isSwiftV2 && nc.Type == v1alpha.VNETBlock,
6464
NetworkInterfaceInfo: cns.NetworkInterfaceInfo{
6565
MACAddress: nc.MacAddress,

cns/restserver/internalapi.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ import (
1818
"strings"
1919
"time"
2020

21-
"github.com/pkg/errors"
22-
2321
"github.com/Azure/azure-container-networking/cns"
2422
"github.com/Azure/azure-container-networking/cns/logger"
2523
"github.com/Azure/azure-container-networking/cns/nodesubnet"
2624
"github.com/Azure/azure-container-networking/cns/types"
2725
"github.com/Azure/azure-container-networking/common"
2826
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
27+
"github.com/pkg/errors"
2928
)
3029

3130
const (
@@ -231,12 +230,8 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
231230
return len(programmedNCs), errors.Wrap(err, "failed to get nc version list from nmagent")
232231
}
233232

234-
// Get IMDS NC versions for delegated NIC scenarios. If any of the NMA API check calls, imds calls fails assume that nma build doesn't have the latest changes and create empty map
233+
// Get IMDS NC versions for delegated NIC scenarios.
235234
imdsNCVersions := service.getIMDSNCs(ctx)
236-
if err != nil {
237-
// If any of the NMA API check calls, imds calls fails assume that nma build doesn't have the latest changes and create empty map
238-
imdsNCVersions = make(map[string]string)
239-
}
240235

241236
nmaNCs := map[string]string{}
242237
for _, nc := range ncVersionListResp.Containers {
@@ -730,7 +725,7 @@ func (service *HTTPRestService) getIMDSNCs(ctx context.Context) map[string]strin
730725
err := service.setPrefixOnNICRegistry(true, iface.MacAddress.String())
731726
if err != nil {
732727
//nolint:staticcheck // SA1019: suppress deprecated logger.Debugf usage. Todo: legacy logger usage is consistent in cns repo. Migrates when all logger usage is migrated
733-
logger.Debugf("failed to add PrefixOnNic keys to Windows registry: %w", err)
728+
logger.Debugf("failed to add PrefixOnNic keys to Windows registry: %v", err)
734729
}
735730
}
736731
}

cns/restserver/internalapi_linux.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import (
66
"os/exec"
77
"strconv"
88

9-
goiptables "github.com/coreos/go-iptables/iptables"
10-
"github.com/pkg/errors"
11-
129
"github.com/Azure/azure-container-networking/cns"
1310
"github.com/Azure/azure-container-networking/cns/logger"
1411
"github.com/Azure/azure-container-networking/cns/types"
1512
"github.com/Azure/azure-container-networking/iptables"
1613
"github.com/Azure/azure-container-networking/network/networkutils"
14+
goiptables "github.com/coreos/go-iptables/iptables"
15+
"github.com/pkg/errors"
1716
)
1817

1918
const SWIFTPOSTROUTING = "SWIFT-POSTROUTING"

cns/restserver/internalapi_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ import (
1717
"testing"
1818
"time"
1919

20-
"github.com/google/uuid"
21-
"github.com/pkg/errors"
22-
"github.com/stretchr/testify/assert"
23-
"github.com/stretchr/testify/require"
24-
"golang.org/x/exp/maps"
25-
2620
"github.com/Azure/azure-container-networking/cns"
2721
"github.com/Azure/azure-container-networking/cns/common"
2822
"github.com/Azure/azure-container-networking/cns/configuration"
@@ -32,6 +26,11 @@ import (
3226
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
3327
nma "github.com/Azure/azure-container-networking/nmagent"
3428
"github.com/Azure/azure-container-networking/store"
29+
"github.com/google/uuid"
30+
"github.com/pkg/errors"
31+
"github.com/stretchr/testify/assert"
32+
"github.com/stretchr/testify/require"
33+
"golang.org/x/exp/maps"
3534
)
3635

3736
const (

cns/restserver/internalapi_windows.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package restserver
33
import (
44
"context"
55
"fmt"
6+
"math"
67
"time"
78

9+
"github.com/Azure/azure-container-networking/cns"
10+
"github.com/Azure/azure-container-networking/cns/types"
811
"github.com/Microsoft/hcsshim"
912
"github.com/pkg/errors"
1013
"golang.org/x/sys/windows/registry"
11-
12-
"github.com/Azure/azure-container-networking/cns"
13-
"github.com/Azure/azure-container-networking/cns/logger"
14-
"github.com/Azure/azure-container-networking/cns/types"
1514
)
1615

1716
const (
@@ -20,10 +19,13 @@ const (
2019
hnsRegistryPath = `SYSTEM\CurrentControlSet\Services\HNS\wcna_state\config`
2120
prefixOnNicRegistryPath = `SYSTEM\CurrentControlSet\Services\HNS\wcna_state\config\PrefixOnNic`
2221
infraNicIfName = "eth0"
23-
enableSNAT = false
2422
)
2523

26-
var errUnsupportedAPI = errors.New("unsupported api")
24+
var (
25+
errUnsupportedAPI = errors.New("unsupported api")
26+
errIntOverflow = errors.New("int value overflows uint32")
27+
errUnsupportedValueType = errors.New("unsupported value type for registry key")
28+
)
2729

2830
type IPtablesProvider struct{}
2931

@@ -83,8 +85,8 @@ func (service *HTTPRestService) getPrimaryNICMACAddress() (string, error) {
8385
return macAddress, nil
8486
}
8587

86-
func (service *HTTPRestService) enablePrefixOnNic(enabled bool) error {
87-
return service.setRegistryValue(prefixOnNicRegistryPath, "enabled", enabled)
88+
func (service *HTTPRestService) enablePrefixOnNic(isEnabled bool) error {
89+
return service.setRegistryValue(prefixOnNicRegistryPath, "enabled", isEnabled)
8890
}
8991

9092
func (service *HTTPRestService) setInfraNicMacAddress(macAddress string) error {
@@ -95,8 +97,8 @@ func (service *HTTPRestService) setInfraNicIfName(ifName string) error {
9597
return service.setRegistryValue(prefixOnNicRegistryPath, "infra_nic_ifname", ifName)
9698
}
9799

98-
func (service *HTTPRestService) setEnableSNAT(enabled bool) error {
99-
return service.setRegistryValue(hnsRegistryPath, "EnableSNAT", enabled)
100+
func (service *HTTPRestService) setEnableSNAT(isEnabled bool) error {
101+
return service.setRegistryValue(hnsRegistryPath, "EnableSNAT", isEnabled)
100102
}
101103

102104
func (service *HTTPRestService) setPrefixOnNICRegistry(enablePrefixOnNic bool, infraNicMacAddress string) error {
@@ -112,7 +114,7 @@ func (service *HTTPRestService) setPrefixOnNICRegistry(enablePrefixOnNic bool, i
112114
return fmt.Errorf("failed to set InfraNicIfName key to windows registry: %w", err)
113115
}
114116

115-
if err := service.setEnableSNAT(enableSNAT); err != nil {
117+
if err := service.setEnableSNAT(!enablePrefixOnNic); err != nil { // for prefix on nic, snat should be disabled
116118
return fmt.Errorf("failed to set EnableSNAT key to windows registry: %w", err)
117119
}
118120

@@ -138,18 +140,16 @@ func (service *HTTPRestService) setRegistryValue(registryPath, keyName string, v
138140
case uint32:
139141
err = key.SetDWordValue(keyName, v)
140142
case int:
141-
case int:
142-
if v < 0 || v > int(^uint32(0)) {
143-
return fmt.Errorf("int value %d overflows uint32 for registry key %s", v, keyName)
143+
if v < 0 || v > math.MaxUint32 {
144+
return fmt.Errorf("%w: %d for registry key %s", errIntOverflow, v, keyName)
144145
}
145146
err = key.SetDWordValue(keyName, uint32(v))
146147
default:
147-
return fmt.Errorf("unsupported value type for registry key %s: %T", keyName, value)
148+
return fmt.Errorf("%w %s: %T", errUnsupportedValueType, keyName, value)
148149
}
149150
if err != nil {
150151
return fmt.Errorf("failed to set registry value '%s': %w", keyName, err)
151152
}
152-
153-
logger.Printf("[setRegistryValue] Set %s\\%s = %v", registryPath, keyName, value)
153+
fmt.Printf("[setRegistryValue] Set %s\\%s = %v\n", registryPath, keyName, value)
154154
return nil
155155
}

0 commit comments

Comments
 (0)