Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fmt: $(MODULES)
lint: $(MODULES)
@for f in $(^D); do \
(cd $$f; echo "Checking golangci-lint $$f"; \
(which $(GOPATH)/bin/golangci-lint || go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2); \
(which $(GOPATH)/bin/golangci-lint || go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1); \
Copy link
Contributor

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!)

Copy link
Contributor Author

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.

$(GOPATH)/bin/golangci-lint run ./...); \
done

Expand Down
38 changes: 38 additions & 0 deletions go/get-started/go.mod
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
)
764 changes: 764 additions & 0 deletions go/get-started/go.sum

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions go/get-started/golden_test.go
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)
}
29 changes: 19 additions & 10 deletions go/get-started/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Do we need json annotations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
}
41 changes: 41 additions & 0 deletions go/get-started/testdata/test1/_expected.yaml
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.17-alpine3.15
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o /usr/local/bin/function ./
FROM alpine:3.15
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
ENTRYPOINT ["function"]
apiVersion: fn.kpt.dev/v1alpha1
kind: YourFunction
metadata:
name: test
fnConfigFoo: example
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
apiVersion: v1
kind: Service
metadata:
name: my-service
name: test
spec:
selector:
app: MyApp
Expand Down
8 changes: 7 additions & 1 deletion go/hack/update-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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