|
| 1 | +/* |
| 2 | +Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 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 v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + networking "k8s.io/api/networking/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// ApplicationNetworkPolicySpec defines the desired state of ApplicationNetworkPolicy |
| 25 | +type ApplicationNetworkPolicySpec struct { |
| 26 | + // PodSelector selects the pods to which this ApplicationNetworkPolicy object applies. |
| 27 | + PodSelector metav1.LabelSelector `json:"podSelector"` |
| 28 | + |
| 29 | + // PolicyTypes is a list of rule types that the ApplicationNetworkPolicy relates to. |
| 30 | + // Valid options are ["Ingress"], ["Egress"], or ["Ingress", "Egress"]. |
| 31 | + // If this field is not specified, it will default based on the existence of ingress or egress rules. |
| 32 | + // +optional |
| 33 | + PolicyTypes []networking.PolicyType `json:"policyTypes,omitempty"` |
| 34 | + |
| 35 | + // Ingress is a list of ingress rules to be applied to the selected pods. |
| 36 | + // Traffic is allowed to a pod if there are no ApplicationNetworkPolicies selecting the pod |
| 37 | + // (and cluster policy otherwise allows the traffic), OR if the traffic source is |
| 38 | + // the pod's local node, OR if the traffic matches at least one ingress rule |
| 39 | + // across all of the ApplicationNetworkPolicy objects whose podSelector matches the pod. |
| 40 | + // +optional |
| 41 | + Ingress []networking.NetworkPolicyIngressRule `json:"ingress,omitempty"` |
| 42 | + |
| 43 | + // Egress is a list of egress rules to be applied to the selected pods. Outgoing traffic |
| 44 | + // is allowed if there are no ApplicationNetworkPolicies selecting the pod (and cluster policy |
| 45 | + // otherwise allows the traffic), OR if the traffic matches at least one egress rule |
| 46 | + // across all of the ApplicationNetworkPolicy objects whose podSelector matches the pod. |
| 47 | + // +optional |
| 48 | + Egress []ApplicationNetworkPolicyEgressRule `json:"egress,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +// DomainName describes one or more domain names to be used as a peer. |
| 52 | +// |
| 53 | +// DomainName can be an exact match, or use the wildcard specifier '*' to match |
| 54 | +// one or more labels. |
| 55 | +// |
| 56 | +// '*', the wildcard specifier, matches one or more entire labels. It does not |
| 57 | +// support partial matches. '*' may only be specified as a prefix. |
| 58 | +// |
| 59 | +// Examples: |
| 60 | +// - `kubernetes.io` matches only `kubernetes.io`. |
| 61 | +// It does not match "www.kubernetes.io", "blog.kubernetes.io", |
| 62 | +// "my-kubernetes.io", or "wikipedia.org". |
| 63 | +// - `blog.kubernetes.io` matches only "blog.kubernetes.io". |
| 64 | +// It does not match "www.kubernetes.io" or "kubernetes.io". |
| 65 | +// - `*.kubernetes.io` matches subdomains of kubernetes.io. |
| 66 | +// "www.kubernetes.io", "blog.kubernetes.io", and |
| 67 | +// "latest.blog.kubernetes.io" match, however "kubernetes.io", and |
| 68 | +// "wikipedia.org" do not. |
| 69 | +// |
| 70 | +// +kubebuilder:validation:Pattern=`^(\*\.)?([a-zA-z0-9]([-a-zA-Z0-9_]*[a-zA-Z0-9])?\.)+[a-zA-z0-9]([-a-zA-Z0-9_]*[a-zA-Z0-9])?\.?$` |
| 71 | +type DomainName string |
| 72 | + |
| 73 | +// ApplicationNetworkPolicyPeer describes a peer to allow traffic to/from. |
| 74 | +// Only certain combinations of fields are allowed |
| 75 | +// +kubebuilder:validation:XValidation:rule="!(has(self.ipBlock) && has(self.domainNames))",message="ipBlock and domainNames are mutually exclusive" |
| 76 | +// +kubebuilder:validation:XValidation:rule="!(has(self.podSelector) && has(self.domainNames))",message="podSelector and domainNames are mutually exclusive" |
| 77 | +// +kubebuilder:validation:XValidation:rule="!(has(self.namespaceSelector) && has(self.domainNames))",message="namespaceSelector and domainNames are mutually exclusive" |
| 78 | +type ApplicationNetworkPolicyPeer struct { |
| 79 | + // PodSelector is a label selector which selects pods. This field follows standard label |
| 80 | + // selector semantics; if present but empty, it selects all pods. |
| 81 | + // |
| 82 | + // If namespaceSelector is also set, then the NetworkPolicyPeer as a whole selects |
| 83 | + // the pods matching podSelector in the Namespaces selected by NamespaceSelector. |
| 84 | + // Otherwise it selects the pods matching podSelector in the policy's own namespace. |
| 85 | + // +optional |
| 86 | + PodSelector *metav1.LabelSelector `json:"podSelector,omitempty"` |
| 87 | + |
| 88 | + // NamespaceSelector selects namespaces using cluster-scoped labels. This field follows |
| 89 | + // standard label selector semantics; if present but empty, it selects all namespaces. |
| 90 | + // |
| 91 | + // If podSelector is also set, then the NetworkPolicyPeer as a whole selects |
| 92 | + // the pods matching podSelector in the namespaces selected by namespaceSelector. |
| 93 | + // Otherwise it selects all pods in the namespaces selected by namespaceSelector. |
| 94 | + // +optional |
| 95 | + NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"` |
| 96 | + |
| 97 | + // IPBlock defines policy on a particular IPBlock. If this field is set then |
| 98 | + // neither of the other fields can be. |
| 99 | + // +optional |
| 100 | + IPBlock *networking.IPBlock `json:"ipBlock,omitempty"` |
| 101 | + |
| 102 | + // DomainNames provides a way to specify domain names as peers. |
| 103 | + // |
| 104 | + // DomainNames is only supported for Allow rules. In order to control |
| 105 | + // access, DomainNames Allow rules should be used with a lower priority |
| 106 | + // egress deny -- this allows the admin to maintain an explicit "allowlist" |
| 107 | + // of reachable domains. |
| 108 | + // |
| 109 | + // This field is mutually exclusive with PodSelector, NamespaceSelector, and IPBlock. |
| 110 | + // FQDN rules are ALLOW-only and do not support DENY semantics. |
| 111 | + // |
| 112 | + // +optional |
| 113 | + // +listType=set |
| 114 | + // +kubebuilder:validation:MinItems=1 |
| 115 | + DomainNames []DomainName `json:"domainNames,omitempty"` |
| 116 | +} |
| 117 | + |
| 118 | +// ApplicationNetworkPolicyEgressRule describes a particular set of traffic that is allowed out of pods |
| 119 | +// matched by an ApplicationNetworkPolicySpec's podSelector. The traffic must match both ports and to. |
| 120 | +type ApplicationNetworkPolicyEgressRule struct { |
| 121 | + // Ports is a list of destination ports for outgoing traffic. |
| 122 | + // Each item in this list is combined using a logical OR. If this field is |
| 123 | + // empty or missing, this rule matches all ports (traffic not restricted by port). |
| 124 | + // If this field is present and contains at least one item, then this rule allows |
| 125 | + // traffic only if the traffic matches at least one port in the list. |
| 126 | + // +optional |
| 127 | + Ports []networking.NetworkPolicyPort `json:"ports,omitempty"` |
| 128 | + |
| 129 | + // To is a list of destinations for outgoing traffic of pods selected for this rule. |
| 130 | + // Items in this list are combined using a logical OR operation. If this field is |
| 131 | + // empty or missing, this rule matches all destinations (traffic not restricted by |
| 132 | + // destination). If this field is present and contains at least one item, this rule |
| 133 | + // allows traffic only if the traffic matches at least one item in the to list. |
| 134 | + // +optional |
| 135 | + To []ApplicationNetworkPolicyPeer `json:"to,omitempty"` |
| 136 | +} |
| 137 | + |
| 138 | +// ApplicationNetworkPolicyStatus defines the observed state of ApplicationNetworkPolicy |
| 139 | +type ApplicationNetworkPolicyStatus struct { |
| 140 | + // Conditions represent the latest available observations of the ApplicationNetworkPolicy's current state. |
| 141 | + // +optional |
| 142 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 143 | +} |
| 144 | + |
| 145 | +//+kubebuilder:object:root=true |
| 146 | +//+kubebuilder:subresource:status |
| 147 | +//+kubebuilder:resource:shortName=anp |
| 148 | + |
| 149 | +// ApplicationNetworkPolicy is the Schema for the applicationnetworkpolicies API |
| 150 | +type ApplicationNetworkPolicy struct { |
| 151 | + metav1.TypeMeta `json:",inline"` |
| 152 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 153 | + |
| 154 | + Spec ApplicationNetworkPolicySpec `json:"spec,omitempty"` |
| 155 | + Status ApplicationNetworkPolicyStatus `json:"status,omitempty"` |
| 156 | +} |
| 157 | + |
| 158 | +//+kubebuilder:object:root=true |
| 159 | + |
| 160 | +// ApplicationNetworkPolicyList contains a list of ApplicationNetworkPolicy |
| 161 | +type ApplicationNetworkPolicyList struct { |
| 162 | + metav1.TypeMeta `json:",inline"` |
| 163 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 164 | + Items []ApplicationNetworkPolicy `json:"items"` |
| 165 | +} |
| 166 | + |
| 167 | +func init() { |
| 168 | + SchemeBuilder.Register(&ApplicationNetworkPolicy{}, &ApplicationNetworkPolicyList{}) |
| 169 | +} |
0 commit comments