|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +// All fields in this package are required unless Explicitly marked optional |
| 15 | +// +kubebuilder:validation:Required |
| 16 | +package v1alpha1 |
| 17 | + |
| 18 | +import ( |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | +) |
| 21 | + |
| 22 | +//+kubebuilder:object:root=true |
| 23 | +//+kubebuilder:subresource:status |
| 24 | + |
| 25 | +// AdminNetworkPolicy is a cluster level resource that is part of the |
| 26 | +// AdminNetworkPolicy API. |
| 27 | +type AdminNetworkPolicy struct { |
| 28 | + metav1.TypeMeta `json:",inline"` |
| 29 | + metav1.ObjectMeta `json:"metadata"` |
| 30 | + |
| 31 | + // Specification of the desired behavior of AdminNetworkPolicy. |
| 32 | + Spec AdminNetworkPolicySpec `json:"spec"` |
| 33 | + |
| 34 | + // Status is the status to be reported by the implementation. |
| 35 | + // +optional |
| 36 | + Status AdminNetworkPolicyStatus `json:"status,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +// AdminNetworkPolicyStatus defines the observed state of AdminNetworkPolicy. |
| 40 | +type AdminNetworkPolicyStatus struct { |
| 41 | + Conditions []metav1.Condition `json:"conditions"` |
| 42 | +} |
| 43 | + |
| 44 | +// AdminNetworkPolicySpec defines the desired state of AdminNetworkPolicy. |
| 45 | +type AdminNetworkPolicySpec struct { |
| 46 | + // Priority is a value from 0 to 1000. Rules with lower priority values have |
| 47 | + // higher precedence, and are checked before rules with higher priority values. |
| 48 | + // All AdminNetworkPolicy rules have higher precedence than NetworkPolicy or |
| 49 | + // BaselineAdminNetworkPolicy rules |
| 50 | + // +kubebuilder:validation:Minimum=0 |
| 51 | + // +kubebuilder:validation:Maximum=1000 |
| 52 | + Priority int32 `json:"priority"` |
| 53 | + |
| 54 | + // Subject defines the pods to which this AdminNetworkPolicy applies. |
| 55 | + Subject AdminNetworkPolicySubject `json:"subject"` |
| 56 | + |
| 57 | + // Ingress is the list of Ingress rules to be applied to the selected pods. |
| 58 | + // A total of 100 rules will be allowed in each ANP instance. ANPs with no |
| 59 | + // ingress rules do not affect ingress traffic. |
| 60 | + // +optional |
| 61 | + // +kubebuilder:validation:MaxItems=100 |
| 62 | + Ingress []AdminNetworkPolicyIngressRule `json:"ingress,omitempty"` |
| 63 | + |
| 64 | + // Egress is the list of Egress rules to be applied to the selected pods. |
| 65 | + // A total of 100 rules will be allowed in each ANP instance. ANPs with no |
| 66 | + // egress rules do not affect egress traffic. |
| 67 | + // +optional |
| 68 | + // +kubebuilder:validation:MaxItems=100 |
| 69 | + Egress []AdminNetworkPolicyEgressRule `json:"egress,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +// AdminNetworkPolicyIngressRule describes an action to take on a particular |
| 73 | +// set of traffic destined for pods selected by an AdminNetworkPolicy's |
| 74 | +// Subject field. |
| 75 | +type AdminNetworkPolicyIngressRule struct { |
| 76 | + // Name is an identifier for this rule, that may be no more than 100 characters |
| 77 | + // in length. This field should be used by the implementation to help |
| 78 | + // improve observability, readability and error-reporting for any applied |
| 79 | + // AdminNetworkPolicies. |
| 80 | + // +optional |
| 81 | + // +kubebuilder:validation:MaxLength=100 |
| 82 | + Name string `json:"name,omitempty"` |
| 83 | + |
| 84 | + // Action specifies the effect this rule will have on matching traffic. |
| 85 | + // Currently the following actions are supported: |
| 86 | + // Allow: allows the selected traffic (even if it would otherwise have been denied by NetworkPolicy) |
| 87 | + // Deny: denies the selected traffic |
| 88 | + // Pass: instructs the selected traffic to skip any remaining ANP rules, and |
| 89 | + // then pass execution to any NetworkPolicies that select the pod. |
| 90 | + // If the pod is not selected by any NetworkPolicies then execution |
| 91 | + // is passed to any BaselineAdminNetworkPolicies that select the pod. |
| 92 | + Action AdminNetworkPolicyRuleAction `json:"action"` |
| 93 | + |
| 94 | + // From is the list of sources whose traffic this rule applies to. |
| 95 | + // If any AdminNetworkPolicyPeer matches the source of incoming |
| 96 | + // traffic then the specified action is applied. |
| 97 | + // This field must be defined and contain at least one item. |
| 98 | + // +kubebuilder:validation:MinItems=1 |
| 99 | + // +kubebuilder:validation:MaxItems=100 |
| 100 | + From []AdminNetworkPolicyPeer `json:"from"` |
| 101 | + |
| 102 | + // Ports allows for matching traffic based on port and protocols. |
| 103 | + // If Ports is not set then the rule does not filter traffic via port. |
| 104 | + // +optional |
| 105 | + // +kubebuilder:validation:MaxItems=100 |
| 106 | + Ports *[]AdminNetworkPolicyPort `json:"ports,omitempty"` |
| 107 | +} |
| 108 | + |
| 109 | +// AdminNetworkPolicyEgressRule describes an action to take on a particular |
| 110 | +// set of traffic originating from pods selected by a AdminNetworkPolicy's |
| 111 | +// Subject field. |
| 112 | +type AdminNetworkPolicyEgressRule struct { |
| 113 | + // Name is an identifier for this rule, that may be no more than 100 characters |
| 114 | + // in length. This field should be used by the implementation to help |
| 115 | + // improve observability, readability and error-reporting for any applied |
| 116 | + // AdminNetworkPolicies. |
| 117 | + // +optional |
| 118 | + // +kubebuilder:validation:MaxLength=100 |
| 119 | + Name string `json:"name,omitempty"` |
| 120 | + |
| 121 | + // Action specifies the effect this rule will have on matching traffic. |
| 122 | + // Currently the following actions are supported: |
| 123 | + // Allow: allows the selected traffic (even if it would otherwise have been denied by NetworkPolicy) |
| 124 | + // Deny: denies the selected traffic |
| 125 | + // Pass: instructs the selected traffic to skip any remaining ANP rules, and |
| 126 | + // then pass execution to any NetworkPolicies that select the pod. |
| 127 | + // If the pod is not selected by any NetworkPolicies then execution |
| 128 | + // is passed to any BaselineAdminNetworkPolicies that select the pod. |
| 129 | + Action AdminNetworkPolicyRuleAction `json:"action"` |
| 130 | + |
| 131 | + // To is the List of destinations whose traffic this rule applies to. |
| 132 | + // If any AdminNetworkPolicyPeer matches the destination of outgoing |
| 133 | + // traffic then the specified action is applied. |
| 134 | + // This field must be defined and contain at least one item. |
| 135 | + // +kubebuilder:validation:MinItems=1 |
| 136 | + // +kubebuilder:validation:MaxItems=100 |
| 137 | + To []AdminNetworkPolicyPeer `json:"to"` |
| 138 | + |
| 139 | + // Ports allows for matching traffic based on port and protocols. |
| 140 | + // If Ports is not set then the rule does not filter traffic via port. |
| 141 | + // +optional |
| 142 | + // +kubebuilder:validation:MaxItems=100 |
| 143 | + Ports *[]AdminNetworkPolicyPort `json:"ports,omitempty"` |
| 144 | +} |
| 145 | + |
| 146 | +// AdminNetworkPolicyRuleAction string describes the AdminNetworkPolicy action type. |
| 147 | +// +enum |
| 148 | +type AdminNetworkPolicyRuleAction string |
| 149 | + |
| 150 | +const ( |
| 151 | + // AdminNetworkPolicyRuleActionAllow indicates that matching traffic will be |
| 152 | + // allowed regardless of NetworkPolicy and BaselineAdminNetworkPolicy |
| 153 | + // rules. Users cannot block traffic which has been matched by an "Allow" |
| 154 | + // rule in an AdminNetworkPolicy. |
| 155 | + AdminNetworkPolicyRuleActionAllow AdminNetworkPolicyRuleAction = "Allow" |
| 156 | + // AdminNetworkPolicyRuleActionDeny indicates that matching traffic will be |
| 157 | + // denied before being checked against NetworkPolicy or |
| 158 | + // BaselineAdminNetworkPolicy rules. Pods will never receive traffic which |
| 159 | + // has been matched by a "Deny" rule in an AdminNetworkPolicy. |
| 160 | + AdminNetworkPolicyRuleActionDeny AdminNetworkPolicyRuleAction = "Deny" |
| 161 | + // AdminNetworkPolicyRuleActionPass indicates that matching traffic will |
| 162 | + // bypass further AdminNetworkPolicy processing (ignoring rules with lower |
| 163 | + // precedence) and be allowed or denied based on NetworkPolicy and |
| 164 | + // BaselineAdminNetworkPolicy rules. |
| 165 | + AdminNetworkPolicyRuleActionPass AdminNetworkPolicyRuleAction = "Pass" |
| 166 | +) |
| 167 | + |
| 168 | +//+kubebuilder:object:root=true |
| 169 | + |
| 170 | +// AdminNetworkPolicyList contains a list of AdminNetworkPolicy |
| 171 | +type AdminNetworkPolicyList struct { |
| 172 | + metav1.TypeMeta `json:",inline"` |
| 173 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 174 | + Items []AdminNetworkPolicy `json:"items"` |
| 175 | +} |
| 176 | + |
| 177 | +func init() { |
| 178 | + SchemeBuilder.Register(&AdminNetworkPolicy{}, &AdminNetworkPolicyList{}) |
| 179 | +} |
0 commit comments