-
Notifications
You must be signed in to change notification settings - Fork 73
feat: add kubeconfig_output_path parameter to get_client #2679
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
Open
rnetser
wants to merge
9
commits into
main
Choose a base branch
from
kc-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
23ff05e
feat: add kubeconfig_output_path parameter to get_client
rnetser 8b560c5
fix: address review - config_file error handling, docstring, write fa…
rnetser 02a67b1
fix: address CodeRabbit review - atomic write, yaml error handling, t…
rnetser a6fde4c
fix: address review - raise on errors, S106 fixes, atomic write mock
rnetser d340e89
fix: update docstring, use shutil.copy2 for config_file path
rnetser 90ec086
fix: remove config_file from save_kubeconfig - redundant when file al…
rnetser 41f4ac7
Merge branch 'main' of github.com:RedHatQE/openshift-python-wrapper i…
rnetser d338e0c
Refactor kubeconfig_output_path to generate_kubeconfig boolean
rnetser 8714151
Remove @overload signatures from get_client
rnetser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import atexit | ||
| import os | ||
| import tempfile | ||
| from typing import Any | ||
|
|
||
| import yaml | ||
| from simple_logger.logger import get_logger | ||
|
|
||
| LOGGER = get_logger(name=__name__) | ||
|
|
||
|
|
||
| def save_kubeconfig( | ||
| host: str | None = None, | ||
| token: str | None = None, | ||
| config_dict: dict[str, Any] | None = None, | ||
| verify_ssl: bool | None = None, | ||
| ) -> str: | ||
| """ | ||
| Save kubeconfig to a temporary file. | ||
|
|
||
| Builds a kubeconfig from the provided parameters and writes it to a | ||
| temporary file with 0o600 permissions. | ||
|
|
||
| Args: | ||
| host (str): cluster API server URL. | ||
| token (str): bearer token for authentication. | ||
| config_dict (dict): existing kubeconfig dict to save as-is. | ||
| verify_ssl (bool): if False, sets insecure-skip-tls-verify in the saved config. | ||
|
|
||
| Returns: | ||
| str: path to the temporary kubeconfig file. | ||
| """ | ||
| if config_dict is not None: | ||
| _config = config_dict | ||
| elif host: | ||
| cluster_config: dict[str, Any] = {"server": host} | ||
| if verify_ssl is False: | ||
| cluster_config["insecure-skip-tls-verify"] = True | ||
|
|
||
| user_config: dict[str, str] = {} | ||
| if token: | ||
| user_config["token"] = token | ||
|
|
||
| _config = { | ||
| "apiVersion": "v1", | ||
| "kind": "Config", | ||
| "clusters": [{"name": "cluster", "cluster": cluster_config}], | ||
| "users": [{"name": "user", "user": user_config}], | ||
| "contexts": [{"name": "context", "context": {"cluster": "cluster", "user": "user"}}], | ||
| "current-context": "context", | ||
| } | ||
| else: | ||
| raise ValueError("Not enough data to build kubeconfig: provide config_dict or host") | ||
|
|
||
| tmp_file = None | ||
| try: | ||
| tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".kubeconfig", mode="w") | ||
| os.chmod(tmp_file.name, 0o600) | ||
| yaml.safe_dump(_config, tmp_file) | ||
| tmp_file.close() | ||
| # Ensures the file is cleaned up when the process exits. | ||
| atexit.register(lambda p: os.unlink(p) if os.path.exists(p) else None, tmp_file.name) | ||
| LOGGER.info(f"kubeconfig saved to {tmp_file.name}") | ||
| return tmp_file.name | ||
|
|
||
| except (OSError, yaml.YAMLError): | ||
rnetser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if tmp_file is not None and os.path.exists(tmp_file.name): | ||
| os.unlink(tmp_file.name) | ||
| LOGGER.error("Failed to save kubeconfig") | ||
| raise | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If none of them passed the function do nothing, please raise error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_resolve_bearer_tokenis a local helper to avoid code duplication — it extracts the Bearer token extraction logic that would otherwise be duplicated between thehost + tokenandhost + username + passwordpaths.It intentionally returns
Nonerather than raising because:kubeconfig_output_pathflows, includingconfig_dictandconfig_filewhere no token exists inclient_configuration.api_key— and that's perfectly validsave_kubeconfig, which now raisesValueErrorwhen it has nothing to work withSo the "raise on insufficient data" feedback is addressed in
save_kubeconfigwhere it belongs, not in this helper.