Merge pull request #123316 from jcaamano/describe-port-range
Describe NetworkPolicy port ranges
This commit is contained in:
		@@ -4576,7 +4576,11 @@ func printNetworkPolicySpecIngressFrom(npirs []networkingv1.NetworkPolicyIngress
 | 
				
			|||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					proto = corev1.ProtocolTCP
 | 
										proto = corev1.ProtocolTCP
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				w.Write(LEVEL_0, "%s%s: %s/%s\n", initialIndent, "To Port", port.Port, proto)
 | 
									if port.EndPort == nil {
 | 
				
			||||||
 | 
										w.Write(LEVEL_0, "%s%s: %s/%s\n", initialIndent, "To Port", port.Port, proto)
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										w.Write(LEVEL_0, "%s%s: %s-%d/%s\n", initialIndent, "To Port Range", port.Port, *port.EndPort, proto)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if len(npir.From) == 0 {
 | 
							if len(npir.From) == 0 {
 | 
				
			||||||
@@ -4620,7 +4624,11 @@ func printNetworkPolicySpecEgressTo(npers []networkingv1.NetworkPolicyEgressRule
 | 
				
			|||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					proto = corev1.ProtocolTCP
 | 
										proto = corev1.ProtocolTCP
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				w.Write(LEVEL_0, "%s%s: %s/%s\n", initialIndent, "To Port", port.Port, proto)
 | 
									if port.EndPort == nil {
 | 
				
			||||||
 | 
										w.Write(LEVEL_0, "%s%s: %s/%s\n", initialIndent, "To Port", port.Port, proto)
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										w.Write(LEVEL_0, "%s%s: %s-%d/%s\n", initialIndent, "To Port Range", port.Port, *port.EndPort, proto)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if len(nper.To) == 0 {
 | 
							if len(nper.To) == 0 {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5483,6 +5483,205 @@ Spec:
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestDescribeNetworkPoliciesWithPortRange(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:
 | 
				
			||||||
 | 
					  PodSelector:     foo in (bar1,bar2),foo2 notin (bar1,bar2),id1=app1,id2=app2
 | 
				
			||||||
 | 
					  Allowing ingress traffic:
 | 
				
			||||||
 | 
					    To Port Range: 80-82/TCP
 | 
				
			||||||
 | 
					    From:
 | 
				
			||||||
 | 
					      NamespaceSelector: id=ns1,id2=ns2
 | 
				
			||||||
 | 
					      PodSelector: id=pod1,id2=pod2
 | 
				
			||||||
 | 
					    From:
 | 
				
			||||||
 | 
					      PodSelector: id=app2,id2=app3
 | 
				
			||||||
 | 
					    From:
 | 
				
			||||||
 | 
					      NamespaceSelector: id=app2,id2=app3
 | 
				
			||||||
 | 
					    From:
 | 
				
			||||||
 | 
					      NamespaceSelector: foo in (bar1,bar2),id=app2,id2=app3
 | 
				
			||||||
 | 
					    From:
 | 
				
			||||||
 | 
					      IPBlock:
 | 
				
			||||||
 | 
					        CIDR: 192.168.0.0/16
 | 
				
			||||||
 | 
					        Except: 192.168.3.0/24, 192.168.4.0/24
 | 
				
			||||||
 | 
					    ----------
 | 
				
			||||||
 | 
					    To Port: <any> (traffic allowed to all ports)
 | 
				
			||||||
 | 
					    From: <any> (traffic not restricted by source)
 | 
				
			||||||
 | 
					  Allowing egress traffic:
 | 
				
			||||||
 | 
					    To Port Range: 80-82/TCP
 | 
				
			||||||
 | 
					    To:
 | 
				
			||||||
 | 
					      NamespaceSelector: id=ns1,id2=ns2
 | 
				
			||||||
 | 
					      PodSelector: id=pod1,id2=pod2
 | 
				
			||||||
 | 
					    To:
 | 
				
			||||||
 | 
					      PodSelector: id=app2,id2=app3
 | 
				
			||||||
 | 
					    To:
 | 
				
			||||||
 | 
					      NamespaceSelector: id=app2,id2=app3
 | 
				
			||||||
 | 
					    To:
 | 
				
			||||||
 | 
					      NamespaceSelector: foo in (bar1,bar2),id=app2,id2=app3
 | 
				
			||||||
 | 
					    To:
 | 
				
			||||||
 | 
					      IPBlock:
 | 
				
			||||||
 | 
					        CIDR: 192.168.0.0/16
 | 
				
			||||||
 | 
					        Except: 192.168.3.0/24, 192.168.4.0/24
 | 
				
			||||||
 | 
					    ----------
 | 
				
			||||||
 | 
					    To Port: <any> (traffic allowed to all ports)
 | 
				
			||||||
 | 
					    To: <any> (traffic not restricted by destination)
 | 
				
			||||||
 | 
					  Policy Types: Ingress, Egress
 | 
				
			||||||
 | 
					`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						port80 := intstr.FromInt(80)
 | 
				
			||||||
 | 
						port82 := int32(82)
 | 
				
			||||||
 | 
						protoTCP := corev1.ProtocolTCP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						versionedFake := fake.NewSimpleClientset(&networkingv1.NetworkPolicy{
 | 
				
			||||||
 | 
							ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
 | 
								Name:              "network-policy-1",
 | 
				
			||||||
 | 
								Namespace:         "default",
 | 
				
			||||||
 | 
								CreationTimestamp: metav1.NewTime(expectedTime),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							Spec: networkingv1.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: []networkingv1.NetworkPolicyIngressRule{
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										Ports: []networkingv1.NetworkPolicyPort{
 | 
				
			||||||
 | 
											{Port: &port80, EndPort: &port82, Protocol: &protoTCP},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										From: []networkingv1.NetworkPolicyPeer{
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												PodSelector: &metav1.LabelSelector{
 | 
				
			||||||
 | 
													MatchLabels: map[string]string{
 | 
				
			||||||
 | 
														"id":  "pod1",
 | 
				
			||||||
 | 
														"id2": "pod2",
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
												NamespaceSelector: &metav1.LabelSelector{
 | 
				
			||||||
 | 
													MatchLabels: map[string]string{
 | 
				
			||||||
 | 
														"id":  "ns1",
 | 
				
			||||||
 | 
														"id2": "ns2",
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												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"}},
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												IPBlock: &networkingv1.IPBlock{
 | 
				
			||||||
 | 
													CIDR:   "192.168.0.0/16",
 | 
				
			||||||
 | 
													Except: []string{"192.168.3.0/24", "192.168.4.0/24"},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									{},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								Egress: []networkingv1.NetworkPolicyEgressRule{
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										Ports: []networkingv1.NetworkPolicyPort{
 | 
				
			||||||
 | 
											{Port: &port80, EndPort: &port82, Protocol: &protoTCP},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										To: []networkingv1.NetworkPolicyPeer{
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												PodSelector: &metav1.LabelSelector{
 | 
				
			||||||
 | 
													MatchLabels: map[string]string{
 | 
				
			||||||
 | 
														"id":  "pod1",
 | 
				
			||||||
 | 
														"id2": "pod2",
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
												NamespaceSelector: &metav1.LabelSelector{
 | 
				
			||||||
 | 
													MatchLabels: map[string]string{
 | 
				
			||||||
 | 
														"id":  "ns1",
 | 
				
			||||||
 | 
														"id2": "ns2",
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												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"}},
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												IPBlock: &networkingv1.IPBlock{
 | 
				
			||||||
 | 
													CIDR:   "192.168.0.0/16",
 | 
				
			||||||
 | 
													Except: []string{"192.168.3.0/24", "192.168.4.0/24"},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									{},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						d := NetworkPolicyDescriber{versionedFake}
 | 
				
			||||||
 | 
						out, err := d.Describe("default", "network-policy-1", 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) {
 | 
					func TestDescribeServiceAccount(t *testing.T) {
 | 
				
			||||||
	fake := fake.NewSimpleClientset(&corev1.ServiceAccount{
 | 
						fake := fake.NewSimpleClientset(&corev1.ServiceAccount{
 | 
				
			||||||
		ObjectMeta: metav1.ObjectMeta{
 | 
							ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user