0
# Configuration and Authentication
1
2
Configuration and authentication management for connecting to Kubernetes clusters. Supports loading configuration from kubeconfig files, in-cluster service accounts, or programmatic configuration with various authentication methods.
3
4
## Capabilities
5
6
### Kubeconfig Loading
7
8
Load cluster configuration from kubeconfig files with support for multiple contexts and custom configuration locations.
9
10
```python { .api }
11
def load_kube_config(
12
config_file: str = None,
13
context: str = None,
14
client_configuration = None,
15
persist_config: bool = True
16
) -> None:
17
"""
18
Load configuration from kubeconfig file.
19
20
Parameters:
21
- config_file: Path to kubeconfig file (default: ~/.kube/config)
22
- context: Context name to use from kubeconfig
23
- client_configuration: Configuration object to populate
24
- persist_config: Whether to persist config in global client
25
"""
26
```
27
28
### In-Cluster Configuration
29
30
Load configuration when running inside a Kubernetes pod using service account tokens and cluster information.
31
32
```python { .api }
33
def load_incluster_config() -> None:
34
"""
35
Load in-cluster configuration from service account.
36
Uses environment variables and mounted service account files.
37
"""
38
```
39
40
### Context Management
41
42
List and manage kubeconfig contexts for working with multiple clusters.
43
44
```python { .api }
45
def list_kube_config_contexts(config_file: str = None) -> tuple:
46
"""
47
List available kubeconfig contexts.
48
49
Parameters:
50
- config_file: Path to kubeconfig file
51
52
Returns:
53
Tuple of (contexts, active_context)
54
"""
55
```
56
57
### Client Creation
58
59
Create API clients with specific configuration without affecting global settings.
60
61
```python { .api }
62
def new_client_from_config(
63
config_file: str = None,
64
context: str = None,
65
persist_config: bool = True
66
) -> ApiClient:
67
"""
68
Create new ApiClient from kubeconfig.
69
70
Parameters:
71
- config_file: Path to kubeconfig file
72
- context: Context name to use
73
- persist_config: Whether to persist config globally
74
75
Returns:
76
Configured ApiClient instance
77
"""
78
79
def new_client_from_config_dict(
80
config_dict: dict,
81
context: str = None,
82
persist_config: bool = True
83
) -> ApiClient:
84
"""
85
Create new ApiClient from configuration dictionary.
86
87
Parameters:
88
- config_dict: Configuration as dictionary
89
- context: Context name to use
90
- persist_config: Whether to persist config globally
91
92
Returns:
93
Configured ApiClient instance
94
"""
95
```
96
97
### Automatic Configuration
98
99
Convenience function that tries multiple configuration methods automatically.
100
101
```python { .api }
102
def load_config(**kwargs) -> None:
103
"""
104
Automatically load configuration using multiple methods.
105
Tries kubeconfig, then in-cluster config as fallback.
106
107
Parameters:
108
- **kwargs: Arguments passed to load_kube_config or load_incluster_config
109
"""
110
```
111
112
## Configuration Classes
113
114
### Configuration
115
```python { .api }
116
class Configuration:
117
host: str
118
api_key: dict
119
api_key_prefix: dict
120
username: str
121
password: str
122
ssl_ca_cert: str
123
cert_file: str
124
key_file: str
125
verify_ssl: bool
126
ssl_context: ssl.SSLContext
127
proxy: str
128
proxy_headers: dict
129
connection_pool_maxsize: int
130
cert_reqs: str
131
ca_certs: str
132
retries: int
133
timeout: int
134
```
135
136
### ApiClient
137
```python { .api }
138
class ApiClient:
139
def __init__(self, configuration: Configuration = None, header_name: str = None, header_value: str = None): ...
140
def call_api(self, resource_path: str, method: str, **kwargs): ...
141
def request(self, method: str, url: str, **kwargs): ...
142
def select_header_accept(self, accepts: list) -> str: ...
143
def select_header_content_type(self, content_types: list) -> str: ...
144
```
145
146
## Constants
147
148
```python { .api }
149
KUBE_CONFIG_DEFAULT_LOCATION: str # ~/.kube/config
150
```
151
152
## Exception Types
153
154
```python { .api }
155
class ConfigException(Exception):
156
"""Configuration-related errors."""
157
pass
158
```
159
160
## Usage Examples
161
162
### Basic Configuration Loading
163
164
```python
165
from kubernetes import client, config
166
167
# Load from default kubeconfig location
168
config.load_kube_config()
169
170
# Load from specific file
171
config.load_kube_config(config_file="/path/to/kubeconfig")
172
173
# Load specific context
174
config.load_kube_config(context="my-cluster-context")
175
176
# Use the configuration
177
v1 = client.CoreV1Api()
178
pods = v1.list_namespaced_pod(namespace="default")
179
```
180
181
### In-Cluster Configuration
182
183
```python
184
from kubernetes import client, config
185
186
# When running inside a Kubernetes pod
187
config.load_incluster_config()
188
189
v1 = client.CoreV1Api()
190
# Client is now configured to use the pod's service account
191
```
192
193
### Multiple Clients with Different Configurations
194
195
```python
196
from kubernetes import client, config
197
198
# Create client for production cluster
199
prod_client = config.new_client_from_config(
200
config_file="/path/to/prod-kubeconfig",
201
context="production",
202
persist_config=False
203
)
204
prod_v1 = client.CoreV1Api(api_client=prod_client)
205
206
# Create client for development cluster
207
dev_client = config.new_client_from_config(
208
config_file="/path/to/dev-kubeconfig",
209
context="development",
210
persist_config=False
211
)
212
dev_v1 = client.CoreV1Api(api_client=dev_client)
213
```
214
215
### Context Management
216
217
```python
218
from kubernetes import config
219
220
# List available contexts
221
contexts, active_context = config.list_kube_config_contexts()
222
print(f"Active context: {active_context['name']}")
223
print("Available contexts:")
224
for context in contexts:
225
print(f" - {context['name']}")
226
```