Init janny
Anirudh Oppiliappan x@icyphox.sh
Sun, 07 Mar 2021 21:35:24 +0530
11 files changed,
208 insertions(+),
0 deletions(-)
A
Dockerfile
@@ -0,0 +1,21 @@
+FROM python + +ENV PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 \ + PIP_NO_CACHE_DIR=off \ + PIP_DISABLE_PIP_VERSION_CHECK=on \ + PIP_DEFAULT_TIMEOUT=100 \ + POETRY_HOME="/opt/poetry" \ + POETRY_VIRTUALENVS_IN_PROJECT=true \ + POETRY_VIRTUALENVS_CREATE=false \ + POETRY_NO_INTERACTION=1 + +ENV JANNY_API_HOST="https://kubernetes.default" + +RUN pip install poetry + +COPY poetry.lock pyproject.toml /janny/ +WORKDIR /janny +RUN poetry install --no-dev +COPY . ./ +ENTRYPOINT ["python3", "-m","janny"]
A
deploy/deployment.yaml
@@ -0,0 +1,25 @@
+apiVersion: apps/v1 +kind: Deployment +metadata: + name: janny + labels: + app: janny + annotations: + janny.ttl: "2m" +spec: + selector: + matchLabels: + app: janny + replicas: 1 + strategy: + type: RollingUpdate + template: + metadata: + labels: + app: janny + spec: + serviceAccountName: janny + containers: + - name: janny + image: janny:latest + imagePullPolicy: IfNotPresent
A
janny/auth.py
@@ -0,0 +1,23 @@
+import requests +import logging +import os + +logging.basicConfig(level=logging.INFO) + + +def kube_auth(): + session = requests.Session() + + # We're in-cluster + if not os.path.exists(os.path.expanduser("~/.kube/config")): + with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as f: + token = f.read() + session.verify = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + session.headers.update({"Authorization": f"Bearer {token}"}) + logging.info("Authenticated with the API server") + else: + logging.info("Not in-cluster, continuing as is") + + return session + +SESSION = kube_auth()
A
janny/config.py
@@ -0,0 +1,3 @@
+import os + +API_HOST = os.getenv("JANNY_API_HOST") or "https://kubernetes.default"
A
janny/main.py
@@ -0,0 +1,20 @@
+from janny.utils import get + +def get_resources(): + apis = get("/apis/") + apiv1 = get("/api/v1") + + resource_objs = list() + resource_objs.append(apiv1.resources) + + for g in apis.groups: + resource_objs.append(get("/apis/" + g.preferredVersion.groupVersion).resources) + + for resources in resource_objs: + for r in resources: + if "/" not in r.name and r.namespaced: + yield r.name + +def main(): + for r in get_resources(): + print(r)
A
janny/utils.py
@@ -0,0 +1,13 @@
+import logging +import json +from types import SimpleNamespace + +from janny.config import API_HOST +from janny.auth import SESSION + +def get(path): + """Convert a JSON response into a Python object""" + s = SESSION + data = s.get(API_HOST + path).content + obj = json.loads(data, object_hook=lambda d: SimpleNamespace(**d)) + return obj
A
poetry.lock
@@ -0,0 +1,81 @@
+[[package]] +name = "certifi" +version = "2020.12.5" +description = "Python package for providing Mozilla's CA Bundle." +category = "main" +optional = false +python-versions = "*" + +[[package]] +name = "chardet" +version = "4.0.0" +description = "Universal encoding detector for Python 2 and 3" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + +[[package]] +name = "idna" +version = "2.10" +description = "Internationalized Domain Names in Applications (IDNA)" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" + +[[package]] +name = "requests" +version = "2.25.1" +description = "Python HTTP for Humans." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + +[package.dependencies] +certifi = ">=2017.4.17" +chardet = ">=3.0.2,<5" +idna = ">=2.5,<3" +urllib3 = ">=1.21.1,<1.27" + +[package.extras] +security = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)"] +socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"] + +[[package]] +name = "urllib3" +version = "1.26.3" +description = "HTTP library with thread-safe connection pooling, file post, and more." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" + +[package.extras] +brotli = ["brotlipy (>=0.6.0)"] +secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] +socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] + +[metadata] +lock-version = "1.1" +python-versions = "^3.8" +content-hash = "c7b0119ca233bd369221338d4e3bd93bfe7ed46cd9c788ddccc23908d1a25c28" + +[metadata.files] +certifi = [ + {file = "certifi-2020.12.5-py2.py3-none-any.whl", hash = "sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830"}, + {file = "certifi-2020.12.5.tar.gz", hash = "sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c"}, +] +chardet = [ + {file = "chardet-4.0.0-py2.py3-none-any.whl", hash = "sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5"}, + {file = "chardet-4.0.0.tar.gz", hash = "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa"}, +] +idna = [ + {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"}, + {file = "idna-2.10.tar.gz", hash = "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6"}, +] +requests = [ + {file = "requests-2.25.1-py2.py3-none-any.whl", hash = "sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e"}, + {file = "requests-2.25.1.tar.gz", hash = "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804"}, +] +urllib3 = [ + {file = "urllib3-1.26.3-py2.py3-none-any.whl", hash = "sha256:1b465e494e3e0d8939b50680403e3aedaa2bc434b7d5af64dfd3c958d7f5ae80"}, + {file = "urllib3-1.26.3.tar.gz", hash = "sha256:de3eedaad74a2683334e282005cd8d7f22f4d55fa690a2a1020a416cb0a47e73"}, +]
A
pyproject.toml
@@ -0,0 +1,16 @@
+[tool.poetry] +name = "janny" +version = "0.1.0" +description = "Delete Kubernetes resources after a set time" +authors = ["Anirudh Oppiliappan <x@icyphox.sh>"] +license = "MIT" + +[tool.poetry.dependencies] +python = "^3.8" +requests = "^2.25.1" + +[tool.poetry.dev-dependencies] + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api"