Skip to content

Commit fb25300

Browse files
chore: first set of files for nodesubnet nc
1 parent f7f98d4 commit fb25300

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

cns/NetworkContainerContract.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const (
5555
JobObject = "JobObject"
5656
COW = "COW" // Container on Windows
5757
BackendNICNC = "BackendNICNC"
58+
NodeSubnet = "NodeSubnet" // Artificial Container Type for NodeSubnet
5859
)
5960

6061
// Orchestrator Types
@@ -66,6 +67,7 @@ const (
6667
AzureFirstParty = "AzureFirstParty"
6768
KubernetesCRD = "KubernetesCRD"
6869
// TODO: Add OrchastratorType as CRD: https://msazure.visualstudio.com/One/_workitems/edit/7711872
70+
KubernetesNodeSubnet = "KubernetesNodeSubnet"
6971
)
7072

7173
// Encap Types
@@ -99,10 +101,13 @@ const (
99101
Managed = "Managed"
100102
CRD = "CRD"
101103
MultiTenantCRD = "MultiTenantCRD"
104+
AzureHost = "AzureHost"
102105
)
103106

104-
var ErrInvalidNCID = errors.New("invalid NetworkContainerID")
105-
var ErrInvalidIP = errors.New("invalid IP")
107+
var (
108+
ErrInvalidNCID = errors.New("invalid NetworkContainerID")
109+
ErrInvalidIP = errors.New("invalid IP")
110+
)
106111

107112
// CreateNetworkContainerRequest specifies request to create a network container or network isolation boundary.
108113
type CreateNetworkContainerRequest struct {

cns/nodesubnet/nodesubnet_nc.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package nodesubnet
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/Azure/azure-container-networking/cns"
7+
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
8+
)
9+
10+
const (
11+
// ID for fake NC that we create to store NodeSubnet IPS
12+
NodeSubnetNCID = "55022629-3854-499b-7133-5e6887959f4ea" // md5sum of "NodeSubnetNC_IPv4"
13+
NodeSubnetNCVersion = 0
14+
NodeSubnetHostVersion = "0"
15+
NodeSubnetNCStatus = v1alpha.NCUpdateSuccess
16+
NodeSubnetHostPrimaryIP = ""
17+
)
18+
19+
// CreateNodeSubnetNCRequest generates a CreateNetworkContainerRequest that simply stores the static secondary IPs.
20+
func CreateNodeSubnetNCRequest(secondaryIPs []string) *cns.CreateNetworkContainerRequest {
21+
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{}
22+
23+
for _, secondaryIP := range secondaryIPs {
24+
// iterate through all secondary IP addresses add them to the request as secondary IPConfigs.
25+
secondaryIPConfigs[secondaryIP] = cns.SecondaryIPConfig{
26+
IPAddress: secondaryIP,
27+
NCVersion: NodeSubnetNCVersion,
28+
}
29+
}
30+
31+
return &cns.CreateNetworkContainerRequest{
32+
HostPrimaryIP: NodeSubnetHostPrimaryIP,
33+
SecondaryIPConfigs: secondaryIPConfigs,
34+
NetworkContainerid: NodeSubnetNCID,
35+
NetworkContainerType: cns.NodeSubnet,
36+
Version: strconv.FormatInt(NodeSubnetNCVersion, 10), //nolint:gomnd // it's decimal
37+
IPConfiguration: cns.IPConfiguration{},
38+
NCStatus: NodeSubnetNCStatus,
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package nodesubnet_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Azure/azure-container-networking/cns"
7+
"github.com/Azure/azure-container-networking/cns/nodesubnet"
8+
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
9+
"github.com/google/go-cmp/cmp"
10+
)
11+
12+
func TestCreateNodeSubnetNCRequest_EmptySecondaryIPs(t *testing.T) {
13+
secondaryIPs := []string{}
14+
expectedRequest := &cns.CreateNetworkContainerRequest{
15+
HostPrimaryIP: nodesubnet.NodeSubnetHostPrimaryIP,
16+
SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{},
17+
NetworkContainerid: nodesubnet.NodeSubnetNCID,
18+
NetworkContainerType: cns.NodeSubnet,
19+
Version: "0",
20+
IPConfiguration: cns.IPConfiguration{},
21+
NCStatus: v1alpha.NCUpdateSuccess,
22+
}
23+
24+
request := nodesubnet.CreateNodeSubnetNCRequest(secondaryIPs)
25+
if !cmp.Equal(request, expectedRequest) {
26+
t.Errorf("Unexepected diff in NodeSubnetNCRequest: %v", cmp.Diff(request, expectedRequest))
27+
}
28+
}
29+
30+
func TestCreateNodeSubnetNCRequest_NonEmptySecondaryIPs(t *testing.T) {
31+
secondaryIPs := []string{"10.0.0.1", "10.0.0.2"}
32+
expectedRequest := &cns.CreateNetworkContainerRequest{
33+
HostPrimaryIP: nodesubnet.NodeSubnetHostPrimaryIP,
34+
SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{
35+
"10.0.0.1": {IPAddress: "10.0.0.1", NCVersion: nodesubnet.NodeSubnetNCVersion},
36+
"10.0.0.2": {IPAddress: "10.0.0.2", NCVersion: nodesubnet.NodeSubnetNCVersion},
37+
},
38+
NetworkContainerid: nodesubnet.NodeSubnetNCID,
39+
NetworkContainerType: cns.NodeSubnet,
40+
Version: "0",
41+
IPConfiguration: cns.IPConfiguration{},
42+
NCStatus: v1alpha.NCUpdateSuccess,
43+
}
44+
45+
request := nodesubnet.CreateNodeSubnetNCRequest(secondaryIPs)
46+
if !cmp.Equal(request, expectedRequest) {
47+
t.Errorf("Unexepected diff in NodeSubnetNCRequest: %v", cmp.Diff(request, expectedRequest))
48+
}
49+
}

cns/restserver/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/Azure/azure-container-networking/cns/dockerclient"
1515
"github.com/Azure/azure-container-networking/cns/logger"
1616
"github.com/Azure/azure-container-networking/cns/networkcontainers"
17+
"github.com/Azure/azure-container-networking/cns/nodesubnet"
1718
"github.com/Azure/azure-container-networking/cns/types"
1819
"github.com/Azure/azure-container-networking/cns/wireserver"
1920
acn "github.com/Azure/azure-container-networking/common"
@@ -158,6 +159,12 @@ func (service *HTTPRestService) saveNetworkContainerGoalState(req cns.CreateNetw
158159
existingSecondaryIPConfigs = existingNCStatus.CreateNetworkContainerRequest.SecondaryIPConfigs
159160
vfpUpdateComplete = existingNCStatus.VfpUpdateComplete
160161
}
162+
163+
if req.NetworkContainerType == cns.NodeSubnet {
164+
hostVersion = nodesubnet.NodeSubnetHostVersion
165+
vfpUpdateComplete = true
166+
}
167+
161168
if hostVersion == "" {
162169
// Host version is the NC version from NMAgent, set it -1 to indicate no result from NMAgent yet.
163170
// TODO, query NMAgent and with aggresive time out and assign latest host version.
@@ -189,6 +196,8 @@ func (service *HTTPRestService) saveNetworkContainerGoalState(req cns.CreateNetw
189196
fallthrough
190197
case cns.COW, cns.BackendNICNC:
191198
fallthrough
199+
case cns.NodeSubnet:
200+
fallthrough
192201
case cns.WebApps:
193202
switch service.state.OrchestratorType {
194203
case cns.Kubernetes:
@@ -224,6 +233,8 @@ func (service *HTTPRestService) saveNetworkContainerGoalState(req cns.CreateNetw
224233
logger.Printf("service.state.ContainerIDByOrchestratorContext[%s] is %+v", orchestratorContext, *service.state.ContainerIDByOrchestratorContext[orchestratorContext])
225234

226235
case cns.KubernetesCRD:
236+
fallthrough
237+
case cns.KubernetesNodeSubnet:
227238
// Validate and Update the SecondaryIpConfig state
228239
returnCode, returnMesage := service.updateIPConfigsStateUntransacted(req, existingSecondaryIPConfigs, hostVersion)
229240
if returnCode != 0 {
@@ -291,6 +302,7 @@ func (service *HTTPRestService) updateIPConfigsStateUntransacted(
291302
if hostNCVersionInInt, err = strconv.Atoi(hostVersion); err != nil {
292303
return types.UnsupportedNCVersion, fmt.Sprintf("Invalid hostVersion is %s, err:%s", hostVersion, err)
293304
}
305+
294306
service.addIPConfigStateUntransacted(req.NetworkContainerid, hostNCVersionInInt, req.SecondaryIPConfigs,
295307
existingSecondaryIPConfigs)
296308

0 commit comments

Comments
 (0)