Skip to content

Commit 5116117

Browse files
add login and out of oc
1 parent 01b4cbb commit 5116117

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/codeflare_sdk/cluster/auth.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Copyright 2022 IBM, Red Hat
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
The auth sub-module contains the definitions for the Authentication objects, which represent
17+
the methods by which a user can authenticate to their cluster(s). The abstract class, `Authentication`,
18+
contains two required methods `login()` and `logout()`. Users can use one of the existing concrete classes to
19+
authenticate to their cluster or add their own custom concrete classes here.
20+
"""
21+
22+
import abc
23+
import openshift as oc
24+
25+
26+
class Authentication(metaclass=abc.ABCMeta):
27+
"""
28+
An abstract class that defines the necessary methods for authenticating to a remote environment.
29+
Specifically, this class defines the need for a `login()` and a `logout()` function.
30+
"""
31+
32+
def login(self):
33+
"""
34+
Method for logging in to a remote cluster.
35+
"""
36+
pass
37+
38+
def logout(self):
39+
"""
40+
Method for logging out of the remote cluster.
41+
"""
42+
pass
43+
44+
45+
class TokenAuthentication(Authentication):
46+
"""
47+
`TokenAuthentication` is a subclass of `Authentication`. It can be used to authenticate to an OpenShift
48+
cluster when the user has an API token and the API server address.
49+
"""
50+
51+
def __init__(
52+
self, token: str = None, server: str = None,
53+
):
54+
"""
55+
Initialize a TokenAuthentication object that requires a value for `token`, the API Token
56+
and `server`, the API server address for authenticating to an OpenShift cluster.
57+
"""
58+
59+
self.token = token
60+
self.server = server
61+
62+
def login(self):
63+
"""
64+
This function is used to login to an OpenShift cluster using the user's API token and API server address.
65+
"""
66+
token = self.token
67+
server = self.server
68+
response = oc.invoke("login", [f"--token={token}", f"--server={server}:6443"])
69+
return response.out()
70+
71+
def logout(self):
72+
"""
73+
This function is used to logout of an OpenShift cluster.
74+
"""
75+
response = oc.invoke("logout")
76+
return response.out()
77+
78+
79+
class PasswordUserAuthentication(Authentication):
80+
"""
81+
`PasswordUserAuthentication` is a subclass of `Authentication`. It can be used to authenticate to an OpenShift
82+
cluster when the user has a username and password.
83+
"""
84+
85+
def __init__(
86+
self, username: str = None, password: str = None,
87+
):
88+
"""
89+
Initialize a PasswordUserAuthentication object that requires a value for `username`
90+
and `password` for authenticating to an OpenShift cluster.
91+
"""
92+
self.username = username
93+
self.password = password
94+
95+
def login(self):
96+
"""
97+
This function is used to login to an OpenShift cluster using the user's `username` and `password`.
98+
"""
99+
response = oc.login(self.username, self.password)
100+
return response.out()
101+
102+
def logout(self):
103+
"""
104+
This function is used to logout of an OpenShift cluster.
105+
"""
106+
response = oc.invoke("logout")
107+
return response.out()

src/codeflare_sdk/cluster/cluster.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def up(self):
9494
Applies the AppWrapper yaml, pushing the resource request onto
9595
the MCAD queue.
9696
"""
97+
self.config.auth.login()
9798
namespace = self.config.namespace
9899
with oc.project(namespace):
99100
oc.invoke("apply", ["-f", self.app_wrapper_yaml])
@@ -106,6 +107,7 @@ def down(self):
106107
namespace = self.config.namespace
107108
with oc.project(namespace):
108109
oc.invoke("delete", ["AppWrapper", self.app_wrapper_name])
110+
self.config.auth.logout()
109111

110112
def status(self, print_to_console: bool = True):
111113
"""

src/codeflare_sdk/cluster/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
from dataclasses import dataclass, field
22+
from .auth import Authentication
2223
import pathlib
2324

2425
dir = pathlib.Path(__file__).parent.parent.resolve()
@@ -46,3 +47,4 @@ class ClusterConfiguration:
4647
instascale: bool = False
4748
envs: dict = field(default_factory=dict)
4849
image: str = "ghcr.io/ibm-ai-foundation/base:ray1.13.0-py38-gpu-pytorch1.12.0cu116-20220826-202124"
50+
auth: Authentication = Authentication()

0 commit comments

Comments
 (0)