-
Notifications
You must be signed in to change notification settings - Fork 72
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
justinsb
wants to merge
1
commit into
kptdev:master
Choose a base branch
from
justinsb:add_setnameprefix_fn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
if err != nil { | ||
return err | ||
} | ||
if err := visitor(ref); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: seems to be more readable as "visit"? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
functions/go/set-name-prefix/testdata/simple1/_expected.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
kind: ConfigMap | ||
apiVersion: v1 |
8 changes: 8 additions & 0 deletions
8
functions/go/set-name-prefix/testdata/simple1/package-context.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
functions/go/set-name-prefix/testdata/simple1/project.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
functions/go/set-name-prefix/testdata/simple1/services.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"!