mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-12-03 12:40:47 +00:00
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:
@@ -3096,13 +3096,63 @@ func describeNetworkPolicy(networkPolicy *networking.NetworkPolicy) (string, err
|
||||
w := NewPrefixWriter(out)
|
||||
w.Write(LEVEL_0, "Name:\t%s\n", networkPolicy.Name)
|
||||
w.Write(LEVEL_0, "Namespace:\t%s\n", networkPolicy.Namespace)
|
||||
w.Write(LEVEL_0, "Created on:\t%s\n", networkPolicy.CreationTimestamp)
|
||||
printLabelsMultiline(w, "Labels", networkPolicy.Labels)
|
||||
printAnnotationsMultiline(w, "Annotations", networkPolicy.Annotations)
|
||||
|
||||
describeNetworkPolicySpec(networkPolicy.Spec, w)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func describeNetworkPolicySpec(nps networking.NetworkPolicySpec, w PrefixWriter) {
|
||||
w.Write(LEVEL_0, "Spec:\n")
|
||||
w.Write(LEVEL_1, "Pod Selector: ")
|
||||
if len(nps.PodSelector.MatchLabels) == 0 && len(nps.PodSelector.MatchExpressions) == 0 {
|
||||
w.Write(LEVEL_2, "<none> (Allowing the specific traffic to all pods in this namespace)\n")
|
||||
} else {
|
||||
w.Write(LEVEL_2, "%s\n", metav1.FormatLabelSelector(&nps.PodSelector))
|
||||
}
|
||||
w.Write(LEVEL_1, "Allowing ingress traffic:\n")
|
||||
printNetworkPolicySpecIngressFrom(nps.Ingress, " ", w)
|
||||
}
|
||||
|
||||
func printNetworkPolicySpecIngressFrom(npirs []networking.NetworkPolicyIngressRule, initialIndent string, w PrefixWriter) {
|
||||
if len(npirs) == 0 {
|
||||
w.WriteLine("<none> (Selected pods are isolated for ingress connectivity)")
|
||||
return
|
||||
}
|
||||
for i, npir := range npirs {
|
||||
if len(npir.Ports) == 0 {
|
||||
w.Write(LEVEL_0, "%s%s\n", initialIndent, "To Port: <any> (traffic allowed to all ports)")
|
||||
} else {
|
||||
for _, port := range npir.Ports {
|
||||
var proto api.Protocol
|
||||
if port.Protocol != nil {
|
||||
proto = *port.Protocol
|
||||
} else {
|
||||
proto = api.ProtocolTCP
|
||||
}
|
||||
w.Write(LEVEL_0, "%s%s: %s/%s\n", initialIndent, "To Port", port.Port, proto)
|
||||
}
|
||||
}
|
||||
if len(npir.From) == 0 {
|
||||
w.Write(LEVEL_0, "%s%s\n", initialIndent, "From: <any> (traffic not restricted by source)")
|
||||
} else {
|
||||
for _, from := range npir.From {
|
||||
w.Write(LEVEL_0, "%s", initialIndent)
|
||||
if from.PodSelector != nil {
|
||||
w.Write(LEVEL_0, "%s: %s\n", "From Pod Selector", metav1.FormatLabelSelector(from.PodSelector))
|
||||
} else if from.NamespaceSelector != nil {
|
||||
w.Write(LEVEL_0, "%s: %s\n", "From Namespace Selector", metav1.FormatLabelSelector(from.NamespaceSelector))
|
||||
}
|
||||
}
|
||||
}
|
||||
if i != len(npirs)-1 {
|
||||
w.Write(LEVEL_0, "%s%s\n", initialIndent, "----------")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type StorageClassDescriber struct {
|
||||
clientset.Interface
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user