Skip to content

Commit

Permalink
get default client if api_config_handler returns None (#395)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin <[email protected]>
  • Loading branch information
KPostOffice authored Nov 20, 2023
1 parent db8b33d commit 083a2ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
7 changes: 2 additions & 5 deletions src/codeflare_sdk/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,11 @@ def _client_verify_tls(self):

@property
def job_client(self):
k8client = api_config_handler() or client.ApiClient()
if self._job_submission_client:
return self._job_submission_client
if self.config.openshift_oauth:
print(
api_config_handler().configuration.get_api_key_with_prefix(
"authorization"
)
)
print(k8client.configuration.get_api_key_with_prefix("authorization"))
self._job_submission_client = JobSubmissionClient(
self.cluster_dashboard_uri(),
headers=self._client_headers,
Expand Down
33 changes: 14 additions & 19 deletions src/codeflare_sdk/utils/openshift_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def create_openshift_oauth_objects(cluster_name, namespace):


def _create_or_replace_oauth_sa(namespace, oauth_sa_name, host):
api_client = api_config_handler()
oauth_sa = client.V1ServiceAccount(
api_version="v1",
kind="ServiceAccount",
Expand All @@ -47,12 +46,12 @@ def _create_or_replace_oauth_sa(namespace, oauth_sa_name, host):
),
)
try:
client.CoreV1Api(api_client).create_namespaced_service_account(
client.CoreV1Api(api_config_handler()).create_namespaced_service_account(
namespace=namespace, body=oauth_sa
)
except client.ApiException as e:
if e.reason == "Conflict":
client.CoreV1Api(api_client).replace_namespaced_service_account(
client.CoreV1Api(api_config_handler()).replace_namespaced_service_account(
namespace=namespace,
body=oauth_sa,
name=oauth_sa_name,
Expand All @@ -62,7 +61,6 @@ def _create_or_replace_oauth_sa(namespace, oauth_sa_name, host):


def _create_or_replace_oauth_rb(cluster_name, namespace, oauth_sa_name):
api_client = api_config_handler()
oauth_crb = client.V1ClusterRoleBinding(
api_version="rbac.authorization.k8s.io/v1",
kind="ClusterRoleBinding",
Expand All @@ -79,14 +77,14 @@ def _create_or_replace_oauth_rb(cluster_name, namespace, oauth_sa_name):
],
)
try:
client.RbacAuthorizationV1Api(api_client).create_cluster_role_binding(
client.RbacAuthorizationV1Api(api_config_handler()).create_cluster_role_binding(
body=oauth_crb
)
except client.ApiException as e:
if e.reason == "Conflict":
client.RbacAuthorizationV1Api(api_client).replace_cluster_role_binding(
body=oauth_crb, name=f"{cluster_name}-rb"
)
client.RbacAuthorizationV1Api(
api_config_handler()
).replace_cluster_role_binding(body=oauth_crb, name=f"{cluster_name}-rb")
else:
raise e

Expand All @@ -98,19 +96,18 @@ def _gen_tls_secret_name(cluster_name):
def delete_openshift_oauth_objects(cluster_name, namespace):
# NOTE: it might be worth adding error handling here, but shouldn't be necessary because cluster.down(...) checks
# for an existing cluster before calling this => the objects should never be deleted twice
api_client = api_config_handler()
oauth_sa_name = f"{cluster_name}-oauth-proxy"
service_name = f"{cluster_name}-oauth"
client.CoreV1Api(api_client).delete_namespaced_service_account(
client.CoreV1Api(api_config_handler()).delete_namespaced_service_account(
name=oauth_sa_name, namespace=namespace
)
client.CoreV1Api(api_client).delete_namespaced_service(
client.CoreV1Api(api_config_handler()).delete_namespaced_service(
name=service_name, namespace=namespace
)
client.NetworkingV1Api(api_client).delete_namespaced_ingress(
client.NetworkingV1Api(api_config_handler()).delete_namespaced_ingress(
name=f"{cluster_name}-ingress", namespace=namespace
)
client.RbacAuthorizationV1Api(api_client).delete_cluster_role_binding(
client.RbacAuthorizationV1Api(api_config_handler()).delete_cluster_role_binding(
name=f"{cluster_name}-rb"
)

Expand All @@ -123,7 +120,6 @@ def _create_or_replace_oauth_service_obj(
service_name: str,
port_name: str,
) -> client.V1Service:
api_client = api_config_handler()
oauth_service = client.V1Service(
api_version="v1",
kind="Service",
Expand Down Expand Up @@ -153,12 +149,12 @@ def _create_or_replace_oauth_service_obj(
),
)
try:
client.CoreV1Api(api_client).create_namespaced_service(
client.CoreV1Api(api_config_handler()).create_namespaced_service(
namespace=namespace, body=oauth_service
)
except client.ApiException as e:
if e.reason == "Conflict":
client.CoreV1Api(api_client).replace_namespaced_service(
client.CoreV1Api(api_config_handler()).replace_namespaced_service(
namespace=namespace, body=oauth_service, name=service_name
)
else:
Expand All @@ -172,7 +168,6 @@ def _create_or_replace_oauth_ingress_object(
port_name: str,
host: str,
) -> client.V1Ingress:
api_client = api_config_handler()
ingress = client.V1Ingress(
api_version="networking.k8s.io/v1",
kind="Ingress",
Expand Down Expand Up @@ -205,12 +200,12 @@ def _create_or_replace_oauth_ingress_object(
),
)
try:
client.NetworkingV1Api(api_client).create_namespaced_ingress(
client.NetworkingV1Api(api_config_handler()).create_namespaced_ingress(
namespace=namespace, body=ingress
)
except client.ApiException as e:
if e.reason == "Conflict":
client.NetworkingV1Api(api_client).replace_namespaced_ingress(
client.NetworkingV1Api(api_config_handler()).replace_namespaced_ingress(
namespace=namespace, body=ingress, name=f"{cluster_name}-ingress"
)
else:
Expand Down

0 comments on commit 083a2ea

Please sign in to comment.