mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-03-27 05:43:02 +00:00
This is a backport of PR #1053 as merged into main (452fb3d).
SUMMARY
Importing from ansible.module_utils._text is deprecated in ansible-core 2.20 and removed in 2.24. All imports of to_bytes, to_native, and to_text now use ansible.module_utils.common.text.converters.
Before:
from ansible.module_utils._text import to_bytes, to_native, to_text
After:
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
ISSUE TYPE
Bugfix Pull Request
COMPONENT NAME
plugins/module_utils/common.py
plugins/action/k8s_info.py
plugins/connection/kubectl.py
plugins/module_utils/{copy.py, k8s/runner.py}
plugins/modules/{k8s_cp.py, k8s_drain.py, k8s_exec.py, k8s_json_patch.py, k8s_scale.py, k8s_taint.py}
ADDITIONAL INFORMATION
It's not an actual Bugfix, more a lifecycle management to ensure compatibility with future Ansible versions.
Tested with ansible-core 2.20 to ensure no deprecation warnings are raised and with ansible-core 2.16 to ensure backward compatibility.
Patrially coauthored-by: GitHub Copilot with Claude Code 4.5 model.
Addresses issue #1052.
Reviewed-by: Bianca Henderson <beeankha@gmail.com>
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
# Copyright 2018 Red Hat | Ansible
|
|
#
|
|
# This file is part of Ansible
|
|
#
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
import base64
|
|
import os
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
|
|
from ansible.module_utils.urls import Request
|
|
|
|
try:
|
|
import urllib3
|
|
|
|
urllib3.disable_warnings()
|
|
except ImportError:
|
|
pass
|
|
|
|
|
|
def fetch_file_from_url(module, url):
|
|
# Download file
|
|
bufsize = 65536
|
|
file_name, file_ext = os.path.splitext(str(url.rsplit("/", 1)[1]))
|
|
temp_file = NamedTemporaryFile(
|
|
dir=module.tmpdir, prefix=file_name, suffix=file_ext, delete=False
|
|
)
|
|
module.add_cleanup_file(temp_file.name)
|
|
try:
|
|
rsp = Request().open("GET", url)
|
|
if not rsp:
|
|
module.fail_json(msg="Failure downloading %s" % url)
|
|
data = rsp.read(bufsize)
|
|
while data:
|
|
temp_file.write(data)
|
|
data = rsp.read(bufsize)
|
|
temp_file.close()
|
|
except Exception as e:
|
|
module.fail_json(msg="Failure downloading %s, %s" % (url, to_native(e)))
|
|
return temp_file.name
|
|
|
|
|
|
def _encode_stringdata(definition):
|
|
if definition["kind"] == "Secret" and "stringData" in definition:
|
|
for k, v in definition["stringData"].items():
|
|
encoded = base64.b64encode(to_bytes(v))
|
|
definition.setdefault("data", {})[k] = to_text(encoded)
|
|
del definition["stringData"]
|
|
return definition
|