{"id":19878397,"url":"https://github.com/maxking/kubecrd","last_synced_at":"2025-05-02T13:30:38.913Z","repository":{"id":62574779,"uuid":"471836895","full_name":"maxking/kubecrd","owner":"maxking","description":"Python utils to work with Kubernetes and CRD","archived":false,"fork":false,"pushed_at":"2024-06-06T20:56:14.000Z","size":222,"stargazers_count":7,"open_issues_count":6,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-20T08:05:52.148Z","etag":null,"topics":["apischema","custom-resource-definition","k8s","kubernetes","kubernetes-crd","kubernetes-operators","openapi-specification"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxking.png","metadata":{"files":{"readme":"README.rst","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":"2022-03-19T23:42:57.000Z","updated_at":"2024-07-31T08:34:35.000Z","dependencies_parsed_at":"2023-01-31T02:16:02.456Z","dependency_job_id":null,"html_url":"https://github.com/maxking/kubecrd","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxking%2Fkubecrd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxking%2Fkubecrd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxking%2Fkubecrd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxking%2Fkubecrd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxking","download_url":"https://codeload.github.com/maxking/kubecrd/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252046037,"owners_count":21685935,"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":["apischema","custom-resource-definition","k8s","kubernetes","kubernetes-crd","kubernetes-operators","openapi-specification"],"created_at":"2024-11-12T17:05:29.166Z","updated_at":"2025-05-02T13:30:38.401Z","avatar_url":"https://github.com/maxking.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"========\nKube CRD\n========\n\nThe primary purpose of this project is to simplify working with Kubernetes\nCustom Resources. To achieve that it provides a base class,\n``kubecrd.KubeResourceBase`` that can create Python\ndataclassses into Kubernetes Custom Resources and also generate and install\nCustom Resource Definitions for those resource into the K8s cluster directly.\n\n  \u003e\u003e\u003e from dataclasses import dataclass, field\n  \u003e\u003e\u003e from uuid import UUID\n  \u003e\u003e\u003e from kubecrd import KubeResourceBase\n  \u003e\u003e\u003e from apischema import schema\n\n  \u003e\u003e\u003e @dataclass\n  ... class Resource(KubeResourceBase):\n  ...     __group__ = 'example.com'\n  ...     __version__ = 'v1alpha1'\n  ...\n  ...     name: str\n  ...     tags: list[str] = field(\n  ...         default_factory=list,\n  ...         metadata=schema(\n  ...            description='regroup multiple resources',\n  ...            unique=False,\n  ...         ),\n  ...     )\n\n  \u003e\u003e\u003e print(Resource.crd_schema())\n  apiVersion: apiextensions.k8s.io/v1\n  kind: CustomResourceDefinition\n  metadata:\n    name: resources.example.com\n  spec:\n    group: example.com\n    names:\n      kind: Resource\n      plural: resources\n      singular: resource\n    scope: Namespaced\n    versions:\n    - name: v1alpha1\n      schema:\n        openAPIV3Schema:\n          properties:\n            spec:\n              properties:\n                name:\n                  type: string\n                tags:\n                  default: []\n                  description: regroup multiple resources\n                  items:\n                    type: string\n                  type: array\n                  uniqueItems: false\n              required:\n              - name\n              type: object\n          type: object\n      served: true\n      storage: true\n  \u003cBLANKLINE\u003e\n\n\nCreate CRD in K8s Cluster\n=========================\n\nIt is also possible to install the CRD in a cluster using a Kubernetes Client\nobject::\n\n  from from kubernetes import client, config\n  config.load_kube_config()\n  k8s_client = client.ApiClient()\n  Resource.install(k8s_client)\n\nYou can then find the resource in the cluster::\n\n  » kubectl get crds/resources.example.com\n  NAME                    CREATED AT\n  resources.example.com   2022-03-20T03:58:25Z\n\n  $ kubectl api-resources | grep example.com\n  resources     example.com/v1alpha1                  true         Resource\n\nInstallation of resource is idempotent, so re-installing an already installed\nresource doesn't raise any exceptions if ``exist_ok=True`` is passed in::\n\n  Resource.install(k8s_client, exist_ok=True)\n\n\nSerialization\n=============\n\nYou can serialize a Resource such that it is suitable to POST to K8s::\n\n  \u003e\u003e\u003e example = Resource(name='myResource', tags=['tag1', 'tag2'])\n  \u003e\u003e\u003e import json\n  \u003e\u003e\u003e print(json.dumps(example.serialize(), sort_keys=True, indent=4))\n  {\n      \"apiVersion\": \"example.com/v1alpha1\",\n      \"kind\": \"Resource\",\n      \"metadata\": {\n          \"name\": \"...\"\n      },\n      \"spec\": {\n          \"name\": \"myResource\",\n          \"tags\": [\n              \"tag1\",\n              \"tag2\"\n          ]\n      }\n  }\n\n\nObjects can also be serialized and saved directly in K8s::\n\n  example.save(k8s_client)\n\nWhere ``client`` in the above is a Kubernetes client object. You can also use\nasyncio with kubernetes_asyncio client and instead do::\n\n  await example.async_save(k8s_async_client)\n\n\nDeserialization\n===============\n\nYou can deserialize the JSON from Kubernetes API into Python CR objects.\n::\n\n   $ cat -p testdata/cr.json\n   {\n    \"apiVersion\": \"example.com/v1alpha1\",\n    \"kind\": \"Resource\",\n    \"metadata\": {\n        \"generation\": 1,\n        \"name\": \"myresource1\",\n        \"namespace\": \"default\",\n        \"resourceVersion\": \"105572812\",\n        \"uid\": \"02102eb3-968b-418a-8023-75df383daa3c\"\n    },\n    \"spec\": {\n        \"name\": \"bestID\",\n        \"tags\": [\n            \"tag1\",\n            \"tag2\"\n        ]\n    }\n    }\n\nby using ``from_json`` classmethod on the resource::\n\n   \u003e\u003e\u003e import json\n   \u003e\u003e\u003e with open('testdata/cr.json') as fd:\n   ...     json_schema = json.load(fd)\n   \u003e\u003e\u003e res = Resource.from_json(json_schema)\n   \u003e\u003e\u003e print(res.name)\n   bestID\n   \u003e\u003e\u003e print(res.tags)\n   ['tag1', 'tag2']\n\n\nThis also loads the Kubernetes's ``V1ObjectMeta`` and sets it as the\n``.metadata`` property of CR::\n\n  \u003e\u003e\u003e print(res.metadata.namespace)\n  default\n  \u003e\u003e\u003e print(res.metadata.name)\n  myresource1\n  \u003e\u003e\u003e print(res.metadata.resource_version)\n  105572812\n\nWatch\n=====\n\nIt is possible to Watch for changes in Custom Resources using the standard\nWatch API in Kubernetes. For example, to watch for all changes in Resources::\n\n\n  async for happened, resource in Resource.async_watch(k8s_async_client):\n      print(f'Resource {resource.metadata.name} was {happened}')\n\n\nOr you can use the block sync API for the watch::\n\n\n  for happened, resource in Resource.watch(k8s_client):\n      print(f'Resource {resource.metadata.name} was {happened}')\n  \n\nInstalling\n==========\n\nKube CRD can be install from PyPI using pip or your favorite tool::\n\n  $ pip install kubecrd\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxking%2Fkubecrd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxking%2Fkubecrd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxking%2Fkubecrd/lists"}