Skip to content

Commit

Permalink
Example and output for search replace
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Mar 19, 2021
1 parent ab43a29 commit 3c5f4bd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/mutators/search/simple/.expected/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runCount: 2
11 changes: 11 additions & 0 deletions examples/mutators/search/simple/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
diff --git a/resources.yaml b/resources.yaml
index 7481208..1055c0f 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -1,5 +1,5 @@
apiVersion: apps/v1
kind: Deployment
metadata:
- name: the-deployment
+ name: my-deployment
namespace: my-space
1 change: 1 addition & 0 deletions examples/mutators/search/simple/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
12 changes: 12 additions & 0 deletions examples/mutators/search/simple/fn-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: my-func-config
annotations:
config.k8s.io/function: |
container:
image: gcr.io/kpt-fn/search:unstable
data:
by-path: metadata.name
by-value: the-deployment
put-value: my-deployment
5 changes: 5 additions & 0 deletions examples/mutators/search/simple/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment
namespace: my-space
3 changes: 2 additions & 1 deletion functions/go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ FUNCTIONS := \
set-annotation \
set-namespace \
starlark \
apply-setters
apply-setters \
search

# Targets for running all function tests
FUNCTION_TESTS := $(patsubst %,%-TEST,$(FUNCTIONS))
Expand Down
58 changes: 52 additions & 6 deletions functions/go/search/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ func main() {
resourceList := &framework.ResourceList{}
resourceList.FunctionConfig = map[string]interface{}{}
cmd := framework.Command(resourceList, func() error {
sr, err := getSearchParams(resourceList.FunctionConfig)
if err != nil {
return fmt.Errorf("failed to parse function config: %w", err)
resourceList.Result = &framework.Result{
Name: "search",
}
_, err = sr.Filter(resourceList.Items)
items, err := run(resourceList)
if err != nil {
return fmt.Errorf("failed to perform search operation: %w", err)
resourceList.Result.Items = getErrorItem(err.Error())
return resourceList.Result
}

resourceList.Result.Items = items
return nil
})

Expand All @@ -32,6 +32,20 @@ func main() {
}
}

// run resolves the function params and runs the function on resources
func run(resourceList *framework.ResourceList) ([]framework.Item, error) {
sr, err := getSearchParams(resourceList.FunctionConfig)
if err != nil {
return nil, err
}

_, err = sr.Filter(resourceList.Items)
if err != nil {
return nil, err
}

return searchResultsToItems(sr), nil
}
func usage() string {
return `` //TODO: will add it in the next PR
}
Expand Down Expand Up @@ -70,3 +84,35 @@ func getValue(m map[string]string, key string) string {
}
return ""
}

// searchResultsToItems converts the Search and Replace results to
// equivalent items([]framework.Item)
func searchResultsToItems(sr SearchReplace) []framework.Item {
var items []framework.Item
for _, res := range sr.Results {

var message string
if sr.PutComment != "" || sr.PutValue != "" {
message = fmt.Sprintf("Mutated field value to %q", res.Value)
} else {
message = fmt.Sprintf("Matched field value %q", res.Value)
}

items = append(items, framework.Item{
Message: message,
Field: framework.Field{Path: res.FieldPath},
File: framework.File{Path: res.FilePath},
})
}
return items
}

// getErrorItem returns the item for input error message
func getErrorItem(errMsg string) []framework.Item {
return []framework.Item{
{
Message: fmt.Sprintf("failed to perform search operation: %q", errMsg),
Severity: framework.Error,
},
}
}

0 comments on commit 3c5f4bd

Please sign in to comment.