Skip to content

Commit c16d0c9

Browse files
Target selectors docs (#2534)
* Eval reference docs * Resource selectors guide * Document the available selectors * SuggestedChanges * More suggested changes * Add diagram and suggested changes * More changes * Minor changes * Update eval flag names * Update flag names * Add colon * Update 01-declarative-function-execution.md * Update docs * Hold packagePath implementation Co-authored-by: Frank Farzan <[email protected]>
1 parent 9cd4aeb commit c16d0c9

File tree

15 files changed

+169
-180
lines changed

15 files changed

+169
-180
lines changed

e2e/testdata/fn-eval/selectors/basicpipeline/.expected/exec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515

1616
set -eo pipefail
1717

18-
kpt fn eval -i set-namespace:v0.1.3 --name nginx-deployment --kind Deployment -- namespace=staging
18+
kpt fn eval -i set-namespace:v0.1.3 --match-name nginx-deployment --match-kind Deployment -- namespace=staging

e2e/testdata/fn-eval/selectors/out-of-place-fnchain-unwrap/.expected/exec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
set -eo pipefail
1818

1919
kpt fn source \
20-
| kpt fn eval - --image gcr.io/kpt-fn/set-namespace:v0.1.3 --kind Deployment -o stdout -- namespace=staging \
21-
| kpt fn eval - --image gcr.io/kpt-fn/set-annotations:v0.1.3 --name custom -- foo=bar \
20+
| kpt fn eval - --image gcr.io/kpt-fn/set-namespace:v0.1.3 --match-kind Deployment -o stdout -- namespace=staging \
21+
| kpt fn eval - --image gcr.io/kpt-fn/set-annotations:v0.1.3 --match-name custom -- foo=bar \
2222
| kpt fn eval - --image gcr.io/kpt-fn/set-labels:v0.1.3 -o unwrap -- tier=backend

e2e/testdata/fn-render/selectors/subpkg/.expected/diff.patch

Lines changed: 0 additions & 51 deletions
This file was deleted.

e2e/testdata/fn-render/selectors/subpkg/.krmignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

e2e/testdata/fn-render/selectors/subpkg/Kptfile

Lines changed: 0 additions & 14 deletions
This file was deleted.

e2e/testdata/fn-render/selectors/subpkg/db/Kptfile

Lines changed: 0 additions & 12 deletions
This file was deleted.

e2e/testdata/fn-render/selectors/subpkg/db/resources.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

e2e/testdata/fn-render/selectors/subpkg/resources.yaml

Lines changed: 0 additions & 26 deletions
This file was deleted.

internal/fnruntime/utils.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"io/ioutil"
2121
"path/filepath"
2222

23-
"github.com/GoogleContainerTools/kpt/internal/pkg"
2423
"github.com/GoogleContainerTools/kpt/internal/types"
2524
fnresult "github.com/GoogleContainerTools/kpt/pkg/api/fnresult/v1"
2625
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
@@ -141,18 +140,14 @@ type SelectionContext struct {
141140
}
142141

143142
// SelectInput returns the selected resources based on criteria in selectors
144-
func SelectInput(input []*yaml.RNode, selectors []kptfilev1.Selector, ctx *SelectionContext) ([]*yaml.RNode, error) {
143+
func SelectInput(input []*yaml.RNode, selectors []kptfilev1.Selector, _ *SelectionContext) ([]*yaml.RNode, error) {
145144
if len(selectors) == 0 {
146145
return input, nil
147146
}
148147
var filteredInput []*yaml.RNode
149148
for _, node := range input {
150149
for _, selector := range selectors {
151-
match, err := isMatch(node, selector, ctx)
152-
if err != nil {
153-
return nil, err
154-
}
155-
if match {
150+
if isMatch(node, selector) {
156151
filteredInput = append(filteredInput, node)
157152
}
158153
}
@@ -161,14 +156,10 @@ func SelectInput(input []*yaml.RNode, selectors []kptfilev1.Selector, ctx *Selec
161156
}
162157

163158
// isMatch returns true if the resource matches input selection criteria
164-
func isMatch(node *yaml.RNode, selector kptfilev1.Selector, ctx *SelectionContext) (bool, error) {
165-
pkgPathMatch, err := packagePathMatch(node, selector, ctx.RootPackagePath.String())
166-
if err != nil {
167-
return false, err
168-
}
159+
func isMatch(node *yaml.RNode, selector kptfilev1.Selector) bool {
169160
// keep expanding with new selectors
170161
return nameMatch(node, selector) && namespaceMatch(node, selector) &&
171-
kindMatch(node, selector) && apiVersionMatch(node, selector) && pkgPathMatch, nil
162+
kindMatch(node, selector) && apiVersionMatch(node, selector)
172163
}
173164

174165
// nameMatch returns true if the resource name matches input selection criteria
@@ -190,13 +181,3 @@ func kindMatch(node *yaml.RNode, selector kptfilev1.Selector) bool {
190181
func apiVersionMatch(node *yaml.RNode, selector kptfilev1.Selector) bool {
191182
return selector.APIVersion == "" || selector.APIVersion == node.GetApiVersion()
192183
}
193-
194-
// packagePathMatch returns true if the package path of resource matches input selection criteria
195-
func packagePathMatch(node *yaml.RNode, selector kptfilev1.Selector, rootPackagePath string) (bool, error) {
196-
resourcePkgPath, err := pkg.GetPkgPathAnnotation(node)
197-
if err != nil {
198-
return false, err
199-
}
200-
// TODO: pmarupaka Make this logic work for WINDOWS
201-
return selector.PackagePath == "" || filepath.Join(rootPackagePath, selector.PackagePath) == resourcePkgPath, nil
202-
}

internal/fnruntime/utils_test.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fnruntime
33
import (
44
"testing"
55

6-
"github.com/GoogleContainerTools/kpt/internal/types"
76
kptfile "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
87
"github.com/stretchr/testify/assert"
98
"sigs.k8s.io/kustomize/kyaml/yaml"
@@ -115,35 +114,14 @@ spec:
115114
},
116115
expected: false,
117116
},
118-
{
119-
name: "packagePath match",
120-
input: `apiVersion: apps/v1
121-
kind: Deployment
122-
metadata:
123-
name: nginx-deployment
124-
annotations:
125-
internal.config.k8s.io/kpt-resource-id: "0"
126-
internal.config.kubernetes.io/package-path: "/path/to/root/pkg/db"
127-
spec:
128-
replicas: 3`,
129-
selector: kptfile.Selector{
130-
PackagePath: "./db",
131-
},
132-
expected: true,
133-
},
134117
}
135118

136119
for i := range tests {
137120
tc := tests[i]
138121
t.Run(tc.name, func(t *testing.T) {
139122
node, err := yaml.Parse(tc.input)
140123
assert.NoError(t, err)
141-
var rootPackagePath string
142-
if tc.selector.PackagePath != "" {
143-
rootPackagePath = "/path/to/root/pkg"
144-
}
145-
actual, err := isMatch(node, tc.selector, &SelectionContext{RootPackagePath: types.UniquePath(rootPackagePath)})
146-
assert.NoError(t, err)
124+
actual := isMatch(node, tc.selector)
147125
assert.Equal(t, tc.expected, actual)
148126
})
149127
}

0 commit comments

Comments
 (0)