Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add methods to allow selection and exclusion of resources by gvk #581

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Where(fn.IsGVK("apps/v1", "Deployment"))
yuwenma marked this conversation as resolved.
Show resolved Hide resolved
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.Where(fn.IsGVK("apps/v1", "Deployment")).
WhereNot(fn.HasLabels(map[string]string{"foo": "bar"}))
return true, nil
}
23 changes: 14 additions & 9 deletions go/fn/examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,41 @@ require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0
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/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // 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
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
58 changes: 38 additions & 20 deletions go/fn/examples/go.sum

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions go/fn/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,39 @@ module github.com/GoogleContainerTools/kpt-functions-sdk/go/fn
go 1.17

require (
github.com/google/go-cmp v0.5.5
github.com/google/go-cmp v0.5.7
github.com/stretchr/testify v1.7.1
// 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/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/yaml v1.3.0 // indirect
)
Loading