Merge pull request #46951 from aanm/kubectl-describe-netpol

Automatic merge from submit-queue (batch tested with PRs 54761, 54748, 53991, 54485, 46951). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Enhanced the network policy describer.

**Which issue this PR fixes**: Fixes https://github.com/kubernetes/kubectl/issues/17

**Special notes for your reviewer**: I need help to set up the right clientset for the unt tests

@kubernetes/sig-network-pr-reviews 

ping @adohe 

As suggested in https://github.com/kubernetes/kubectl/issues/17 , the output is similar to:
```
Name:           access-backend
Namespace:      default
Created on:     2017-06-04 21:45:56 -0700 PDT
Labels:         <none>
Annotations:    <none>
Spec:
  Pod Selector:     foo in (bar1,bar2),foo2 notin (bar1,bar2),id=app1,id2=app3
  Allowing ingress traffic:
    To Port: 80/TCP
    To Port: 82/TCP
    From Pod Selector: id=app2,id2=app3
    From Namespace Selector: id=app2,id2=app3
    From Namespace Selector: foo in (bar1,bar2),id=app2,id2=app3
    ----------
    To Port: <any> (traffic allowed to all ports)
    From: <any> (traffic not restricted by source)
```
This commit is contained in:
Kubernetes Submit Queue
2017-10-30 15:38:36 -07:00
committed by GitHub
3 changed files with 147 additions and 1 deletions

View File

@@ -36,6 +36,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/autoscaling"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/apis/networking"
"k8s.io/kubernetes/pkg/apis/policy"
"k8s.io/kubernetes/pkg/apis/storage"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
@@ -1645,6 +1646,100 @@ func TestDescribeResourceQuota(t *testing.T) {
}
}
func TestDescribeNetworkPolicies(t *testing.T) {
expectedTime, err := time.Parse("2006-01-02 15:04:05 Z0700 MST", "2017-06-04 21:45:56 -0700 PDT")
if err != nil {
t.Errorf("unable to parse time %q error: %s", "2017-06-04 21:45:56 -0700 PDT", err)
}
expectedOut := `Name: network-policy-1
Namespace: default
Created on: 2017-06-04 21:45:56 -0700 PDT
Labels: <none>
Annotations: <none>
Spec:
Pod Selector: foo in (bar1,bar2),foo2 notin (bar1,bar2),id1=app1,id2=app2
Allowing ingress traffic:
To Port: 80/TCP
To Port: 82/TCP
From Pod Selector: id=app2,id2=app3
From Namespace Selector: id=app2,id2=app3
From Namespace Selector: foo in (bar1,bar2),id=app2,id2=app3
----------
To Port: <any> (traffic allowed to all ports)
From: <any> (traffic not restricted by source)
`
port80 := intstr.FromInt(80)
port82 := intstr.FromInt(82)
protoTCP := api.ProtocolTCP
versionedFake := fake.NewSimpleClientset(&networking.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "network-policy-1",
Namespace: "default",
CreationTimestamp: metav1.NewTime(expectedTime),
},
Spec: networking.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"id1": "app1",
"id2": "app2",
},
MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "foo", Operator: "In", Values: []string{"bar1", "bar2"}},
{Key: "foo2", Operator: "NotIn", Values: []string{"bar1", "bar2"}},
},
},
Ingress: []networking.NetworkPolicyIngressRule{
{
Ports: []networking.NetworkPolicyPort{
{Port: &port80},
{Port: &port82, Protocol: &protoTCP},
},
From: []networking.NetworkPolicyPeer{
{
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"id": "app2",
"id2": "app3",
},
},
},
{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"id": "app2",
"id2": "app3",
},
},
},
{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"id": "app2",
"id2": "app3",
},
MatchExpressions: []metav1.LabelSelectorRequirement{
{Key: "foo", Operator: "In", Values: []string{"bar1", "bar2"}},
},
},
},
},
},
{},
},
},
})
d := NetworkPolicyDescriber{versionedFake}
out, err := d.Describe("", "network-policy-1", printers.DescriberSettings{})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if out != expectedOut {
t.Errorf("want:\n%s\ngot:\n%s", expectedOut, out)
}
}
func TestDescribeServiceAccount(t *testing.T) {
fake := fake.NewSimpleClientset(&api.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{