-
Notifications
You must be signed in to change notification settings - Fork 38
feat: add example for effective go krm function #602
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module github.com/GoogleContainerTools/kpt-functions-sdk/go/get-started | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221018174030-e63010a12b00 // indirect | ||
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221018174030-e63010a12b00 // indirect | ||
github.com/PuerkitoBio/purell v1.2.0 // 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.4.2 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.5 // indirect | ||
github.com/go-openapi/jsonreference v0.20.0 // indirect | ||
github.com/go-openapi/swag v0.22.3 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/gnostic v0.6.9 // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/google/gofuzz v1.2.0 // 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/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.8.1 // indirect | ||
github.com/xlab/treeprint v1.1.0 // indirect | ||
golang.org/x/net v0.1.0 // indirect | ||
golang.org/x/sys v0.1.0 // indirect | ||
golang.org/x/text v0.4.0 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/apimachinery v0.25.3 // indirect | ||
k8s.io/klog/v2 v2.80.1 // indirect | ||
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect | ||
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/testhelpers" | ||
) | ||
|
||
const TestDataPath = "testdata" | ||
|
||
func TestFunction(t *testing.T) { | ||
fnRunner := fn.WithContext(context.TODO(), &YourFunction{}) | ||
// This golden test expects each sub-directory of `testdata` can has its input resources (in `resources.yaml`) | ||
// be modified to the output resources (in `_expected.yaml`). | ||
testhelpers.RunGoldenTests(t, TestDataPath, fnRunner) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,24 +15,33 @@ | |
package main | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"os" | ||
|
||
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" | ||
) | ||
|
||
// EDIT THIS FUNCTION! | ||
// This is the main logic. rl is the input `ResourceList` which has the `FunctionConfig` and `Items` fields. | ||
// You can modify the `Items` and add result information to `rl.Result`. | ||
func Run(rl *fn.ResourceList) (bool, error) { | ||
// Your code | ||
var _ fn.Runner = &YourFunction{} | ||
|
||
// TODO: Change to your functionConfig "Kind" name. | ||
type YourFunction struct { | ||
FnConfigBool bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to. |
||
FnConfigInt int | ||
FnConfigFoo string | ||
} | ||
|
||
// Run is the main function logic. | ||
// `items` is parsed from the STDIN "ResourceList.Items". | ||
// `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r attributes | ||
// `results` is the "ResourceList.Results" that you can write result info to. | ||
func (r *YourFunction) Run(ctx *fn.Context, functionConfig *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool { | ||
// TODO: Write your code. | ||
return true | ||
} | ||
|
||
func main() { | ||
// CUSTOMIZE IF NEEDED | ||
// `AsMain` accepts a `ResourceListProcessor` interface. | ||
// You can explore other `ResourceListProcessor` structs in the SDK or define your own. | ||
if err := fn.AsMain(fn.ResourceListProcessorFunc(Run)); err != nil { | ||
runner := fn.WithContext(context.Background(), &YourFunction{}) | ||
if err := fn.AsMain(runner); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
apiVersion: config.kubernetes.io/v1 | ||
kind: ResourceList | ||
items: | ||
- apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
labels: | ||
app: nginx | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 | ||
- apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: test | ||
spec: | ||
selector: | ||
app: MyApp | ||
ports: | ||
- protocol: TCP | ||
port: 80 | ||
targetPort: 9376 | ||
functionConfig: | ||
apiVersion: fn.kpt.dev/v1alpha1 | ||
kind: YourFunction | ||
metadata: | ||
name: test | ||
fnConfigFoo: example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: my-service | ||
name: test | ||
spec: | ||
selector: | ||
app: MyApp | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,10 @@ | |
# don't add licenses to the site directory, it will break the docs | ||
# and will add them to the theme which is a submodule (bad) | ||
command -v addlicense || go install github.com/google/[email protected] | ||
find . -print0 | xargs "$GOBIN"/addlicense -y 2022 -l apache | ||
# - `addlicense` skips the .yaml files (specifically for golden test _expected.yaml) to avoid the conflict where | ||
# the `addlicense` adds unexpected license header that fails the golden tests diff-comparison in _expected.yaml. | ||
# - the [0-9a-z] is a trick to avoid using `**/**/*.yaml` directly which hits the shellcheck SC2035. SC2035 only accepts | ||
# ./*glob* or -- *glob* which can't be recoganized by addlicese. | ||
# See https://www.shellcheck.net/wiki/SC2035 | ||
find . -print0 | xargs "$GOBIN"/addlicense -y 2022 -l apache -ignore [0-9a-z]**/**/_expected.yaml | ||
yuwenma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I really like using
go run
where it works - it seems plenty fast enough now. It also works really nicely with go generate.(No need to fix, just one for the future!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Yeah,
go run
is more proper in this case in a follow-up PR.