Skip to content

Conversation

@saiyan86
Copy link
Contributor

@saiyan86 saiyan86 commented Jun 9, 2018

What this PR does / why we need it:

This PR adds Azure Network Policy Manager, a.k.a, azure-npm, to Azure CNI.
Azure-npm is a Microsoft Azure's fault-tolerant and scalable implementation of Kubernetes Network Policy plugin.
Azure-npm supports all network policies specified by current version of Kubernetes, and will continue to support future versions.

Azure-npm only supports Kubernetes v1.8 or later, as it requires NetworkPolicy API v1beta1.

@saiyan86 saiyan86 force-pushed the master branch 3 times, most recently from 59bf95f to c612106 Compare June 26, 2018 23:17
set: util.GetHashedName(listName),
}

errCode, err := ipsMgr.Run(entry)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
if errCode == 1 {
log.Printf("Cannot delete list %s as it's being referred or doesn't exist.\n", listName)
return nil
}

log.Printf("Error deleting ipset %s\n %+v\n", listName, entry)
return err  

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/ipsm/ipsm.go Outdated
}

if _, err := ipsMgr.Run(entry); err != nil {
log.Printf("Error creating ipset rules.\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine logs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return err
}

ipsMgr.setMap[setName] = NewIpset(setName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space to stick with styling

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/ipsm/ipsm.go Outdated
}

if err != nil {
log.Printf("Error deleting ipset %s", setName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine log

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment as above -

if err != nil {
if errCode == 1 {

log.Printf("rule: %+v\n", entry)
return err
}
ipsMgr.setMap[setName].elements = append(ipsMgr.setMap[setName].elements, ip)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after if brace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/namespace.go Outdated

allNs := npMgr.nsMap[util.KubeAllNamespacesFlag]
// Create ipset for the namespace.
ipsMgr := allNs.ipsMgr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipsMgr := npMgr.nsMap[util.KubeAllNamespacesFlag].ipsMgr?

npm/nwpolicy.go Outdated
iptMgr := allNs.iptMgr
for _, iptEntry := range iptEntries {
if err := iptMgr.Delete(iptEntry); err != nil {
log.Printf("Error applying iptables rule\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine logs

entries = append(entries, egressEntries...)

entries = append(entries, getDefaultDropEntries(affectedSets)...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove space

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a different block. Need space here.

npm/pod.go Outdated
return nil
}

oldPodObjNs, oldPodObjName, oldPodObjPhase, oldPodObjIP, newPodObjNs, newPodObjName, newPodObjPhase, newPodObjIP := oldPodObj.ObjectMeta.Namespace, oldPodObj.ObjectMeta.Name, oldPodObj.Status.Phase, oldPodObj.Status.PodIP, newPodObj.ObjectMeta.Namespace, newPodObj.ObjectMeta.Name, newPodObj.Status.Phase, newPodObj.Status.PodIP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var (
oldPodObjNs = oldPodObj.ObjectMeta.Namespace
...
...
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/pod.go Outdated
return nil
}

podNs, podName, podNodeName, podLabels := podObj.ObjectMeta.Namespace, podObj.ObjectMeta.Name, podObj.Spec.NodeName, podObj.ObjectMeta.Labels
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var initialize all of these guys

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/ipsm/ipsm.go Outdated

// NotReferredByNwPolicy checks if a specific ipset is referred by any network policy.
func (ipsMgr *IpsetManager) NotReferredByNwPolicy(setName string) bool {
return ipsMgr.setMap[setName].referCount == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if setName is not present in the setMap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused func. It is removed.

errCode, err := ipsMgr.Run(entry)
if errCode > 1 && err != nil {
log.Printf("Error deleting ipset entry.\n")
log.Printf("%+v\n", entry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine the logs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/ipsm/ipsm.go Outdated
}

if err != nil {
log.Printf("Error deleting ipset %s", setName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar comment as above -

if err != nil {
if errCode == 1 {


if msg, failed := err.(*exec.ExitError); failed {
errCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
if errCode > 1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine these as
if errCode := msg.Sys().(syscall.WaitStatus).ExitStatus(); errCode > 1 {
?

npm/iptm/iptm.go Outdated

// InitNpmChains initializes Azure NPM chains in iptables.
func (iptMgr *IptablesManager) InitNpmChains() error {
log.Printf("Initializing AZURE-NPM")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is initializing azure-npm same as azure-npm-chain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to "Initializing AZURE-NPM chains"

util.IptablesJumpFlag,
util.IptablesAzureChain,
},
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one code block. entry is used right after declaration

return nil
}

if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as above about restructure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

npm/iptm/iptm.go Outdated

// Add adds a rule in iptables.
func (iptMgr *IptablesManager) Add(entry *IptEntry) error {
log.Printf("%+v\n", entry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this log helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added description.

npm/iptm/iptm.go Outdated

// Delete removes a rule in iptables.
func (iptMgr *IptablesManager) Delete(entry *IptEntry) error {
log.Printf("%+v\n", entry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this log helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added description.


if msg, failed := err.(*exec.ExitError); failed {
errCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
if errCode > 1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combine these as
if errCode := msg.Sys().(syscall.WaitStatus).ExitStatus(); errCode > 1 {
?

npm/namespace.go Outdated
name string
setMap map[string]string
podMap map[types.UID]*corev1.Pod
npMap map[string]*networkingv1.NetworkPolicy // TODO: Optimize to ordered map.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO..

Copy link
Contributor Author

@saiyan86 saiyan86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address comments


allNs := npMgr.nsMap[util.KubeAllNamespacesFlag]

_, _, iptEntries := parsePolicy(npObj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can parsePolicy return an error? What happens in case of error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically pure string manipulation. This function has no effect by itself. Its return value is used in other functions, and I catch errors there.


allNs, err := newNs(util.KubeAllNamespacesFlag)
if err != nil {
panic(err.Error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you handling these panics? I don't see any recover.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error is fatal. It shouldn't be recovered.

npm/parse.go Outdated
isAppliedToNs = true
}

//TODO: handle IPBlock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still a TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Removed. Thanks!

log.SetName("azure-npm")
log.SetLevel(log.LevelInfo)
err := log.SetTarget(log.TargetLogfile)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err := log.SetTarget(log.TargetLogfile); err != nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// Creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to above - where are you handing these panics?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. Thanks!

npm/pod.go Outdated
podNodeName := podObj.Spec.NodeName
podLabels := podObj.ObjectMeta.Labels
podIP := podObj.Status.PodIP
log.Printf("POD DELETED: %s/%s/%s\n", podNs, podName, podNodeName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to line 138 - which makes sure we log 'deleted' only when the operation is successful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed from deleted to deleting

npm/pod_test.go Outdated
"testing"

"github.com/Azure/azure-container-networking/npm/ipsm"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason for space here?

@@ -0,0 +1,173 @@
package npm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added a bunch of new files - I think we need to add correct copyright info in those.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should we add to copyright info?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the top of https://github.com/Azure/azure-container-networking/blob/master/cni/network/network.go .
Please check with Sushant if these are needed. I think we need to have Microsoft under copyright statement for new files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I added Microsoft copyright info.

}
}

func TestUpdatePod(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can combine TestUpdatePod and TestAddPod (like TestAddUpdatePod) because 95% of the code is same. Is there any particular reason to have these separate? It's possible to combine delete in this as well. But I don't have any strong push for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incremental test. This will give a bit more coverage.

@@ -0,0 +1,42 @@
package util
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this util package be brought out of npm and at azure-container-networking scope? That way we can build this util and can use it everywhere within azure-container-networking. If easily possible, otherwise no worries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion. I'll do it later. But can we check this in first? This is pretty important :D

Yongli Chen added 5 commits July 18, 2018 20:15
* azure-npm

* set logging file

* parameterize telemetry API

* avoid null ptr derefence

* add telemetry to npm
@saiyan86 saiyan86 merged commit 6a0f9ff into Azure:master Jul 19, 2018
@saiyan86
Copy link
Contributor Author

@ashvindeodhar @jaer-tsun Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants