Skip to content

Commit 083a2ea

Browse files
authored
get default client if api_config_handler returns None (#395)
Signed-off-by: Kevin <[email protected]>
1 parent db8b33d commit 083a2ea

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/codeflare_sdk/cluster/cluster.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,11 @@ def _client_verify_tls(self):
9191

9292
@property
9393
def job_client(self):
94+
k8client = api_config_handler() or client.ApiClient()
9495
if self._job_submission_client:
9596
return self._job_submission_client
9697
if self.config.openshift_oauth:
97-
print(
98-
api_config_handler().configuration.get_api_key_with_prefix(
99-
"authorization"
100-
)
101-
)
98+
print(k8client.configuration.get_api_key_with_prefix("authorization"))
10299
self._job_submission_client = JobSubmissionClient(
103100
self.cluster_dashboard_uri(),
104101
headers=self._client_headers,

src/codeflare_sdk/utils/openshift_oauth.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def create_openshift_oauth_objects(cluster_name, namespace):
3434

3535

3636
def _create_or_replace_oauth_sa(namespace, oauth_sa_name, host):
37-
api_client = api_config_handler()
3837
oauth_sa = client.V1ServiceAccount(
3938
api_version="v1",
4039
kind="ServiceAccount",
@@ -47,12 +46,12 @@ def _create_or_replace_oauth_sa(namespace, oauth_sa_name, host):
4746
),
4847
)
4948
try:
50-
client.CoreV1Api(api_client).create_namespaced_service_account(
49+
client.CoreV1Api(api_config_handler()).create_namespaced_service_account(
5150
namespace=namespace, body=oauth_sa
5251
)
5352
except client.ApiException as e:
5453
if e.reason == "Conflict":
55-
client.CoreV1Api(api_client).replace_namespaced_service_account(
54+
client.CoreV1Api(api_config_handler()).replace_namespaced_service_account(
5655
namespace=namespace,
5756
body=oauth_sa,
5857
name=oauth_sa_name,
@@ -62,7 +61,6 @@ def _create_or_replace_oauth_sa(namespace, oauth_sa_name, host):
6261

6362

6463
def _create_or_replace_oauth_rb(cluster_name, namespace, oauth_sa_name):
65-
api_client = api_config_handler()
6664
oauth_crb = client.V1ClusterRoleBinding(
6765
api_version="rbac.authorization.k8s.io/v1",
6866
kind="ClusterRoleBinding",
@@ -79,14 +77,14 @@ def _create_or_replace_oauth_rb(cluster_name, namespace, oauth_sa_name):
7977
],
8078
)
8179
try:
82-
client.RbacAuthorizationV1Api(api_client).create_cluster_role_binding(
80+
client.RbacAuthorizationV1Api(api_config_handler()).create_cluster_role_binding(
8381
body=oauth_crb
8482
)
8583
except client.ApiException as e:
8684
if e.reason == "Conflict":
87-
client.RbacAuthorizationV1Api(api_client).replace_cluster_role_binding(
88-
body=oauth_crb, name=f"{cluster_name}-rb"
89-
)
85+
client.RbacAuthorizationV1Api(
86+
api_config_handler()
87+
).replace_cluster_role_binding(body=oauth_crb, name=f"{cluster_name}-rb")
9088
else:
9189
raise e
9290

@@ -98,19 +96,18 @@ def _gen_tls_secret_name(cluster_name):
9896
def delete_openshift_oauth_objects(cluster_name, namespace):
9997
# NOTE: it might be worth adding error handling here, but shouldn't be necessary because cluster.down(...) checks
10098
# for an existing cluster before calling this => the objects should never be deleted twice
101-
api_client = api_config_handler()
10299
oauth_sa_name = f"{cluster_name}-oauth-proxy"
103100
service_name = f"{cluster_name}-oauth"
104-
client.CoreV1Api(api_client).delete_namespaced_service_account(
101+
client.CoreV1Api(api_config_handler()).delete_namespaced_service_account(
105102
name=oauth_sa_name, namespace=namespace
106103
)
107-
client.CoreV1Api(api_client).delete_namespaced_service(
104+
client.CoreV1Api(api_config_handler()).delete_namespaced_service(
108105
name=service_name, namespace=namespace
109106
)
110-
client.NetworkingV1Api(api_client).delete_namespaced_ingress(
107+
client.NetworkingV1Api(api_config_handler()).delete_namespaced_ingress(
111108
name=f"{cluster_name}-ingress", namespace=namespace
112109
)
113-
client.RbacAuthorizationV1Api(api_client).delete_cluster_role_binding(
110+
client.RbacAuthorizationV1Api(api_config_handler()).delete_cluster_role_binding(
114111
name=f"{cluster_name}-rb"
115112
)
116113

@@ -123,7 +120,6 @@ def _create_or_replace_oauth_service_obj(
123120
service_name: str,
124121
port_name: str,
125122
) -> client.V1Service:
126-
api_client = api_config_handler()
127123
oauth_service = client.V1Service(
128124
api_version="v1",
129125
kind="Service",
@@ -153,12 +149,12 @@ def _create_or_replace_oauth_service_obj(
153149
),
154150
)
155151
try:
156-
client.CoreV1Api(api_client).create_namespaced_service(
152+
client.CoreV1Api(api_config_handler()).create_namespaced_service(
157153
namespace=namespace, body=oauth_service
158154
)
159155
except client.ApiException as e:
160156
if e.reason == "Conflict":
161-
client.CoreV1Api(api_client).replace_namespaced_service(
157+
client.CoreV1Api(api_config_handler()).replace_namespaced_service(
162158
namespace=namespace, body=oauth_service, name=service_name
163159
)
164160
else:
@@ -172,7 +168,6 @@ def _create_or_replace_oauth_ingress_object(
172168
port_name: str,
173169
host: str,
174170
) -> client.V1Ingress:
175-
api_client = api_config_handler()
176171
ingress = client.V1Ingress(
177172
api_version="networking.k8s.io/v1",
178173
kind="Ingress",
@@ -205,12 +200,12 @@ def _create_or_replace_oauth_ingress_object(
205200
),
206201
)
207202
try:
208-
client.NetworkingV1Api(api_client).create_namespaced_ingress(
203+
client.NetworkingV1Api(api_config_handler()).create_namespaced_ingress(
209204
namespace=namespace, body=ingress
210205
)
211206
except client.ApiException as e:
212207
if e.reason == "Conflict":
213-
client.NetworkingV1Api(api_client).replace_namespaced_ingress(
208+
client.NetworkingV1Api(api_config_handler()).replace_namespaced_ingress(
214209
namespace=namespace, body=ingress, name=f"{cluster_name}-ingress"
215210
)
216211
else:

0 commit comments

Comments
 (0)