configmap_reader 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- configmap_reader-0.1.0/LICENSE +21 -0
- configmap_reader-0.1.0/PKG-INFO +111 -0
- configmap_reader-0.1.0/README.md +87 -0
- configmap_reader-0.1.0/pyproject.toml +47 -0
- configmap_reader-0.1.0/src/configmap_reader/__init__.py +0 -0
- configmap_reader-0.1.0/src/configmap_reader/cli.py +25 -0
- configmap_reader-0.1.0/src/configmap_reader/main.py +123 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Siak Hooi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: configmap_reader
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Kafka Mock Messages Sender
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: kafka
|
|
8
|
+
Author: Siak Hooi
|
|
9
|
+
Author-email: siakhooi@gmail.com
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Topic :: Utilities
|
|
15
|
+
Requires-Dist: fastapi (>=0.123.9,<0.124.0)
|
|
16
|
+
Requires-Dist: kubernetes (>=34.1.0,<35.0.0)
|
|
17
|
+
Requires-Dist: uvicorn[standard] (>=0.38.0,<0.39.0)
|
|
18
|
+
Project-URL: Bug Tracker, https://github.com/siakhooi/configmap-reader/issues
|
|
19
|
+
Project-URL: Documentation, https://github.com/siakhooi/configmap-reader/wiki
|
|
20
|
+
Project-URL: Homepage, https://github.com/siakhooi/configmap-reader
|
|
21
|
+
Project-URL: Repository, https://github.com/siakhooi/configmap-reader
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# configmap-reader
|
|
25
|
+
|
|
26
|
+
microservice to read and return content of a configmap
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Deploy to clusters
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
kubectl apply -f ./configmap-reader
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Use
|
|
36
|
+
|
|
37
|
+
### Port forwarding
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
kubectl port-forward svc/configmap-reader 8080:80
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Test
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
curl http://localhost:8080/config
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- edit the configmap `configmap-reader-data` and call again will return latest value
|
|
50
|
+
|
|
51
|
+
## Links
|
|
52
|
+
|
|
53
|
+
- https://hub.docker.com/r/siakhooi/configmap-reader
|
|
54
|
+
- https://pypi.org/project/configmap_reader/
|
|
55
|
+
- https://github.com/siakhooi/configmap-reader
|
|
56
|
+
- https://sonarcloud.io/project/overview?id=siakhooi_configmap-reader
|
|
57
|
+
- https://qlty.sh/gh/siakhooi/projects/configmap-reader
|
|
58
|
+
|
|
59
|
+
## Badges
|
|
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
|
+
[](https://qlty.sh/gh/siakhooi/projects/configmap-reader)
|
|
85
|
+
[](https://qlty.sh/gh/siakhooi/projects/configmap-reader)
|
|
86
|
+
|
|
87
|
+

|
|
88
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
89
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
90
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
91
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
92
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
93
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
94
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
95
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
96
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
97
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
98
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
99
|
+

|
|
100
|
+

|
|
101
|
+

|
|
102
|
+

|
|
103
|
+

|
|
104
|
+

|
|
105
|
+

|
|
106
|
+
|
|
107
|
+
[](https://www.buymeacoffee.com/siakhooi)
|
|
108
|
+
[](https://ko-fi.com/siakhooi)
|
|
109
|
+
|
|
110
|
+

|
|
111
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# configmap-reader
|
|
2
|
+
|
|
3
|
+
microservice to read and return content of a configmap
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Deploy to clusters
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
kubectl apply -f ./configmap-reader
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Use
|
|
13
|
+
|
|
14
|
+
### Port forwarding
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
kubectl port-forward svc/configmap-reader 8080:80
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Test
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
curl http://localhost:8080/config
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- edit the configmap `configmap-reader-data` and call again will return latest value
|
|
27
|
+
|
|
28
|
+
## Links
|
|
29
|
+
|
|
30
|
+
- https://hub.docker.com/r/siakhooi/configmap-reader
|
|
31
|
+
- https://pypi.org/project/configmap_reader/
|
|
32
|
+
- https://github.com/siakhooi/configmap-reader
|
|
33
|
+
- https://sonarcloud.io/project/overview?id=siakhooi_configmap-reader
|
|
34
|
+
- https://qlty.sh/gh/siakhooi/projects/configmap-reader
|
|
35
|
+
|
|
36
|
+
## Badges
|
|
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
|
+
[](https://qlty.sh/gh/siakhooi/projects/configmap-reader)
|
|
62
|
+
[](https://qlty.sh/gh/siakhooi/projects/configmap-reader)
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
66
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
67
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
68
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
69
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
70
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
71
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
72
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
73
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
74
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
75
|
+
[](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
|
|
76
|
+

|
|
77
|
+

|
|
78
|
+

|
|
79
|
+

|
|
80
|
+

|
|
81
|
+

|
|
82
|
+

|
|
83
|
+
|
|
84
|
+
[](https://www.buymeacoffee.com/siakhooi)
|
|
85
|
+
[](https://ko-fi.com/siakhooi)
|
|
86
|
+
|
|
87
|
+

|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "configmap_reader"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Kafka Mock Messages Sender"
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Siak Hooi",email = "siakhooi@gmail.com"}
|
|
7
|
+
]
|
|
8
|
+
keywords = [ "kafka" ]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 4 - Beta",
|
|
11
|
+
"Environment :: Console",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Topic :: Utilities"
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
license = {text = "MIT"}
|
|
17
|
+
readme = "README.md"
|
|
18
|
+
requires-python = ">=3.10"
|
|
19
|
+
dependencies = [
|
|
20
|
+
"fastapi (>=0.123.9,<0.124.0)",
|
|
21
|
+
"uvicorn[standard] (>=0.38.0,<0.39.0)",
|
|
22
|
+
"kubernetes (>=34.1.0,<35.0.0)"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
homepage = "https://github.com/siakhooi/configmap-reader"
|
|
27
|
+
repository = "https://github.com/siakhooi/configmap-reader"
|
|
28
|
+
documentation = "https://github.com/siakhooi/configmap-reader/wiki"
|
|
29
|
+
"Bug Tracker" = "https://github.com/siakhooi/configmap-reader/issues"
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
configmap-reader = 'configmap_reader.cli:run'
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
[tool.poetry]
|
|
36
|
+
packages = [{include = "configmap_reader", from = "src"}]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
[tool.poetry.group.dev.dependencies]
|
|
40
|
+
pytest = "^9.0.1"
|
|
41
|
+
pytest-cov = "^7.0.0"
|
|
42
|
+
flake8 = "^7.2.0"
|
|
43
|
+
pytest-mock = "^3.15.1"
|
|
44
|
+
|
|
45
|
+
[build-system]
|
|
46
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
47
|
+
build-backend = "poetry.core.masonry.api"
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
from . import main
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def print_to_stderr_and_exit(e: Exception, exit_code: int) -> None:
|
|
8
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
9
|
+
exit(exit_code)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def run() -> None:
|
|
13
|
+
__version__: str = version("configmap-reader")
|
|
14
|
+
|
|
15
|
+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
|
|
16
|
+
description="Read and return content of a configmap"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
parser.add_argument(
|
|
20
|
+
"-v", "--version", action="version", version=f"%(prog)s {__version__}"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
parser.parse_args()
|
|
24
|
+
|
|
25
|
+
main.run()
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
from fastapi import FastAPI, HTTPException
|
|
2
|
+
from fastapi.responses import JSONResponse, PlainTextResponse
|
|
3
|
+
import os
|
|
4
|
+
import pathlib
|
|
5
|
+
import json
|
|
6
|
+
import uvicorn
|
|
7
|
+
|
|
8
|
+
app = FastAPI()
|
|
9
|
+
|
|
10
|
+
CONFIG_DIR = os.getenv("CONFIG_DIR", "/config")
|
|
11
|
+
READ_MODE = os.getenv("READ_MODE", "volume").lower() # 'volume' or 'api'
|
|
12
|
+
CONFIGMAP_NAME = os.getenv("CONFIGMAP_NAME")
|
|
13
|
+
K8S_NAMESPACE = os.getenv("NAMESPACE") or os.getenv("K8S_NAMESPACE")
|
|
14
|
+
|
|
15
|
+
_k8s_client = None
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _get_k8s_client():
|
|
19
|
+
global _k8s_client
|
|
20
|
+
if _k8s_client is not None:
|
|
21
|
+
return _k8s_client
|
|
22
|
+
try:
|
|
23
|
+
from kubernetes import client, config
|
|
24
|
+
|
|
25
|
+
# In-cluster config first; fallback to local kubeconfig for dev
|
|
26
|
+
try:
|
|
27
|
+
config.load_incluster_config()
|
|
28
|
+
except Exception:
|
|
29
|
+
config.load_kube_config()
|
|
30
|
+
_k8s_client = client.CoreV1Api()
|
|
31
|
+
return _k8s_client
|
|
32
|
+
except Exception as e:
|
|
33
|
+
raise HTTPException(
|
|
34
|
+
status_code=500, detail=f"Failed to init Kubernetes client: {e}"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def read_config_dir() -> dict:
|
|
39
|
+
path = pathlib.Path(CONFIG_DIR)
|
|
40
|
+
if not path.exists() or not path.is_dir():
|
|
41
|
+
raise FileNotFoundError(f"Config directory not found: {CONFIG_DIR}")
|
|
42
|
+
result = {}
|
|
43
|
+
for p in path.iterdir():
|
|
44
|
+
if p.is_file():
|
|
45
|
+
try:
|
|
46
|
+
content = p.read_text(encoding="utf-8")
|
|
47
|
+
except UnicodeDecodeError:
|
|
48
|
+
continue
|
|
49
|
+
result[p.name] = content
|
|
50
|
+
return result
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def read_config_via_api() -> dict:
|
|
54
|
+
if not CONFIGMAP_NAME:
|
|
55
|
+
raise HTTPException(
|
|
56
|
+
status_code=500,
|
|
57
|
+
detail="CONFIGMAP_NAME is not set for API read mode", # noqa: E501
|
|
58
|
+
)
|
|
59
|
+
if not K8S_NAMESPACE:
|
|
60
|
+
raise HTTPException(
|
|
61
|
+
status_code=500, detail="NAMESPACE is not set for API read mode"
|
|
62
|
+
)
|
|
63
|
+
api = _get_k8s_client()
|
|
64
|
+
try:
|
|
65
|
+
cm = api.read_namespaced_config_map(
|
|
66
|
+
name=CONFIGMAP_NAME, namespace=K8S_NAMESPACE
|
|
67
|
+
)
|
|
68
|
+
except Exception as e:
|
|
69
|
+
raise HTTPException(
|
|
70
|
+
status_code=500,
|
|
71
|
+
detail=f"Failed to read ConfigMap {K8S_NAMESPACE}/{CONFIGMAP_NAME}: {e}", # noqa: E501
|
|
72
|
+
)
|
|
73
|
+
data = cm.data or {}
|
|
74
|
+
# Return as filename -> string content just like volume mode
|
|
75
|
+
return dict(data)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@app.get("/config")
|
|
79
|
+
def get_config():
|
|
80
|
+
if READ_MODE == "api":
|
|
81
|
+
data = read_config_via_api()
|
|
82
|
+
else:
|
|
83
|
+
try:
|
|
84
|
+
data = read_config_dir()
|
|
85
|
+
except FileNotFoundError as e:
|
|
86
|
+
raise HTTPException(status_code=500, detail=str(e))
|
|
87
|
+
|
|
88
|
+
if not isinstance(data, dict):
|
|
89
|
+
raise HTTPException(status_code=500, detail="Invalid config data")
|
|
90
|
+
|
|
91
|
+
status_code_raw = data.get("statusCode")
|
|
92
|
+
body = data.get("body")
|
|
93
|
+
|
|
94
|
+
if status_code_raw is None or body is None:
|
|
95
|
+
raise HTTPException(
|
|
96
|
+
status_code=500, detail="Missing required keys: statusCode or body"
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
status_code = int(status_code_raw)
|
|
101
|
+
except Exception:
|
|
102
|
+
raise HTTPException(status_code=500, detail="Invalid statusCode value")
|
|
103
|
+
|
|
104
|
+
try:
|
|
105
|
+
parsed = json.loads(body)
|
|
106
|
+
return JSONResponse(content=parsed, status_code=status_code)
|
|
107
|
+
except Exception:
|
|
108
|
+
return PlainTextResponse(content=body, status_code=status_code)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@app.get("/health")
|
|
112
|
+
def health():
|
|
113
|
+
return {"status": "ok"}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def run():
|
|
117
|
+
|
|
118
|
+
uvicorn.run(
|
|
119
|
+
"app.main:app",
|
|
120
|
+
host="0.0.0.0",
|
|
121
|
+
port=int(os.getenv("PORT", "8000")),
|
|
122
|
+
reload=False,
|
|
123
|
+
)
|