Coverage for ocp_resources/service.py: 0%

71 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 10:48 +0200

1# Generated using https://github.com/RedHatQE/openshift-python-wrapper/blob/main/scripts/resource/README.md 

2 

3from typing import Any, Dict, List, Optional 

4from ocp_resources.resource import NamespacedResource 

5 

6 

7class Service(NamespacedResource): 

8 """ 

9 Service is a named abstraction of software service (for example, mysql) consisting of local port (for example 3306) that the proxy listens on, and the selector that determines which pods will answer requests sent through the proxy. 

10 """ 

11 

12 api_version: str = NamespacedResource.ApiVersion.V1 

13 

14 def __init__( 

15 self, 

16 allocate_load_balancer_node_ports: Optional[bool] = None, 

17 cluster_ip: Optional[str] = "", 

18 cluster_ips: Optional[List[Any]] = None, 

19 external_ips: Optional[List[Any]] = None, 

20 external_name: Optional[str] = "", 

21 external_traffic_policy: Optional[str] = "", 

22 health_check_node_port: Optional[int] = None, 

23 internal_traffic_policy: Optional[str] = "", 

24 ip_families: Optional[List[Any]] = None, 

25 ip_family_policy: Optional[str] = "", 

26 load_balancer_class: Optional[str] = "", 

27 load_balancer_ip: Optional[str] = "", 

28 load_balancer_source_ranges: Optional[List[Any]] = None, 

29 ports: Optional[List[Any]] = None, 

30 publish_not_ready_addresses: Optional[bool] = None, 

31 selector: Optional[Dict[str, Any]] = None, 

32 session_affinity: Optional[str] = "", 

33 session_affinity_config: Optional[Dict[str, Any]] = None, 

34 traffic_distribution: Optional[str] = "", 

35 type: Optional[str] = "", 

36 **kwargs: Any, 

37 ) -> None: 

38 """ 

39 Args: 

40 allocate_load_balancer_node_ports (bool): allocateLoadBalancerNodePorts defines if NodePorts will be 

41 automatically allocated for services with type LoadBalancer. 

42 Default is "true". It may be set to "false" if the cluster load- 

43 balancer does not rely on NodePorts. If the caller requests 

44 specific NodePorts (by specifying a value), those requests will be 

45 respected, regardless of this field. This field may only be set 

46 for services with type LoadBalancer and will be cleared if the 

47 type is changed to any other type. 

48 

49 cluster_ip (str): clusterIP is the IP address of the service and is usually assigned 

50 randomly. If an address is specified manually, is in-range (as per 

51 system configuration), and is not in use, it will be allocated to 

52 the service; otherwise creation of the service will fail. This 

53 field may not be changed through updates unless the type field is 

54 also being changed to ExternalName (which requires this field to 

55 be blank) or the type field is being changed from ExternalName (in 

56 which case this field may optionally be specified, as describe 

57 above). Valid values are "None", empty string (""), or a valid IP 

58 address. Setting this to "None" makes a "headless service" (no 

59 virtual IP), which is useful when direct endpoint connections are 

60 preferred and proxying is not required. Only applies to types 

61 ClusterIP, NodePort, and LoadBalancer. If this field is specified 

62 when creating a Service of type ExternalName, creation will fail. 

63 This field will be wiped when updating a Service to type 

64 ExternalName. More info: 

65 https://kubernetes.io/docs/concepts/services- 

66 networking/service/#virtual-ips-and-service-proxies 

67 

68 cluster_ips (List[Any]): ClusterIPs is a list of IP addresses assigned to this service, and are 

69 usually assigned randomly. If an address is specified manually, 

70 is in-range (as per system configuration), and is not in use, it 

71 will be allocated to the service; otherwise creation of the 

72 service will fail. This field may not be changed through updates 

73 unless the type field is also being changed to ExternalName (which 

74 requires this field to be empty) or the type field is being 

75 changed from ExternalName (in which case this field may optionally 

76 be specified, as describe above). Valid values are "None", empty 

77 string (""), or a valid IP address. Setting this to "None" makes 

78 a "headless service" (no virtual IP), which is useful when direct 

79 endpoint connections are preferred and proxying is not required. 

80 Only applies to types ClusterIP, NodePort, and LoadBalancer. If 

81 this field is specified when creating a Service of type 

82 ExternalName, creation will fail. This field will be wiped when 

83 updating a Service to type ExternalName. If this field is not 

84 specified, it will be initialized from the clusterIP field. If 

85 this field is specified, clients must ensure that clusterIPs[0] 

86 and clusterIP have the same value. This field may hold a maximum 

87 of two entries (dual-stack IPs, in either order). These IPs must 

88 correspond to the values of the ipFamilies field. Both clusterIPs 

89 and ipFamilies are governed by the ipFamilyPolicy field. More 

90 info: https://kubernetes.io/docs/concepts/services- 

91 networking/service/#virtual-ips-and-service-proxies 

92 

93 external_ips (List[Any]): externalIPs is a list of IP addresses for which nodes in the cluster 

94 will also accept traffic for this service. These IPs are not 

95 managed by Kubernetes. The user is responsible for ensuring that 

96 traffic arrives at a node with this IP. A common example is 

97 external load-balancers that are not part of the Kubernetes 

98 system. 

99 

100 external_name (str): externalName is the external reference that discovery mechanisms will 

101 return as an alias for this service (e.g. a DNS CNAME record). No 

102 proxying will be involved. Must be a lowercase RFC-1123 hostname 

103 (https://tools.ietf.org/html/rfc1123) and requires `type` to be 

104 "ExternalName". 

105 

106 external_traffic_policy (str): externalTrafficPolicy describes how nodes distribute service traffic 

107 they receive on one of the Service's "externally-facing" addresses 

108 (NodePorts, ExternalIPs, and LoadBalancer IPs). If set to "Local", 

109 the proxy will configure the service in a way that assumes that 

110 external load balancers will take care of balancing the service 

111 traffic between nodes, and so each node will deliver traffic only 

112 to the node-local endpoints of the service, without masquerading 

113 the client source IP. (Traffic mistakenly sent to a node with no 

114 endpoints will be dropped.) The default value, "Cluster", uses the 

115 standard behavior of routing to all endpoints evenly (possibly 

116 modified by topology and other features). Note that traffic sent 

117 to an External IP or LoadBalancer IP from within the cluster will 

118 always get "Cluster" semantics, but clients sending to a NodePort 

119 from within the cluster may need to take traffic policy into 

120 account when picking a node. Possible enum values: - `"Cluster"` 

121 routes traffic to all endpoints. - `"Local"` preserves the source 

122 IP of the traffic by routing only to endpoints on the same node as 

123 the traffic was received on (dropping the traffic if there are no 

124 local endpoints). 

125 

126 health_check_node_port (int): healthCheckNodePort specifies the healthcheck nodePort for the 

127 service. This only applies when type is set to LoadBalancer and 

128 externalTrafficPolicy is set to Local. If a value is specified, is 

129 in-range, and is not in use, it will be used. If not specified, a 

130 value will be automatically allocated. External systems (e.g. 

131 load-balancers) can use this port to determine if a given node 

132 holds endpoints for this service or not. If this field is 

133 specified when creating a Service which does not need it, creation 

134 will fail. This field will be wiped when updating a Service to no 

135 longer need it (e.g. changing type). This field cannot be updated 

136 once set. 

137 

138 internal_traffic_policy (str): InternalTrafficPolicy describes how nodes distribute service traffic 

139 they receive on the ClusterIP. If set to "Local", the proxy will 

140 assume that pods only want to talk to endpoints of the service on 

141 the same node as the pod, dropping the traffic if there are no 

142 local endpoints. The default value, "Cluster", uses the standard 

143 behavior of routing to all endpoints evenly (possibly modified by 

144 topology and other features). Possible enum values: - 

145 `"Cluster"` routes traffic to all endpoints. - `"Local"` routes 

146 traffic only to endpoints on the same node as the client pod 

147 (dropping the traffic if there are no local endpoints). 

148 

149 ip_families (List[Any]): IPFamilies is a list of IP families (e.g. IPv4, IPv6) assigned to this 

150 service. This field is usually assigned automatically based on 

151 cluster configuration and the ipFamilyPolicy field. If this field 

152 is specified manually, the requested family is available in the 

153 cluster, and ipFamilyPolicy allows it, it will be used; otherwise 

154 creation of the service will fail. This field is conditionally 

155 mutable: it allows for adding or removing a secondary IP family, 

156 but it does not allow changing the primary IP family of the 

157 Service. Valid values are "IPv4" and "IPv6". This field only 

158 applies to Services of types ClusterIP, NodePort, and 

159 LoadBalancer, and does apply to "headless" services. This field 

160 will be wiped when updating a Service to type ExternalName. This 

161 field may hold a maximum of two entries (dual-stack families, in 

162 either order). These families must correspond to the values of 

163 the clusterIPs field, if specified. Both clusterIPs and ipFamilies 

164 are governed by the ipFamilyPolicy field. 

165 

166 ip_family_policy (str): IPFamilyPolicy represents the dual-stack-ness requested or required by 

167 this Service. If there is no value provided, then this field will 

168 be set to SingleStack. Services can be "SingleStack" (a single IP 

169 family), "PreferDualStack" (two IP families on dual-stack 

170 configured clusters or a single IP family on single-stack 

171 clusters), or "RequireDualStack" (two IP families on dual-stack 

172 configured clusters, otherwise fail). The ipFamilies and 

173 clusterIPs fields depend on the value of this field. This field 

174 will be wiped when updating a service to type ExternalName. 

175 Possible enum values: - `"PreferDualStack"` indicates that this 

176 service prefers dual-stack when the cluster is configured for 

177 dual-stack. If the cluster is not configured for dual-stack the 

178 service will be assigned a single IPFamily. If the IPFamily is not 

179 set in service.spec.ipFamilies then the service will be assigned 

180 the default IPFamily configured on the cluster - 

181 `"RequireDualStack"` indicates that this service requires dual- 

182 stack. Using IPFamilyPolicyRequireDualStack on a single stack 

183 cluster will result in validation errors. The IPFamilies (and 

184 their order) assigned to this service is based on 

185 service.spec.ipFamilies. If service.spec.ipFamilies was not 

186 provided then it will be assigned according to how they are 

187 configured on the cluster. If service.spec.ipFamilies has only one 

188 entry then the alternative IPFamily will be added by apiserver - 

189 `"SingleStack"` indicates that this service is required to have a 

190 single IPFamily. The IPFamily assigned is based on the default 

191 IPFamily used by the cluster or as identified by 

192 service.spec.ipFamilies field 

193 

194 load_balancer_class (str): loadBalancerClass is the class of the load balancer implementation 

195 this Service belongs to. If specified, the value of this field 

196 must be a label-style identifier, with an optional prefix, e.g. 

197 "internal-vip" or "example.com/internal-vip". Unprefixed names are 

198 reserved for end-users. This field can only be set when the 

199 Service type is 'LoadBalancer'. If not set, the default load 

200 balancer implementation is used, today this is typically done 

201 through the cloud provider integration, but should apply for any 

202 default implementation. If set, it is assumed that a load balancer 

203 implementation is watching for Services with a matching class. Any 

204 default load balancer implementation (e.g. cloud providers) should 

205 ignore Services that set this field. This field can only be set 

206 when creating or updating a Service to type 'LoadBalancer'. Once 

207 set, it can not be changed. This field will be wiped when a 

208 service is updated to a non 'LoadBalancer' type. 

209 

210 load_balancer_ip (str): Only applies to Service Type: LoadBalancer. This feature depends on 

211 whether the underlying cloud-provider supports specifying the 

212 loadBalancerIP when a load balancer is created. This field will be 

213 ignored if the cloud-provider does not support the feature. 

214 Deprecated: This field was under-specified and its meaning varies 

215 across implementations. Using it is non-portable and it may not 

216 support dual-stack. Users are encouraged to use implementation- 

217 specific annotations when available. 

218 

219 load_balancer_source_ranges (List[Any]): If specified and supported by the platform, this will restrict traffic 

220 through the cloud-provider load-balancer will be restricted to the 

221 specified client IPs. This field will be ignored if the cloud- 

222 provider does not support the feature." More info: 

223 https://kubernetes.io/docs/tasks/access-application- 

224 cluster/create-external-load-balancer/ 

225 

226 ports (List[Any]): The list of ports that are exposed by this service. More info: 

227 https://kubernetes.io/docs/concepts/services- 

228 networking/service/#virtual-ips-and-service-proxies 

229 

230 publish_not_ready_addresses (bool): publishNotReadyAddresses indicates that any agent which deals with 

231 endpoints for this Service should disregard any indications of 

232 ready/not-ready. The primary use case for setting this field is 

233 for a StatefulSet's Headless Service to propagate SRV DNS records 

234 for its Pods for the purpose of peer discovery. The Kubernetes 

235 controllers that generate Endpoints and EndpointSlice resources 

236 for Services interpret this to mean that all endpoints are 

237 considered "ready" even if the Pods themselves are not. Agents 

238 which consume only Kubernetes generated endpoints through the 

239 Endpoints or EndpointSlice resources can safely assume this 

240 behavior. 

241 

242 selector (Dict[str, Any]): Route service traffic to pods with label keys and values matching this 

243 selector. If empty or not present, the service is assumed to have 

244 an external process managing its endpoints, which Kubernetes will 

245 not modify. Only applies to types ClusterIP, NodePort, and 

246 LoadBalancer. Ignored if type is ExternalName. More info: 

247 https://kubernetes.io/docs/concepts/services-networking/service/ 

248 

249 session_affinity (str): Supports "ClientIP" and "None". Used to maintain session affinity. 

250 Enable client IP based session affinity. Must be ClientIP or None. 

251 Defaults to None. More info: 

252 https://kubernetes.io/docs/concepts/services- 

253 networking/service/#virtual-ips-and-service-proxies Possible enum 

254 values: - `"ClientIP"` is the Client IP based. - `"None"` - no 

255 session affinity. 

256 

257 session_affinity_config (Dict[str, Any]): SessionAffinityConfig represents the configurations of session 

258 affinity. 

259 

260 traffic_distribution (str): TrafficDistribution offers a way to express preferences for how 

261 traffic is distributed to Service endpoints. Implementations can 

262 use this field as a hint, but are not required to guarantee strict 

263 adherence. If the field is not set, the implementation will apply 

264 its default routing strategy. If set to "PreferClose", 

265 implementations should prioritize endpoints that are topologically 

266 close (e.g., same zone). This is an alpha field and requires 

267 enabling ServiceTrafficDistribution feature. 

268 

269 type (str): type determines how the Service is exposed. Defaults to ClusterIP. 

270 Valid options are ExternalName, ClusterIP, NodePort, and 

271 LoadBalancer. "ClusterIP" allocates a cluster-internal IP address 

272 for load-balancing to endpoints. Endpoints are determined by the 

273 selector or if that is not specified, by manual construction of an 

274 Endpoints object or EndpointSlice objects. If clusterIP is "None", 

275 no virtual IP is allocated and the endpoints are published as a 

276 set of endpoints rather than a virtual IP. "NodePort" builds on 

277 ClusterIP and allocates a port on every node which routes to the 

278 same endpoints as the clusterIP. "LoadBalancer" builds on NodePort 

279 and creates an external load-balancer (if supported in the current 

280 cloud) which routes to the same endpoints as the clusterIP. 

281 "ExternalName" aliases this service to the specified externalName. 

282 Several other fields do not apply to ExternalName services. More 

283 info: https://kubernetes.io/docs/concepts/services- 

284 networking/service/#publishing-services-service-types Possible 

285 enum values: - `"ClusterIP"` means a service will only be 

286 accessible inside the cluster, via the cluster IP. - 

287 `"ExternalName"` means a service consists of only a reference to 

288 an external name that kubedns or equivalent will return as a CNAME 

289 record, with no exposing or proxying of any pods involved. - 

290 `"LoadBalancer"` means a service will be exposed via an external 

291 load balancer (if the cloud provider supports it), in addition to 

292 'NodePort' type. - `"NodePort"` means a service will be exposed 

293 on one port of every node, in addition to 'ClusterIP' type. 

294 

295 """ 

296 super().__init__(**kwargs) 

297 

298 self.allocate_load_balancer_node_ports = allocate_load_balancer_node_ports 

299 self.cluster_ip = cluster_ip 

300 self.cluster_ips = cluster_ips 

301 self.external_ips = external_ips 

302 self.external_name = external_name 

303 self.external_traffic_policy = external_traffic_policy 

304 self.health_check_node_port = health_check_node_port 

305 self.internal_traffic_policy = internal_traffic_policy 

306 self.ip_families = ip_families 

307 self.ip_family_policy = ip_family_policy 

308 self.load_balancer_class = load_balancer_class 

309 self.load_balancer_ip = load_balancer_ip 

310 self.load_balancer_source_ranges = load_balancer_source_ranges 

311 self.ports = ports 

312 self.publish_not_ready_addresses = publish_not_ready_addresses 

313 self.selector = selector 

314 self.session_affinity = session_affinity 

315 self.session_affinity_config = session_affinity_config 

316 self.traffic_distribution = traffic_distribution 

317 self.type = type 

318 

319 def to_dict(self) -> None: 

320 super().to_dict() 

321 

322 if not self.kind_dict and not self.yaml_file: 

323 self.res["spec"] = {} 

324 _spec = self.res["spec"] 

325 

326 if self.allocate_load_balancer_node_ports is not None: 

327 _spec["allocateLoadBalancerNodePorts"] = self.allocate_load_balancer_node_ports 

328 

329 if self.cluster_ip: 

330 _spec["clusterIP"] = self.cluster_ip 

331 

332 if self.cluster_ips: 

333 _spec["clusterIPs"] = self.cluster_ips 

334 

335 if self.external_ips: 

336 _spec["externalIPs"] = self.external_ips 

337 

338 if self.external_name: 

339 _spec["externalName"] = self.external_name 

340 

341 if self.external_traffic_policy: 

342 _spec["externalTrafficPolicy"] = self.external_traffic_policy 

343 

344 if self.health_check_node_port: 

345 _spec["healthCheckNodePort"] = self.health_check_node_port 

346 

347 if self.internal_traffic_policy: 

348 _spec["internalTrafficPolicy"] = self.internal_traffic_policy 

349 

350 if self.ip_families: 

351 _spec["ipFamilies"] = self.ip_families 

352 

353 if self.ip_family_policy: 

354 _spec["ipFamilyPolicy"] = self.ip_family_policy 

355 

356 if self.load_balancer_class: 

357 _spec["loadBalancerClass"] = self.load_balancer_class 

358 

359 if self.load_balancer_ip: 

360 _spec["loadBalancerIP"] = self.load_balancer_ip 

361 

362 if self.load_balancer_source_ranges: 

363 _spec["loadBalancerSourceRanges"] = self.load_balancer_source_ranges 

364 

365 if self.ports: 

366 _spec["ports"] = self.ports 

367 

368 if self.publish_not_ready_addresses is not None: 

369 _spec["publishNotReadyAddresses"] = self.publish_not_ready_addresses 

370 

371 if self.selector: 

372 _spec["selector"] = self.selector 

373 

374 if self.session_affinity: 

375 _spec["sessionAffinity"] = self.session_affinity 

376 

377 if self.session_affinity_config: 

378 _spec["sessionAffinityConfig"] = self.session_affinity_config 

379 

380 if self.traffic_distribution: 

381 _spec["trafficDistribution"] = self.traffic_distribution 

382 

383 if self.type: 

384 _spec["type"] = self.type 

385 

386 # End of generated code