-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubeconfig_usage.py
More file actions
297 lines (235 loc) · 9.24 KB
/
kubeconfig_usage.py
File metadata and controls
297 lines (235 loc) · 9.24 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# Copyright 2025 Vantage Compute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Kubeconfig Usage Example
This example demonstrates different ways to configure kubeconfig in helm-sdkpy:
1. Using the default kubeconfig (~/.kube/config or $KUBECONFIG)
2. Using an explicit file path
3. Using a kubeconfig YAML string directly
The kubeconfig string approach is particularly useful when:
- Working with dynamic or programmatically generated configurations
- Retrieving kubeconfig from Kubernetes secrets, environment variables, or APIs
- Working in containerized environments where file access may be limited
"""
import asyncio
import os
from pathlib import Path
import helm_sdkpy
async def example_default_kubeconfig():
"""Example 1: Use default kubeconfig location.
This uses $KUBECONFIG environment variable if set,
otherwise falls back to ~/.kube/config.
"""
print("=" * 60)
print("Example 1: Default Kubeconfig")
print("=" * 60)
# When kubeconfig is None, uses default locations
config = helm_sdkpy.Configuration(
namespace="default",
# kubeconfig=None uses default: $KUBECONFIG or ~/.kube/config
)
# List releases to verify connection
list_action = helm_sdkpy.List(config)
releases = await list_action.run()
print(f"Found {len(releases)} releases in default namespace")
return config
async def example_kubeconfig_filepath():
"""Example 2: Use explicit kubeconfig file path.
Useful when you have multiple kubeconfig files for different clusters.
"""
print("\n" + "=" * 60)
print("Example 2: Kubeconfig from File Path")
print("=" * 60)
# Specify an explicit path to a kubeconfig file
kubeconfig_path = os.path.expanduser("~/.kube/config")
# Or use a different file for a specific cluster
# kubeconfig_path = "/path/to/production-cluster.yaml"
# kubeconfig_path = "/etc/rancher/k3s/k3s.yaml"
config = helm_sdkpy.Configuration(
namespace="kube-system",
kubeconfig=kubeconfig_path,
)
# List releases in kube-system namespace
list_action = helm_sdkpy.List(config)
releases = await list_action.run()
print(f"Found {len(releases)} releases in kube-system namespace")
return config
async def example_kubeconfig_string():
"""Example 3: Use kubeconfig YAML content as a string.
This is useful when:
- Retrieving kubeconfig from environment variables
- Loading from Kubernetes secrets
- Programmatically generating configurations
- Working in serverless or containerized environments
"""
print("\n" + "=" * 60)
print("Example 3: Kubeconfig from String")
print("=" * 60)
# Example: Read kubeconfig content from a file into a string
# In practice, this could come from an environment variable,
# API response, Kubernetes secret, etc.
kubeconfig_path = os.path.expanduser("~/.kube/config")
if Path(kubeconfig_path).exists():
with open(kubeconfig_path, "r") as f:
kubeconfig_content = f.read()
else:
# Example kubeconfig YAML structure (won't work without real cluster)
kubeconfig_content = """
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://kubernetes.example.com:6443
certificate-authority-data: <base64-encoded-ca-cert>
name: my-cluster
contexts:
- context:
cluster: my-cluster
user: my-user
namespace: default
name: my-context
current-context: my-context
users:
- name: my-user
user:
token: <your-token-here>
"""
# Pass the YAML content directly as the kubeconfig parameter
# helm-sdkpy automatically detects if it's a file path or YAML content
config = helm_sdkpy.Configuration(
namespace="default",
kubeconfig=kubeconfig_content, # YAML string, not a file path!
)
# List releases to verify connection
list_action = helm_sdkpy.List(config)
releases = await list_action.run()
print(f"Found {len(releases)} releases using kubeconfig string")
return config
async def example_kubeconfig_from_env():
"""Example 4: Load kubeconfig from environment variable.
Many CI/CD systems and container orchestrators provide kubeconfig
as an environment variable.
"""
print("\n" + "=" * 60)
print("Example 4: Kubeconfig from Environment Variable")
print("=" * 60)
# Common patterns for kubeconfig in environment variables:
# - KUBECONFIG_CONTENT: Full YAML content
# - KUBECONFIG_BASE64: Base64-encoded YAML content
import base64
kubeconfig_content = None
# Try getting YAML content directly
if "KUBECONFIG_CONTENT" in os.environ:
kubeconfig_content = os.environ["KUBECONFIG_CONTENT"]
print("Loaded kubeconfig from KUBECONFIG_CONTENT env var")
# Try getting base64-encoded content (common in CI/CD)
elif "KUBECONFIG_BASE64" in os.environ:
kubeconfig_content = base64.b64decode(
os.environ["KUBECONFIG_BASE64"]
).decode("utf-8")
print("Loaded kubeconfig from KUBECONFIG_BASE64 env var")
# Fall back to file path from KUBECONFIG env var
elif "KUBECONFIG" in os.environ:
kubeconfig_path = os.environ["KUBECONFIG"]
print(f"Using kubeconfig file from KUBECONFIG env var: {kubeconfig_path}")
config = helm_sdkpy.Configuration(
namespace="default",
kubeconfig=kubeconfig_path,
)
list_action = helm_sdkpy.List(config)
releases = await list_action.run()
print(f"Found {len(releases)} releases")
return config
else:
print("No kubeconfig environment variable found")
print("Set KUBECONFIG_CONTENT, KUBECONFIG_BASE64, or KUBECONFIG")
return None
# Use the content string directly
config = helm_sdkpy.Configuration(
namespace="default",
kubeconfig=kubeconfig_content,
)
list_action = helm_sdkpy.List(config)
releases = await list_action.run()
print(f"Found {len(releases)} releases")
return config
async def example_kubeconfig_with_context():
"""Example 5: Use kubeconfig with specific context.
When your kubeconfig has multiple contexts (clusters), you can
specify which one to use.
"""
print("\n" + "=" * 60)
print("Example 5: Kubeconfig with Specific Context")
print("=" * 60)
kubeconfig_path = os.path.expanduser("~/.kube/config")
# Specify both the kubeconfig file and the context to use
config = helm_sdkpy.Configuration(
namespace="default",
kubeconfig=kubeconfig_path,
kubecontext="my-cluster-context", # Use a specific context
)
print(f"Using kubeconfig: {kubeconfig_path}")
print(f"Using context: my-cluster-context")
# This would fail if the context doesn't exist
# list_action = helm_sdkpy.List(config)
# releases = await list_action.run()
return config
async def main():
"""Run all kubeconfig usage examples."""
print("helm-sdkpy Kubeconfig Usage Examples")
print("=" * 60)
print(f"helm-sdkpy version: {helm_sdkpy.__version__}")
try:
print(f"Library version: {helm_sdkpy.get_version()}")
except helm_sdkpy.HelmLibraryNotFound:
print("\nLibrary not found - please build the library first with 'just build-lib'")
return
# Run examples that work with available kubeconfig
try:
await example_default_kubeconfig()
except Exception as e:
print(f"Example 1 failed (expected if no cluster): {e}")
try:
await example_kubeconfig_filepath()
except Exception as e:
print(f"Example 2 failed (expected if no cluster): {e}")
try:
await example_kubeconfig_string()
except Exception as e:
print(f"Example 3 failed (expected if no cluster): {e}")
try:
await example_kubeconfig_from_env()
except Exception as e:
print(f"Example 4 failed (expected if no env vars set): {e}")
print("\n" + "=" * 60)
print("Summary: Kubeconfig Configuration Options")
print("=" * 60)
print("""
Configuration(namespace, kubeconfig, kubecontext) parameters:
1. kubeconfig=None (default)
- Uses $KUBECONFIG environment variable if set
- Falls back to ~/.kube/config
2. kubeconfig="/path/to/config.yaml"
- Uses explicit file path to kubeconfig
3. kubeconfig="apiVersion: v1\\nkind: Config\\n..."
- YAML content passed directly as a string
- Auto-detected by looking for apiVersion:, kind:, or clusters: markers
- Useful for dynamic configurations, secrets, or environment variables
4. kubecontext="my-context"
- Select a specific context from the kubeconfig
- Works with both file paths and string content
""")
if __name__ == "__main__":
asyncio.run(main())