Skip to content

Commit 0c4fbf2

Browse files
author
Mengqi Yu
authored
Swith to the Kptfile v1 types in the SDK repo (#521)
1 parent c7b7e86 commit 0c4fbf2

File tree

8 files changed

+651
-167
lines changed

8 files changed

+651
-167
lines changed

functions/go/fix/v1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"sigs.k8s.io/kustomize/kyaml/yaml"
88
)
99

10+
// NOTE: Please do NOT import this package in other functions. You can import
11+
// the one in
12+
// https://github.com/GoogleContainerTools/kpt-functions-sdk/tree/master/go/pkg/api
13+
1014
const (
1115
KptFileName = "Kptfile"
1216
KptFileGroup = "kpt.dev"

functions/go/list-setters/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/list-s
33
go 1.16
44

55
require (
6-
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix v0.0.0-20210723002714-5e92d58c7664
6+
github.com/GoogleContainerTools/kpt-functions-sdk/go v0.0.0-20210810181223-632b30549de6
77
github.com/stretchr/testify v1.7.0
88
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
9-
sigs.k8s.io/kustomize/kyaml v0.10.21
9+
sigs.k8s.io/kustomize/kyaml v0.11.0
1010
)

functions/go/list-setters/go.sum

Lines changed: 286 additions & 147 deletions
Large diffs are not rendered by default.

functions/go/list-setters/listsetters/list_setters.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"sort"
88
"strings"
99

10-
kptv1 "github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix/v1"
10+
kptfilev1 "github.com/GoogleContainerTools/kpt-functions-sdk/go/pkg/api/kptfile/v1"
11+
kptutil "github.com/GoogleContainerTools/kpt-functions-sdk/go/pkg/api/util"
1112
"sigs.k8s.io/kustomize/kyaml/errors"
1213
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
1314
"sigs.k8s.io/kustomize/kyaml/yaml"
@@ -84,10 +85,14 @@ const (
8485
)
8586

8687
// FindKptfile discovers Kptfile of the root package from slice of nodes
87-
func FindKptfile(nodes []*yaml.RNode) (*kptv1.KptFile, error) {
88+
func FindKptfile(nodes []*yaml.RNode) (*kptfilev1.KptFile, error) {
8889
for _, node := range nodes {
89-
if node.GetAnnotations()[kioutil.PathAnnotation] == kptv1.KptFileName {
90-
kf, err := kptv1.ReadFile(node)
90+
if node.GetAnnotations()[kioutil.PathAnnotation] == kptfilev1.KptFileName {
91+
s, err := node.String()
92+
if err != nil {
93+
return nil, errors.WrapPrefixf(err, "unable to read Kptfile")
94+
}
95+
kf, err := kptutil.DecodeKptfile(s)
9196
return kf, errors.WrapPrefixf(err, "unable to read Kptfile")
9297
}
9398
}

functions/go/list-setters/listsetters/list_setters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ metadata:
7878
app: my-app # kpt-set: ${app}
7979
name: mungebot
8080
`},
81-
errMsg: "unable to read Kptfile: please make sure the package has a valid 'v1' Kptfile: yaml: unmarshal errors:\n line 8: field foo not found in type v1.KptFile",
81+
errMsg: "unable to read Kptfile: invalid 'v1' Kptfile: yaml: unmarshal errors:\n line 8: field foo not found in type v1.KptFile",
8282
},
8383
{
8484
name: "Scalar Simple missing apply-setters",

functions/go/set-project-id/go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-pr
33
go 1.16
44

55
require (
6-
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix v0.0.0-20210723002714-5e92d58c7664
6+
github.com/GoogleContainerTools/kpt-functions-sdk/go v0.0.0-20210810181223-632b30549de6
7+
github.com/stretchr/testify v1.7.0 // indirect
8+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
79
sigs.k8s.io/kustomize/kyaml v0.11.0
810
)

functions/go/set-project-id/go.sum

Lines changed: 332 additions & 3 deletions
Large diffs are not rendered by default.

functions/go/set-project-id/main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
1212
"sigs.k8s.io/kustomize/kyaml/yaml"
1313

14-
kptv1 "github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix/v1"
14+
kptfilev1 "github.com/GoogleContainerTools/kpt-functions-sdk/go/pkg/api/kptfile/v1"
15+
kptutil "github.com/GoogleContainerTools/kpt-functions-sdk/go/pkg/api/util"
1516
)
1617

1718
var (
@@ -33,11 +34,15 @@ func findSetterNode(nodes []*yaml.RNode, path string) (*yaml.RNode, error) {
3334
return nil, fmt.Errorf(`file %s doesn't exist, please ensure the file specified in "configPath" exists and retry`, path)
3435
}
3536

36-
func findKptfiles(nodes []*yaml.RNode) ([]*kptv1.KptFile, error) {
37-
kptfiles := []*kptv1.KptFile{}
37+
func findKptfiles(nodes []*yaml.RNode) ([]*kptfilev1.KptFile, error) {
38+
kptfiles := []*kptfilev1.KptFile{}
3839
for _, node := range nodes {
39-
if node.GetKind() == kptv1.KptFileName {
40-
kf, err := kptv1.ReadFile(node)
40+
if node.GetKind() == kptfilev1.KptFileKind {
41+
s, err := node.String()
42+
if err != nil {
43+
return nil, fmt.Errorf("unable to read Kptfile: %w", err)
44+
}
45+
kf, err := kptutil.DecodeKptfile(s)
4146
if err != nil {
4247
return nil, fmt.Errorf("failed to read Kptfile: %w", err)
4348
}
@@ -50,7 +55,7 @@ func findKptfiles(nodes []*yaml.RNode) ([]*kptv1.KptFile, error) {
5055
return kptfiles, nil
5156
}
5257

53-
func setKptfile(nodes []*yaml.RNode, kf *kptv1.KptFile) error {
58+
func setKptfile(nodes []*yaml.RNode, kf *kptfilev1.KptFile) error {
5459
b, err := yaml.Marshal(kf)
5560
if err != nil {
5661
return fmt.Errorf("failed to marshal updated Kptfile: %w", err)
@@ -70,9 +75,9 @@ func setKptfile(nodes []*yaml.RNode, kf *kptv1.KptFile) error {
7075

7176
}
7277

73-
func setSettersOnKptfile(nodes []*yaml.RNode, kf *kptv1.KptFile, projectID string) error {
78+
func setSettersOnKptfile(nodes []*yaml.RNode, kf *kptfilev1.KptFile, projectID string) error {
7479
if kf.Pipeline == nil {
75-
kf.Pipeline = &kptv1.Pipeline{}
80+
kf.Pipeline = &kptfilev1.Pipeline{}
7681
}
7782
for _, fn := range kf.Pipeline.Mutators {
7883
if !strings.Contains(fn.Image, "apply-setters") {
@@ -102,7 +107,7 @@ func setSettersOnKptfile(nodes []*yaml.RNode, kf *kptv1.KptFile, projectID strin
102107
}
103108
}
104109

105-
fn := kptv1.Function{
110+
fn := kptfilev1.Function{
106111
Image: "gcr.io/kpt-fn/apply-setters:v0.1",
107112
ConfigMap: map[string]string{
108113
projectIDSetterName: projectID,

0 commit comments

Comments
 (0)