You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/user_guide/etcd.rst
+64-11Lines changed: 64 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,22 +53,33 @@ Create an Etcd source directly:
53
53
From Environment Variables (Recommended)
54
54
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55
55
56
-
The recommended approach is to use ``Etcd.from_env()`` to read configuration from environment variables. This solves the "bootstrap problem" of how to configure the etcd source itself.
56
+
The recommended approach is to read environment variables yourself and pass them to ``Etcd()``. This aligns with the principle that the library should not implicitly read environment variables for its own configuration.
57
57
58
58
.. code-block:: python
59
59
60
60
from varlord import Config
61
61
from varlord.sources import Etcd
62
62
from dataclasses import dataclass, field
63
+
import os
63
64
64
65
@dataclass
65
66
classAppConfig:
66
67
host: str= field()
67
68
port: int= field(default=8000)
68
69
debug: bool= field(default=False)
69
70
70
-
# From environment variables
71
-
etcd_source = Etcd.from_env(prefix="/app/")
71
+
# Read environment variables and pass to Etcd
72
+
etcd_source = Etcd(
73
+
host=os.environ.get("ETCD_HOST", "127.0.0.1"),
74
+
port=int(os.environ.get("ETCD_PORT", "2379")),
75
+
prefix=os.environ.get("ETCD_PREFIX", "/app/"),
76
+
ca_cert=os.environ.get("ETCD_CA_CERT"),
77
+
cert_key=os.environ.get("ETCD_CERT_KEY"),
78
+
cert_cert=os.environ.get("ETCD_CERT_CERT"),
79
+
user=os.environ.get("ETCD_USER"),
80
+
password=os.environ.get("ETCD_PASSWORD"),
81
+
watch=os.environ.get("ETCD_WATCH", "").lower() in ("true", "1", "yes", "on"),
82
+
)
72
83
73
84
cfg = Config(
74
85
model=AppConfig,
@@ -77,6 +88,8 @@ The recommended approach is to use ``Etcd.from_env()`` to read configuration fro
77
88
78
89
app = cfg.load()
79
90
91
+
**Note**: The ``Etcd.from_env()`` method has been removed. All parameters must be passed explicitly via ``__init__``.
92
+
80
93
Environment Variables
81
94
---------------------
82
95
@@ -138,11 +151,20 @@ Then load it with python-dotenv:
138
151
139
152
load_dotenv() # Load .env file
140
153
154
+
import os
155
+
156
+
etcd_source = Etcd(
157
+
host=os.environ.get("ETCD_HOST", "127.0.0.1"),
158
+
port=int(os.environ.get("ETCD_PORT", "2379")),
159
+
prefix=os.environ.get("ETCD_PREFIX", "/app/"),
160
+
ca_cert=os.environ.get("ETCD_CA_CERT"),
161
+
cert_key=os.environ.get("ETCD_CERT_KEY"),
162
+
cert_cert=os.environ.get("ETCD_CERT_CERT"),
163
+
)
164
+
141
165
cfg = Config(
142
166
model=AppConfig,
143
-
sources=[
144
-
Etcd.from_env(), # Read from environment variables
145
-
],
167
+
sources=[etcd_source],
146
168
)
147
169
148
170
TLS Configuration
@@ -257,7 +279,15 @@ Enable watch support for dynamic configuration updates. Etcd source can watch fo
257
279
cfg = Config(
258
280
model=AppConfig,
259
281
sources=[
260
-
Etcd.from_env(prefix="/app/", watch=True),
282
+
Etcd(
283
+
host=os.environ.get("ETCD_HOST", "127.0.0.1"),
284
+
port=int(os.environ.get("ETCD_PORT", "2379")),
285
+
prefix="/app/",
286
+
watch=True,
287
+
ca_cert=os.environ.get("ETCD_CA_CERT"),
288
+
cert_key=os.environ.get("ETCD_CERT_KEY"),
289
+
cert_cert=os.environ.get("ETCD_CERT_CERT"),
290
+
),
261
291
],
262
292
)
263
293
@@ -341,7 +371,15 @@ Watch events include:
341
371
cfg = Config(
342
372
model=AppConfig,
343
373
sources=[
344
-
Etcd.from_env(prefix="/app/", watch=True),
374
+
Etcd(
375
+
host=os.environ.get("ETCD_HOST", "127.0.0.1"),
376
+
port=int(os.environ.get("ETCD_PORT", "2379")),
377
+
prefix="/app/",
378
+
watch=True,
379
+
ca_cert=os.environ.get("ETCD_CA_CERT"),
380
+
cert_key=os.environ.get("ETCD_CERT_KEY"),
381
+
cert_cert=os.environ.get("ETCD_CERT_CERT"),
382
+
),
345
383
],
346
384
)
347
385
@@ -371,7 +409,15 @@ For automatic updates, use ``ConfigStore`` which handles watch automatically:
371
409
cfg = Config(
372
410
model=AppConfig,
373
411
sources=[
374
-
Etcd.from_env(prefix="/app/", watch=True),
412
+
Etcd(
413
+
host=os.environ.get("ETCD_HOST", "127.0.0.1"),
414
+
port=int(os.environ.get("ETCD_PORT", "2379")),
415
+
prefix="/app/",
416
+
watch=True,
417
+
ca_cert=os.environ.get("ETCD_CA_CERT"),
418
+
cert_key=os.environ.get("ETCD_CERT_KEY"),
419
+
cert_cert=os.environ.get("ETCD_CERT_CERT"),
420
+
),
375
421
],
376
422
)
377
423
@@ -402,7 +448,14 @@ Etcd source can be combined with other sources. Later sources override earlier o
402
448
cfg = Config(
403
449
model=AppConfig,
404
450
sources=[
405
-
Etcd.from_env(prefix="/app/"), # Load from etcd
451
+
Etcd(
452
+
host=os.environ.get("ETCD_HOST", "127.0.0.1"),
453
+
port=int(os.environ.get("ETCD_PORT", "2379")),
454
+
prefix="/app/",
455
+
ca_cert=os.environ.get("ETCD_CA_CERT"),
456
+
cert_key=os.environ.get("ETCD_CERT_KEY"),
457
+
cert_cert=os.environ.get("ETCD_CERT_CERT"),
458
+
), # Load from etcd
406
459
Env(), # Env can override etcd
407
460
CLI(), # CLI can override all
408
461
],
@@ -478,7 +531,7 @@ Nested Configuration
478
531
Best Practices
479
532
--------------
480
533
481
-
1. **Use Environment Variables**: Use ``Etcd.from_env()`` instead of hardcoding connection parameters
534
+
1. **Use Environment Variables**: Read environment variables yourself and pass them to ``Etcd()`` instead of hardcoding connection parameters
482
535
2. **Use .env Files**: Manage configuration in development with ``.env`` files
483
536
3. **Enable Watch**: Enable ``watch=True`` for configurations that need dynamic updates
484
537
4. **Use Prefixes**: Use different etcd prefixes for different applications to avoid key conflicts
0 commit comments