Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package v1 |
| 18 | |
| 19 | import ( |
| 20 | "k8s.io/api/core/v1" |
| 21 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | "k8s.io/apimachinery/pkg/util/intstr" |
| 23 | ) |
| 24 | |
| 25 | // +genclient |
| 26 | // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 27 | |
| 28 | // NetworkPolicy describes what network traffic is allowed for a set of Pods |
| 29 | type NetworkPolicy struct { |
| 30 | metav1.TypeMeta `json:",inline"` |
| 31 | // Standard object's metadata. |
| 32 | // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata |
| 33 | // +optional |
| 34 | metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` |
| 35 | |
| 36 | // Specification of the desired behavior for this NetworkPolicy. |
| 37 | // +optional |
| 38 | Spec NetworkPolicySpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"` |
| 39 | } |
| 40 | |
| 41 | // Policy Type string describes the NetworkPolicy type |
| 42 | // This type is beta-level in 1.8 |
| 43 | type PolicyType string |
| 44 | |
| 45 | const ( |
| 46 | // PolicyTypeIngress is a NetworkPolicy that affects ingress traffic on selected pods |
| 47 | PolicyTypeIngress PolicyType = "Ingress" |
| 48 | // PolicyTypeEgress is a NetworkPolicy that affects egress traffic on selected pods |
| 49 | PolicyTypeEgress PolicyType = "Egress" |
| 50 | ) |
| 51 | |
| 52 | // NetworkPolicySpec provides the specification of a NetworkPolicy |
| 53 | type NetworkPolicySpec struct { |
| 54 | // Selects the pods to which this NetworkPolicy object applies. The array of |
| 55 | // ingress rules is applied to any pods selected by this field. Multiple network |
| 56 | // policies can select the same set of pods. In this case, the ingress rules for |
| 57 | // each are combined additively. This field is NOT optional and follows standard |
| 58 | // label selector semantics. An empty podSelector matches all pods in this |
| 59 | // namespace. |
| 60 | PodSelector metav1.LabelSelector `json:"podSelector" protobuf:"bytes,1,opt,name=podSelector"` |
| 61 | |
| 62 | // List of ingress rules to be applied to the selected pods. Traffic is allowed to |
| 63 | // a pod if there are no NetworkPolicies selecting the pod |
| 64 | // (and cluster policy otherwise allows the traffic), OR if the traffic source is |
| 65 | // the pod's local node, OR if the traffic matches at least one ingress rule |
| 66 | // across all of the NetworkPolicy objects whose podSelector matches the pod. If |
| 67 | // this field is empty then this NetworkPolicy does not allow any traffic (and serves |
| 68 | // solely to ensure that the pods it selects are isolated by default) |
| 69 | // +optional |
| 70 | Ingress []NetworkPolicyIngressRule `json:"ingress,omitempty" protobuf:"bytes,2,rep,name=ingress"` |
| 71 | |
| 72 | // List of egress rules to be applied to the selected pods. Outgoing traffic is |
| 73 | // allowed if there are no NetworkPolicies selecting the pod (and cluster policy |
| 74 | // otherwise allows the traffic), OR if the traffic matches at least one egress rule |
| 75 | // across all of the NetworkPolicy objects whose podSelector matches the pod. If |
| 76 | // this field is empty then this NetworkPolicy limits all outgoing traffic (and serves |
| 77 | // solely to ensure that the pods it selects are isolated by default). |
| 78 | // This field is beta-level in 1.8 |
| 79 | // +optional |
| 80 | Egress []NetworkPolicyEgressRule `json:"egress,omitempty" protobuf:"bytes,3,rep,name=egress"` |
| 81 | |
| 82 | // List of rule types that the NetworkPolicy relates to. |
| 83 | // Valid options are "Ingress", "Egress", or "Ingress,Egress". |
| 84 | // If this field is not specified, it will default based on the existence of Ingress or Egress rules; |
| 85 | // policies that contain an Egress section are assumed to affect Egress, and all policies |
| 86 | // (whether or not they contain an Ingress section) are assumed to affect Ingress. |
| 87 | // If you want to write an egress-only policy, you must explicitly specify policyTypes [ "Egress" ]. |
| 88 | // Likewise, if you want to write a policy that specifies that no egress is allowed, |
| 89 | // you must specify a policyTypes value that include "Egress" (since such a policy would not include |
| 90 | // an Egress section and would otherwise default to just [ "Ingress" ]). |
| 91 | // This field is beta-level in 1.8 |
| 92 | // +optional |
| 93 | PolicyTypes []PolicyType `json:"policyTypes,omitempty" protobuf:"bytes,4,rep,name=policyTypes,casttype=PolicyType"` |
| 94 | } |
| 95 | |
| 96 | // NetworkPolicyIngressRule describes a particular set of traffic that is allowed to the pods |
| 97 | // matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and from. |
| 98 | type NetworkPolicyIngressRule struct { |
| 99 | // List of ports which should be made accessible on the pods selected for this |
| 100 | // rule. Each item in this list is combined using a logical OR. If this field is |
| 101 | // empty or missing, this rule matches all ports (traffic not restricted by port). |
| 102 | // If this field is present and contains at least one item, then this rule allows |
| 103 | // traffic only if the traffic matches at least one port in the list. |
| 104 | // +optional |
| 105 | Ports []NetworkPolicyPort `json:"ports,omitempty" protobuf:"bytes,1,rep,name=ports"` |
| 106 | |
| 107 | // List of sources which should be able to access the pods selected for this rule. |
| 108 | // Items in this list are combined using a logical OR operation. If this field is |
| 109 | // empty or missing, this rule matches all sources (traffic not restricted by |
| 110 | // source). If this field is present and contains at least on item, this rule |
| 111 | // allows traffic only if the traffic matches at least one item in the from list. |
| 112 | // +optional |
| 113 | From []NetworkPolicyPeer `json:"from,omitempty" protobuf:"bytes,2,rep,name=from"` |
| 114 | } |
| 115 | |
| 116 | // NetworkPolicyEgressRule describes a particular set of traffic that is allowed out of pods |
| 117 | // matched by a NetworkPolicySpec's podSelector. The traffic must match both ports and to. |
| 118 | // This type is beta-level in 1.8 |
| 119 | type NetworkPolicyEgressRule struct { |
| 120 | // List of destination ports for outgoing traffic. |
| 121 | // Each item in this list is combined using a logical OR. If this field is |
| 122 | // empty or missing, this rule matches all ports (traffic not restricted by port). |
| 123 | // If this field is present and contains at least one item, then this rule allows |
| 124 | // traffic only if the traffic matches at least one port in the list. |
| 125 | // +optional |
| 126 | Ports []NetworkPolicyPort `json:"ports,omitempty" protobuf:"bytes,1,rep,name=ports"` |
| 127 | |
| 128 | // List of destinations for outgoing traffic of pods selected for this rule. |
| 129 | // Items in this list are combined using a logical OR operation. If this field is |
| 130 | // empty or missing, this rule matches all destinations (traffic not restricted by |
| 131 | // destination). If this field is present and contains at least one item, this rule |
| 132 | // allows traffic only if the traffic matches at least one item in the to list. |
| 133 | // +optional |
| 134 | To []NetworkPolicyPeer `json:"to,omitempty" protobuf:"bytes,2,rep,name=to"` |
| 135 | } |
| 136 | |
| 137 | // NetworkPolicyPort describes a port to allow traffic on |
| 138 | type NetworkPolicyPort struct { |
| 139 | // The protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this |
| 140 | // field defaults to TCP. |
| 141 | // +optional |
| 142 | Protocol *v1.Protocol `json:"protocol,omitempty" protobuf:"bytes,1,opt,name=protocol,casttype=k8s.io/api/core/v1.Protocol"` |
| 143 | |
| 144 | // The port on the given protocol. This can either be a numerical or named port on |
| 145 | // a pod. If this field is not provided, this matches all port names and numbers. |
| 146 | // +optional |
| 147 | Port *intstr.IntOrString `json:"port,omitempty" protobuf:"bytes,2,opt,name=port"` |
| 148 | } |
| 149 | |
| 150 | // IPBlock describes a particular CIDR (Ex. "192.168.1.1/24") that is allowed to the pods |
| 151 | // matched by a NetworkPolicySpec's podSelector. The except entry describes CIDRs that should |
| 152 | // not be included within this rule. |
| 153 | type IPBlock struct { |
| 154 | // CIDR is a string representing the IP Block |
| 155 | // Valid examples are "192.168.1.1/24" |
| 156 | CIDR string `json:"cidr" protobuf:"bytes,1,name=cidr"` |
| 157 | // Except is a slice of CIDRs that should not be included within an IP Block |
| 158 | // Valid examples are "192.168.1.1/24" |
| 159 | // Except values will be rejected if they are outside the CIDR range |
| 160 | // +optional |
| 161 | Except []string `json:"except,omitempty" protobuf:"bytes,2,rep,name=except"` |
| 162 | } |
| 163 | |
| 164 | // NetworkPolicyPeer describes a peer to allow traffic from. Only certain combinations of |
| 165 | // fields are allowed |
| 166 | type NetworkPolicyPeer struct { |
| 167 | // This is a label selector which selects Pods. This field follows standard label |
| 168 | // selector semantics; if present but empty, it selects all pods. |
| 169 | // |
| 170 | // If NamespaceSelector is also set, then the NetworkPolicyPeer as a whole selects |
| 171 | // the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. |
| 172 | // Otherwise it selects the Pods matching PodSelector in the policy's own Namespace. |
| 173 | // +optional |
| 174 | PodSelector *metav1.LabelSelector `json:"podSelector,omitempty" protobuf:"bytes,1,opt,name=podSelector"` |
| 175 | |
| 176 | // Selects Namespaces using cluster-scoped labels. This field follows standard label |
| 177 | // selector semantics; if present but empty, it selects all namespaces. |
| 178 | // |
| 179 | // If PodSelector is also set, then the NetworkPolicyPeer as a whole selects |
| 180 | // the Pods matching PodSelector in the Namespaces selected by NamespaceSelector. |
| 181 | // Otherwise it selects all Pods in the Namespaces selected by NamespaceSelector. |
| 182 | // +optional |
| 183 | NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty" protobuf:"bytes,2,opt,name=namespaceSelector"` |
| 184 | |
| 185 | // IPBlock defines policy on a particular IPBlock. If this field is set then |
| 186 | // neither of the other fields can be. |
| 187 | // +optional |
| 188 | IPBlock *IPBlock `json:"ipBlock,omitempty" protobuf:"bytes,3,rep,name=ipBlock"` |
| 189 | } |
| 190 | |
| 191 | // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 192 | |
| 193 | // NetworkPolicyList is a list of NetworkPolicy objects. |
| 194 | type NetworkPolicyList struct { |
| 195 | metav1.TypeMeta `json:",inline"` |
| 196 | // Standard list metadata. |
| 197 | // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata |
| 198 | // +optional |
| 199 | metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` |
| 200 | |
| 201 | // Items is a list of schema objects. |
| 202 | Items []NetworkPolicy `json:"items" protobuf:"bytes,2,rep,name=items"` |
| 203 | } |