-
Notifications
You must be signed in to change notification settings - Fork 63
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
Support multiple clusters in sync_tron_from_k8s #991
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import base64 | ||
import hashlib | ||
import logging | ||
import os | ||
import subprocess | ||
import sys | ||
from typing import Any | ||
|
@@ -58,7 +59,18 @@ def limit_size_with_hash(name: str, limit: int = 63, suffix: int = 4) -> str: | |
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--kubeconfig-path", dest="kubeconfig_path", help="KUBECONFIG path") | ||
parser.add_argument( | ||
"--kubeconfig-path", | ||
dest="kubeconfig_path", | ||
help="KUBECONFIG path; multiple can be specified to find pods in multiple clusters", | ||
nargs="+", | ||
) | ||
parser.add_argument( | ||
"--kubecontext", | ||
dest="kubecontext", | ||
help="kubecontext to use from specified kubeconfig. multiple can be specified to find pods in multiple clusters, ONLY if a single kubeconfig-path is provided", | ||
nargs="*", | ||
) | ||
parser.add_argument( | ||
"--do-work", | ||
dest="do_work", | ||
|
@@ -77,6 +89,10 @@ def parse_args(): | |
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose logging") | ||
args = parser.parse_args() | ||
|
||
# We can only have multiple kubeconfigs, or multiple contexts with a single config | ||
if len(args.kubeconfig_path) > 1 and args.kubecontext: | ||
parser.error("You can only specify a single --kubeconfig-path if specifying --kubecontext arguments.") | ||
|
||
# tron's base level is critical, not info, adjust accoringly | ||
if args.verbose: | ||
level = logging.DEBUG | ||
|
@@ -97,7 +113,10 @@ def parse_args(): | |
return args | ||
|
||
|
||
def fetch_pods(kubeconfig_path: str) -> Dict[str, V1Pod]: | ||
def fetch_pods(kubeconfig_path: str, kubecontext: Optional[str]) -> Dict[str, V1Pod]: | ||
if kubecontext: | ||
# KubeClient only uses the environment variable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops, sorry - i should have also allowed for passing this through as an arg |
||
os.environ["KUBECONTEXT"] = kubecontext | ||
kube_client = KubeClient(kubeconfig_path=kubeconfig_path, user_agent="sync_tron_state_from_k8s") | ||
|
||
# Bit of a hack, no helper to fetch pods so reach into core api | ||
|
@@ -214,7 +233,16 @@ def update_tron_from_pods( | |
jobs = get_tron_state_from_api(args.tron_url, args.num_runs) | ||
log.debug(f"Found {len(jobs)} jobs.") | ||
|
||
pods = fetch_pods(args.kubeconfig_path) | ||
pods = {} | ||
kube_client_args = ( | ||
[(args.kubeconfig_path[0], kubecontext) for kubecontext in args.kubecontext] | ||
if args.kubecontext | ||
else [(kubeconfig_path, None) for kubeconfig_path in args.kubeconfig_path] | ||
) | ||
|
||
for kubeconfig_path, kubecontext in kube_client_args: | ||
pods.update(fetch_pods(kubeconfig_path, kubecontext)) | ||
|
||
log.debug(f"Found {len(pods.keys())} pods.") | ||
|
||
update_tron_from_pods(jobs, pods, args.tronctl_wrapper, args.do_work) |
Oops, something went wrong.
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.
i'm assuming this will also do a sys.exit() or something similar?
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.
Yup that's been my experience, it's using https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.error :