{"id":31816671,"url":"https://github.com/splunk/k8s-yaml-patch","last_synced_at":"2025-10-11T09:57:38.418Z","repository":{"id":64307025,"uuid":"171958156","full_name":"splunk/k8s-yaml-patch","owner":"splunk","description":"jsonnet library to patch objects loaded from yaml","archived":false,"fork":false,"pushed_at":"2023-10-24T01:20:33.000Z","size":27,"stargazers_count":16,"open_issues_count":1,"forks_count":4,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-06-20T12:59:54.301Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","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/splunk.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":"2019-02-21T22:58:24.000Z","updated_at":"2024-03-22T00:36:07.000Z","dependencies_parsed_at":"2024-06-20T12:17:16.441Z","dependency_job_id":"90106700-6df6-4c18-9a3a-8246119d4e0e","html_url":"https://github.com/splunk/k8s-yaml-patch","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/splunk/k8s-yaml-patch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/splunk%2Fk8s-yaml-patch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/splunk%2Fk8s-yaml-patch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/splunk%2Fk8s-yaml-patch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/splunk%2Fk8s-yaml-patch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/splunk","download_url":"https://codeload.github.com/splunk/k8s-yaml-patch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/splunk%2Fk8s-yaml-patch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006743,"owners_count":26084185,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-10-11T09:57:35.725Z","updated_at":"2025-10-11T09:57:38.401Z","avatar_url":"https://github.com/splunk.png","language":"Go","readme":"k8s-yaml-patch\n===\n\n[![Build Status](https://travis-ci.org/splunk/k8s-yaml-patch.svg?branch=master)](https://travis-ci.org/splunk/k8s-yaml-patch)\n\n_ashes to ashes, dust to dust, YAML to YAML..._\n\nk8s-yaml-patch is a jsonnet library that can patch Kubernetes objects loaded from YAML documents for environment-specific\nruntime configurations.\n\nConventional wisdom tells us that there are exactly two ways to create K8s objects using jsonnet:\n\n* Load a JSON or YAML file as-is with no modifications.\n* Create an object from scratch using jsonnet and env-specific params, perhaps using helper libraries like `ksonnet-lib`.\n\nThis library does it the third way by loading YAML documents and patching them for environment specific parameters.\nThe rationale is that developers get used to the YAML syntax when using `kubectl` for debugging anyway and hiding the\nYAML in favor of a DSL does more harm than good.\n\nIt is not _elegant_ (it makes some assumptions that can offend purists) or _complete_\n(does not have a full set of mutating operations for all possible attributes) but we have found it to be\n_useful_. It supports common mutations to common objects with specialized methods and trusts the\nuser to apply jsonnet object transforms for the more complex use-cases. It can be easily extended for types and methods.\n\nA simple example\n---\n\n* YAML file: `service.yaml`\n\n```yaml\n---\napiVersion: v1\nkind: ServiceAccount\nmetadata:\n    name: svc1\n---\napiVersion: v1\nkind: ConfigMap\nmetadata:\n    name: svc1-config\ndata:\n    index.html: insert-real-value-here\n---\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n  labels:\n    app: svc1\n  name: svc1\nspec:\n  replicas: 1\n  template:\n    metadata:\n      labels:\n        app: svc1\n    spec:\n      containers:\n      - image: nginx:latest\n        imagePullPolicy: Always\n        name: main\n        ports:\n        - containerPort: 8080\n        volumeMounts:\n        - mountPath: /web\n          name: content\n          readOnly: true\n      serviceAccountName: svc1\n      volumes:\n      - name: content\n        configMap:\n          name: svc1-config\n```\n\n* jsonnet file `service.jsonnet`\n\n```jsonnet\n\nlocal patchlib = import 'patch.libsonnet'; // the patch library\n\n// parse the YAML string loaded from the file as a map\n// Objects are keyed by \u003cname\u003e\u003cdot\u003e\u003ckind\u003e\nlocal map = patchlib.parseYamlAsMap(importstr './service.yaml');\n\n// extract the configmap and deployment objects\nlocal cm = map.configmap('svc1-config.ConfigMap');\nlocal deploy = map.deployment('svc1.Deployment');\n\n// get runtime params from somewhere\nlocal params = {\n    replicas: 2,\n    image: 'nginx:stable',\n    indexContent: '\u003ch1\u003eHello world\u003c/h1\u003e',\n};\n\n// patch objects that you want, every object has a \"key\" attribute that is the\n// object key.\nlocal componentMap = map {\n    [cm.key]: cm.patchDataValue('index.html', params.indexContent),\n    [deploy.key]: deploy.patchImage('main', params.image)\n                        .patchReplicas(params.replicas),\n};\n\n// turn the map back to a list and emit it\ncomponentMap.list\n```\n\nMain ideas\n---\n\n* Turn a YAML containing multiple documents into a map so that you can pick individual objects\n  for patching. The keys in this map are of the form `\u003cobject-name\u003e.\u003ckind\u003e`. Specifically, the key does not\n  include namespace or API version. This strategy makes cluster and namespaced objects look uniform,\n  allows the namespace to be unspecified in the YAML for default processing and does not depend on\n  API version that can change over time when upgrading k8s.\n\n  This sets the limitation that you cannot load a file that contains two objects with the same name and\n  kind, differing only in namespace. You'll probably never hit this issue in practice. If you do, just\n  load two files into separate objects and concatenate the results in the end.\n\n* Specialized type wrappers for common objects (e.g. deployment, daemonset, configmap, secret etc.)\n  can be gotten from the map. Objects of a type for which no specialization exists can be accessed\n  via the `keyed` method. Such objects have an extra `key` attribute that replays their key.\n\n* Most methods center around fixing up the pod and container templates since this is the 90% use-case.\n\n* Many methods require a named entity that is being patched to be already part of the YAML to\n  guard against accidental typos.\n\n* The library is extensible for addition of methods to specific types that you may need as well\n  as addition of new types that do not have any specializations defined.\n\nThis way most objects are fully specified in the YAML and the patching code clearly shows the runtime\nattributes that are being customized.\n\nAvailable types and methods\n---\n\nThe `jsonnet` files under [tests/testdata](tests/testdata) contain examples of all types and methods supported for\neach one. It also has an example of how to extend the library, in the `extending` subdirectory.\n\n### Sign the CLA\n\nFollow the steps here [cla-assistant](https://github.com/splunk/cla-agreement)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsplunk%2Fk8s-yaml-patch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsplunk%2Fk8s-yaml-patch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsplunk%2Fk8s-yaml-patch/lists"}