Skip to content

Commit

Permalink
move connections to sub-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Jun 16, 2023
1 parent 9edbbf2 commit dc65181
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
30 changes: 3 additions & 27 deletions clientmanager/clientmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import time
from pathlib import Path

import htcondor # type: ignore[import]
import kubernetes # type: ignore[import]
from wipac_dev_tools import argparse_tools, logging_tools

from . import condor, k8s
Expand Down Expand Up @@ -56,33 +54,11 @@ def main() -> None:
# Go!
match args.orchestrator:
case "condor":
# condor auth & go
with htcondor.SecMan() as secman:
secman.setToken(htcondor.Token(ENV.CONDOR_TOKEN))
schedd_obj = condor.condor_tools.get_schedd_obj(
args.collector, args.schedd
)
condor.act(args, schedd_obj)
condor.act(args)
case "k8s":
# Creating K8S cluster client
k8s_client_config = kubernetes.client.Configuration()
if args.host == "local":
# use *this* pod's service account
kubernetes.config.load_incluster_config(k8s_client_config)
else:
# connect to remote host
if args.cluster_config:
kubernetes.config.load_kube_config(
config_file=args.cluster_config,
client_configuration=k8s_client_config,
)
k8s_client_config.host = args.host
k8s_client_config.api_key["authorization"] = ENV.WORKER_K8S_TOKEN
# connect & go
with kubernetes.client.ApiClient(k8s_client_config) as k8s_api_client:
k8s.act(args, k8s_api_client)
k8s.act(args)
case other:
raise RuntimeError(f"Not supported orchestrator: {other}")
raise RuntimeError(f"Orchestrator not supported: {other}")


class OrchestratorArgs:
Expand Down
17 changes: 11 additions & 6 deletions clientmanager/condor/act.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
import htcondor # type: ignore[import]

from .. import utils
from ..config import LOGGER
from . import starter, stopper
from ..config import ENV, LOGGER
from . import condor_tools, starter, stopper


def act(
args: argparse.Namespace,
schedd_obj: htcondor.Schedd,
) -> None:
def act(args: argparse.Namespace) -> None:
"""Do the action."""
# condor auth & go
with htcondor.SecMan() as secman:
secman.setToken(htcondor.Token(ENV.CONDOR_TOKEN))
schedd_obj = condor_tools.get_schedd_obj(args.collector, args.schedd)
_act(args, schedd_obj)


def _act(args: argparse.Namespace, schedd_obj: htcondor.Schedd) -> None:
match args.action:
case "start":
LOGGER.info(
Expand Down
37 changes: 29 additions & 8 deletions clientmanager/k8s/act.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,37 @@
from . import starter, stopper


def act(args: argparse.Namespace, k8s_client: kubernetes.client.ApiClient) -> None:
def act(args: argparse.Namespace) -> None:
"""Do the action."""
try:
LOGGER.debug("testing k8s credentials")
api_response = kubernetes.client.BatchV1Api(k8s_client).get_api_resources()
LOGGER.debug(api_response)
except kubernetes.client.rest.ApiException as e:
LOGGER.exception(e)
raise

# Creating K8S cluster client
k8s_client_config = kubernetes.client.Configuration()
if args.host == "local":
# use *this* pod's service account
kubernetes.config.load_incluster_config(k8s_client_config)
else:
# connect to remote host
if args.cluster_config:
kubernetes.config.load_kube_config(
config_file=args.cluster_config,
client_configuration=k8s_client_config,
)
k8s_client_config.host = args.host
k8s_client_config.api_key["authorization"] = ENV.WORKER_K8S_TOKEN

# connect & go
with kubernetes.client.ApiClient(k8s_client_config) as k8s_client:
try:
LOGGER.debug("testing k8s credentials")
api_response = kubernetes.client.BatchV1Api(k8s_client).get_api_resources()
LOGGER.debug(api_response)
except kubernetes.client.rest.ApiException as e:
LOGGER.exception(e)
raise
_act(args, k8s_client)


def _act(args: argparse.Namespace, k8s_client: kubernetes.client.ApiClient) -> None:
match args.action:
case "start":
LOGGER.info(
Expand Down

0 comments on commit dc65181

Please sign in to comment.