-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathcli_utils.py
57 lines (48 loc) · 1.57 KB
/
cli_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import ast
import click
from kubernetes import client, config
import pickle
from codeflare_sdk.cluster.auth import _create_api_client_config
from codeflare_sdk.utils.kube_api_helpers import _kube_api_error_handling
import codeflare_sdk.cluster.auth as sdk_auth
class PythonLiteralOption(click.Option):
def type_cast_value(self, ctx, value):
try:
if not value:
return None
return ast.literal_eval(value)
except:
raise click.BadParameter(value)
class AuthenticationConfig:
"""
Authentication configuration that will be stored in a file once
the user logs in using `codeflare login`
"""
def __init__(
self,
token: str,
server: str,
skip_tls: bool,
ca_cert_path: str,
):
self.api_client_config = _create_api_client_config(
token, server, skip_tls, ca_cert_path
)
self.server = server
self.token = token
def create_client(self):
return client.ApiClient(self.api_client_config)
def load_auth():
"""
Loads AuthenticationConfiguration and stores it in global variables
which can be used by the SDK for authentication
"""
try:
with open("auth", "rb") as file:
auth = pickle.load(file)
sdk_auth.api_client = auth.create_client()
return auth
except (IOError, EOFError):
click.echo("No authentication found, trying default kubeconfig")
except client.ApiException:
click.echo("Invalid authentication, trying default kubeconfig")