Skip to content

Commit dff72be

Browse files
szaherkryanbeane
andauthored
use kube-authkit for better k8s authentication (#970)
* use kube-authkit for better k8s authentication Signed-off-by: Saad Zaher <szaher@redhat.com> * update poetry.lock Signed-off-by: Saad Zaher <szaher@redhat.com> * fix tests Signed-off-by: Saad Zaher <szaher@redhat.com> * fix test issues Signed-off-by: Saad Zaher <szaher@redhat.com> * fix tests for ci Signed-off-by: Saad Zaher <szaher@redhat.com> * mock k8s client calls Signed-off-by: Saad Zaher <szaher@redhat.com> * tests: mock k8s clients and add test resource files to fix CI Replace global no-op of kubeconfig loaders with targeted test-only mocks so tests that assert loader behavior can still patch them locally. Add fake k8s clients (AuthenticationApi, CoreV1Api, CustomObjectsApi) implementing the methods used by unit tests (get_api_group, read_namespaced_secret, list_namespaced_custom_object, list_cluster_custom_object, get/create/delete_namespaced_custom_object, etc.). Create minimal resource YAMLs under $HOME/.codeflare/resources and TLS files in a tmp CWD so tests do not raise FileNotFoundError. Test-only change: no production code modified. Signed-off-by: Saad Zaher <szaher@redhat.com> * Introduce set_k8s_client in codeflare-sdk Signed-off-by: Saad Zaher <szaher@redhat.com> * fix the unit tests Signed-off-by: Saad Zaher <szaher@redhat.com> * fix linting issues Signed-off-by: Saad Zaher <szaher@redhat.com> * upgrade to kube-authkit 0.2.0 Signed-off-by: Saad Zaher <szaher@redhat.com> * update docs to use latest params from authkit 0.2.0 Signed-off-by: Saad Zaher <szaher@redhat.com> * Update src/codeflare_sdk/common/kubernetes_cluster/auth.py Co-authored-by: Bryan Keane <bryankeane0@gmail.com> * fix check if authkit available Signed-off-by: Saad Zaher <szaher@redhat.com> --------- Signed-off-by: Saad Zaher <szaher@redhat.com> Co-authored-by: Bryan Keane <bryankeane0@gmail.com>
1 parent ac5ae0e commit dff72be

14 files changed

Lines changed: 3760 additions & 1910 deletions

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,86 @@ Full documentation can be found [here](https://project-codeflare.github.io/codef
1717

1818
Can be installed via `pip`: `pip install codeflare-sdk`
1919

20+
## Authentication
21+
22+
CodeFlare SDK uses [kube-authkit](https://github.com/opendatahub-io/kube-authkit) for Kubernetes authentication, supporting multiple authentication methods:
23+
24+
- **Auto-Detection** - Automatically detects kubeconfig or in-cluster authentication
25+
- **Token-Based** - Authenticate with API server token
26+
- **OIDC** - OpenID Connect authentication with device flow or client credentials
27+
- **OpenShift OAuth** - Native OpenShift OAuth support
28+
- **Kubeconfig** - Traditional kubeconfig file authentication
29+
- **In-Cluster** - Service account authentication when running in a pod
30+
31+
### Quick Start
32+
33+
```python
34+
from kube_authkit import get_k8s_client, AuthConfig
35+
from codeflare_sdk import set_api_client, Cluster, ClusterConfiguration
36+
37+
# Option 1: Auto-detect authentication (recommended - no explicit auth needed!)
38+
cluster = Cluster(ClusterConfiguration(
39+
name='my-cluster',
40+
num_workers=2,
41+
))
42+
cluster.apply()
43+
44+
# Option 2: OIDC authentication
45+
auth_config = AuthConfig(
46+
method="oidc",
47+
oidc_issuer="https://your-oidc-provider.com",
48+
client_id="your-client-id",
49+
use_device_flow=True
50+
)
51+
api_client = get_k8s_client(config=auth_config)
52+
set_api_client(api_client) # Register with CodeFlare SDK
53+
54+
# Option 3: OpenShift OAuth with token
55+
auth_config = AuthConfig(
56+
k8s_api_host="https://api.example.com:6443",
57+
token="your-token"
58+
)
59+
api_client = get_k8s_client(config=auth_config)
60+
set_api_client(api_client) # Register with CodeFlare SDK
61+
62+
# Now create your cluster
63+
cluster = Cluster(ClusterConfiguration(
64+
name='my-cluster',
65+
num_workers=2,
66+
))
67+
cluster.apply()
68+
```
69+
70+
### Migration from Legacy Authentication
71+
72+
If you're using the deprecated `TokenAuthentication` or `KubeConfigFileAuthentication` classes, please see our [Migration Guide](./docs/auth_migration_guide.md) for detailed instructions on updating to kube-authkit.
73+
74+
**Legacy classes (deprecated):**
75+
```python
76+
# ⚠️ Deprecated - will be removed in v1.0.0
77+
from codeflare_sdk import TokenAuthentication
78+
auth = TokenAuthentication(token="...", server="...")
79+
auth.login()
80+
```
81+
82+
**New recommended approach:**
83+
```python
84+
# ✅ Recommended - Auto-detection (no explicit auth needed!)
85+
from codeflare_sdk import Cluster, ClusterConfiguration
86+
cluster = Cluster(ClusterConfiguration(name="my-cluster"))
87+
88+
# ✅ For OIDC or OpenShift OAuth with token
89+
from kube_authkit import AuthConfig, get_k8s_client
90+
from codeflare_sdk import set_api_client
91+
92+
auth_config = AuthConfig(
93+
k8s_api_host="https://api.example.com:6443",
94+
token="your-token"
95+
)
96+
api_client = get_k8s_client(config=auth_config)
97+
set_api_client(api_client) # Register with CodeFlare SDK
98+
```
99+
20100
## Development
21101

22102
Please see our [CONTRIBUTING.md](./CONTRIBUTING.md) for detailed instructions.

0 commit comments

Comments
 (0)