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

add in_cluster option for KubernetesScheduler #917

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions torchx/schedulers/kubernetes_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ class KubernetesOpts(TypedDict, total=False):
image_repo: Optional[str]
service_account: Optional[str]
priority_class: Optional[str]
in_cluster: Optional[bool]


class KubernetesScheduler(DockerWorkspaceMixin, Scheduler[KubernetesOpts]):
Expand Down Expand Up @@ -553,10 +554,16 @@ def _api_client(self) -> "ApiClient":
c = self._client
if c is None:
configuration = client.Configuration()
try:
config.load_kube_config(client_configuration=configuration)
except config.ConfigException as e:
warnings.warn(f"failed to load kube config: {e}")
if self._in_cluster:
try:
config.load_incluster_config(client_configuration=configuration)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to automatically detect this? Maybe we should try the kube_config and then this if KubeConfig doesn't exist via a try-except?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will try to add it

except config.ConfigException as e:
warnings.warn(f"failed to load incluster config: {e}")
else:
try:
config.load_kube_config(client_configuration=configuration)
except config.ConfigException as e:
warnings.warn(f"failed to load kube config: {e}")

c = self._client = client.ApiClient(configuration)

Expand Down Expand Up @@ -586,6 +593,12 @@ def schedule(self, dryrun_info: AppDryRunInfo[KubernetesJob]) -> str:
cfg = dryrun_info._cfg
assert cfg is not None, f"{dryrun_info} missing cfg"
namespace = cfg.get("namespace") or "default"

in_cluster = cfg.get("in_cluster") or False
if not isinstance(in_cluster, bool):
raise TypeError(f"config value 'in_cluster' must be a bool, got {in_cluster}")

self._in_cluster = in_cluster

images_to_push = dryrun_info.request.images_to_push
self.push_images(images_to_push)
Expand Down Expand Up @@ -675,6 +688,11 @@ def _run_opts(self) -> runopts:
type_=str,
help="The name of the PriorityClass to set on the job specs",
)
opts.add(
"in_cluster",
type_=bool,
help="Type of run to use local cluster if KUBECONFIG not provided"
)
return opts

def describe(self, app_id: str) -> Optional[DescribeAppResponse]:
Expand Down
Loading