Skip to content

Commit afb40f4

Browse files
fix lint errors
1 parent 9ae3fff commit afb40f4

File tree

6 files changed

+82
-77
lines changed

6 files changed

+82
-77
lines changed

cns/NetworkContainerContract.go

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

10-
"github.com/Azure/azure-container-networking/cns/types"
11-
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
12-
"github.com/Azure/azure-container-networking/network/policy"
1310
"github.com/google/uuid"
1411
"github.com/pkg/errors"
1512
corev1 "k8s.io/api/core/v1"
13+
14+
"github.com/Azure/azure-container-networking/cns/types"
15+
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
16+
"github.com/Azure/azure-container-networking/network/policy"
1617
)
1718

1819
// Container Network Service DNC Contract
@@ -128,7 +129,7 @@ type CreateNetworkContainerRequest struct {
128129
AllowNCToHostCommunication bool
129130
EndpointPolicies []NetworkContainerRequestPolicies
130131
NCStatus v1alpha.NCStatus
131-
SwiftV2PrefixOnNic bool // Indicates if is swiftv2 nc, PrefixOnNic scenario (isSwiftV2 && nc.Type == VNETBlock)
132+
SwiftV2PrefixOnNic bool // Indicates if is swiftv2 nc, PrefixOnNic scenario (isSwiftV2 && nc.Type == VNETBlock)
132133
NetworkInterfaceInfo NetworkInterfaceInfo //nolint // introducing new field for backendnic, to be used later by cni code
133134
}
134135

cns/kubecontroller/nodenetworkconfig/conversion.go

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

9+
"github.com/pkg/errors"
10+
911
"github.com/Azure/azure-container-networking/cns"
1012
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
11-
"github.com/pkg/errors"
1213
)
1314

1415
var (
@@ -62,7 +63,7 @@ func CreateNCRequestFromDynamicNC(nc v1alpha.NetworkContainer) (*cns.CreateNetwo
6263
NetworkContainerid: nc.ID,
6364
NetworkContainerType: cns.Docker,
6465
Version: strconv.FormatInt(nc.Version, 10), //nolint:gomnd // it's decimal
65-
SwiftV2PrefixOnNic: false, // Dynamic NCs don't use SwiftV2 PrefixOnNic
66+
SwiftV2PrefixOnNic: false, // Dynamic NCs don't use SwiftV2 PrefixOnNic
6667
IPConfiguration: cns.IPConfiguration{
6768
IPSubnet: subnet,
6869
GatewayIPAddress: nc.DefaultGateway,

cns/restserver/internalapi.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ import (
1212
"net/http"
1313
"net/http/httptest"
1414
"reflect"
15+
"runtime"
1516
"strconv"
1617
"strings"
1718
"time"
18-
"runtime"
19+
20+
"github.com/pkg/errors"
1921

2022
"github.com/Azure/azure-container-networking/cns"
2123
"github.com/Azure/azure-container-networking/cns/logger"
2224
"github.com/Azure/azure-container-networking/cns/nodesubnet"
2325
"github.com/Azure/azure-container-networking/cns/types"
2426
"github.com/Azure/azure-container-networking/common"
2527
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
26-
"github.com/pkg/errors"
2728
)
2829

2930
const (
@@ -731,13 +732,12 @@ func (service *HTTPRestService) getIMDSNCs(ctx context.Context) (map[string]stri
731732

732733
// Check whether NC is SwiftV2 NIC associated NC and prefix on nic is enabled
733734
func (service *HTTPRestService) isPrefixonNicSwiftV2() bool {
734-
for _, containerStatus := range service.state.ContainerStatus {
735-
req := containerStatus.CreateNetworkContainerRequest
735+
for i := range service.state.ContainerStatus {
736+
req := service.state.ContainerStatus[i].CreateNetworkContainerRequest
736737

737-
//
738738
if req.SwiftV2PrefixOnNic {
739739
return true
740740
}
741741
}
742742
return false
743-
}
743+
}

cns/restserver/internalapi_linux.go

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

9+
goiptables "github.com/coreos/go-iptables/iptables"
10+
"github.com/pkg/errors"
11+
912
"github.com/Azure/azure-container-networking/cns"
1013
"github.com/Azure/azure-container-networking/cns/logger"
1114
"github.com/Azure/azure-container-networking/cns/types"
1215
"github.com/Azure/azure-container-networking/iptables"
1316
"github.com/Azure/azure-container-networking/network/networkutils"
14-
goiptables "github.com/coreos/go-iptables/iptables"
15-
"github.com/pkg/errors"
1617
)
1718

1819
const SWIFTPOSTROUTING = "SWIFT-POSTROUTING"
@@ -183,10 +184,9 @@ func (service *HTTPRestService) setVFForAccelnetNICs() error {
183184
}
184185

185186
func (service *HTTPRestService) setPrefixOnNICRegistry(enabled bool, infraNicMacAddress string) error {
186-
// Assigning parameters to '_' to avoid unused parameter linting errors.
187-
// These parameters are only used in the Windows implementation.
188-
_ = enabled
189-
_ = infraNicMacAddress
190-
logger.Printf("[setPrefixOnNicEnabled winDebug] No-op on Linux platform")
187+
// Assigning parameters to '_' to avoid unused parameter linting errors.
188+
// These parameters are only used in the Windows implementation.
189+
_ = enabled
190+
_ = infraNicMacAddress
191191
return nil
192-
}
192+
}

cns/restserver/internalapi_test.go

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ import (
1010
"net"
1111
"os"
1212
"reflect"
13+
"runtime"
1314
"strconv"
1415
"strings"
1516
"sync"
1617
"testing"
1718
"time"
18-
"runtime"
19+
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"
1925

2026
"github.com/Azure/azure-container-networking/cns"
2127
"github.com/Azure/azure-container-networking/cns/common"
@@ -26,11 +32,6 @@ import (
2632
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
2733
nma "github.com/Azure/azure-container-networking/nmagent"
2834
"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"
3435
)
3536

3637
const (
@@ -1684,19 +1685,19 @@ func setupIMDSMockAPIsWithCustomIDs(svc *HTTPRestService, interfaceIDs []string)
16841685

16851686
// TestSyncHostNCVersionWithWindowsSwiftV2 tests SyncHostNCVersion and verifies it calls Windows SwiftV2 PrefixOnNic scenario
16861687
func TestSyncHostNCVersionWithWindowsSwiftV2(t *testing.T) {
1687-
svc := getTestService(cns.Kubernetes)
1688-
1688+
testSvc := getTestService(cns.Kubernetes)
1689+
16891690
// Set up test NCs with different scenarios
16901691
regularNCID := "regular-nc-id"
16911692
swiftV2NCID := "swift-v2-vnet-block-nc"
1692-
1693+
16931694
// Initialize ContainerStatus map if nil
1694-
if svc.state.ContainerStatus == nil {
1695-
svc.state.ContainerStatus = make(map[string]containerstatus)
1695+
if testSvc.state.ContainerStatus == nil {
1696+
testSvc.state.ContainerStatus = make(map[string]containerstatus)
16961697
}
1697-
1698+
16981699
// Add a regular NC
1699-
svc.state.ContainerStatus[regularNCID] = containerstatus{
1700+
testSvc.state.ContainerStatus[regularNCID] = containerstatus{
17001701
ID: regularNCID,
17011702
CreateNetworkContainerRequest: cns.CreateNetworkContainerRequest{
17021703
NetworkContainerid: regularNCID,
@@ -1706,49 +1707,49 @@ func TestSyncHostNCVersionWithWindowsSwiftV2(t *testing.T) {
17061707
},
17071708
HostVersion: "1",
17081709
}
1709-
1710+
17101711
// Add a SwiftV2 VNETBlock NC that should trigger Windows registry operations
1711-
svc.state.ContainerStatus[swiftV2NCID] = containerstatus{
1712+
testSvc.state.ContainerStatus[swiftV2NCID] = containerstatus{
17121713
ID: swiftV2NCID,
17131714
CreateNetworkContainerRequest: cns.CreateNetworkContainerRequest{
17141715
NetworkContainerid: swiftV2NCID,
1715-
SwiftV2PrefixOnNic: true,
1716+
SwiftV2PrefixOnNic: true,
17161717
NetworkContainerType: cns.Docker,
17171718
Version: "2",
17181719
},
17191720
HostVersion: "1",
17201721
}
1721-
1722+
17221723
// Set up mock NMAgent with NC versions
17231724
mockNMA := &fakes.NMAgentClientFake{}
1724-
mockNMA.GetNCVersionListF = func(ctx context.Context) (nma.NCVersionList, error) {
1725+
mockNMA.GetNCVersionListF = func(_ context.Context) (nma.NCVersionList, error) {
17251726
return nma.NCVersionList{
17261727
Containers: []nma.NCVersion{
17271728
{
17281729
NetworkContainerID: regularNCID,
1729-
Version: "2",
1730+
Version: "2",
17301731
},
17311732
{
17321733
NetworkContainerID: swiftV2NCID,
1733-
Version: "2",
1734+
Version: "2",
17341735
},
17351736
},
17361737
}, nil
17371738
}
1738-
svc.nma = mockNMA
1739-
1739+
testSvc.nma = mockNMA
1740+
17401741
// Set up mock IMDS client for Windows SwiftV2 scenario
17411742
mac1, _ := net.ParseMAC("AA:BB:CC:DD:EE:FF")
17421743
mac2, _ := net.ParseMAC("11:22:33:44:55:66")
1743-
1744+
17441745
interfaceMap := map[string]imds.NetworkInterface{
17451746
"interface1": {
17461747
InterfaceCompartmentID: "", // Empty for Windows condition
1747-
MacAddress: imds.HardwareAddr(mac1),
1748+
MacAddress: imds.HardwareAddr(mac1),
17481749
},
17491750
"interface2": {
17501751
InterfaceCompartmentID: "nc-with-compartment-id",
1751-
MacAddress: imds.HardwareAddr(mac2),
1752+
MacAddress: imds.HardwareAddr(mac2),
17521753
},
17531754
}
17541755
mockIMDS := &mockIMDSAdapter{
@@ -1770,46 +1771,44 @@ func TestSyncHostNCVersionWithWindowsSwiftV2(t *testing.T) {
17701771
},
17711772
},
17721773
}
1773-
1774+
17741775
// Replace the IMDS client
1775-
originalIMDS := svc.imdsClient
1776-
svc.imdsClient = mockIMDS
1777-
defer func() { svc.imdsClient = originalIMDS }()
1778-
1776+
originalIMDS := testSvc.imdsClient
1777+
testSvc.imdsClient = mockIMDS
1778+
defer func() { testSvc.imdsClient = originalIMDS }()
1779+
17791780
// Verify preconditions
1780-
assert.True(t, svc.isPrefixonNicSwiftV2(), "isPrefixonNicSwiftV2() should return true")
1781+
assert.True(t, testSvc.isPrefixonNicSwiftV2(), "isPrefixonNicSwiftV2() should return true")
17811782

17821783
ctx := context.Background()
1783-
svc.SyncHostNCVersion(ctx, cns.CRD)
1784-
1784+
testSvc.SyncHostNCVersion(ctx, cns.CRD)
1785+
17851786
// Verify that NC versions were updated
1786-
updatedRegularNC := svc.state.ContainerStatus[regularNCID]
1787-
updatedSwiftV2NC := svc.state.ContainerStatus[swiftV2NCID]
1788-
1787+
updatedRegularNC := testSvc.state.ContainerStatus[regularNCID]
1788+
updatedSwiftV2NC := testSvc.state.ContainerStatus[swiftV2NCID]
1789+
17891790
assert.Equal(t, "2", updatedRegularNC.HostVersion, "Regular NC host version should be updated to 2")
17901791
assert.Equal(t, "2", updatedSwiftV2NC.HostVersion, "SwiftV2 NC host version should be updated to 2")
1791-
1792-
imdsNCs, err := svc.getIMDSNCs(ctx)
1792+
1793+
imdsNCs, err := testSvc.getIMDSNCs(ctx)
17931794
assert.NoError(t, err, "getIMDSNCs should not return error")
1794-
1795+
17951796
// Verify IMDS results
17961797
assert.Contains(t, imdsNCs, "nc-with-compartment-id", "NC with compartment ID should be in results")
17971798
assert.Equal(t, PrefixOnNicNCVersion, imdsNCs["nc-with-compartment-id"], "NC should have expected version")
1798-
1799+
17991800
// Log the conditions that would trigger Windows registry operations
18001801
isWindows := runtime.GOOS == "windows"
1801-
hasSwiftV2PrefixOnNic := svc.isPrefixonNicSwiftV2()
1802-
1803-
t.Logf("Windows SwiftV2 PrefixOnNic conditions: (runtime.GOOS == 'windows' && service.isPrefixonNicSwiftV2()): %t",
1802+
hasSwiftV2PrefixOnNic := testSvc.isPrefixonNicSwiftV2()
1803+
1804+
t.Logf("Windows SwiftV2 PrefixOnNic conditions: (runtime.GOOS == 'windows' && service.isPrefixonNicSwiftV2()): %t",
18041805
isWindows && hasSwiftV2PrefixOnNic)
1805-
1806-
1806+
18071807
// Test with no SwiftV2 NCs
1808-
delete(svc.state.ContainerStatus, swiftV2NCID)
1809-
assert.False(t, svc.isPrefixonNicSwiftV2(), "isPrefixonNicSwiftV2() should return false without SwiftV2 NCs")
1810-
1808+
delete(testSvc.state.ContainerStatus, swiftV2NCID)
1809+
assert.False(t, testSvc.isPrefixonNicSwiftV2(), "isPrefixonNicSwiftV2() should return false without SwiftV2 NCs")
1810+
18111811
// Call getIMDSNCs again to verify condition is not triggered
1812-
_, err2 := svc.getIMDSNCs(ctx)
1812+
_, err2 := testSvc.getIMDSNCs(ctx)
18131813
assert.NoError(t, err2, "getIMDSNCs should not return error")
18141814
}
1815-

cns/restserver/internalapi_windows.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/Azure/azure-container-networking/cns"
9-
"github.com/Azure/azure-container-networking/cns/logger"
10-
"github.com/Azure/azure-container-networking/cns/types"
118
"github.com/Microsoft/hcsshim"
129
"github.com/pkg/errors"
1310
"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"
1415
)
1516

1617
const (
1718
// timeout for powershell command to return the interfaces list
18-
pwshTimeout = 120 * time.Second
19+
pwshTimeout = 120 * time.Second
1920
hnsRegistryPath = `SYSTEM\CurrentControlSet\Services\HNS\wcna_state\config`
2021
prefixOnNicRegistryPath = `SYSTEM\CurrentControlSet\Services\HNS\wcna_state\config\PrefixOnNic`
2122
infraNicIfName = "eth0"
@@ -137,15 +138,18 @@ func (service *HTTPRestService) setRegistryValue(registryPath, keyName string, v
137138
case uint32:
138139
err = key.SetDWordValue(keyName, v)
139140
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)
144+
}
140145
err = key.SetDWordValue(keyName, uint32(v))
141146
default:
142147
return fmt.Errorf("unsupported value type for registry key %s: %T", keyName, value)
143148
}
144-
145149
if err != nil {
146150
return fmt.Errorf("failed to set registry value '%s': %w", keyName, err)
147151
}
148152

149-
logger.Printf("[setRegistryValue] Set %s\\%s = %v", registryPath, keyName, value)
153+
logger.Printf("[setRegistryValue] Set %s\\%s = %v", registryPath, keyName, value)
150154
return nil
151-
}
155+
}

0 commit comments

Comments
 (0)