Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Deprecate Kubernetes modules for move to saltext-kubernetes #65566

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/65565.deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate Kubernetes modules for move to saltext-kubernetes in version 3009
58 changes: 32 additions & 26 deletions salt/modules/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
import salt.utils.http as http
import salt.utils.json

__deprecated__ = (
3009,
"kubernetes",
"https://github.com/salt-extensions/saltext-kubernetes",
)

__virtualname__ = "k8s"

# Setup the logger
Expand Down Expand Up @@ -174,14 +180,14 @@ def _guess_node_id(node):
def _get_labels(node, apiserver_url):
"""Get all labels from a kube node."""
# Prepare URL
url = "{}/api/v1/nodes/{}".format(apiserver_url, node)
url = f"{apiserver_url}/api/v1/nodes/{node}"
# Make request
ret = http.query(url)
# Check requests status
if "body" in ret:
ret = salt.utils.json.loads(ret.get("body"))
elif ret.get("status", 0) == 404:
return "Node {} doesn't exist".format(node)
return f"Node {node} doesn't exist"
else:
return ret
# Get and return labels
Expand All @@ -191,13 +197,13 @@ def _get_labels(node, apiserver_url):
def _set_labels(node, apiserver_url, labels):
"""Replace labels dict by a new one"""
# Prepare URL
url = "{}/api/v1/nodes/{}".format(apiserver_url, node)
url = f"{apiserver_url}/api/v1/nodes/{node}"
# Prepare data
data = [{"op": "replace", "path": "/metadata/labels", "value": labels}]
# Make request
ret = _kpatch(url, data)
if ret.get("status") == 404:
return "Node {} doesn't exist".format(node)
return f"Node {node} doesn't exist"
return ret


Expand Down Expand Up @@ -264,9 +270,9 @@ def label_present(name, value, node=None, apiserver_url=None):
# there is an update during operation, need to retry
log.debug("Got 409, will try later")
ret["changes"] = {}
ret["comment"] = "Could not create label {}, please retry".format(name)
ret["comment"] = f"Could not create label {name}, please retry"
else:
ret["comment"] = "Label {} created".format(name)
ret["comment"] = f"Label {name} created"
elif labels.get(name) != str(value):
# This is a old label and we are going to edit it
ret["changes"] = {name: str(value)}
Expand All @@ -276,12 +282,12 @@ def label_present(name, value, node=None, apiserver_url=None):
# there is an update during operation, need to retry
log.debug("Got 409, will try later")
ret["changes"] = {}
ret["comment"] = "Could not update label {}, please retry".format(name)
ret["comment"] = f"Could not update label {name}, please retry"
else:
ret["comment"] = "Label {} updated".format(name)
ret["comment"] = f"Label {name} updated"
else:
# This is a old label and it has already the wanted value
ret["comment"] = "Label {} already set".format(name)
ret["comment"] = f"Label {name} already set"

return ret

Expand Down Expand Up @@ -316,18 +322,18 @@ def label_absent(name, node=None, apiserver_url=None):
# Compare old labels and what we want
if labels == old_labels:
# Label already absent
ret["comment"] = "Label {} already absent".format(name)
ret["comment"] = f"Label {name} already absent"
else:
# Label needs to be delete
res = _set_labels(node, apiserver_url, labels)
if res.get("status") == 409:
# there is an update during operation, need to retry
log.debug("Got 409, will try later")
ret["changes"] = {}
ret["comment"] = "Could not delete label {}, please retry".format(name)
ret["comment"] = f"Could not delete label {name}, please retry"
else:
ret["changes"] = {"deleted": name}
ret["comment"] = "Label {} absent".format(name)
ret["comment"] = f"Label {name} absent"

return ret

Expand Down Expand Up @@ -365,7 +371,7 @@ def label_folder_absent(name, node=None, apiserver_url=None):
# Prepare a temp labels dict
if labels == old_labels:
# Label already absent
ret["comment"] = "Label folder {} already absent".format(folder)
ret["comment"] = f"Label folder {folder} already absent"
else:
# Label needs to be delete
res = _set_labels(node, apiserver_url, labels)
Expand All @@ -377,7 +383,7 @@ def label_folder_absent(name, node=None, apiserver_url=None):
)
else:
ret["changes"] = {"deleted": folder}
ret["comment"] = "Label folder {} absent".format(folder)
ret["comment"] = f"Label folder {folder} absent"

return ret

Expand All @@ -386,7 +392,7 @@ def label_folder_absent(name, node=None, apiserver_url=None):
def _get_namespaces(apiserver_url, name=""):
"""Get namespace is namespace is defined otherwise return all namespaces"""
# Prepare URL
url = "{}/api/v1/namespaces/{}".format(apiserver_url, name)
url = f"{apiserver_url}/api/v1/namespaces/{name}"
# Make request
ret = http.query(url)
if ret.get("body"):
Expand All @@ -398,7 +404,7 @@ def _get_namespaces(apiserver_url, name=""):
def _create_namespace(namespace, apiserver_url):
"""create namespace on the defined k8s cluster"""
# Prepare URL
url = "{}/api/v1/namespaces".format(apiserver_url)
url = f"{apiserver_url}/api/v1/namespaces"
# Prepare data
data = {"kind": "Namespace", "apiVersion": "v1", "metadata": {"name": namespace}}
log.trace("namespace creation requests: %s", data)
Expand Down Expand Up @@ -438,9 +444,9 @@ def create_namespace(name, apiserver_url=None):
# This is a new namespace
_create_namespace(name, apiserver_url)
ret["changes"] = name
ret["comment"] = "Namespace {} created".format(name)
ret["comment"] = f"Namespace {name} created"
else:
ret["comment"] = "Namespace {} already present".format(name)
ret["comment"] = f"Namespace {name} already present"
return ret


Expand Down Expand Up @@ -484,7 +490,7 @@ def get_namespaces(namespace="", apiserver_url=None):
def _get_secrets(namespace, name, apiserver_url):
"""Get secrets of the namespace."""
# Prepare URL
url = "{}/api/v1/namespaces/{}/secrets/{}".format(apiserver_url, namespace, name)
url = f"{apiserver_url}/api/v1/namespaces/{namespace}/secrets/{name}"
# Make request
ret = http.query(url)
if ret.get("body"):
Expand All @@ -496,20 +502,20 @@ def _get_secrets(namespace, name, apiserver_url):
def _update_secret(namespace, name, data, apiserver_url):
"""Replace secrets data by a new one"""
# Prepare URL
url = "{}/api/v1/namespaces/{}/secrets/{}".format(apiserver_url, namespace, name)
url = f"{apiserver_url}/api/v1/namespaces/{namespace}/secrets/{name}"
# Prepare data
data = [{"op": "replace", "path": "/data", "value": data}]
# Make request
ret = _kpatch(url, data)
if ret.get("status") == 404:
return "Node {} doesn't exist".format(url)
return f"Node {url} doesn't exist"
return ret


def _create_secret(namespace, name, data, apiserver_url):
"""create namespace on the defined k8s cluster"""
# Prepare URL
url = "{}/api/v1/namespaces/{}/secrets".format(apiserver_url, namespace)
url = f"{apiserver_url}/api/v1/namespaces/{namespace}/secrets"
# Prepare data
request = {
"apiVersion": "v1",
Expand Down Expand Up @@ -738,7 +744,7 @@ def create_secret(
return {
"name": name,
"result": False,
"comment": "Secret {} is already present".format(name),
"comment": f"Secret {name} is already present",
"changes": {},
}

Expand All @@ -755,7 +761,7 @@ def create_secret(
if sname == encoded == "":
ret[
"comment"
] += "Source file {} is missing or name is incorrect\n".format(v)
] += f"Source file {v} is missing or name is incorrect\n"
if force:
continue
else:
Expand Down Expand Up @@ -825,8 +831,8 @@ def delete_secret(namespace, name, apiserver_url=None, force=True):
"changes": {},
}

url = "{}/api/v1/namespaces/{}/secrets/{}".format(apiserver_url, namespace, name)
url = f"{apiserver_url}/api/v1/namespaces/{namespace}/secrets/{name}"
res = http.query(url, method="DELETE")
if res.get("body"):
ret["comment"] = "Removed secret {} in {} namespace".format(name, namespace)
ret["comment"] = f"Removed secret {name} in {namespace} namespace"
return ret
Loading
Loading