Skip to content

Fix possible nil-pointer error; add golden test for set-name-prefix #926

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 28 additions & 25 deletions functions/go/bind/pkg/meta/refvisitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,37 @@ func VisitRefs(objects fn.KubeObjects, visitor func(ref Ref) error) error {
}

func visitFields(object *fn.KubeObject, subObject *fn.SubObject, fields []string, refInfo *refInfo, visitor func(ref Ref) error) error {
// Guard against nil objects
if subObject == nil {
return nil
}

if len(fields) == 0 {
if subObject != nil {
// Ignore external refs, these don't get renamed
// TODO: Check if supports external ??
external := subObject.GetString("external")
if external != "" {
return nil
}
// Ignore external refs, these don't get renamed
// TODO: Check if supports external ??
external := subObject.GetString("external")
if external != "" {
return nil
}

ref, err := refInfo.buildFullyQualifiedRef(object, subObject)
if err != nil {
return err
}
if err := visitor(ref); err != nil {
return err
}
ref, err := refInfo.buildFullyQualifiedRef(object, subObject)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the name "FullyQualified"!

if err != nil {
return err
}
if err := visitor(ref); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit: seems to be more readable as "visit"?

Copy link
Contributor

Choose a reason for hiding this comment

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

ignore this comment. Didn't notice this is a common naming convention in go.

return err
}

if refInfo.NamespaceFieldPath != "" {
namespaceFieldPath := strings.Split(refInfo.NamespaceFieldPath, ".")
namespace, _, _ := subObject.NestedString(namespaceFieldPath...)
if namespace != "" {
ref, err := buildRefNamespaceReference(subObject, namespaceFieldPath...)
if err != nil {
return err
}
if err := visitor(ref); err != nil {
return err
}
if refInfo.NamespaceFieldPath != "" {
namespaceFieldPath := strings.Split(refInfo.NamespaceFieldPath, ".")
namespace, _, _ := subObject.NestedString(namespaceFieldPath...)
if namespace != "" {
ref, err := buildRefNamespaceReference(subObject, namespaceFieldPath...)
if err != nil {
return err
}
if err := visitor(ref); err != nil {
return err
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion functions/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go
go 1.18

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

Expand Down
10 changes: 6 additions & 4 deletions functions/go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221007213718-5fa523b306fe h1:8PNHR5cev3FqOduItCH59KZdabDwz9I5xNcB8Woc6es=
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221007213718-5fa523b306fe/go.mod h1:prNhhUAODrB2VqHVead9tB8nLU9ffY4e4jjBwLMNO1M=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220929172014-8888fc9691d1 h1:DFLHb51Av8m6ETGvd+J4Ok0pvfo735OW0sP+wbJLQyg=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220929172014-8888fc9691d1/go.mod h1:TZd94D9b8alIDuF3mW9g+ofbl0RfwT0JewXmXm7phDI=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221007213718-5fa523b306fe h1:BjbTCT1mkPf0s4X3osOleEcaa89psOlcEJGUcLc+YPo=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221007213718-5fa523b306fe/go.mod h1:mqc5jH6i0Ll6T4wCmTXsVNVxhwBGlVtrtmoF/g3E9lE=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221104163847-f50fe3a2977e h1:sSHJXFviCfgx6t8lDjjJ7Z1X4+Ed0m/pYP/MHJ0vWo4=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221104163847-f50fe3a2977e/go.mod h1:mqc5jH6i0Ll6T4wCmTXsVNVxhwBGlVtrtmoF/g3E9lE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
Expand Down Expand Up @@ -67,7 +67,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
Expand All @@ -76,8 +77,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -92,6 +93,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
26 changes: 26 additions & 0 deletions functions/go/set-name-prefix/golden_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 (
"testing"

"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/testhelpers"
)

func TestFunctions(t *testing.T) {
testhelpers.RunGoldenTests(t, "testdata", fn.ResourceListProcessorFunc(Run))
}
74 changes: 74 additions & 0 deletions functions/go/set-name-prefix/testdata/simple1/_expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
name: simple1-editors
namespace: config-control
spec:
bindings:
- members:
- member: user:[email protected]
role: roles/editor
resourceRef:
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
kind: Project
name: simple1
- apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
kind: Folder
metadata:
name: simple1
namespace: config-control
spec:
displayName: packagename
folderRef:
name: environments
- apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
kind: Project
metadata:
name: simple1
namespace: config-control
spec:
name: packagename
folderRef:
name: environments
- # We need to enable some services so we can enable other services
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
kind: Service
metadata:
annotations:
cnrm.cloud.google.com/deletion-policy: "abandon"
name: simple1-serviceusage
namespace: config-control
spec:
resourceID: serviceusage.googleapis.com
projectRef:
name: simple1
- # Needed to grant GCP IAM permissions on projects
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
kind: Service
metadata:
annotations:
cnrm.cloud.google.com/deletion-policy: "abandon"
name: cloudresourcemanager.googleapis.com
namespace: packagename
- apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
kind: Service
metadata:
annotations:
cnrm.cloud.google.com/deletion-policy: "abandon"
name: container.googleapis.com
namespace: packagename
- apiVersion: v1
kind: ConfigMap
metadata:
name: kptfile.kpt.dev
annotations:
config.kubernetes.io/local-config: "true"
data:
name: simple1
functionConfig:
kind: ConfigMap
apiVersion: v1
data: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kind: ConfigMap
apiVersion: v1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: kptfile.kpt.dev
annotations:
config.kubernetes.io/local-config: "true"
data:
name: simple1
37 changes: 37 additions & 0 deletions functions/go/set-name-prefix/testdata/simple1/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
kind: Folder
metadata:
name: packagename
namespace: config-control
spec:
displayName: packagename
folderRef:
name: environments

---

apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
kind: Project
metadata:
name: packagename
namespace: config-control
spec:
name: packagename
folderRef:
name: environments

---

# We need to enable some services so we can enable other services
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
kind: Service
metadata:
annotations:
cnrm.cloud.google.com/deletion-policy: "abandon"
name: packagename-serviceusage
namespace: config-control
spec:
resourceID: serviceusage.googleapis.com
projectRef:
name: packagename

14 changes: 14 additions & 0 deletions functions/go/set-name-prefix/testdata/simple1/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
name: packagename-editors
namespace: config-control
spec:
bindings:
- members:
- member: user:[email protected]
role: roles/editor
resourceRef:
apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
kind: Project
name: packagename
18 changes: 18 additions & 0 deletions functions/go/set-name-prefix/testdata/simple1/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
kind: Service
metadata:
annotations:
cnrm.cloud.google.com/deletion-policy: "abandon"
name: container.googleapis.com
namespace: packagename

---

# Needed to grant GCP IAM permissions on projects
apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
kind: Service
metadata:
annotations:
cnrm.cloud.google.com/deletion-policy: "abandon"
name: cloudresourcemanager.googleapis.com
namespace: packagename