Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply-setters and search-replace README and generated docs #196

Merged
merged 7 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions functions/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ SHELL=/bin/bash
GOPATH := $(shell go env GOPATH)
TAG ?= unstable
GCR = gcr.io/kpt-fn
GOBIN := $(shell go env GOPATH)/bin

# Edit this list to contain all go functions
FUNCTIONS := \
Expand Down Expand Up @@ -117,3 +118,9 @@ func-build: func-verify
func-push:
@echo Pushing image $(CURRENT_FUNCTION)...
../../scripts/go-function-release.sh push curated

generate:
GO111MODULE=on go get -v github.com/GoogleContainerTools/kpt/mdtogo
$(GOBIN)/mdtogo search-replace search-replace/generated --license=none --strategy=cmdDocs
$(GOBIN)/mdtogo apply-setters apply-setters/generated --license=none --strategy=cmdDocs

94 changes: 94 additions & 0 deletions functions/go/apply-setters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# apply-setters

### Overview

<!--mdtogo:Short-->

Apply setter values on resources fields. May set either the complete or partial field value.

<!--mdtogo-->

Setters provide a solution for template-free setting of field values. They are a
safer alternative to other substitution techniques which do not have the context
of the structured data. Setters may be invoked to modify the configuration
using `apply-setters` function to set values.

### Synopsis

<!--mdtogo:Long-->

```
kpt fn eval apply-setters:VERSION [DIR] -- [setter_name=setter_value]
```

Data model

1. Fields reference setters specified as line comments -- e.g.

```
# kpt-set: replicas
```

2. Input values to setters are provided as key-value pairs -- e.g.

```
kpt fn eval apply-setters:unstable -- replicas=3
```

Control flow

1. Read the package resources.
2. Locate all fields which reference the setter and change their values.
3. Write the modified resources back to the package.

<!--mdtogo-->

### Examples

<!--mdtogo:Examples-->

Let's start with the input resources

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: the-map # kpt-set: ${name}
data:
some-key: some-value
---
apiVersion: v1
kind: MyKind
metadata:
name: ns
environments: # kpt-set: ${env}
- dev
- stage
```

Invoke apply-setters function on the input resources

```
kpt fn eval apply-setters:unstable -- 'name=my-map' 'env=[prod, dev]'
```

The resources are transformed to

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-map # kpt-set: ${name}
data:
some-key: some-value
---
apiVersion: v1
kind: MyKind
metadata:
name: ns
environments: # kpt-set: ${env}
- prod
- dev
```

<!--mdtogo-->
64 changes: 64 additions & 0 deletions functions/go/apply-setters/generated/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 5 additions & 52 deletions functions/go/apply-setters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/apply-setters/generated"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
)
Expand All @@ -25,64 +26,16 @@ func main() {
return nil
})

cmd.Long = usage()
cmd.Short = generated.ApplySettersShort
cmd.Long = generated.ApplySettersLong
cmd.Example = generated.ApplySettersExamples

if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func usage() string {
return `Apply setter values to resource fields with setter references.

Example:

Here is an example resource config to start with

apiVersion: v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 1 # kpt-set: ${replicas}

Use ConfigMap to configure the 'apply-setters' function. The desired setter values
are provided as key-value pairs using data field where key is the name of the
setter(as seen in the reference comments) and value is the new desired value for
the tagged field

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
replicas: '3'

Invoking apply-setters function would apply the changes to resource configs

apiVersion: v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3 # kpt-set: ${replicas}


Values to array setters must be array nodes wrapped into strings. Here is the
example config to apply array setters.

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
projectId: my-project
environments: |
- dev
- staging
`
}

// getSetters retrieve the setters from input config
func getSetters(fc interface{}) (ApplySetters, error) {
var fcd ApplySetters
Expand Down
Loading