Skip to content

Commit 97d6216

Browse files
authored
Merge pull request #50 from CharlieTLe/feature/kubeconfig-style-config
Add kubeconfig-style configuration file support
2 parents ac154e1 + 7aed040 commit 97d6216

File tree

11 files changed

+1820
-181
lines changed

11 files changed

+1820
-181
lines changed

CONFIG.md

Lines changed: 377 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
# Configuration File
2+
3+
cortextool supports a kubeconfig-style configuration file for managing multiple Cortex clusters with ease. This allows you to define multiple clusters, authentication credentials, and contexts, and easily switch between them.
4+
5+
## Quick Start
6+
7+
1. **Create a config file** at `~/.cortextool/config`:
8+
9+
```yaml
10+
current-context: production
11+
12+
contexts:
13+
- name: production
14+
context:
15+
cluster: prod-cortex
16+
user: prod-user
17+
18+
clusters:
19+
- name: prod-cortex
20+
cluster:
21+
address: https://cortex.prod.example.com
22+
tls-ca-path: /path/to/ca.crt
23+
tls-cert-path: /path/to/client.crt
24+
tls-key-path: /path/to/client.key
25+
26+
users:
27+
- name: prod-user
28+
user:
29+
id: tenant-123
30+
auth-token: your-jwt-token
31+
```
32+
33+
2. **Use cortextool commands** without specifying connection details:
34+
35+
```bash
36+
# The address, tenant ID, and TLS certs are loaded from the config file
37+
cortextool rules list
38+
cortextool alertmanager get
39+
```
40+
41+
3. **Switch between contexts**:
42+
43+
```bash
44+
cortextool config use-context staging
45+
```
46+
47+
## Configuration Structure
48+
49+
The configuration file consists of four main sections:
50+
51+
### 1. Current Context
52+
53+
Specifies which context to use by default:
54+
55+
```yaml
56+
current-context: production
57+
```
58+
59+
### 2. Contexts
60+
61+
Contexts bind together a cluster and user credentials:
62+
63+
```yaml
64+
contexts:
65+
- name: production
66+
context:
67+
cluster: prod-cortex # references a cluster name
68+
user: prod-user # references a user name
69+
- name: staging
70+
context:
71+
cluster: staging-cortex
72+
user: staging-user
73+
```
74+
75+
### 3. Clusters
76+
77+
Clusters define connection details for Cortex instances:
78+
79+
```yaml
80+
clusters:
81+
- name: prod-cortex
82+
cluster:
83+
address: https://cortex.prod.example.com
84+
tls-ca-path: /path/to/ca.crt # Optional: TLS CA certificate
85+
tls-cert-path: /path/to/client.crt # Optional: Client certificate for mTLS
86+
tls-key-path: /path/to/client.key # Optional: Client certificate key
87+
use-legacy-routes: false # Optional: Use /api/prom/ routes
88+
ruler-api-path: /api/v1/rules # Optional: Custom ruler API path
89+
```
90+
91+
### 4. Users
92+
93+
Users define authentication credentials:
94+
95+
```yaml
96+
users:
97+
- name: prod-user
98+
user:
99+
id: tenant-123 # Tenant ID
100+
auth-token: jwt-token-here # Bearer token for JWT auth
101+
# OR use basic auth:
102+
# user: username
103+
# key: password
104+
```
105+
106+
## Configuration Precedence
107+
108+
Configuration values are resolved in the following order (highest priority first):
109+
110+
1. **Command-line flags**: `--address`, `--id`, `--tls-cert-path`, etc.
111+
2. **Environment variables**: `CORTEX_ADDRESS`, `CORTEX_TENANT_ID`, `CORTEX_TLS_CLIENT_CERT`, etc.
112+
3. **`--context` flag**: Override which context to use (instead of `current-context`)
113+
4. **Config file**: Values from the current context
114+
5. **Defaults**: Built-in defaults
115+
116+
### Examples
117+
118+
```bash
119+
# Use config file defaults (current-context)
120+
cortextool rules list
121+
122+
# Override address with flag (other values still from config)
123+
cortextool rules list --address https://different-cortex.com
124+
125+
# Override with environment variable
126+
export CORTEX_TENANT_ID=different-tenant
127+
cortextool rules list
128+
129+
# Use a different context temporarily with --context flag
130+
cortextool --context staging rules list
131+
132+
# Combine --context with individual flags
133+
cortextool --context production --id different-tenant rules list
134+
135+
# Use a different config file
136+
cortextool --config /path/to/custom-config rules list
137+
```
138+
139+
### Using the `--context` Flag
140+
141+
The `--context` flag allows you to temporarily use a different context without changing the `current-context` in your config file. This is useful for:
142+
143+
- **Quickly switching environments**: `cortextool --context staging rules list`
144+
- **Testing with different credentials**: `cortextool --context test-user alertmanager get`
145+
- **Scripts that need specific contexts**: Always use a specific context regardless of current-context
146+
147+
**Example workflow:**
148+
149+
```bash
150+
# Set up multiple contexts
151+
cortextool config set-context prod --cluster prod-cluster --user prod-user
152+
cortextool config set-context staging --cluster staging-cluster --user staging-user
153+
cortextool config use-context prod # Set prod as default
154+
155+
# Use prod (current-context)
156+
cortextool rules list
157+
158+
# Temporarily use staging without changing current-context
159+
cortextool --context staging rules list
160+
161+
# Still using prod by default
162+
cortextool rules list
163+
```
164+
165+
**Error handling:**
166+
167+
If you specify a context that doesn't exist, cortextool will fail with a clear error:
168+
169+
```bash
170+
cortextool --context nonexistent rules list
171+
# Error: context "nonexistent" not found in config file
172+
```
173+
174+
## Config Management Commands
175+
176+
cortextool provides subcommands to manage your configuration file:
177+
178+
### View Configuration
179+
180+
Display the current configuration:
181+
182+
```bash
183+
cortextool config view
184+
```
185+
186+
### List Contexts
187+
188+
Show all available contexts:
189+
190+
```bash
191+
cortextool config get-contexts
192+
```
193+
194+
Output:
195+
```
196+
CURRENT NAME
197+
* production
198+
staging
199+
development
200+
```
201+
202+
### Show Current Context
203+
204+
Display which context is currently active:
205+
206+
```bash
207+
cortextool config current-context
208+
```
209+
210+
### Switch Context
211+
212+
Change the active context:
213+
214+
```bash
215+
cortextool config use-context staging
216+
```
217+
218+
### Manage Contexts
219+
220+
Create or update a context:
221+
222+
```bash
223+
# Create a new context
224+
cortextool config set-context my-context --cluster my-cluster --user my-user
225+
226+
# Update an existing context
227+
cortextool config set-context production --cluster new-prod-cluster
228+
```
229+
230+
### Manage Clusters
231+
232+
Create or update cluster configuration:
233+
234+
```bash
235+
cortextool config set-cluster prod-cortex \
236+
--address https://cortex.prod.example.com \
237+
--tls-ca-path /path/to/ca.crt \
238+
--tls-cert-path /path/to/client.crt \
239+
--tls-key-path /path/to/client.key
240+
```
241+
242+
### Manage Credentials
243+
244+
Create or update user credentials:
245+
246+
```bash
247+
# JWT authentication
248+
cortextool config set-credentials prod-user \
249+
--id tenant-123 \
250+
--auth-token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
251+
252+
# Basic authentication
253+
cortextool config set-credentials staging-user \
254+
--id tenant-456 \
255+
--user admin \
256+
--key password123
257+
```
258+
259+
### Delete Context
260+
261+
Remove a context:
262+
263+
```bash
264+
cortextool config delete-context old-context
265+
```
266+
267+
## Default Config Location
268+
269+
By default, cortextool looks for the config file at:
270+
271+
```
272+
$HOME/.cortextool/config
273+
```
274+
275+
You can override this with:
276+
277+
- The `--config` flag: `cortextool --config /custom/path rules list`
278+
- Set a custom default when creating the config file
279+
280+
## Example Workflows
281+
282+
### Multi-Cluster Setup
283+
284+
```bash
285+
# Set up production cluster
286+
cortextool config set-cluster prod --address https://cortex.prod.example.com
287+
cortextool config set-credentials prod-user --id prod-tenant --auth-token <token>
288+
cortextool config set-context prod --cluster prod --user prod-user
289+
290+
# Set up staging cluster
291+
cortextool config set-cluster staging --address https://cortex.staging.example.com
292+
cortextool config set-credentials staging-user --id staging-tenant --auth-token <token>
293+
cortextool config set-context staging --cluster staging --user staging-user
294+
295+
# Use production
296+
cortextool config use-context prod
297+
cortextool rules list
298+
299+
# Switch to staging
300+
cortextool config use-context staging
301+
cortextool rules list
302+
```
303+
304+
### Local Development
305+
306+
```bash
307+
# Set up local development environment
308+
cortextool config set-cluster local --address http://localhost:9009
309+
cortextool config set-credentials dev --id dev
310+
cortextool config set-context dev --cluster local --user dev
311+
cortextool config use-context dev
312+
313+
# Now work with local Cortex
314+
cortextool rules load rules.yaml
315+
```
316+
317+
### Using TLS with mTLS
318+
319+
```bash
320+
cortextool config set-cluster secure-cluster \
321+
--address https://cortex.secure.example.com \
322+
--tls-ca-path ~/.cortextool/certs/ca.crt \
323+
--tls-cert-path ~/.cortextool/certs/client.crt \
324+
--tls-key-path ~/.cortextool/certs/client.key
325+
326+
cortextool config set-credentials secure-user --id tenant-secure --auth-token <token>
327+
cortextool config set-context secure --cluster secure-cluster --user secure-user
328+
cortextool config use-context secure
329+
```
330+
331+
## Migration from Environment Variables
332+
333+
If you're currently using environment variables, you can continue to use them alongside the config file. They will override config file values when set.
334+
335+
To migrate, convert your environment variables to a config file:
336+
337+
```bash
338+
# Before (environment variables)
339+
export CORTEX_ADDRESS=https://cortex.example.com
340+
export CORTEX_TENANT_ID=my-tenant
341+
export CORTEX_AUTH_TOKEN=my-token
342+
cortextool rules list
343+
344+
# After (config file)
345+
cortextool config set-cluster my-cluster --address https://cortex.example.com
346+
cortextool config set-credentials my-user --id my-tenant --auth-token my-token
347+
cortextool config set-context default --cluster my-cluster --user my-user
348+
cortextool config use-context default
349+
cortextool rules list # No environment variables needed!
350+
```
351+
352+
## Troubleshooting
353+
354+
### Config file not found
355+
356+
If cortextool can't find your config file, ensure it exists at `~/.cortextool/config` or specify it with `--config`.
357+
358+
### Invalid current context
359+
360+
If you get an error about the current context:
361+
362+
```bash
363+
# Check available contexts
364+
cortextool config get-contexts
365+
366+
# Switch to a valid context
367+
cortextool config use-context <valid-context-name>
368+
```
369+
370+
### Missing required fields
371+
372+
If a command fails with "cortex address is required" or "tenant ID is required", ensure your context references valid cluster and user entries with all required fields set.
373+
374+
## See Also
375+
376+
- [cortextool.example.yaml](./cortextool.example.yaml) - Full example configuration file
377+
- [README.md](./README.md) - Main documentation

0 commit comments

Comments
 (0)