Skip to content

Commit 3a8c6c2

Browse files
author
Mengqi Yu
authored
isolate dependencies for tests and examples (#566)
* isolate dependencies for tests and examples If the dependencies for tests and examples are leaked into the core sdk module, it will make the consumers hard or impossible to use when there is a k8s dependency skew. * fix linter
1 parent 00bb6fe commit 3a8c6c2

23 files changed

+1574
-88
lines changed

go/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fmt: $(MODULES)
2323
lint: $(MODULES)
2424
@for f in $(^D); do \
2525
(cd $$f; echo "Checking golangci-lint $$f"; \
26-
(which $(GOPATH)/bin/golangci-lint || go get github.com/golangci/golangci-lint/cmd/[email protected]); \
26+
(which $(GOPATH)/bin/golangci-lint || go install github.com/golangci/golangci-lint/cmd/[email protected]); \
2727
$(GOPATH)/bin/golangci-lint run ./...); \
2828
done
2929

go/fn/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ const (
3939
ConfigPrefix string = "config.kubernetes.io/"
4040

4141
// KptLocalConfig marks a KRM resource to be skipped from deploying to the cluster via `kpt live apply`.
42-
KptLocalConfig = ConfigPrefix + "local-config"
42+
KptLocalConfig = ConfigPrefix + "local-config"
4343
)

go/fn/examples/const.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package example
16+
17+
const (
18+
appsv1 = "apps/v1"
19+
)

go/fn/examples/example_builtin_function_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package example_test
216

317
import (

go/fn/examples/example_generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func generate(rl *fn.ResourceList) (bool, error) {
6969

7070
func fetchDashboard(revision, id string) (string, error) {
7171
url := fmt.Sprintf("https://grafana.com/api/dashboards/%s/revisions/%s/download", id, revision)
72-
resp, err := http.Get(url)
72+
resp, err := http.Get(url) // nolint:gosec
7373
if err != nil {
7474
return "", err
7575
}

go/fn/examples/example_logger_injector_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func injectLogger(rl *fn.ResourceList) (bool, error) {
4040
return false, err
4141
}
4242
for i, obj := range rl.Items {
43-
if obj.GetAPIVersion() == "apps/v1" && (obj.GetKind() == "Deployment" || obj.GetKind() == "StatefulSet" || obj.GetKind() == "DaemonSet" || obj.GetKind() == "ReplicaSet") {
43+
if obj.IsGVK(appsv1, "Deployment") || obj.IsGVK(appsv1, "StatefulSet") ||
44+
obj.IsGVK(appsv1, "DaemonSet") || obj.IsGVK(appsv1, "ReplicaSet") {
4445
var containers []corev1.Container
4546
obj.GetOrDie(&containers, "spec", "template", "spec", "containers")
4647
foundTargetContainer := false

go/fn/examples/example_read_field_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Example_aReadField() {
3030

3131
func readField(rl *fn.ResourceList) (bool, error) {
3232
for _, obj := range rl.Items {
33-
if obj.GetAPIVersion() == "apps/v1" && obj.GetKind() == "Deployment" {
33+
if obj.IsGVK("apps/v1", "Deployment") {
3434
var replicas int
3535
obj.GetOrDie(&replicas, "spec", "replicas")
3636
fn.Logf("replicas is %v\n", replicas)

go/fn/examples/example_validator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func Example_validator() {
3232
func validator(rl *fn.ResourceList) (bool, error) {
3333
var results fn.Results
3434
for _, obj := range rl.Items {
35-
if obj.GetAPIVersion() == "apps/v1" && (obj.GetKind() == "Deployment" || obj.GetKind() == "StatefulSet" || obj.GetKind() == "DaemonSet" || obj.GetKind() == "ReplicaSet") {
35+
if obj.IsGVK(appsv1, "Deployment") || obj.IsGVK(appsv1, "StatefulSet") ||
36+
obj.IsGVK(appsv1, "DaemonSet") || obj.IsGVK(appsv1, "ReplicaSet") {
3637
var runAsNonRoot bool
3738
obj.GetOrDie(&runAsNonRoot, "spec", "template", "spec", "securityContext", "runAsNonRoot")
3839
if !runAsNonRoot {

go/fn/examples/go.mod

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/examples
2+
3+
go 1.17
4+
5+
replace github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0 => ../
6+
7+
require (
8+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0
9+
k8s.io/api v0.23.4
10+
k8s.io/apimachinery v0.23.4
11+
sigs.k8s.io/kustomize/kyaml v0.13.6
12+
)
13+
14+
require (
15+
github.com/PuerkitoBio/purell v1.1.1 // indirect
16+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/go-errors/errors v1.0.1 // indirect
19+
github.com/go-logr/logr v1.2.0 // indirect
20+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
21+
github.com/go-openapi/jsonreference v0.19.3 // indirect
22+
github.com/go-openapi/swag v0.19.5 // indirect
23+
github.com/gogo/protobuf v1.3.2 // indirect
24+
github.com/google/go-cmp v0.5.5 // indirect
25+
github.com/google/gofuzz v1.1.0 // indirect
26+
github.com/json-iterator/go v1.1.12 // indirect
27+
github.com/mailru/easyjson v0.7.0 // indirect
28+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
29+
github.com/modern-go/reflect2 v1.0.2 // indirect
30+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
31+
github.com/pkg/errors v0.9.1 // indirect
32+
github.com/pmezard/go-difflib v1.0.0 // indirect
33+
github.com/stretchr/testify v1.7.0 // indirect
34+
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
35+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
36+
golang.org/x/text v0.3.7 // indirect
37+
gopkg.in/inf.v0 v0.9.1 // indirect
38+
gopkg.in/yaml.v2 v2.4.0 // indirect
39+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
40+
k8s.io/klog/v2 v2.30.0 // indirect
41+
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
42+
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
43+
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
44+
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
45+
)

0 commit comments

Comments
 (0)