{"id":17681765,"url":"https://github.com/jgaskins/kubernetes","last_synced_at":"2025-09-10T05:42:59.123Z","repository":{"id":48524324,"uuid":"397357140","full_name":"jgaskins/kubernetes","owner":"jgaskins","description":"Kubernetes API client in Crystal, providing a framework for writing controllers/operators","archived":false,"fork":false,"pushed_at":"2025-03-26T22:42:25.000Z","size":105,"stargazers_count":18,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-06T18:16:17.411Z","etag":null,"topics":["kubernetes"],"latest_commit_sha":null,"homepage":"","language":"Crystal","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/jgaskins.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-17T18:36:48.000Z","updated_at":"2025-03-26T22:42:28.000Z","dependencies_parsed_at":"2023-11-14T00:29:22.833Z","dependency_job_id":"2127332f-8421-449f-9b25-19deb408f75f","html_url":"https://github.com/jgaskins/kubernetes","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/jgaskins%2Fkubernetes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fkubernetes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fkubernetes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fkubernetes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgaskins","download_url":"https://codeload.github.com/jgaskins/kubernetes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252798768,"owners_count":21805879,"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"],"created_at":"2024-10-24T09:12:08.437Z","updated_at":"2025-05-07T01:45:36.542Z","avatar_url":"https://github.com/jgaskins.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kubernetes\n\nA Kubernetes client that allows you to manage Kubernetes resources programmatically, similarly to how you might with `kubectl`.\n\n## Installation\n\nAdd this to your `shards.yml`:\n\n```yaml\ndependencies:\n  kubernetes:\n    github: jgaskins/kubernetes\n```\n\n## Usage\n\nFirst, instantiate your Kubernetes client:\n\n```crystal\nrequire \"kubernetes\"\n\nk8s = Kubernetes::Client.new(\n  server: URI.parse(\"https://#{ENV[\"KUBERNETES_SERVICE_HOST\"]}:#{ENV[\"KUBERNETES_SERVICE_PORT\"]}\"),\n  token: File.read(\"/var/run/secrets/kubernetes.io/serviceaccount/token\"),\n  certificate_file: \"/var/run/secrets/kubernetes.io/serviceaccount/ca.crt\",\n)\n```\n\nWhen you're running this inside a Kubernetes cluster, it will automatically find the Kubernetes API server, token, and cert file, so you can simplify even more:\n\n```crystal\nrequire \"kubernetes\"\n\nk8s = Kubernetes::Client.new\n```\n\nThen you can fetch information about your deployments or pods:\n\n```crystal\npp k8s.deployments\npp k8s.pods\n```\n\n### `CustomResourceDefinition`s\n\nYou can import a CRD directly from YAML. Let's say you have this CRD (taken from [the example in the Kubernetes CRD docs](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/)):\n\n```yaml\n# k8s/crd-crontab.yaml\n---\napiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\nmetadata:\n  name: crontabs.stable.example.com\nspec:\n  group: stable.example.com\n  versions:\n    - name: v1\n      served: true\n      storage: true\n      schema:\n        openAPIV3Schema:\n          type: object\n          properties:\n            spec:\n              type: object\n              properties:\n                cronSpec:\n                  type: string\n                image:\n                  type: string\n                replicas:\n                  type: integer\n  scope: Namespaced\n  names:\n    plural: crontabs\n    singular: crontab\n    kind: CronTab\n    shortNames:\n    - ct\n```\n\nWhen you run `Kubernetes.import_crd(\"k8s/crd-crontab.yaml\")`, the CRD will provide the following:\n\n1. A `CronTab` struct\n   - Represents the `spec` of the top-level `CronTab` resource in Kubernetes\n   - The top-level resource is a `Kubernetes::Resource(CronTab)`\n   - Has the following `getter` methods:\n     - `cron_spec : String`\n     - `image : String`\n     - `replicas : Int64`\n2. The following methods added to `Kubernetes::Client`:\n   - `crontabs(namespace : String? = nil) : Array(Kubernetes::Resource(CronTab))`\n     - When `namespace` is `nil`, _all_ `CronTab`s are returned\n   - `crontab(name : String, namespace : String = \"default\") : Kubernetes::Resource(CronTab)`\n   - `apply_crontab(api_version : String, kind : String, metadata, spec, force = false)`\n     - Allows you to specify a complete Kubernetes manifest programmatically\n     - On success, returns a `Kubernetes::Resource(CronTab)` representing the applied configuration\n     - On failure, returns a `Kubernetes::Status` with information about the failure\n   - `delete_crontab(name : String, namespace : String)`\n   - `watch_crontabs(namespace : String, \u0026on_change : Kubernetes::Watch(Kubernetes::Resource(CronTab)) -\u003e)`\n     - Allows you to define controllers that respond to changes in your custom resources\n\n### Building a Controller\n\nKubernetes controllers respond to changes in resources, often by making changes to other resources. For example, you might deploy a service that needs the following:\n\n- `Deployment` for a web app\n- `Service` to direct requests to that web app\n- `Ingress` to bring web requests to that service\n\nAll of these things can be configured with a `ServiceApp` CRD. Let's say we have the following CRD:\n\n```yaml\n# k8s/crd-serviceapp.yaml\n---\napiVersion: apiextensions.k8s.io/v1\nkind: CustomResourceDefinition\nmetadata:\n  name: serviceapps.example.com\nspec:\n  group: example.com\n  versions:\n    - name: v1\n      served: true\n      storage: true\n      schema:\n        openAPIV3Schema:\n          type: object\n          properties:\n            spec:\n              type: object\n              properties:\n                domain:\n                  type: string\n                command:\n                  type: array\n                  items:\n                    type: string\n                  minItems: 1\n                image:\n                  type: string\n                replicas:\n                  type: integer\n  scope: Namespaced\n  names:\n    plural: serviceapps\n    singular: serviceapp\n    kind: ServiceApp\n    shortNames:\n    - svcapp\n```\n\nAnd whenever someone creates, updates, or deletes a `ServiceApp` resource, it needs to create, update, or delete the `Deployment`, `Service`, and `Ingress` resources accordingly:\n\n```crystal\nrequire \"kubernetes\"\n\nKubernetes.import_crd \"k8s/crd-serviceapps.yaml\"\nlog = Log.for(\"serviceapps.controller\", level: :info)\n\nk8s = Kubernetes::Client.new(\n  server: URI.parse(ENV[\"K8S\"]),\n  token: ENV[\"TOKEN\"],\n  certificate_file: ENV[\"CA_CERT\"],\n)\n\nk8s.watch_serviceapps do |watch|\n  svcapp = watch.object\n\n  case watch\n  when .added?, .modified?\n    log.info { \"ServiceApp #{svcapp.metadata.namespace}/#{svcapp.metadata.name} updated\" }\n    labels = {app: svcapp.metadata.name}\n    metadata = {\n      name:      svcapp.metadata.name,\n      namespace: svcapp.metadata.namespace,\n    }\n\n    deployment = k8s.apply_deployment(\n      api_version: \"apps/v1\",\n      kind: \"Deployment\",\n      metadata: metadata,\n      spec: {\n        replicas: svcapp.spec.replicas,\n        selector: {matchLabels: labels},\n        template: {\n          metadata: {labels: labels},\n          spec: {\n            containers: [\n              {\n                image:   svcapp.spec.image,\n                command: svcapp.spec.command,\n                name:    \"web\",\n                env:     [\n                  { name: \"VAR_NAME\", value: \"var value\" },\n                  # ...\n                ],\n              },\n            ],\n          },\n        },\n      },\n    )\n    log.info { \"Deployment #{deployment} applied\" }\n\n    svc = k8s.apply_service(\n      api_version: \"v1\",\n      kind: \"Service\",\n      metadata: metadata,\n      spec: {\n        type: \"ClusterIP\",\n        selector: labels,\n        ports: [{ port: 3000 }],\n        sessionAffinity: \"None\",\n      },\n    )\n    case svc\n    in Kubernetes::Service\n      log.info { \"Service #{svc.metadata.namespace}/#{svc.metadata.name} applied\" }\n    in Kubernetes::Status\n      log.info { \"Service #{svcapp.metadata.namespace}/#{svcapp.metadata.name} could not be applied!\" }\n    end\n\n    ingress = k8s.apply_ingress(\n      api_version: \"networking.k8s.io/v1\",\n      kind: \"Ingress\",\n      metadata: metadata,\n      spec: {\n        rules: [\n          {\n            host: svcapp.spec.domain,\n            http: {\n              paths: [\n                {\n                  backend: {\n                    service: {\n                      name: metadata[:name],\n                      port: { number: 3000 },\n                    },\n                  },\n                  path: \"/\",\n                  pathType: \"Prefix\",\n                },\n              ],\n            },\n          },\n        ],\n      },\n    )\n    case ingress\n    in Kubernetes::Resource(Kubernetes::Networking::Ingress)\n      log.info { \"Ingress #{ingress.metadata.namespace}/#{ingress.metadata.name} applied\" }\n    in Kubernetes::Status\n      log.info { \"Ingress #{svcapp.metadata.namespace}/#{svcapp.metadata.name} could not be applied: #{ingress.inspect}\" }\n    end\n  when .deleted?\n    name = svcapp.metadata.name\n    namespace = svcapp.metadata.namespace\n\n    k8s.delete_ingress(name: name, namespace: namespace)\n    log.info { \"Ingress #{namespace}/#{name} deleted\" }\n    k8s.delete_service(name: name, namespace: namespace)\n    log.info { \"Service #{namespace}/#{name} deleted\" }\n    k8s.delete_deployment(name: name, namespace: namespace)\n    log.info { \"Deployment #{namespace}/#{name} deleted\" }\n  end\nend\n```\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/jgaskins/kubernetes/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Jamie Gaskins](https://github.com/jgaskins) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgaskins%2Fkubernetes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgaskins%2Fkubernetes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgaskins%2Fkubernetes/lists"}