{"id":19299929,"url":"https://github.com/cloudogu/k8s-apply-lib","last_synced_at":"2026-01-08T08:45:04.186Z","repository":{"id":38242206,"uuid":"498630067","full_name":"cloudogu/k8s-apply-lib","owner":"cloudogu","description":"A go library to apply generic resources to a Kubernetes cluster","archived":false,"fork":false,"pushed_at":"2024-09-18T14:14:54.000Z","size":178,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":10,"default_branch":"develop","last_synced_at":"2024-09-19T00:24:20.003Z","etag":null,"topics":["golang-library","kubernetes"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloudogu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-06-01T07:14:14.000Z","updated_at":"2024-09-18T14:14:58.000Z","dependencies_parsed_at":"2022-08-24T14:27:20.002Z","dependency_job_id":null,"html_url":"https://github.com/cloudogu/k8s-apply-lib","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudogu%2Fk8s-apply-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudogu%2Fk8s-apply-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudogu%2Fk8s-apply-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudogu%2Fk8s-apply-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudogu","download_url":"https://codeload.github.com/cloudogu/k8s-apply-lib/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223893055,"owners_count":17220834,"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":["golang-library","kubernetes"],"created_at":"2024-11-09T23:13:10.620Z","updated_at":"2026-01-08T08:45:04.158Z","avatar_url":"https://github.com/cloudogu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# k8s-apply-lib\n\nA library to generically apply Kubernetes resources (similar to `kubectl`).\n\n## Usages\n\n### Basic\n\n```go\nfunc yourCode() {\n  yamlBytes := readFile(\"/your/file.yaml\")\n  \n  applier, _, err := apply.New(yourRestConfig, \"your-app-name\")\n  err := applier.NewBuilder().\n    WithNamespace(\"your-namespace\").\n    WithYamlResource(\"/your/file.yaml\", doc).\n    ExecuteApply()\n}\n```\n\n### Advanced: Templating included\n\nOften, some data is only available at runtime where `kustomize` does not really cut it. `k8s-apply-lib` provides of course [Go templating](https://golangdocs.com/templates-in-golang). Consider a resource file like this:\n```yaml\napiVersion: v1\nkind: Namespace\nmetadata:\n  labels:\n    something: different\n  name: {{ .Namespace }}\n---\napiVersion: v1\nkind: ServiceAccount\nmetadata:\n  name: another-service-account\n```\n\nJust add your templating data with the method `WithTemplating()`, and you're good to go! If you do like so, all occurrences of `.Namespace` in the given file will be rendered and then applied against the cluster.\n\n```go\nfunc yourCode() {\n  filename := \"/your/fileWithGoTemplating.yaml\"\n  yamlBytes := readFile(filename)\n   templateData := struct {\n     Namespace string\n   }{ Namespace: \"your-namespace\" }\n   \n  applier, _, err := apply.New(yourRestConfig, \"your-app-name\")\n  err := applier.NewBuilder().\n    WithNamespace(\"your-namespace\").\n    WithYamlResource(filename, doc).\n    WithTemplating(filename, templateData).\n    ExecuteApply()\n}\n```\n\n### Advanced: Owner Resources\n\nWhen working with your own CRDs inside a [Kubernetes Operator](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) garbage collection is a thing to be taken seriously. `k8s-apply-lib` provides a way of setting an owning resource. This way, if the owning resource is going to be deleted, the applied resources will be deleted as well. Please note, that setting an owner reference works only for namespace-scoped-to-namespace-scoped [ownership relations](https://kubernetes.io/docs/concepts/overview/working-with-objects/owners-dependents/). There can only be one owner per builder run.\n\n```go\nfunc yourCode() {\n  filename := \"/your/fileWithGoTemplating.yaml\"\n  yamlBytes := readFile(filename)\n  owner := \u0026v1.ServiceAccount{\n      TypeMeta: metav1.TypeMeta{\n         APIVersion: \"v1\",\n         Kind:       \"ServiceAccount\",\n      },\n      ObjectMeta: metav1.ObjectMeta{\n         Name:      \"le-service-account\",\n      },\n   }\n   \n  applier, _, err := apply.New(yourRestConfig, \"your-app-name\")\n  err := applier.NewBuilder().\n    WithNamespace(\"your-namespace\").\n    WithOwner(owner).\n    WithYamlResource(filename, doc).\n    ExecuteApply()\n}\n```\n### Advanced: Resource Collection\n\nSometimes a resource being applied to the Kubernetes API needs to be re-used somewhere else (f. i. a ServiceAccount must be mounted by name). `k8s-apply-lib` provides a way of matching and collecting resources while they stream through the `Applier`. `PredicatedResourceCollector` is an interface with two methods which you should implement to collect your resources:\n- `Predicate(doc YamlDocument) (bool, error)`\n  - should return true if the generic resource in the YAML document should be collected \n- `Collect(doc YamlDocument)`\n  - takes care of the actual collection of your liking\n\nPlease see the interface `PredicatedResourceCollector` in `Builder.go` for more information.\n\n```go\nfunc yourCode() {\n  filename := \"/your/file.yaml\"\n  yamlBytes := readFile(filename)\n   \n  applier, _, err := apply.New(yourRestConfig, \"your-app-name\")\n  err := applier.NewBuilder().\n    WithNamespace(\"your-namespace\").\n    WithYamlResource(filename, doc).\n    WithCollector(owner).\n    ExecuteApply()\n}\n```\n\n### Advanced: Apply Filter\n\nSometimes it is required to prevent applying a specific resource contained in a collection of yaml documents. \n`k8s-apply-lib` provides a way of filtering resources before applying them. \n`ApplyFilter` is an interface with one method which you should implement to filter your resources:\n- `Predicate(doc YamlDocument) (bool, error)`\n  - should return true if the generic resource in the YAML document should be filter, i.e., it should be applied.\n\nPlease see the interface `ApplyFilter` in `Builder.go` for more information.\n\n```go\nfunc yourCode() {\n  filename := \"/your/file.yaml\"\n  yamlBytes := readFile(filename)\n   \n  applier, _, err := apply.New(yourRestConfig, \"your-app-name\")\n  err := applier.NewBuilder().\n    WithNamespace(\"your-namespace\").\n    WithYamlResource(filename, doc).\n\tWithApplyFilter(myFilterImplementation).\n    ExecuteApply()\n}\n```\n---\n\n## What is the Cloudogu EcoSystem?\nThe Cloudogu EcoSystem is an open platform, which lets you choose how and where your team creates great software. Each service or tool is delivered as a Dogu, a Docker container. Each Dogu can easily be integrated in your environment just by pulling it from our registry.\n\nWe have a growing number of ready-to-use Dogus, e.g. SCM-Manager, Jenkins, Nexus Repository, SonarQube, Redmine and many more. Every Dogu can be tailored to your specific needs. Take advantage of a central authentication service, a dynamic navigation, that lets you easily switch between the web UIs and a smart configuration magic, which automatically detects and responds to dependencies between Dogus.\n\nThe Cloudogu EcoSystem is open source and it runs either on-premises or in the cloud. The Cloudogu EcoSystem is developed by Cloudogu GmbH under [AGPL-3.0-only](https://spdx.org/licenses/AGPL-3.0-only.html).\n\n## License\nCopyright © 2020 - present Cloudogu GmbH\nThis program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3.\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.\nYou should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.\nSee [LICENSE](LICENSE) for details.\n\n\n---\nMADE WITH :heart:\u0026nbsp;FOR DEV ADDICTS. [Legal notice / Imprint](https://cloudogu.com/en/imprint/?mtm_campaign=ecosystem\u0026mtm_kwd=imprint\u0026mtm_source=github\u0026mtm_medium=link)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudogu%2Fk8s-apply-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudogu%2Fk8s-apply-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudogu%2Fk8s-apply-lib/lists"}