{"id":18621264,"url":"https://github.com/yiling-j/pharos","last_synced_at":"2026-02-23T01:32:21.172Z","repository":{"id":51085275,"uuid":"355053556","full_name":"Yiling-J/pharos","owner":"Yiling-J","description":"Managing Kubernetes resources in Python(Django ORM style)","archived":false,"fork":false,"pushed_at":"2021-05-24T15:14:46.000Z","size":247,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T15:07:53.776Z","etag":null,"topics":["kubernetes","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yiling-J.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":"2021-04-06T04:13:26.000Z","updated_at":"2023-03-12T11:05:52.000Z","dependencies_parsed_at":"2022-09-02T08:33:26.932Z","dependency_job_id":null,"html_url":"https://github.com/Yiling-J/pharos","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yiling-J%2Fpharos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yiling-J%2Fpharos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yiling-J%2Fpharos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yiling-J%2Fpharos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yiling-J","download_url":"https://codeload.github.com/Yiling-J/pharos/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248329596,"owners_count":21085565,"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":["kubernetes","python"],"created_at":"2024-11-07T04:10:06.144Z","updated_at":"2025-10-25T11:45:02.929Z","avatar_url":"https://github.com/Yiling-J.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pharos\n\n![Workflow](https://github.com/Yiling-J/pharos/actions/workflows/main.yaml/badge.svg)\n\nManaging Kubernetes resources in Python.\n\n\n## Installation\n\n```\npip install pharos-k8s\n```\n\n## Example\n\n#### create client\n\n```python\nfrom pharos.client import Client\n\n\nclient = Client('YOUR_PATH/.kube/config')\n\n# if you have multiple context in one config file\nclient = Client('YOUR_PATH/.kube/config', context='context_name_you_want')\n\n# switch client context\nclient.use_context('new_context_name')\n\n```\n\n#### create client with settings\n\n```python\nfrom pharos.client import Client\n\n\nclient = Client('YOUR_PATH/.kube/config', disable_compress=True, chunk_size=500)\n\n# access settings\nclient.settings.chunk_size\n\n```\n\nPharos default settings\n\n```python\n{\n    \"disable_compress\": False,  # disable gzip\n    \"enable_chunk\": True,  # enable chunk\n    \"chunk_size\": 200,  # chunk size\n    \"template_engine\": \"pharos.jinja.JinjaEngine\",  # templating engine\n    \"jinja_loader\": None,  # loader for Jinja template\n}\n\n```\n\nIf you enable [chunk](https://kubernetes.io/docs/reference/using-api/api-concepts/#retrieving-large-results-sets-in-chunks), Pharos will use `limit/continue` parameters to retrieve API results in small chunks, avoiding large responses.\n\n\n#### basic query syntax, follow Django ORM style. See all available resources in models.py\n\n```python\nfrom pharos.models import Deployment\nfrom pharos.client import Client\n\n\nclient = Client('YOUR_PATH/.kube/config')\n\n# filter or get\ndeployments = Deployment.objects.using(client).filter(namespace='default')\ndeployment = deployments[0]\ndeployment = Deployment.objects.using(client).get(name='foo', namespace='default')\n\n# print configuration yaml\nprint(deployment.yaml)\n\n# print configuration json object\nprint(deployment.k8s_object)\n\n# get pods owned by deployment, notice that no ```.using(client)``` here\npods = deployment.pods.all()\n\n# select labels, also support chain select\nDeployment.objects.using(client).filter(selector='app=test,version=v1')\nDeployment.objects.using(client).filter(selector='app=test').filter(selector='version=v1')\n\n# limit results\npods = Pod.objects.all().limit(100)\n\n# various lookups\nDeployment.objects.using(client).filter(name__contains='foo')\nDeployment.objects.using(client).filter(name__startswith='foo')\nDeployment.objects.using(client).filter(name__in=['foo', 'bar'])\nDeployment.objects.using(client).filter(replicas__gt=1)\nDeployment.objects.using(client).filter(replicas__lt=5)\n\n\n# refresh query\npods_refreshed = pods.all()\n\n# refresh instance\npod = pods[0]\npod.refresh()\n\n```\n\n#### extend existing model\n\n```python\nfrom pharos.models import Deployment\nfrom pharos.client import Client\nfrom pharos import fields\n\n\nclass MyDeployment(Deployment):\n    uid = fields.JsonPathField(path=\"metadata.uid\")\n    created = fields.DateTimeField(path=\"metadata.creationTimestamp\")\n\n\ndeployment = MyDeployment.objects.using(client).all()[0]\nprint(deployment.uid)\nprint(deployment.created)\n\n# you can use filter and lookups on your fields\nMyDeployment.objects.using(client).filter(uid='123')\nMyDeployment.objects.using(client).filter(created__gt=datetime(2010, 1, 1, tzinfo=timezone.utc))\n\n```\n\n#### custom resource example\n\ncreate custom resource definition\n```python\nfrom pharos.models import CustomResourceDefinition\n\n\nCustomResourceDefinition.objects.using(client).create('crontab.yaml', {})\n\n```\n\nadd model\n```python\nfrom pharos.models import Model\n\n\nclass CronTab(Model):\n\n    class Meta:\n        api_version = \"stable.example.com/v1\"\n        kind = \"CronTab\"\n\n```\n\n#### template engine\nPharos using pluggable template engine for resource creating/updating. A template engine means\ninput an identifier string and a json serializable object, output json k8s configuration.\nThen Pharos can use that configuration in creating/updating.\nFor example, the default template engine is Jinja, so the identifier string would be path to your yaml\ntemplate, and json serializable object would be variables used in the template.\n\nPharos add 2 annotations to your resource automatically, one for identifier and one for variable, in\nthis way when updating resource, you don't need to repeat the same thing twice.\n\n\n#### create resource(Jinja example)\n\n```python\nfrom jinja2 import FileSystemLoader\nfrom pharos.models import Deployment\nfrom pharos.client import Client\n\n\n# jinja_loader settings is required because pharos need to know\n# where to find template\nclient = Client('config', jinja_loader=FileSystemLoader('./templates/'))\n\n# jinja is the default engine, for other template engines\n# client = Client('config', template_engine='your_template_engine_class')\n\n# render template and print yaml\nDeployment.objects.using(client).render('test.yaml', {'foo': 'bar'})\n\n# here test.yaml is template and {'foo': 'bar'} is variable\ndeployment = Deployment.objects.using(client).create('test.yaml', {'foo': 'bar'})\n\n# dry run\ndeployment = Deployment.objects.using(client).create('test.yaml', {'foo': 'bar'}, dry_run=True)\n\n# access template and variable\ntemplate = deployment.template\nvariable = deployment.variable.get()\nvariable_data = variable.data\n\n```\n\n#### update resource(Jinja example)\n\n```python\nfrom jinja2 import FileSystemLoader\nfrom pharos.models import Deployment\nfrom pharos.client import Client\n\n\nclient = Client('config', jinja_loader=FileSystemLoader('./templates/'))\n\n# template change\ndeployment = Deployment.objects.using(client).all()[0]\ndeployment.deploy()\n\n# dry run\ndeployment.deploy(dry_run=True)\n\n# also change variable\ndeployment.set_variable({'bar': 'foo'})\ndeployment.deploy()\n\n```\n\n#### migrate existing resource\nmigrate will replace existing resource with provided template and variable\n\n```python\nfrom jinja2 import FileSystemLoader\nfrom pharos.models import Deployment\nfrom pharos.client import Client\n\n\nclient = Client('config', jinja_loader=FileSystemLoader('./templates/'))\n\ndeployment = Deployment.objects.using(client).all()[0]\ndeployment.sync('test.yaml', {'foo': 'bar'})\n\n# dry run\ndeployment.sync('test.yaml', {'foo': 'bar'}, dry_run=True)\n\n```\n\n\n#### delete resource\n\n```python\nfrom pharos.models import Deployment\nfrom pharos.client import Client\n\n\nclient = Client('YOUR_PATH/.kube/config')\n\ndeployment = Deployment.objects.using(client).filter(namespace='default')[0]\ndeployment.delete()\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiling-j%2Fpharos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiling-j%2Fpharos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiling-j%2Fpharos/lists"}