Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7a4dbf8
adding initial dataplane interface
vakalapa Aug 27, 2021
4385d46
adding some skeleton code for crud functions
vakalapa Aug 30, 2021
013aa67
adding dirty cache initial support for ipset manager
vakalapa Aug 30, 2021
3e32783
correcting some go lints
vakalapa Aug 30, 2021
a907ea2
Adding some skeleton around os specific files
vakalapa Aug 30, 2021
58d7782
removing interface and adding some network logic
Aug 31, 2021
5c5403e
adding apply ipsets logic
Sep 1, 2021
6c5afdc
Addressing some comments and also adding comments to code
Sep 2, 2021
cf8e556
Fixing some golints
Sep 2, 2021
bfff6a5
addressing some comments
Sep 2, 2021
4fe2916
adding some golint checks
Sep 2, 2021
9441d08
Adding a new field setProperty and also adding structure for policies
Sep 7, 2021
14ef65f
applying some comments
Sep 7, 2021
9c1c558
correcting a condition
Sep 7, 2021
57d78af
Adding some comments
Sep 7, 2021
f702d88
Adding some test cases
Sep 8, 2021
a9dfbc3
Addressing some comments
Sep 8, 2021
a5cbd0b
Addressing more comments
Sep 8, 2021
c921c1d
resolving some comments
Sep 9, 2021
af60247
resolving some comments
Sep 9, 2021
987f820
fixing some comments
Sep 9, 2021
ae527f4
removing lock on policymap
Sep 10, 2021
d0eb0c6
fixing some golints
Sep 10, 2021
15cae9a
Merge branch 'master' into vakr/dataplaneinterface
Sep 10, 2021
7c200c5
Merge branch 'master' into vakr/dataplaneinterface
Sep 10, 2021
607ba43
merging with master
Sep 10, 2021
942f894
fixingsome wrap checks
Sep 10, 2021
50315e6
fixing lints
Sep 10, 2021
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
10 changes: 0 additions & 10 deletions npm/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Azure/azure-container-networking/npm/metrics"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/exec"
utilexec "k8s.io/utils/exec"
)

// To indicate the object is needed to be DeletedFinalStateUnknown Object
Expand All @@ -29,15 +28,6 @@ func getKey(obj interface{}, t *testing.T) string {
return key
}

func newNPMgr(t *testing.T, exec utilexec.Interface) *NetworkPolicyManager {
npMgr := &NetworkPolicyManager{
ipsMgr: ipsm.NewIpsetManager(exec),
TelemetryEnabled: false,
}

return npMgr
}

func TestMain(m *testing.M) {
metrics.InitializeAll()
exec := exec.New()
Expand Down
162 changes: 162 additions & 0 deletions npm/pkg/dataplane/dataplane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package dataplane

import (
"fmt"

"github.com/Azure/azure-container-networking/npm"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/policies"
)

type DataPlane struct {
policyMgr policies.PolicyManager
ipsetMgr ipsets.IPSetManager
networkID string
// key is PodKey
endpointCache map[string]*NPMEndpoint
}

type NPMEndpoint struct {
Name string
ID string
// Map with Key as Network Policy name to to emulate set
// and value as struct{} for minimal memory consumption
NetPolReference map[string]struct{}
}

func NewDataPlane() *DataPlane {
return &DataPlane{
policyMgr: policies.NewPolicyManager(),
ipsetMgr: ipsets.NewIPSetManager(),
endpointCache: make(map[string]*NPMEndpoint),
}
}

// InitializeDataPlane helps in setting up dataplane for NPM
func (dp *DataPlane) InitializeDataPlane() error {
return dp.initializeDataPlane()
}

// ResetDataPlane helps in cleaning up dataplane sets and policies programmed
// by NPM, retunring a clean slate
func (dp *DataPlane) ResetDataPlane() error {
return dp.resetDataPlane()
}

// CreateIPSet takes in a set object and updates local cache with this set
func (dp *DataPlane) CreateIPSet(set *ipsets.IPSet) error {
err := dp.ipsetMgr.CreateIPSet(set)
if err != nil {
return fmt.Errorf("[DataPlane] error while creating set: %w", err)
}
return nil
}

// DeleteSet checks for members and references of the given "set" type ipset
// if not used then will delete it from cache
func (dp *DataPlane) DeleteSet(name string) error {
err := dp.ipsetMgr.DeleteSet(name)
if err != nil {
return fmt.Errorf("[DataPlane] error while deleting set: %w", err)
}
return nil
}

// DeleteList sanity checks and deletes a list ipset
func (dp *DataPlane) DeleteList(name string) error {
err := dp.ipsetMgr.DeleteList(name)
if err != nil {
return fmt.Errorf("[DataPlane] error while deleting list: %w", err)
}
return nil
}

// AddToSet takes in a list of IPset objects along with IP member
// and then updates it local cache
func (dp *DataPlane) AddToSet(setNames []*ipsets.IPSet, ip, podKey string) error {
err := dp.ipsetMgr.AddToSet(setNames, ip, podKey)
if err != nil {
return fmt.Errorf("[DataPlane] error while adding to set: %w", err)
}
return nil
}

// RemoveFromSet takes in list of setnames from which a given IP member should be
// removed and will update the local cache
func (dp *DataPlane) RemoveFromSet(setNames []string, ip, podKey string) error {
err := dp.ipsetMgr.RemoveFromSet(setNames, ip, podKey)
if err != nil {
return fmt.Errorf("[DataPlane] error while removing from set: %w", err)
}
return nil
}

// AddToList takes a list name and list of sets which are to be added as members
// to given list
func (dp *DataPlane) AddToList(listName string, setNames []string) error {
err := dp.ipsetMgr.AddToList(listName, setNames)
if err != nil {
return fmt.Errorf("[DataPlane] error while adding to list: %w", err)
}
return nil
}

// RemoveFromList takes a list name and list of sets which are to be removed as members
// to given list
func (dp *DataPlane) RemoveFromList(listName string, setNames []string) error {
err := dp.ipsetMgr.RemoveFromList(listName, setNames)
if err != nil {
return fmt.Errorf("[DataPlane] error while removing from list: %w", err)
}
return nil
}

// UpdatePod is to be called by pod_controller ONLY when a new pod is CREATED.
func (dp *DataPlane) UpdatePod(pod *npm.NpmPod) error {
err := dp.updatePod(pod)
if err != nil {
return fmt.Errorf("[DataPlane] error while updating pod: %w", err)
}
return nil
}

// ApplyDataPlane all the IPSet operations just update cache and update a dirty ipset structure,
// they do not change apply changes into dataplane. This function needs to be called at the
// end of IPSet operations of a given controller event, it will check for the dirty ipset list
// and accordingly makes changes in dataplane. This function helps emulate a single call to
// dataplane instead of multiple ipset operations calls ipset operations calls to dataplane
func (dp *DataPlane) ApplyDataPlane() error {
err := dp.ipsetMgr.ApplyIPSets(dp.networkID)
if err != nil {
return fmt.Errorf("[DataPlane] error while applying IPSets: %w", err)
}
return nil
}

// AddPolicy takes in a translated NPMNetworkPolicy object and applies on dataplane
func (dp *DataPlane) AddPolicy(policies *policies.NPMNetworkPolicy) error {
err := dp.policyMgr.AddPolicy(policies)
if err != nil {
return fmt.Errorf("[DataPlane] error while adding policy: %w", err)
}
return nil
}

// RemovePolicy takes in network policy name and removes it from dataplane and cache
func (dp *DataPlane) RemovePolicy(policyName string) error {
err := dp.policyMgr.RemovePolicy(policyName)
if err != nil {
return fmt.Errorf("[DataPlane] error while removing policy: %w", err)
}
return nil
}

// UpdatePolicy takes in updated policy object, calculates the delta and applies changes
// onto dataplane accordingly
func (dp *DataPlane) UpdatePolicy(policies *policies.NPMNetworkPolicy) error {
err := dp.policyMgr.UpdatePolicy(policies)
if err != nil {
return fmt.Errorf("[DataPlane] error while updating policy: %w", err)
}
return nil
}
21 changes: 21 additions & 0 deletions npm/pkg/dataplane/dataplane_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dataplane

import (
"github.com/Azure/azure-container-networking/npm"
"k8s.io/klog"
)

// initializeDataPlane should be adding required chains and rules
func (dp *DataPlane) initializeDataPlane() error {
klog.Infof("Initializing dataplane for linux")
return nil
}

// updatePod is no-op in Linux
func (dp *DataPlane) updatePod(pod *npm.NpmPod) error {
return nil
}

func (dp *DataPlane) resetDataPlane() error {
return nil
}
23 changes: 23 additions & 0 deletions npm/pkg/dataplane/dataplane_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dataplane

import (
"testing"

"github.com/Azure/azure-container-networking/npm/metrics"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
)

func TestNewDataPlane(t *testing.T) {
metrics.InitializeAll()
dp := NewDataPlane()

if dp == nil {
t.Error("NewDataPlane() returned nil")
}
set := ipsets.NewIPSet("test", ipsets.NameSpace)

err := dp.CreateIPSet(set)
if err != nil {
t.Error("CreateIPSet() returned error")
}
}
54 changes: 54 additions & 0 deletions npm/pkg/dataplane/dataplane_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dataplane

import (
"github.com/Azure/azure-container-networking/npm"
"github.com/Microsoft/hcsshim/hcn"
"k8s.io/klog"
)

const (
// Windows specific constants
AzureNetworkName = "azure"
)

// initializeDataPlane will help gather network and endpoint details
func (dp *DataPlane) initializeDataPlane() error {
klog.Infof("Initializing dataplane for windows")

// Get Network ID
network, err := hcn.GetNetworkByName(AzureNetworkName)
if err != nil {
return err
}

dp.networkID = network.Id

endpoints, err := hcn.ListEndpointsOfNetwork(dp.networkID)
if err != nil {
return err
}

for _, endpoint := range endpoints {
klog.Infof("Endpoints info %+v", endpoint.Policies)
ep := &NPMEndpoint{
Name: endpoint.Name,
ID: endpoint.Id,
NetPolReference: make(map[string]struct{}),
}

dp.endpointCache[ep.Name] = ep
}

return nil
}

// updatePod has two responsibilities in windows
// 1. Will call into dataplane and updates endpoint references of this pod.
// 2. Will check for existing applicable network policies and applies it on endpoint
func (dp *DataPlane) updatePod(pod *npm.NpmPod) error {
return nil
}

func (dp *DataPlane) resetDataPlane() error {
return nil
}
Loading