github.com/cilium/cilium@v1.16.2/pkg/bgpv1/test/objects.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package test 5 6 import ( 7 "net/netip" 8 9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "k8s.io/apimachinery/pkg/types" 11 "k8s.io/utils/ptr" 12 13 ipam_types "github.com/cilium/cilium/pkg/ipam/types" 14 v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 15 v2alpha1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1" 16 slim_core_v1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1" 17 slim_meta_v1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1" 18 ) 19 20 var ( 21 // uid is static ID used in tests 22 uid = types.UID("610a54cf-e0e5-4dc6-ace9-0e6ca9a4aaae") 23 ) 24 25 // policyConfig data used to create CiliumBGPPeeringPolicy 26 type policyConfig struct { 27 nodeSelector map[string]slim_meta_v1.MatchLabelsValue 28 virtualRouters []v2alpha1.CiliumBGPVirtualRouter 29 } 30 31 // newPolicyObj created CiliumBGPPeeringPolicy based on passed policy config 32 func newPolicyObj(conf policyConfig) v2alpha1.CiliumBGPPeeringPolicy { 33 policyObj := v2alpha1.CiliumBGPPeeringPolicy{ 34 ObjectMeta: metav1.ObjectMeta{ 35 Name: "policy-1", 36 UID: uid, 37 CreationTimestamp: metav1.Now(), 38 }, 39 } 40 41 if conf.nodeSelector != nil { 42 policyObj.Spec.NodeSelector = &slim_meta_v1.LabelSelector{ 43 MatchLabels: conf.nodeSelector, 44 } 45 } 46 47 if conf.virtualRouters != nil { 48 // Override ConnectRetryTimeSeconds to 1 for all neighbors 49 // unless it is specified explicitly. Otherwise, the default 50 // value of 120 seconds would timeout the tests when the 51 // initial connect fails. 52 for i, vrouter := range conf.virtualRouters { 53 for j, neigh := range vrouter.Neighbors { 54 if neigh.ConnectRetryTimeSeconds == nil { 55 conf.virtualRouters[i].Neighbors[j].ConnectRetryTimeSeconds = ptr.To[int32](1) 56 } 57 } 58 } 59 policyObj.Spec.VirtualRouters = conf.virtualRouters 60 } 61 62 return policyObj 63 } 64 65 // lbSrvConfig contains lb service configuration data 66 type lbSrvConfig struct { 67 name string 68 ingressIP string 69 } 70 71 // newLBServiceObj creates slim_core_v1.Service object based on lbSrvConfig 72 func newLBServiceObj(conf lbSrvConfig) slim_core_v1.Service { 73 srvObj := slim_core_v1.Service{ 74 ObjectMeta: slim_meta_v1.ObjectMeta{ 75 Name: conf.name, 76 }, 77 Spec: slim_core_v1.ServiceSpec{ 78 Type: slim_core_v1.ServiceTypeLoadBalancer, 79 }, 80 } 81 82 srvObj.Status = slim_core_v1.ServiceStatus{ 83 LoadBalancer: slim_core_v1.LoadBalancerStatus{ 84 Ingress: []slim_core_v1.LoadBalancerIngress{{IP: conf.ingressIP}}, 85 }, 86 } 87 88 return srvObj 89 } 90 91 // clusterIPSrvConfig contains ClusterIP service configuration data 92 type clusterIPSrvConfig struct { 93 name string 94 clusterIP string 95 } 96 97 // newClusterIPServiceObj creates slim_core_v1.Service object based on lbSrvConfig 98 func newClusterIPServiceObj(conf clusterIPSrvConfig) slim_core_v1.Service { 99 srvObj := slim_core_v1.Service{ 100 ObjectMeta: slim_meta_v1.ObjectMeta{ 101 Name: conf.name, 102 }, 103 Spec: slim_core_v1.ServiceSpec{ 104 Type: slim_core_v1.ServiceTypeClusterIP, 105 ClusterIP: conf.clusterIP, 106 ClusterIPs: []string{conf.clusterIP}, 107 }, 108 } 109 110 srvObj.Status = slim_core_v1.ServiceStatus{ 111 LoadBalancer: slim_core_v1.LoadBalancerStatus{}, 112 } 113 114 return srvObj 115 } 116 117 // externalIPsSrvConfig contains ExternalIPs service configuration data 118 type externalIPsSrvConfig struct { 119 name string 120 externalIP string 121 } 122 123 // newExternalIPServiceObj creates slim_core_v1.Service object based on externalIPsSrvConfig 124 func newExternalIPServiceObj(conf externalIPsSrvConfig) slim_core_v1.Service { 125 srvObj := slim_core_v1.Service{ 126 ObjectMeta: slim_meta_v1.ObjectMeta{ 127 Name: conf.name, 128 }, 129 Spec: slim_core_v1.ServiceSpec{ 130 Type: slim_core_v1.ServiceTypeClusterIP, 131 ExternalIPs: []string{conf.externalIP}, 132 }, 133 } 134 135 srvObj.Status = slim_core_v1.ServiceStatus{ 136 LoadBalancer: slim_core_v1.LoadBalancerStatus{}, 137 } 138 139 return srvObj 140 } 141 142 // lbSrvConfig contains lb service configuration data 143 type lbPoolConfig struct { 144 name string 145 labels map[string]string 146 cidrs []string 147 } 148 149 // newLBPoolObj creates CiliumLoadBalancerIPPool object based on lbSrvConfig 150 func newLBPoolObj(conf lbPoolConfig) v2alpha1.CiliumLoadBalancerIPPool { 151 obj := v2alpha1.CiliumLoadBalancerIPPool{ 152 ObjectMeta: metav1.ObjectMeta{ 153 Name: conf.name, 154 UID: uid, 155 CreationTimestamp: metav1.Now(), 156 Labels: make(map[string]string), 157 }, 158 } 159 if conf.labels != nil { 160 obj.Labels = conf.labels 161 } 162 for _, cidr := range conf.cidrs { 163 obj.Spec.Blocks = append(obj.Spec.Blocks, v2alpha1.CiliumLoadBalancerIPPoolIPBlock{Cidr: v2alpha1.IPv4orIPv6CIDR(cidr)}) 164 } 165 return obj 166 } 167 168 // ipPoolConfig data used to create a CiliumPodIPPool resource. 169 type ipPoolConfig struct { 170 name string 171 cidrs []ipam_types.IPAMPodCIDR 172 labels map[string]string 173 } 174 175 // newIPPoolObj creates a CiliumPodIPPool resource based on the provided conf. 176 func newIPPoolObj(conf ipPoolConfig) *v2alpha1.CiliumPodIPPool { 177 obj := &v2alpha1.CiliumPodIPPool{ 178 ObjectMeta: metav1.ObjectMeta{ 179 Name: conf.name, 180 UID: uid, 181 CreationTimestamp: metav1.Now(), 182 Labels: make(map[string]string), 183 }, 184 Spec: v2alpha1.IPPoolSpec{ 185 IPv4: &v2alpha1.IPv4PoolSpec{ 186 CIDRs: []v2alpha1.PoolCIDR{}, 187 MaskSize: 24, 188 }, 189 IPv6: &v2alpha1.IPv6PoolSpec{ 190 CIDRs: []v2alpha1.PoolCIDR{}, 191 MaskSize: 64, 192 }, 193 }, 194 } 195 196 if conf.labels != nil { 197 obj.Labels = conf.labels 198 } 199 200 for _, cidr := range conf.cidrs { 201 if p := netip.MustParsePrefix(string(cidr)); p.Addr().Is4() { 202 obj.Spec.IPv4.CIDRs = append(obj.Spec.IPv4.CIDRs, v2alpha1.PoolCIDR(cidr)) 203 } 204 if p := netip.MustParsePrefix(string(cidr)); p.Addr().Is6() { 205 obj.Spec.IPv6.CIDRs = append(obj.Spec.IPv6.CIDRs, v2alpha1.PoolCIDR(cidr)) 206 } 207 } 208 209 return obj 210 } 211 212 // ciliumNodeConfig data used to create a CiliumNode resource. 213 type ciliumNodeConfig struct { 214 name string 215 labels map[string]string 216 annotations map[string]string 217 ipamAllocs map[string][]string 218 } 219 220 // newCiliumNode creates a CiliumNode resource based on the provided conf. 221 func newCiliumNode(conf ciliumNodeConfig) v2.CiliumNode { 222 obj := v2.CiliumNode{ 223 ObjectMeta: metav1.ObjectMeta{ 224 Name: conf.name, 225 UID: uid, 226 CreationTimestamp: metav1.Now(), 227 }, 228 } 229 230 if conf.labels != nil { 231 obj.Labels = conf.labels 232 } 233 234 if conf.annotations != nil { 235 obj.Annotations = conf.annotations 236 } 237 238 if conf.ipamAllocs != nil { 239 var allocs []ipam_types.IPAMPoolAllocation 240 for pool, cidrs := range conf.ipamAllocs { 241 poolCIDRs := []ipam_types.IPAMPodCIDR{} 242 for _, c := range cidrs { 243 poolCIDRs = append(poolCIDRs, ipam_types.IPAMPodCIDR(c)) 244 } 245 alloc := ipam_types.IPAMPoolAllocation{ 246 Pool: pool, 247 CIDRs: poolCIDRs, 248 } 249 allocs = append(allocs, alloc) 250 } 251 obj.Spec.IPAM.Pools.Allocated = allocs 252 } 253 254 return obj 255 }