Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
ace96ff
fix overlay IPAM not reporting version
nddq Jul 27, 2023
f0b4f19
Merge branch 'master' into master
nddq Jul 31, 2023
f1b5dcb
revert file and var naming, add correct path to makefile
nddq Jul 31, 2023
eebfbc8
Merge branch 'Azure:master' into master
nddq Aug 7, 2023
84abe01
Merge branch 'Azure:master' into master
nddq Aug 9, 2023
994dc04
Merge branch 'Azure:master' into master
nddq Aug 11, 2023
8475126
Merge branch 'Azure:master' into master
nddq Aug 14, 2023
29c8206
Merge branch 'Azure:master' into master
nddq Aug 16, 2023
62e6df8
Merge branch 'Azure:master' into master
nddq Aug 16, 2023
20988bd
proposal design for multitenant IPAM flow
nddq Aug 9, 2023
2e6842f
change podipinfo + linter issue
nddq Aug 9, 2023
227fd89
pointer issues for printf
nddq Aug 10, 2023
d5b00db
update IPAM branching
nddq Aug 15, 2023
e53f1e7
remove comments
nddq Aug 15, 2023
f99525c
pod client placeholder
nddq Aug 18, 2023
c7f1bca
address lint issue for httpservicefake
nddq Aug 18, 2023
f2359ac
getting pod info in validator
nddq Aug 21, 2023
fa2cac5
linter issue
nddq Aug 21, 2023
79aef2e
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 21, 2023
fd2803f
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 22, 2023
dc08bfe
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 23, 2023
7792ac4
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 23, 2023
dfd8764
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 23, 2023
a60f9e1
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 24, 2023
c58fef7
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 24, 2023
50deef4
Merge branch 'master' into swift2-cns-ipam-branching
nddq Aug 25, 2023
cc91071
update network container contract
nddq Aug 23, 2023
0674e06
renaming
nddq Aug 25, 2023
fd03f7c
mtpnc changes
nddq Aug 29, 2023
baeb65b
Merge branch 'Azure:master' into master
nddq Aug 29, 2023
1e7c99d
rebase
nddq Aug 29, 2023
6e71052
revert file and var naming, add correct path to makefile
nddq Jul 31, 2023
9225521
resolved merge conflicts
nddq Aug 29, 2023
ad1bf07
add default route
nddq Aug 29, 2023
78bc86f
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 3, 2023
31ebd90
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 7, 2023
e26f7c3
add unit tests
nddq Sep 8, 2023
9f97340
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 8, 2023
1d4a4bf
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 8, 2023
0860321
update unit tests for ipam
nddq Sep 11, 2023
1286167
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 11, 2023
5a89c51
go get to fix linter
nddq Sep 11, 2023
e193b82
go mod tidy
nddq Sep 11, 2023
d1db1ab
update routes
nddq Sep 11, 2023
c3e470a
update routes
nddq Sep 11, 2023
7458cf1
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 12, 2023
c091ec3
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 15, 2023
dd45bed
remove stale comments + remove redundant method
nddq Sep 15, 2023
745f2cf
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 18, 2023
bf483aa
add contexts + change address type
nddq Sep 18, 2023
47ce73f
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 20, 2023
01310d2
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 20, 2023
16e7c50
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 20, 2023
99d1e8d
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 21, 2023
fcdaae0
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 22, 2023
843a64e
addressed review
nddq Sep 22, 2023
04ee54c
embedded client to mock + enum for address type
nddq Sep 22, 2023
08b4a78
fix error
nddq Sep 22, 2023
da54a6e
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 22, 2023
01cc008
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 25, 2023
6bb2b7d
change addressType to NICType
nddq Sep 25, 2023
4ee5937
change isDefaultRoute to SkipDefaultRoutes
nddq Sep 25, 2023
7c49876
address comments
nddq Sep 25, 2023
aa80c2b
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 25, 2023
c9baa54
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 26, 2023
cb8481f
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 27, 2023
d36f4e7
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 27, 2023
33c25f6
refractor: make changes according to cni/cns contract
nddq Sep 27, 2023
2d5c6bd
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 28, 2023
c0e2ae7
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 29, 2023
0972ce4
refractor: make adding route its own func + move swift v2 ipam branch…
nddq Sep 29, 2023
3fc14bd
refractor: change vars naming
nddq Sep 29, 2023
28ba7b2
refractor: more var naming
nddq Sep 29, 2023
b067200
test: add test for podv6cidr
nddq Sep 29, 2023
4ffe8d6
refractor: make the returning podIpInfo init cleaner in swiftv2.go
nddq Sep 29, 2023
975e9b4
Merge branch 'master' into swift2-cns-ipam-branching
nddq Sep 30, 2023
0a25e65
refractor + tests: add contexts to ipconfigs req validators + set rou…
nddq Oct 2, 2023
d87e4dc
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 3, 2023
7153f9b
refractor: change labels for swift v2 pods
nddq Oct 3, 2023
d294df3
fix: fix swift v2 UT
nddq Oct 3, 2023
e4c60cf
refractor: add v4/v6 distinction for service cidr
nddq Oct 3, 2023
0437f9b
Merge branch 'Azure:master' into swift2-cns-ipam-branching
nddq Oct 4, 2023
5113094
rebase
nddq Aug 29, 2023
aee4f2e
revert file and var naming, add correct path to makefile
nddq Jul 31, 2023
68e65e7
rebase
nddq Aug 29, 2023
fdebcf4
revert file and var naming, add correct path to makefile
nddq Jul 31, 2023
f4802eb
change podipinfo + linter issue
nddq Aug 9, 2023
ece68a6
update IPAM branching
nddq Aug 15, 2023
ddf472a
pod client placeholder
nddq Aug 18, 2023
691f733
getting pod info in validator
nddq Aug 21, 2023
e11247b
linter issue
nddq Aug 21, 2023
b6d6c26
rebase
nddq Aug 29, 2023
48a911d
revert file and var naming, add correct path to makefile
nddq Jul 31, 2023
29b6f67
refractor: fix conflicts
nddq Oct 4, 2023
c79a0e6
refractor: revert podwatcher code changes
nddq Oct 4, 2023
ff291a2
docs: change comment
nddq Oct 4, 2023
17702e9
refractor: change CIDR to CDIRs
nddq Oct 4, 2023
62080d6
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 4, 2023
40964f9
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 5, 2023
43d7d40
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 5, 2023
0e88376
refractor: parse CIDRs as semicolons separated string from env in Set…
nddq Oct 6, 2023
b99f5fa
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 7, 2023
308d799
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 9, 2023
e59220e
docs: add minor comment
nddq Oct 10, 2023
edcdb2f
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 11, 2023
8f7e3c0
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 13, 2023
7e086db
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 13, 2023
979c417
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 13, 2023
48f3895
Merge branch 'master' into swift2-cns-ipam-branching
nddq Oct 16, 2023
627b1a0
refractor: change separator for parsing CIDRs
nddq Oct 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ type PodInfo interface {
Equals(PodInfo) bool
// String implements string for logging PodInfos
String() string
// SecondaryInterfacesExist returns true if there exist a secondary interface for this pod
SecondaryInterfacesExist() bool
}

type KubernetesPodInfo struct {
Expand All @@ -194,9 +196,10 @@ var _ PodInfo = (*podInfo)(nil)
// podInfo implements PodInfo for multiple schemas of Key
type podInfo struct {
KubernetesPodInfo
PodInfraContainerID string
PodInterfaceID string
Version podInfoScheme
PodInfraContainerID string
PodInterfaceID string
Version podInfoScheme
SecondaryInterfaceSet bool
}

func (p podInfo) String() string {
Expand Down Expand Up @@ -250,6 +253,10 @@ func (p *podInfo) OrchestratorContext() (json.RawMessage, error) {
return jsonContext, nil
}

func (p *podInfo) SecondaryInterfacesExist() bool {
return p.SecondaryInterfaceSet
}

// NewPodInfo returns an implementation of PodInfo that returns the passed
// configuration for their namesake functions.
func NewPodInfo(infraContainerID, interfaceID, name, namespace string) PodInfo {
Expand Down Expand Up @@ -287,6 +294,7 @@ func NewPodInfoFromIPConfigsRequest(req IPConfigsRequest) (PodInfo, error) {
}
p.(*podInfo).PodInfraContainerID = req.InfraContainerID
p.(*podInfo).PodInterfaceID = req.PodInterfaceID
p.(*podInfo).SecondaryInterfaceSet = req.SecondaryInterfacesExist
return p, nil
}

Expand Down Expand Up @@ -442,11 +450,12 @@ type IPConfigRequest struct {

// Same as IPConfigRequest except that DesiredIPAddresses is passed in as a slice
type IPConfigsRequest struct {
DesiredIPAddresses []string `json:"desiredIPAddresses"`
PodInterfaceID string `json:"podInterfaceID"`
InfraContainerID string `json:"infraContainerID"`
OrchestratorContext json.RawMessage `json:"orchestratorContext"`
Ifname string `json:"ifname"` // Used by delegated IPAM
DesiredIPAddresses []string `json:"desiredIPAddresses"`
PodInterfaceID string `json:"podInterfaceID"`
InfraContainerID string `json:"infraContainerID"`
OrchestratorContext json.RawMessage `json:"orchestratorContext"`
Ifname string `json:"ifname"` // Used by delegated IPAM
SecondaryInterfacesExist bool `json:"secondaryInterfacesExist"` // will be set by SWIFT v2 validator func
}

// IPConfigResponse is used in CNS IPAM mode as a response to CNI ADD
Expand Down
10 changes: 10 additions & 0 deletions cns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,18 @@ type HTTPService interface {
GetPendingReleaseIPConfigs() []IPConfigurationStatus
GetPodIPConfigState() map[string]IPConfigurationStatus
MarkIPAsPendingRelease(numberToMark int) (map[string]IPConfigurationStatus, error)
AttachSWIFTv2Middleware(middleware SWIFTv2Middleware)
}

// Middleware interface for testing later on
type SWIFTv2Middleware interface {
ValidateIPConfigsRequest(context.Context, *IPConfigsRequest) (types.ResponseCode, string)
GetIPConfig(context.Context, PodInfo) (PodIpInfo, error)
SetRoutes(*PodIpInfo) error
}

type IPConfigsRequestValidator func(context.Context, *IPConfigsRequest) (types.ResponseCode, string)

// This is used for KubernetesCRD orchestrator Type where NC has multiple ips.
// This struct captures the state for SecondaryIPs associated to a given NC
type IPConfigurationStatus struct {
Expand Down
30 changes: 28 additions & 2 deletions cns/configuration/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ const (
EnvNodeName = "NODENAME"
// EnvNodeIP is the IP of the node running this CNS binary
EnvNodeIP = "NODE_IP"
// LabelSwiftV2 is the Node label for Swift V2
LabelSwiftV2 = "kubernetes.azure.com/podnetwork-multi-tenancy"
// LabelNodeSwiftV2 is the Node label for Swift V2
LabelNodeSwiftV2 = "kubernetes.azure.com/podnetwork-multi-tenancy-enabled"
// LabelPodSwiftV2 is the Pod label for Swift V2
LabelPodSwiftV2 = "kubernetes.azure.com/pod-network"
EnvPodCIDRs = "POD_CIDRs"
EnvServiceCIDRs = "SERVICE_CIDRs"
)

// ErrNodeNameUnset indicates the the $EnvNodeName variable is unset in the environment.
var ErrNodeNameUnset = errors.Errorf("must declare %s environment variable", EnvNodeName)

// ErrPodCIDRsUnset indicates the the $EnvPodCIDRs variable is unset in the environment.
var ErrPodCIDRsUnset = errors.Errorf("must declare %s environment variable", EnvPodCIDRs)

// ErrServiceCIDRsUnset indicates the the $EnvServiceCIDRs variable is unset in the environment.
var ErrServiceCIDRsUnset = errors.Errorf("must declare %s environment variable", EnvServiceCIDRs)

// NodeName checks the environment variables for the NODENAME and returns it or an error if unset.
func NodeName() (string, error) {
nodeName := os.Getenv(EnvNodeName)
Expand All @@ -31,3 +41,19 @@ func NodeName() (string, error) {
func NodeIP() string {
return os.Getenv(EnvNodeIP)
}

func PodCIDRs() (string, error) {
podCIDRs := os.Getenv(EnvPodCIDRs)
if podCIDRs == "" {
return "", ErrPodCIDRsUnset
}
return podCIDRs, nil
}

func ServiceCIDRs() (string, error) {
serviceCIDRs := os.Getenv(EnvServiceCIDRs)
if serviceCIDRs == "" {
return "", ErrServiceCIDRsUnset
}
return serviceCIDRs, nil
}
20 changes: 20 additions & 0 deletions cns/configuration/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ func TestNodeName(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "test", name)
}

func TestPodCIDRs(t *testing.T) {
_, err := PodCIDRs()
require.Error(t, err)
require.ErrorIs(t, err, ErrPodCIDRsUnset)
os.Setenv(EnvPodCIDRs, "test")
cidr, err := PodCIDRs()
assert.NoError(t, err)
assert.Equal(t, "test", cidr)
}

func TestServiceCIDRs(t *testing.T) {
_, err := ServiceCIDRs()
require.Error(t, err)
require.ErrorIs(t, err, ErrServiceCIDRsUnset)
os.Setenv(EnvServiceCIDRs, "test")
cidr, err := ServiceCIDRs()
assert.NoError(t, err)
assert.Equal(t, "test", cidr)
}
2 changes: 2 additions & 0 deletions cns/fakes/cnsfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,5 @@ func (fake *HTTPServiceFake) Init(*common.ServiceConfig) error {
}

func (fake *HTTPServiceFake) Stop() {}

func (fake *HTTPServiceFake) AttachSWIFTv2Middleware(cns.SWIFTv2Middleware) {}
65 changes: 65 additions & 0 deletions cns/middlewares/mock/mockClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package middlewares

import (
"context"
"errors"

"github.com/Azure/azure-container-networking/cns/configuration"
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
errPodNotFound = errors.New("pod not found")
errMTPNCNotFound = errors.New("mtpnc not found")
)

// MockClient implements the client.Client interface for testing. We only care about Get, the rest is nil ops.
type MockClient struct {
client.Client
mtPodCache map[string]*v1.Pod
mtpncCache map[string]*v1alpha1.MultitenantPodNetworkConfig
}

// NewMockClient returns a new MockClient.
func NewMockClient() *MockClient {
testPod1 := v1.Pod{}
testPod1.Labels = make(map[string]string)
testPod1.Labels[configuration.LabelPodSwiftV2] = "true"

testMTPNC1 := v1alpha1.MultitenantPodNetworkConfig{}
testMTPNC1.Status.PrimaryIP = "192.168.0.1"
testMTPNC1.Status.MacAddress = "00:00:00:00:00:00"
testMTPNC1.Status.GatewayIP = "10.0.0.1"
testMTPNC1.Status.NCID = "testncid"

testMTPNC3 := v1alpha1.MultitenantPodNetworkConfig{}

return &MockClient{
mtPodCache: map[string]*v1.Pod{"testpod1namespace/testpod1": &testPod1},
mtpncCache: map[string]*v1alpha1.MultitenantPodNetworkConfig{
"testpod1namespace/testpod1": &testMTPNC1,
"testpod3namespace/testpod3": &testMTPNC3,
},
}
}

// Get implements client.Client.Get.
func (c *MockClient) Get(_ context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
switch o := obj.(type) {
case *v1.Pod:
if pod, ok := c.mtPodCache[key.String()]; ok {
*o = *pod
} else {
return errPodNotFound
}
case *v1alpha1.MultitenantPodNetworkConfig:
if mtpnc, ok := c.mtpncCache[key.String()]; ok {
*o = *mtpnc
} else {
return errMTPNCNotFound
}
}
return nil
}
92 changes: 92 additions & 0 deletions cns/middlewares/mock/mockSWIFTv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package middlewares

import (
"context"
"errors"
"fmt"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/configuration"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
v1 "k8s.io/api/core/v1"
k8types "k8s.io/apimachinery/pkg/types"
)

var (
errMTPNCNotReady = errors.New("mtpnc is not ready")
errFailedToGetPod = errors.New("failed to get pod")
)

type MockSWIFTv2Middleware struct {
mtPodState map[string]*v1.Pod
mtpncState map[string]*v1alpha1.MultitenantPodNetworkConfig
}

func NewMockSWIFTv2Middleware() *MockSWIFTv2Middleware {
testPod1 := v1.Pod{}
testPod1.Labels = make(map[string]string)
testPod1.Labels[configuration.LabelPodSwiftV2] = "true"

testMTPNC1 := v1alpha1.MultitenantPodNetworkConfig{}
testMTPNC1.Status.PrimaryIP = "192.168.0.1"
testMTPNC1.Status.MacAddress = "00:00:00:00:00:00"
testMTPNC1.Status.GatewayIP = "10.0.0.1"
testMTPNC1.Status.NCID = "testncid"

return &MockSWIFTv2Middleware{
mtPodState: map[string]*v1.Pod{"testpod1namespace/testpod1": &testPod1},
mtpncState: map[string]*v1alpha1.MultitenantPodNetworkConfig{"testpod1namespace/testpod1": &testMTPNC1},
}
}

// validateMultitenantIPConfigsRequest validates if pod is multitenant
// nolint
func (m *MockSWIFTv2Middleware) ValidateIPConfigsRequest(_ context.Context, req *cns.IPConfigsRequest) (respCode types.ResponseCode, message string) {
// Retrieve the pod from the cluster
podInfo, err := cns.UnmarshalPodInfo(req.OrchestratorContext)
if err != nil {
errBuf := fmt.Sprintf("unmarshalling pod info from ipconfigs request %v failed with error %v", req, err)
return types.UnexpectedError, errBuf
}
podNamespacedName := k8types.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
pod, ok := m.mtPodState[podNamespacedName.String()]
if !ok {
errBuf := fmt.Sprintf("failed to get pod %v with error %v", podNamespacedName, err)
return types.UnexpectedError, errBuf
}
// check the pod labels for Swift V2, enrich the request with the multitenant flag.
if _, ok := pod.Labels[configuration.LabelPodSwiftV2]; ok {
req.SecondaryInterfacesExist = true
}
return types.Success, ""
}

// GetSWIFTv2IPConfig(podInfo PodInfo) (*PodIpInfo, error)
// GetMultitenantIPConfig returns the IP config for a multitenant pod from the MTPNC CRD
func (m *MockSWIFTv2Middleware) GetIPConfig(_ context.Context, podInfo cns.PodInfo) (cns.PodIpInfo, error) {
// Check if the MTPNC CRD exists for the pod, if not, return error
mtpncNamespacedName := k8types.NamespacedName{Namespace: podInfo.Namespace(), Name: podInfo.Name()}
mtpnc, ok := m.mtpncState[mtpncNamespacedName.String()]
if !ok {
return cns.PodIpInfo{}, errFailedToGetPod
}

// Check if the MTPNC CRD is ready. If one of the fields is empty, return error
if mtpnc.Status.PrimaryIP == "" || mtpnc.Status.MacAddress == "" || mtpnc.Status.NCID == "" || mtpnc.Status.GatewayIP == "" {
return cns.PodIpInfo{}, errMTPNCNotReady
}
podIPInfo := cns.PodIpInfo{}
podIPInfo.PodIPConfig = cns.IPSubnet{
IPAddress: mtpnc.Status.PrimaryIP,
}
podIPInfo.MacAddress = mtpnc.Status.MacAddress
podIPInfo.NICType = cns.DelegatedVMNIC
podIPInfo.SkipDefaultRoutes = false

return podIPInfo, nil
}

func (m *MockSWIFTv2Middleware) SetRoutes(_ *cns.PodIpInfo) error {
return nil
}
Loading