https://github.com/rhecosystemappeng/patch-utils
Go module hosting utilities for patching Kubernetes resources.
https://github.com/rhecosystemappeng/patch-utils
Last synced: 11 months ago
JSON representation
Go module hosting utilities for patching Kubernetes resources.
- Host: GitHub
- URL: https://github.com/rhecosystemappeng/patch-utils
- Owner: RHEcosystemAppEng
- License: apache-2.0
- Created: 2024-09-09T17:48:35.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-14T00:45:17.000Z (over 1 year ago)
- Last Synced: 2025-08-09T21:46:52.521Z (11 months ago)
- Language: Go
- Size: 51.8 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kubernetes Operator Patch Utils
When developing Kubernetes Operators for production use, patching is often the most robust approach for updating
existing objects. This module hosts utilities for creating standard patching functions.
## Usage
### JSON Patching
```go
package yourpkg
import (
"context"
patchutils "github.com/rhecosystemappeng/patch-utils/pkg"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func yourFunc() {
// assign with your own
var ctx context.Context
var clt client.Client
var obj client.Object
// #####################################################################
// ##### Patch Labels, Annotations, or any other map[string]string #####
// #####################################################################
// patches is a list of patchutils.JsonPatch that will get executed once you execute the
// patchFunc, patchutils.PatchFunc. err is an error from the patch generation process;
// executing patchFunc might return an error arising from the API calls.
// use the wrapper function patchutils.JsonPatchMapP if you don't need the generation error.
// use the wrapper function patchutils.JsonPatchMapQ if you only need the patchFunc.
patches, patchFunc, err := patchutils.JsonPatchMap(
ctx, clt, obj, "/metadata/labels", obj.GetLabels(), map[string]string{
patchutils.SanitizeKeyForJsonPatch("your.label/first.key"): "label-value",
patchutils.SanitizeKeyForJsonPatch("your.label/second.key"): "another-label-value",
})
patchErr := patchFunc() // this statement executes the patches against the api
// #################################
// ##### Patch any Spec object #####
// #################################
// assign with your own spec
var spec *interface{}
// patch is the patchutils.JsonPatch that will get executed once you execute the patchFunc,
// patchutils.PatchFunc. err is an error from the patch generation process; executing patchFunc
// might return an error arising from the API calls.
// use the wrapper function patchutils.JsonPatchSpecP if you don't need the generation error.
// use the wrapper function patchutils.JsonPatchSpecQ if you only need the patchFunc.
patch, patchFunc, err := patchutils.JsonPatchSpec(ctx, clt, obj, spec)
patchErr := patchFunc() // this statement executes the patches against the api
// ############################################
// ##### Patch a finalizer into an object #####
// ############################################
// patch is the patchutils.JsonPatch that will get executed once you execute the patchFunc,
// patchutils.PatchFunc. err is an error from the patch generation process; executing patchFunc
// might return an error arising from the API calls.
// use the wrapper function patchutils.JsonPatchFinalizerInP if you don't need the generation error.
// use the wrapper function patchutils.JsonPatchFinalizerInQ if you only need the patchFunc.
patch, patchFunc, err := patchutils.JsonPatchFinalizerIn(ctx, clt, obj, "my.custom/cleanup-finalizer")
patchErr := patchFunc() // this statement executes the patches against the api
// ##############################################
// ##### Patch a finalizer out of an object #####
// ##############################################
// patch is the patchutils.JsonPatch that will get executed once you execute the patchFunc,
// patchutils.PatchFunc. err is an error from the patch generation process; executing patchFunc
// might return an error arising from the API calls.
// use the wrapper function patchutils.JsonPatchFinalizerOutP if you don't need the generation error.
// use the wrapper function patchutils.JsonPatchFinalizerOutQ if you only need the patchFunc.
patch, patchFunc, err := patchutils.JsonPatchFinalizerOut(ctx, clt, obj, "my.custom/cleanup-finalizer")
patchErr := patchFunc() // this statement executes the patches against the api
}
```