configmap_reader 0.1.0__py3-none-any.whl

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.
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
+ )
@@ -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
+ ![GitHub](https://img.shields.io/github/license/siakhooi/configmap-reader?logo=github)
62
+ ![GitHub last commit](https://img.shields.io/github/last-commit/siakhooi/configmap-reader?logo=github)
63
+ ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/siakhooi/configmap-reader?logo=github)
64
+ ![GitHub issues](https://img.shields.io/github/issues/siakhooi/configmap-reader?logo=github)
65
+ ![GitHub closed issues](https://img.shields.io/github/issues-closed/siakhooi/configmap-reader?logo=github)
66
+ ![GitHub pull requests](https://img.shields.io/github/issues-pr-raw/siakhooi/configmap-reader?logo=github)
67
+ ![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed-raw/siakhooi/configmap-reader?logo=github)
68
+ ![GitHub top language](https://img.shields.io/github/languages/top/siakhooi/configmap-reader?logo=github)
69
+ ![GitHub language count](https://img.shields.io/github/languages/count/siakhooi/configmap-reader?logo=github)
70
+ ![Lines of code](https://img.shields.io/tokei/lines/github/siakhooi/configmap-reader?logo=github)
71
+ ![GitHub repo size](https://img.shields.io/github/repo-size/siakhooi/configmap-reader?logo=github)
72
+ ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/siakhooi/configmap-reader?logo=github)
73
+
74
+ ![Workflow](https://img.shields.io/badge/Workflow-github-purple)
75
+ ![workflow](https://github.com/siakhooi/configmap-reader/actions/workflows/build.yaml/badge.svg)
76
+ ![workflow](https://github.com/siakhooi/configmap-reader/actions/workflows/workflow-deployments.yml/badge.svg)
77
+
78
+ ![Release](https://img.shields.io/badge/Release-github-purple)
79
+ ![GitHub release (latest by date)](https://img.shields.io/github/v/release/siakhooi/configmap-reader?label=GPR%20release&logo=github)
80
+ ![GitHub all releases](https://img.shields.io/github/downloads/siakhooi/configmap-reader/total?color=33cb56&logo=github)
81
+ ![GitHub Release Date](https://img.shields.io/github/release-date/siakhooi/configmap-reader?logo=github)
82
+
83
+ ![Quality-Qlty](https://img.shields.io/badge/Quality-Qlty-purple)
84
+ [![Maintainability](https://qlty.sh/gh/siakhooi/projects/configmap-reader/maintainability.svg)](https://qlty.sh/gh/siakhooi/projects/configmap-reader)
85
+ [![Code Coverage](https://qlty.sh/gh/siakhooi/projects/configmap-reader/coverage.svg)](https://qlty.sh/gh/siakhooi/projects/configmap-reader)
86
+
87
+ ![Quality-Sonar](https://img.shields.io/badge/Quality-SonarCloud-purple)
88
+ [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
89
+ [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
90
+ [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=bugs)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
91
+ [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
92
+ [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
93
+ [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
94
+ [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
95
+ [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
96
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
97
+ [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
98
+ [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=siakhooi_configmap-reader&metric=coverage)](https://sonarcloud.io/summary/new_code?id=siakhooi_configmap-reader)
99
+ ![Sonar Violations (short format)](https://img.shields.io/sonar/violations/siakhooi_configmap-reader?server=https%3A%2F%2Fsonarcloud.io)
100
+ ![Sonar Violations (short format)](https://img.shields.io/sonar/blocker_violations/siakhooi_configmap-reader?server=https%3A%2F%2Fsonarcloud.io)
101
+ ![Sonar Violations (short format)](https://img.shields.io/sonar/critical_violations/siakhooi_configmap-reader?server=https%3A%2F%2Fsonarcloud.io)
102
+ ![Sonar Violations (short format)](https://img.shields.io/sonar/major_violations/siakhooi_configmap-reader?server=https%3A%2F%2Fsonarcloud.io)
103
+ ![Sonar Violations (short format)](https://img.shields.io/sonar/minor_violations/siakhooi_configmap-reader?server=https%3A%2F%2Fsonarcloud.io)
104
+ ![Sonar Violations (short format)](https://img.shields.io/sonar/info_violations/siakhooi_configmap-reader?server=https%3A%2F%2Fsonarcloud.io)
105
+ ![Sonar Violations (long format)](https://img.shields.io/sonar/violations/siakhooi_configmap-reader?format=long&server=http%3A%2F%2Fsonarcloud.io)
106
+
107
+ [![Generic badge](https://img.shields.io/badge/Funding-BuyMeACoffee-33cb56.svg)](https://www.buymeacoffee.com/siakhooi)
108
+ [![Generic badge](https://img.shields.io/badge/Funding-Ko%20Fi-33cb56.svg)](https://ko-fi.com/siakhooi)
109
+
110
+ ![visitors](https://hit-tztugwlsja-uc.a.run.app/?outputtype=badge&counter=ghmd-configmap-reader)
111
+
@@ -0,0 +1,8 @@
1
+ configmap_reader/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ configmap_reader/cli.py,sha256=LXXOhKSX3Yan0-EP65ubIA8fSF27oqoS3_xobtsom4k,572
3
+ configmap_reader/main.py,sha256=JX8af68SSVk8OSsMq-x2tfK6Wv5zMg9e3kFZoWN8nXI,3459
4
+ configmap_reader-0.1.0.dist-info/METADATA,sha256=GUsBn7N4f7ExAo0DnOLQDkqG8pVcz2SbnYOOh62MVXY,7371
5
+ configmap_reader-0.1.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
6
+ configmap_reader-0.1.0.dist-info/entry_points.txt,sha256=jV947-uuHjHbgW-U1sYeHl_r2-A0pkY-vF5qIMSj3b0,61
7
+ configmap_reader-0.1.0.dist-info/licenses/LICENSE,sha256=AOdG9K9C03otrAbOnh2P_Urw9y0Sc0zs6Qb0Nj-zscs,1066
8
+ configmap_reader-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.2.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ configmap-reader=configmap_reader.cli:run
3
+
@@ -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.