{"id":16174233,"url":"https://github.com/relaxdiego/python-based-operator","last_synced_at":"2025-03-19T00:30:56.710Z","repository":{"id":44728741,"uuid":"274041413","full_name":"relaxdiego/python-based-operator","owner":"relaxdiego","description":"A Kubernetes operator written in Python. It's meant as an introduction for those just starting out their journey writing operators.","archived":false,"fork":false,"pushed_at":"2022-12-08T11:02:15.000Z","size":106,"stargazers_count":25,"open_issues_count":1,"forks_count":14,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-15T04:44:10.910Z","etag":null,"topics":["concept-operator","helm","k8s-operator","kubernetes","microk8s","operators","prometheus-cluster","prometheus-operator","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/relaxdiego.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-22T04:42:34.000Z","updated_at":"2024-09-02T20:11:19.000Z","dependencies_parsed_at":"2023-01-25T04:45:56.525Z","dependency_job_id":null,"html_url":"https://github.com/relaxdiego/python-based-operator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relaxdiego%2Fpython-based-operator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relaxdiego%2Fpython-based-operator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relaxdiego%2Fpython-based-operator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/relaxdiego%2Fpython-based-operator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/relaxdiego","download_url":"https://codeload.github.com/relaxdiego/python-based-operator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243955805,"owners_count":20374373,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concept-operator","helm","k8s-operator","kubernetes","microk8s","operators","prometheus-cluster","prometheus-operator","python"],"created_at":"2024-10-10T04:24:16.318Z","updated_at":"2025-03-19T00:30:56.458Z","avatar_url":"https://github.com/relaxdiego.png","language":"Python","readme":"# A Python-Based k8s Operator\n\nThis project demonstrates how you can use plain Python to create a\nfully-functional k8s operator. To avoid re-inventing the wheel, Helm 3 is\nused internally by the operator to maintain the releases of the\napplication it manages. However, if you want to customize this project\nto fit your needs and if your needs don't include Helm 3, you may safely\nremove that requirement from the code.\n\nThis operator can manage multiple instances of Prometheus. You can modify\nit to manage other types of applications if you wish. Prometheus was just\nchosen in this case because most engineers in the DevOps space are already\nfamiliar with it.\n\nYou instantiate Prometheus in a namespace by creating a PrometheusCluster\ncustom resource in said namespace. A simple instance with defaults can be\ncreated via the following custom resource:\n\n```yaml\napiVersion: relaxdiego.com/v1alpha1\nkind: PrometheusCluster\nmetadata:\n  name: simple-prometheus-instance\nspec: {}\n```\n\n## Demo\n\nWatch it [on YouTube](https://youtu.be/lsuW9XGWosQ).\n\n\n## Prior Art\n\nInspired by [this Medium article](https://link.medium.com/rC0Nqcrgw7)\n\n\n## Dependencies\n\n1. Kubernetes 1.18 or higher\n2. [Docker CE](https://docs.docker.com/engine/install/) 17.06 or higher (For building the operator image)\n3. GNU Make\n\n\n## Optionally Use Microk8s\n\nUse [microk8s](https://microk8s.io/) for testing this operator. It will\nmake your life so much easier. Go on, I'll wait!\n\nOnce you have microk8s installed, run the following:\n\n```\nmicrok8s.enable dns rbac ingress registry storage\nmkdir -p ~/.kube\nmicrok8s.config \u003e ~/.kube/config\nkubectl cluster-info\n```\n\n## Try It Out!\n\n#### Build and Deploy the Operator\n\nThe following will build the image and deploy it in the `python-based-operator`\nnamespace.\n\n```\nmake image deploy tag=localhost:32000/python-based-operator\n```\n\nNOTE: The address `localhost:32000` is the address of the microk8s registry\n      addon that we enabled in the previous step. If you're not using microk8s,\n      just replace that address with either another registry address that you\n      have access to, or your Docker Hub username.\n\n\n#### Create Your First Prometheus Cluster\n\nThere are sample PrometheusCluster files under the `examples/` directory. After\ndeploying the operator, create a sample Prometheus cluster via kubectl:\n\n```\nkubectl create ns simple-prometheus-cluster\nkubectl config set-context --current --namespace=simple-prometheus-cluster\nkubectl apply -f examples/simple.yaml\n```\n\n#### Scale Up Your Prometheus Cluster\n\n```\nkubectl edit -f examples/simple.yaml\n```\n\nGo to the `replicas:` field and change its value. Quit, save, then see your\nnumber of prometheus pods scale accordingly.\n\n\n#### Delete the Prometheus Cluster While Retaining its Data\n\nJust run:\n\n```\nkubectl delete -f examples/simple.yaml\n```\n\nThe volumes assocated with the pods will be retained and will be re-attached to\nthe correct pod later on if you want to revive them.\n\n\n#### Delete the Operator and Everything in the Example Namespace\n\n```\nkubectl delete -f examples/simple.yaml\nmake uninstall\nkubectl delete ns simple-prometheus-cluster\n```\n\n\n## Development Guide\n\n\n#### Dependencies\n\n1. Python 3.8.x or higher\n\nTIP: Make your life easier by using [pyenv](https://github.com/pyenv/pyenv-installer)\n\n\n#### Prepare Your Virtualenv (venv style)\n\n```\npython3 -m venv --prompt prometheus-operator .venv\nsource .venv/bin/activate\n```\n\n#### Prepare Your Virtualenv (pyenv-virtual style)\n\n```\npyenv virtualenv 3.8.3 prometheus-operator-3.8.3\n```\n\nMore on [pyenv-virtualenv](https://github.com/pyenv/pyenv-virtualenv):\n\n\n#### Install Development Dependencies\n\n```\nmake dependencies\n```\n\n\n#### Add a Development Dependency\n\n```\necho 'foo' \u003e\u003e src/requirements-dev.in\nmake dependencies\n```\n\nThe `src/requirements-dev.txt` file should now be updated and the `foo`\npackage installed in your local machine. Make sure to commit both files\nto the repo to let your teammates know of the new dependency.\n\n```\ngit add src/requirements-dev.*\ngit commit -m \"Add foo to src/requirements-dev.txt\"\ngit push origin\n```\n\n\n#### Add a Runtime Dependency\n\nAdd it to the `install_requires` argument of the `setup()` call in\n`src/setup.py`. For example:\n\n```\nsetup(\n    name=_NAME,\n    version='0.1.0',\n\n    ...\n\n    install_requires=[\n        'kubernetes\u003e=11.0.0,\u003c11.1.0',\n        'bar\u003e=1.0.0,\u003c2.0.0'\n    ],\n\n    ...\n\n)\n```\n\nAfter having added the `bar` dependency above, run the following:\n\n```\nmake dependencies\n```\n\nThe `src/requirements.txt` file should now be updated and the bar package\ninstalled in your local machine. Make sure to commit both files to the repo\nto let your teammates know of the new dependency.\n\n```\ngit add src/setup.py src/requirements.txt\ngit commit -m \"Add bar as a runtime dependency\"\ngit push origin\n```\n\n#### Run the Operator in Development Mode (Experimental)\n\nThis mode speeds up your development workflow by skipping the image creation\nprocess, opting instead for to deploying it directly on your local machine.\nTo achieve a near-production runtime environment, we will create all the\nresources in the k8s cluster except for the Deployment resource. Furthermore\nthis mode auto-generates a kubeconfig file from the Service Account we create\nin the cluster so that the operator will still be constrained by the RBAC rules\nthat we specify under `templates/rbac.yml`.\n\nIn order for Dev mode to work properly, we have to ensure that all runtime\ndependencies are installed locally. This includes [Helm 3](https://helm.sh/docs/intro/install/).\nMake sure to install that before proceeding. You do not need to manually install\nthe requirements in `src/requirements.txt` since that will be done for you\nautomatically.\n\nWhen all requirements are satisfied, go ahead and run:\n\n```\nmake deploy-dev\n```\n\nYou should see something like the following in the terminal:\n\n```\npython_based_operator.operator DEBUG   Looking for credentials...\npython_based_operator.operator DEBUG   Loading from dev kube config\npython_based_operator.operator DEBUG   Loading CustomObjectsApi client\npython_based_operator.operator INFO    Watching prometheusclusters.relaxdiego.com/v1alpha1 events\n```\n\nIf you need to make changes to the code, just press `Ctrl-C`, edit the code,\nthen run `make deploy-dev` again.\n\nIf you need something more streamlined than this, [Okteto](https://okteto.com/blog/how-to-develop-python-apps-in-kubernetes/)\nmight be something of interest to you.\n\n#### Force Re-Install Depedencies and Uninstall the Operator\n\nRun the following\n\n```\nmake reset\nmake dependencies\n```\n\nIf you want something more thorough (and potentially destructive) than that,\ndelete your virtual environment. Then start from the beginning of the\nDevelopment Guide section.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frelaxdiego%2Fpython-based-operator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frelaxdiego%2Fpython-based-operator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frelaxdiego%2Fpython-based-operator/lists"}