Skip to content

Commit

Permalink
Add README for example and punt usage docs
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Mar 22, 2021
1 parent 5f3d4a8 commit b719411
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 218 deletions.
167 changes: 0 additions & 167 deletions examples/mutators/search/README.md

This file was deleted.

58 changes: 58 additions & 0 deletions examples/mutators/search/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# search: Simple Example

The `search-replace` function performs search and optionally replace fields
across all resources.

This is a simple example depicting search and replace operation on KRM resource config.

Let's start with the input resource

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment
namespace: my-space
```
Search matchers are provided with `by-` prefix. When multiple matchers are
provided they are AND’ed together. `put-` matchers are mutually exclusive.

We use the following ConfigMap to provide input matchers to the function.

```yaml
apiVersion: v1
kind: ConfigMap
metadata: ...
data:
by-path: metadata.name
by-value: the-deployment
put-value: my-deployment
```

Invoking `search-replace` function would apply the changes to resource configs

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: my-space
```

## Function invocation

Get the config example and try it out by running the following commands:

```sh
kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/mutators/search/simple .
kpt fn run simple
```

## Expected result

Check the value of deployment `the-deloyment` is changed to `my-deloyment`.

```sh
kpt cfg cat simple
```
52 changes: 1 addition & 51 deletions functions/go/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,57 +47,7 @@ func run(resourceList *framework.ResourceList) ([]framework.Item, error) {
return searchResultsToItems(sr), nil
}
func usage() string {
return `Search and optionally replace fields across all resources.
Search matchers are provided with 'by-' prefix. When multiple matchers
are provided they are AND’ed together. 'put-' matchers are mutually exclusive.
Here are the list of matchers:
by-value
Match by value of a field.
by-value-regex
Match by Regex for the value of a field. The syntax of the regular expressions
accepted is the same general syntax used by Go, Perl, Python, and other languages.
More precisely, it is the syntax accepted by RE2 and described at
https://golang.org/s/re2syntax. With the exception that it matches the entire
value of the field by default without requiring start (^) and end ($) characters.
by-path
Match by path expression of a field. Path expressions are used to deeply navigate
and match particular yaml nodes. Please note that the path expressions are not
regular expressions.
put-value
Set or update the value of the matching fields. Input can be a pattern for which
the numbered capture groups are resolved using --by-value-regex input.
put-comment
Set or update the line comment for matching fields. Input can be a pattern for
which the numbered capture groups are resolved using --by-value-regex input.
To search and replace field value to all resources:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
by-value: nginx
put-value: ubuntu
To put the setter pattern as a line comment for matching fields:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
by-value: my-project-id-foo
put-value: 'kpt-set: ${project-id}-foo'
`
return `` // TODO: pmarupaka add usage docs
}

// getSearchParams retrieve the search parameters from input config
Expand Down

0 comments on commit b719411

Please sign in to comment.