| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
|
| 14 |
module: k8s_service |
| 15 |
|
| 16 |
short_description: Manage Services on Kubernetes |
| 17 |
|
| 18 |
author: KubeVirt Team (@kubevirt) |
| 19 |
|
| 20 |
description: |
| 21 |
- Use Kubernetes Python SDK to manage Services on Kubernetes |
| 22 |
|
| 23 |
extends_documentation_fragment: |
| 24 |
- kubernetes.core.k8s_auth_options |
| 25 |
- kubernetes.core.k8s_resource_options |
| 26 |
- kubernetes.core.k8s_state_options |
| 27 |
|
| 28 |
options: |
| 29 |
merge_type: |
| 30 |
description: |
| 31 |
- Whether to override the default patch merge approach with a specific type. By default, the strategic |
| 32 |
merge will typically be used. |
| 33 |
- For example, Custom Resource Definitions typically aren't updatable by the usual strategic merge. You may |
| 34 |
want to use C(merge) if you see "strategic merge patch format is not supported" |
| 35 |
- See U(https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment) |
| 36 |
- If more than one C(merge_type) is given, the merge_types will be tried in order |
| 37 |
- This defaults to C(['strategic-merge', 'merge']), which is ideal for using the same parameters |
| 38 |
on resource kinds that combine Custom Resources and built-in resources. |
| 39 |
choices: |
| 40 |
- json |
| 41 |
- merge |
| 42 |
- strategic-merge |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
name: |
| 46 |
description: |
| 47 |
- Use to specify a Service object name. |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
namespace: |
| 51 |
description: |
| 52 |
- Use to specify a Service object namespace. |
| 53 |
required: true |
| 54 |
type: str |
| 55 |
type: |
| 56 |
description: |
| 57 |
- Specifies the type of Service to create. |
| 58 |
- See U(https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types) |
| 59 |
choices: |
| 60 |
- NodePort |
| 61 |
- ClusterIP |
| 62 |
- LoadBalancer |
| 63 |
- ExternalName |
| 64 |
type: str |
| 65 |
ports: |
| 66 |
description: |
| 67 |
- A list of ports to expose. |
| 68 |
- U(https://kubernetes.io/docs/concepts/services-networking/service/#multi-port-services) |
| 69 |
type: list |
| 70 |
elements: dict |
| 71 |
selector: |
| 72 |
description: |
| 73 |
- Label selectors identify objects this Service should apply to. |
| 74 |
- U(https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) |
| 75 |
type: dict |
| 76 |
apply: |
| 77 |
description: |
| 78 |
- C(apply) compares the desired resource definition with the previously supplied resource definition, |
| 79 |
ignoring properties that are automatically generated |
| 80 |
- C(apply) works better with Services than 'force=yes' |
| 81 |
- mutually exclusive with C(merge_type) |
| 82 |
default: False |
| 83 |
type: bool |
| 84 |
|
| 85 |
requirements: |
| 86 |
- python >= 3.9 |
| 87 |
- kubernetes >= 24.2.0 |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Expose https port with ClusterIP |
| 92 |
kubernetes.core.k8s_service: |
| 93 |
state: present |
| 94 |
name: test-https |
| 95 |
namespace: default |
| 96 |
ports: |
| 97 |
- port: 443 |
| 98 |
protocol: TCP |
| 99 |
selector: |
| 100 |
key: special |
| 101 |
|
| 102 |
- name: Expose https port with ClusterIP using spec |
| 103 |
kubernetes.core.k8s_service: |
| 104 |
state: present |
| 105 |
name: test-https |
| 106 |
namespace: default |
| 107 |
inline: |
| 108 |
spec: |
| 109 |
ports: |
| 110 |
- port: 443 |
| 111 |
protocol: TCP |
| 112 |
selector: |
| 113 |
key: special |
| 114 |
""" |
| 115 |
|
| 116 |
RETURN = r""" |
| 117 |
result: |
| 118 |
description: |
| 119 |
- The created, patched, or otherwise present Service object. Will be empty in the case of a deletion. |
| 120 |
returned: success |
| 121 |
type: complex |
| 122 |
contains: |
| 123 |
api_version: |
| 124 |
description: The versioned schema of this representation of an object. |
| 125 |
returned: success |
| 126 |
type: str |
| 127 |
kind: |
| 128 |
description: Always 'Service'. |
| 129 |
returned: success |
| 130 |
type: str |
| 131 |
metadata: |
| 132 |
description: Standard object metadata. Includes name, namespace, annotations, labels, etc. |
| 133 |
returned: success |
| 134 |
type: complex |
| 135 |
spec: |
| 136 |
description: Specific attributes of the object. Will vary based on the I(api_version) and I(kind). |
| 137 |
returned: success |
| 138 |
type: complex |
| 139 |
status: |
| 140 |
description: Current status details for the object. |
| 141 |
returned: success |
| 142 |
type: complex |
| 143 |
""" |
| 144 |
|
| 145 |
import copy |
| 146 |
from collections import defaultdict |
| 147 |
|
| 148 |
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( |
| 149 |
AnsibleModule, |
| 150 |
) |
| 151 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 152 |
AUTH_ARG_SPEC, |
| 153 |
COMMON_ARG_SPEC, |
| 154 |
RESOURCE_ARG_SPEC, |
| 155 |
) |
| 156 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( |
| 157 |
get_api_client, |
| 158 |
) |
| 159 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( |
| 160 |
AnsibleK8SModule, |
| 161 |
) |
| 162 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( |
| 163 |
CoreException, |
| 164 |
) |
| 165 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import ( |
| 166 |
create_definitions, |
| 167 |
) |
| 168 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.runner import ( |
| 169 |
perform_action, |
| 170 |
) |
| 171 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( |
| 172 |
K8sService, |
| 173 |
) |
| 174 |
|
| 175 |
SERVICE_ARG_SPEC = { |
| 176 |
"apply": {"type": "bool", "default": False}, |
| 177 |
"name": {"required": True}, |
| 178 |
"namespace": {"required": True}, |
| 179 |
"merge_type": { |
| 180 |
"type": "list", |
| 181 |
"elements": "str", |
| 182 |
"choices": ["json", "merge", "strategic-merge"], |
| 183 |
}, |
| 184 |
"selector": {"type": "dict"}, |
| 185 |
"type": { |
| 186 |
"type": "str", |
| 187 |
"choices": ["NodePort", "ClusterIP", "LoadBalancer", "ExternalName"], |
| 188 |
}, |
| 189 |
"ports": {"type": "list", "elements": "dict"}, |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
def merge_dicts(x, y): |
| 194 |
for k in set(x.keys()).union(y.keys()): |
| 195 |
if k in x and k in y: |
| 196 |
if isinstance(x[k], dict) and isinstance(y[k], dict): |
| 197 |
yield (k, dict(merge_dicts(x[k], y[k]))) |
| 198 |
else: |
| 199 |
yield (k, y[k] if y[k] else x[k]) |
| 200 |
elif k in x: |
| 201 |
yield (k, x[k]) |
| 202 |
else: |
| 203 |
yield (k, y[k]) |
| 204 |
|
| 205 |
|
| 206 |
def argspec(): |
| 207 |
"""argspec property builder""" |
| 208 |
argument_spec = copy.deepcopy(AUTH_ARG_SPEC) |
| 209 |
argument_spec.update(COMMON_ARG_SPEC) |
| 210 |
argument_spec.update(RESOURCE_ARG_SPEC) |
| 211 |
argument_spec.update(SERVICE_ARG_SPEC) |
| 212 |
return argument_spec |
| 213 |
|
| 214 |
|
| 215 |
def execute_module(svc): |
| 216 |
"""Module execution""" |
| 217 |
module = svc.module |
| 218 |
api_version = "v1" |
| 219 |
selector = module.params.get("selector") |
| 220 |
service_type = module.params.get("type") |
| 221 |
ports = module.params.get("ports") |
| 222 |
|
| 223 |
definition = defaultdict(defaultdict) |
| 224 |
|
| 225 |
definition["kind"] = "Service" |
| 226 |
definition["apiVersion"] = api_version |
| 227 |
|
| 228 |
def_spec = definition["spec"] |
| 229 |
def_spec["type"] = service_type |
| 230 |
def_spec["ports"] = ports |
| 231 |
def_spec["selector"] = selector |
| 232 |
|
| 233 |
def_meta = definition["metadata"] |
| 234 |
def_meta["name"] = module.params.get("name") |
| 235 |
def_meta["namespace"] = module.params.get("namespace") |
| 236 |
|
| 237 |
definitions = create_definitions(module.params) |
| 238 |
|
| 239 |
|
| 240 |
definition = dict(merge_dicts(definitions[0], definition)) |
| 241 |
|
| 242 |
result = perform_action(svc, definition, module.params) |
| 243 |
|
| 244 |
module.exit_json(**result) |
| 245 |
|
| 246 |
|
| 247 |
def main(): |
| 248 |
module = AnsibleK8SModule( |
| 249 |
module_class=AnsibleModule, |
| 250 |
argument_spec=argspec(), |
| 251 |
supports_check_mode=True, |
| 252 |
) |
| 253 |
|
| 254 |
try: |
| 255 |
client = get_api_client(module=module) |
| 256 |
svc = K8sService(client, module) |
| 257 |
execute_module(svc) |
| 258 |
except CoreException as e: |
| 259 |
module.fail_from_exception(e) |
| 260 |
|
| 261 |
|
| 262 |
if __name__ == "__main__": |
| 263 |
main() |