| 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 |
module: k8s_rollback |
| 14 |
short_description: Rollback Kubernetes (K8S) Deployments and DaemonSets |
| 15 |
version_added: 1.0.0 |
| 16 |
author: |
| 17 |
- "Julien Huon (@julienhuon)" |
| 18 |
description: |
| 19 |
- Use the Kubernetes Python client to perform the Rollback. |
| 20 |
- Authenticate using either a config file, certificates, password or token. |
| 21 |
- Similar to the C(kubectl rollout undo) command. |
| 22 |
options: |
| 23 |
label_selectors: |
| 24 |
description: List of label selectors to use to filter results. |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
default: [] |
| 28 |
field_selectors: |
| 29 |
description: List of field selectors to use to filter results. |
| 30 |
type: list |
| 31 |
elements: str |
| 32 |
default: [] |
| 33 |
extends_documentation_fragment: |
| 34 |
- kubernetes.core.k8s_auth_options |
| 35 |
- kubernetes.core.k8s_name_options |
| 36 |
requirements: |
| 37 |
- "python >= 3.9" |
| 38 |
- "kubernetes >= 24.2.0" |
| 39 |
- "PyYAML >= 3.11" |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = r""" |
| 43 |
- name: Rollback a failed deployment |
| 44 |
kubernetes.core.k8s_rollback: |
| 45 |
api_version: apps/v1 |
| 46 |
kind: Deployment |
| 47 |
name: web |
| 48 |
namespace: testing |
| 49 |
""" |
| 50 |
|
| 51 |
RETURN = r""" |
| 52 |
rollback_info: |
| 53 |
description: |
| 54 |
- The object that was rolled back. |
| 55 |
returned: success |
| 56 |
type: complex |
| 57 |
contains: |
| 58 |
api_version: |
| 59 |
description: The versioned schema of this representation of an object. |
| 60 |
returned: success |
| 61 |
type: str |
| 62 |
code: |
| 63 |
description: The HTTP Code of the response |
| 64 |
returned: success |
| 65 |
type: str |
| 66 |
kind: |
| 67 |
description: Status |
| 68 |
returned: success |
| 69 |
type: str |
| 70 |
metadata: |
| 71 |
description: |
| 72 |
- Standard object metadata. |
| 73 |
- Includes name, namespace, annotations, labels, etc. |
| 74 |
returned: success |
| 75 |
type: dict |
| 76 |
status: |
| 77 |
description: Current status details for the object. |
| 78 |
returned: success |
| 79 |
type: dict |
| 80 |
""" |
| 81 |
|
| 82 |
import copy |
| 83 |
|
| 84 |
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( |
| 85 |
AnsibleModule, |
| 86 |
) |
| 87 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 88 |
AUTH_ARG_SPEC, |
| 89 |
NAME_ARG_SPEC, |
| 90 |
) |
| 91 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( |
| 92 |
get_api_client, |
| 93 |
) |
| 94 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( |
| 95 |
AnsibleK8SModule, |
| 96 |
) |
| 97 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( |
| 98 |
CoreException, |
| 99 |
) |
| 100 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( |
| 101 |
K8sService, |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def get_managed_resource(kind): |
| 106 |
managed_resource = {} |
| 107 |
|
| 108 |
if kind == "DaemonSet": |
| 109 |
managed_resource["kind"] = "ControllerRevision" |
| 110 |
managed_resource["api_version"] = "apps/v1" |
| 111 |
elif kind == "Deployment": |
| 112 |
managed_resource["kind"] = "ReplicaSet" |
| 113 |
managed_resource["api_version"] = "apps/v1" |
| 114 |
else: |
| 115 |
raise CoreException( |
| 116 |
"Cannot perform rollback on resource of kind {0}".format(kind) |
| 117 |
) |
| 118 |
return managed_resource |
| 119 |
|
| 120 |
|
| 121 |
def execute_module(svc): |
| 122 |
results = [] |
| 123 |
module = svc.module |
| 124 |
|
| 125 |
resources = svc.find( |
| 126 |
module.params["kind"], |
| 127 |
module.params["api_version"], |
| 128 |
module.params["name"], |
| 129 |
module.params["namespace"], |
| 130 |
module.params["label_selectors"], |
| 131 |
module.params["field_selectors"], |
| 132 |
) |
| 133 |
|
| 134 |
changed = False |
| 135 |
for resource in resources["resources"]: |
| 136 |
result = perform_action(svc, resource) |
| 137 |
changed = result["changed"] or changed |
| 138 |
results.append(result) |
| 139 |
|
| 140 |
module.exit_json(**{"changed": changed, "rollback_info": results}) |
| 141 |
|
| 142 |
|
| 143 |
def perform_action(svc, resource): |
| 144 |
module = svc.module |
| 145 |
|
| 146 |
if module.params["kind"] == "DaemonSet": |
| 147 |
current_revision = resource["metadata"]["generation"] |
| 148 |
elif module.params["kind"] == "Deployment": |
| 149 |
current_revision = resource["metadata"]["annotations"][ |
| 150 |
"deployment.kubernetes.io/revision" |
| 151 |
] |
| 152 |
|
| 153 |
managed_resource = get_managed_resource(module.params["kind"]) |
| 154 |
managed_resources = svc.find( |
| 155 |
managed_resource["kind"], |
| 156 |
managed_resource["api_version"], |
| 157 |
"", |
| 158 |
module.params["namespace"], |
| 159 |
resource["spec"]["selector"]["matchLabels"], |
| 160 |
"", |
| 161 |
) |
| 162 |
|
| 163 |
prev_managed_resource = get_previous_revision( |
| 164 |
managed_resources["resources"], current_revision |
| 165 |
) |
| 166 |
if not prev_managed_resource: |
| 167 |
warn = "No rollout history found for resource %s/%s" % ( |
| 168 |
module.params["kind"], |
| 169 |
resource["metadata"]["name"], |
| 170 |
) |
| 171 |
if warn: |
| 172 |
module.warn(warn) |
| 173 |
result = {"changed": False} |
| 174 |
return result |
| 175 |
|
| 176 |
if module.params["kind"] == "Deployment": |
| 177 |
del prev_managed_resource["spec"]["template"]["metadata"]["labels"][ |
| 178 |
"pod-template-hash" |
| 179 |
] |
| 180 |
|
| 181 |
resource_patch = [ |
| 182 |
{ |
| 183 |
"op": "replace", |
| 184 |
"path": "/spec/template", |
| 185 |
"value": prev_managed_resource["spec"]["template"], |
| 186 |
}, |
| 187 |
{ |
| 188 |
"op": "replace", |
| 189 |
"path": "/metadata/annotations", |
| 190 |
"value": { |
| 191 |
"deployment.kubernetes.io/revision": prev_managed_resource[ |
| 192 |
"metadata" |
| 193 |
]["annotations"]["deployment.kubernetes.io/revision"] |
| 194 |
}, |
| 195 |
}, |
| 196 |
] |
| 197 |
|
| 198 |
api_target = "deployments" |
| 199 |
content_type = "application/json-patch+json" |
| 200 |
elif module.params["kind"] == "DaemonSet": |
| 201 |
resource_patch = prev_managed_resource["data"] |
| 202 |
|
| 203 |
api_target = "daemonsets" |
| 204 |
content_type = "application/strategic-merge-patch+json" |
| 205 |
|
| 206 |
rollback = resource |
| 207 |
if not module.check_mode: |
| 208 |
rollback = svc.client.client.request( |
| 209 |
"PATCH", |
| 210 |
"/apis/{0}/namespaces/{1}/{2}/{3}".format( |
| 211 |
module.params["api_version"], |
| 212 |
module.params["namespace"], |
| 213 |
api_target, |
| 214 |
module.params["name"], |
| 215 |
), |
| 216 |
body=resource_patch, |
| 217 |
content_type=content_type, |
| 218 |
).to_dict() |
| 219 |
|
| 220 |
result = {"changed": True} |
| 221 |
result["method"] = "patch" |
| 222 |
result["body"] = resource_patch |
| 223 |
result["resources"] = rollback |
| 224 |
return result |
| 225 |
|
| 226 |
|
| 227 |
def argspec(): |
| 228 |
args = copy.deepcopy(AUTH_ARG_SPEC) |
| 229 |
args.update(NAME_ARG_SPEC) |
| 230 |
args.update( |
| 231 |
dict( |
| 232 |
label_selectors=dict(type="list", elements="str", default=[]), |
| 233 |
field_selectors=dict(type="list", elements="str", default=[]), |
| 234 |
) |
| 235 |
) |
| 236 |
return args |
| 237 |
|
| 238 |
|
| 239 |
def get_previous_revision(all_resources, current_revision): |
| 240 |
for resource in all_resources: |
| 241 |
if resource["kind"] == "ReplicaSet": |
| 242 |
if ( |
| 243 |
int( |
| 244 |
resource["metadata"]["annotations"][ |
| 245 |
"deployment.kubernetes.io/revision" |
| 246 |
] |
| 247 |
) |
| 248 |
== int(current_revision) - 1 |
| 249 |
): |
| 250 |
return resource |
| 251 |
elif resource["kind"] == "ControllerRevision": |
| 252 |
if ( |
| 253 |
int( |
| 254 |
resource["metadata"]["annotations"][ |
| 255 |
"deprecated.daemonset.template.generation" |
| 256 |
] |
| 257 |
) |
| 258 |
== int(current_revision) - 1 |
| 259 |
): |
| 260 |
return resource |
| 261 |
return None |
| 262 |
|
| 263 |
|
| 264 |
def main(): |
| 265 |
module = AnsibleK8SModule( |
| 266 |
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True |
| 267 |
) |
| 268 |
|
| 269 |
try: |
| 270 |
client = get_api_client(module=module) |
| 271 |
svc = K8sService(client, module) |
| 272 |
execute_module(svc) |
| 273 |
except CoreException as e: |
| 274 |
module.fail_from_exception(e) |
| 275 |
|
| 276 |
|
| 277 |
if __name__ == "__main__": |
| 278 |
main() |