k8s.io/kubernetes@v1.29.3/test/e2e/network/netpol/policies.go (about)

     1  /*
     2  Copyright 2020 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 netpol
    18  
    19  import (
    20  	networkingv1 "k8s.io/api/networking/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  type SetFunc func(policy *networkingv1.NetworkPolicy)
    25  
    26  func GenNetworkPolicy(fn ...SetFunc) *networkingv1.NetworkPolicy {
    27  	policy := &networkingv1.NetworkPolicy{}
    28  	for _, f := range fn {
    29  		f(policy)
    30  	}
    31  	return policy
    32  }
    33  
    34  func GenNetworkPolicyWithNameAndPodMatchLabel(name string, targetLabels map[string]string, otherFunc ...SetFunc) *networkingv1.NetworkPolicy {
    35  	otherFunc = append(otherFunc, SetObjectMetaName(name), SetSpecPodSelectorMatchLabels(targetLabels))
    36  	return GenNetworkPolicy(otherFunc...)
    37  }
    38  
    39  func GenNetworkPolicyWithNameAndPodSelector(name string, targetSelector metav1.LabelSelector, otherFunc ...SetFunc) *networkingv1.NetworkPolicy {
    40  	otherFunc = append(otherFunc, SetObjectMetaName(name), SetSpecPodSelector(targetSelector))
    41  	return GenNetworkPolicy(otherFunc...)
    42  }
    43  
    44  func SetObjectMetaName(name string) SetFunc {
    45  	return func(policy *networkingv1.NetworkPolicy) {
    46  		policy.ObjectMeta.Name = name
    47  	}
    48  }
    49  
    50  func SetGenerateName(name string) SetFunc {
    51  	return func(policy *networkingv1.NetworkPolicy) {
    52  		policy.ObjectMeta.GenerateName = name
    53  	}
    54  }
    55  
    56  func SetObjectMetaLabel(targetLabels map[string]string) SetFunc {
    57  	return func(policy *networkingv1.NetworkPolicy) {
    58  		policy.ObjectMeta.Labels = targetLabels
    59  	}
    60  }
    61  
    62  func SetSpecPodSelector(targetSelector metav1.LabelSelector) SetFunc {
    63  	return func(policy *networkingv1.NetworkPolicy) {
    64  		policy.Spec.PodSelector = targetSelector
    65  	}
    66  }
    67  
    68  func SetSpecPodSelectorMatchLabels(targetLabels map[string]string) SetFunc {
    69  	return func(policy *networkingv1.NetworkPolicy) {
    70  		policy.Spec.PodSelector = metav1.LabelSelector{
    71  			MatchLabels: targetLabels,
    72  		}
    73  	}
    74  }
    75  
    76  func SetSpecIngressRules(rules ...networkingv1.NetworkPolicyIngressRule) SetFunc {
    77  	return func(policy *networkingv1.NetworkPolicy) {
    78  		if policy.Spec.Ingress == nil {
    79  			policy.Spec.Ingress = []networkingv1.NetworkPolicyIngressRule{}
    80  			policy.Spec.PolicyTypes = append(policy.Spec.PolicyTypes, networkingv1.PolicyTypeIngress)
    81  		}
    82  		policy.Spec.Ingress = append(policy.Spec.Ingress, rules...)
    83  	}
    84  }
    85  
    86  func SetSpecEgressRules(rules ...networkingv1.NetworkPolicyEgressRule) SetFunc {
    87  	return func(policy *networkingv1.NetworkPolicy) {
    88  		if policy.Spec.Egress == nil {
    89  			policy.Spec.Egress = []networkingv1.NetworkPolicyEgressRule{}
    90  			policy.Spec.PolicyTypes = append(policy.Spec.PolicyTypes, networkingv1.PolicyTypeEgress)
    91  		}
    92  		policy.Spec.Egress = append(policy.Spec.Egress, rules...)
    93  	}
    94  }