Skip to content

Commit 2e9b530

Browse files
committed
Fix possible nil-pointer error; add golden test for set-name-prefix
Using the golden test support in the functions SDK.
1 parent f21fbb5 commit 2e9b530

File tree

10 files changed

+215
-30
lines changed

10 files changed

+215
-30
lines changed

functions/go/bind/pkg/meta/refvisitor.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,37 @@ func VisitRefs(objects fn.KubeObjects, visitor func(ref Ref) error) error {
7676
}
7777

7878
func visitFields(object *fn.KubeObject, subObject *fn.SubObject, fields []string, refInfo *refInfo, visitor func(ref Ref) error) error {
79+
// Guard against nil objects
80+
if subObject == nil {
81+
return nil
82+
}
83+
7984
if len(fields) == 0 {
80-
if subObject != nil {
81-
// Ignore external refs, these don't get renamed
82-
// TODO: Check if supports external ??
83-
external := subObject.GetString("external")
84-
if external != "" {
85-
return nil
86-
}
85+
// Ignore external refs, these don't get renamed
86+
// TODO: Check if supports external ??
87+
external := subObject.GetString("external")
88+
if external != "" {
89+
return nil
90+
}
8791

88-
ref, err := refInfo.buildFullyQualifiedRef(object, subObject)
89-
if err != nil {
90-
return err
91-
}
92-
if err := visitor(ref); err != nil {
93-
return err
94-
}
92+
ref, err := refInfo.buildFullyQualifiedRef(object, subObject)
93+
if err != nil {
94+
return err
95+
}
96+
if err := visitor(ref); err != nil {
97+
return err
98+
}
9599

96-
if refInfo.NamespaceFieldPath != "" {
97-
namespaceFieldPath := strings.Split(refInfo.NamespaceFieldPath, ".")
98-
namespace, _, _ := subObject.NestedString(namespaceFieldPath...)
99-
if namespace != "" {
100-
ref, err := buildRefNamespaceReference(subObject, namespaceFieldPath...)
101-
if err != nil {
102-
return err
103-
}
104-
if err := visitor(ref); err != nil {
105-
return err
106-
}
100+
if refInfo.NamespaceFieldPath != "" {
101+
namespaceFieldPath := strings.Split(refInfo.NamespaceFieldPath, ".")
102+
namespace, _, _ := subObject.NestedString(namespaceFieldPath...)
103+
if namespace != "" {
104+
ref, err := buildRefNamespaceReference(subObject, namespaceFieldPath...)
105+
if err != nil {
106+
return err
107+
}
108+
if err := visitor(ref); err != nil {
109+
return err
107110
}
108111
}
109112
}

functions/go/go.mod

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

55
require (
6-
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221007213718-5fa523b306fe
6+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221104163847-f50fe3a2977e
7+
github.com/google/go-cmp v0.5.9
78
k8s.io/apimachinery v0.25.2
89
)
910

functions/go/go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
44
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221007213718-5fa523b306fe h1:8PNHR5cev3FqOduItCH59KZdabDwz9I5xNcB8Woc6es=
55
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221007213718-5fa523b306fe/go.mod h1:prNhhUAODrB2VqHVead9tB8nLU9ffY4e4jjBwLMNO1M=
6-
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220929172014-8888fc9691d1 h1:DFLHb51Av8m6ETGvd+J4Ok0pvfo735OW0sP+wbJLQyg=
7-
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220929172014-8888fc9691d1/go.mod h1:TZd94D9b8alIDuF3mW9g+ofbl0RfwT0JewXmXm7phDI=
86
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221007213718-5fa523b306fe h1:BjbTCT1mkPf0s4X3osOleEcaa89psOlcEJGUcLc+YPo=
97
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221007213718-5fa523b306fe/go.mod h1:mqc5jH6i0Ll6T4wCmTXsVNVxhwBGlVtrtmoF/g3E9lE=
8+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221104163847-f50fe3a2977e h1:sSHJXFviCfgx6t8lDjjJ7Z1X4+Ed0m/pYP/MHJ0vWo4=
9+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221104163847-f50fe3a2977e/go.mod h1:mqc5jH6i0Ll6T4wCmTXsVNVxhwBGlVtrtmoF/g3E9lE=
1010
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
1111
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
1212
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
@@ -67,7 +67,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
6767
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6868
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6969
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
70-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
70+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
71+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7172
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
7273
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7374
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
@@ -76,8 +77,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
7677
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
7778
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
7879
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
79-
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
8080
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
81+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
8182
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
8283
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
8384
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -92,6 +93,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
9293
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9394
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
9495
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
96+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
9597
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
9698
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
9799
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 main
16+
17+
import (
18+
"testing"
19+
20+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
21+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/testhelpers"
22+
)
23+
24+
func TestFunctions(t *testing.T) {
25+
testhelpers.RunGoldenTests(t, "testdata", fn.ResourceListProcessorFunc(Run))
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
apiVersion: config.kubernetes.io/v1
2+
kind: ResourceList
3+
items:
4+
- apiVersion: iam.cnrm.cloud.google.com/v1beta1
5+
kind: IAMPartialPolicy
6+
metadata:
7+
name: simple1-editors
8+
namespace: config-control
9+
spec:
10+
bindings:
11+
- members:
12+
- member: user:[email protected]
13+
role: roles/editor
14+
resourceRef:
15+
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
16+
kind: Project
17+
name: simple1
18+
- apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
19+
kind: Folder
20+
metadata:
21+
name: simple1
22+
namespace: config-control
23+
spec:
24+
displayName: packagename
25+
folderRef:
26+
name: environments
27+
- apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
28+
kind: Project
29+
metadata:
30+
name: simple1
31+
namespace: config-control
32+
spec:
33+
name: packagename
34+
folderRef:
35+
name: environments
36+
- # We need to enable some services so we can enable other services
37+
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
38+
kind: Service
39+
metadata:
40+
annotations:
41+
cnrm.cloud.google.com/deletion-policy: "abandon"
42+
name: simple1-serviceusage
43+
namespace: config-control
44+
spec:
45+
resourceID: serviceusage.googleapis.com
46+
projectRef:
47+
name: simple1
48+
- # Needed to grant GCP IAM permissions on projects
49+
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
50+
kind: Service
51+
metadata:
52+
annotations:
53+
cnrm.cloud.google.com/deletion-policy: "abandon"
54+
name: cloudresourcemanager.googleapis.com
55+
namespace: packagename
56+
- apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
57+
kind: Service
58+
metadata:
59+
annotations:
60+
cnrm.cloud.google.com/deletion-policy: "abandon"
61+
name: container.googleapis.com
62+
namespace: packagename
63+
- apiVersion: v1
64+
kind: ConfigMap
65+
metadata:
66+
name: kptfile.kpt.dev
67+
annotations:
68+
config.kubernetes.io/local-config: "true"
69+
data:
70+
name: simple1
71+
functionConfig:
72+
kind: ConfigMap
73+
apiVersion: v1
74+
data: {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
kind: ConfigMap
2+
apiVersion: v1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: kptfile.kpt.dev
5+
annotations:
6+
config.kubernetes.io/local-config: "true"
7+
data:
8+
name: simple1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
2+
kind: Folder
3+
metadata:
4+
name: packagename
5+
namespace: config-control
6+
spec:
7+
displayName: packagename
8+
folderRef:
9+
name: environments
10+
11+
---
12+
13+
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
14+
kind: Project
15+
metadata:
16+
name: packagename
17+
namespace: config-control
18+
spec:
19+
name: packagename
20+
folderRef:
21+
name: environments
22+
23+
---
24+
25+
# We need to enable some services so we can enable other services
26+
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
27+
kind: Service
28+
metadata:
29+
annotations:
30+
cnrm.cloud.google.com/deletion-policy: "abandon"
31+
name: packagename-serviceusage
32+
namespace: config-control
33+
spec:
34+
resourceID: serviceusage.googleapis.com
35+
projectRef:
36+
name: packagename
37+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: iam.cnrm.cloud.google.com/v1beta1
2+
kind: IAMPartialPolicy
3+
metadata:
4+
name: packagename-editors
5+
namespace: config-control
6+
spec:
7+
bindings:
8+
- members:
9+
- member: user:[email protected]
10+
role: roles/editor
11+
resourceRef:
12+
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
13+
kind: Project
14+
name: packagename
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
2+
kind: Service
3+
metadata:
4+
annotations:
5+
cnrm.cloud.google.com/deletion-policy: "abandon"
6+
name: container.googleapis.com
7+
namespace: packagename
8+
9+
---
10+
11+
# Needed to grant GCP IAM permissions on projects
12+
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
13+
kind: Service
14+
metadata:
15+
annotations:
16+
cnrm.cloud.google.com/deletion-policy: "abandon"
17+
name: cloudresourcemanager.googleapis.com
18+
namespace: packagename

0 commit comments

Comments
 (0)