Skip to content

Commit

Permalink
add methods to allow selection and exclusion of resources by gvkn
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed May 26, 2022
1 parent 68c3f91 commit eec9c4f
Show file tree
Hide file tree
Showing 9 changed files with 1,251 additions and 54 deletions.
7 changes: 3 additions & 4 deletions go/fn/examples/example_filter_GVK_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ func updateReplicas(rl *fn.ResourceList) (bool, error) {
}
var replicas int
rl.FunctionConfig.GetOrDie(&replicas, "replicas")
for i, obj := range rl.Items {
if obj.IsGVK("apps/v1", "Deployment") {
rl.Items[i].SetOrDie(replicas, "spec", "replicas")
}
deployments := rl.Items.SelectByGvk("apps/v1", "Deployment")
for i := range deployments {
rl.Items[i].SetOrDie(replicas, "spec", "replicas")
}
return true, nil
}
23 changes: 23 additions & 0 deletions go/fn/examples/example_select_exclude_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package example

import (
"os"

"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
)

// This example implements a function that selectively includes or excludes some resources.

func Example_selectExclude() {
if err := fn.AsMain(fn.ResourceListProcessorFunc(selectResources)); err != nil {
os.Exit(1)
}
}

// selectResources keeps all resources with the GVK apps/v1 Deployment that do
// NOT have the label foo=bar, and removes the rest.
func selectResources(rl *fn.ResourceList) (bool, error) {
rl.Items = rl.Items.SelectByGvk("apps/v1", "Deployment").
ExcludeByLabels(map[string]string{"foo": "bar"})
return true, nil
}
32 changes: 19 additions & 13 deletions go/fn/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,41 @@ module github.com/GoogleContainerTools/kpt-functions-sdk/go/fn
go 1.17

require (
github.com/google/go-cmp v0.5.5
github.com/GoogleContainerTools/kpt v1.0.0-beta.16
github.com/google/go-cmp v0.5.7
// We must not include any core k8s modules (e.g. k8s.io/apimachinery) in
// the dependencies, depending on them will likely to cause version skew for
// consumers. The dependencies for tests and examples should be isolated.
k8s.io/klog/v2 v2.60.1
sigs.k8s.io/kustomize/kyaml v0.13.6
sigs.k8s.io/kustomize/kyaml v0.13.7-0.20220418212550-9d5491c2e20c
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.3 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/kube-openapi v0.0.0-20220401212409-b28bf2818661 // indirect
sigs.k8s.io/kustomize/api v0.11.4 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
491 changes: 477 additions & 14 deletions go/fn/go.sum

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions go/fn/internal/test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,40 @@ replace github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0 => ../..

require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.1
k8s.io/api v0.24.0
k8s.io/apimachinery v0.24.0
sigs.k8s.io/kustomize/kyaml v0.13.6
sigs.k8s.io/kustomize/kyaml v0.13.7-0.20220418212550-9d5491c2e20c
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.3 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/kube-openapi v0.0.0-20220401212409-b28bf2818661 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
Expand Down
Loading

0 comments on commit eec9c4f

Please sign in to comment.