Skip to content

Commit

Permalink
Add update-configmap v1
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed Apr 18, 2024
1 parent 99d234a commit 5f1bc7a
Show file tree
Hide file tree
Showing 26 changed files with 710 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# krm-functions
Catalog of my KRM functions
# Catalog of KRM functions written by kispaljr

See the documentation of individual KRM functions in their own subfolder:
* (declarative) [upsert-resource](upsert-resource/README.md)
* [update-configmap](update-configmap/README.md)


33 changes: 33 additions & 0 deletions update-configmap/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
],
"env" : {
}
},
{
"name": "Overwrite golden test DB",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": [
"-test.run",
"TestFunction"
],
"env" : {
"WRITE_GOLDEN_OUTPUT": "yes"
}
}
]
}
13 changes: 13 additions & 0 deletions update-configmap/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#target: ghcr.io/kispaljr/update-configmap:v1

FROM golang:1.21-alpine
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY go.* ./
RUN go mod download
COPY ./ ./
RUN go build -o /usr/local/bin/function ./

FROM gcr.io/distroless/static:latest
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
ENTRYPOINT ["function"]
39 changes: 39 additions & 0 deletions update-configmap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
update-configmap KRM function
============================

## Overview

Updates key-value pairs in any ConfigMap-like KRM resource.

## Usage

Finds the ConfigMap whose name matches with the `targetName` FunctionConfig parameter, and updates the key-value pairs in its `data` field with the key-value pairs given in FunctionConfig.

The KRM function actually works for not just ConfigMaps, but for any KRM resource that has a `data` field with a type of `map[string]string`.

Can be used both declaratively and imperatively.

## FunctionConfig

The KRM function supposed to be configured via a ConfigMap.
The ConfigMap must contain the `targetName` key that contains the name of the ConfigMap-like resource to update.
All other key-value pairs in FunctionConfig are used to update the target.

## Example

This will ensure that the ConfigMap named `test-data` in the package has a `.data.newKey` field and contains the value `newValue`:

```yaml
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
annotations:
config.kubernetes.io/local-config: "true"
name: testpkg
pipeline:
mutators:
- image: ghcr.io/kispaljr/update-configmap:v1
configMap:
targetName: test-data
newKey: newValue
```
13 changes: 13 additions & 0 deletions update-configmap/example-pkg/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
annotations:
config.kubernetes.io/local-config: "true"
name: testpkg
pipeline:
mutators:
- image: ghcr.io/kispaljr/update-configmap:v1
configMap:
targetName: test-data
newKey: newValue
apple: plum
7 changes: 7 additions & 0 deletions update-configmap/example-pkg/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test-data
namespace: default
data:
apple: pear
39 changes: 39 additions & 0 deletions update-configmap/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module github.com/kispaljr/krm-functions/upsert-resource

go 1.21

toolchain go1.21.2

require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20230427202446-3255accc518d
sigs.k8s.io/kustomize/kyaml v0.14.2
)

require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221018174030-e63010a12b00 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.27.2 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
)
Loading

0 comments on commit 5f1bc7a

Please sign in to comment.