Skip to content

Commit

Permalink
Apply setters README and godocs
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Mar 24, 2021
1 parent d296ec7 commit dc32c1b
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 52 deletions.
1 change: 1 addition & 0 deletions functions/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@ func-push:
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

0 comments on commit dc32c1b

Please sign in to comment.