-
Notifications
You must be signed in to change notification settings - Fork 10
add function to get object namespaced name #24
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
Vicente-Cheng
wants to merge
2
commits into
harvester:main
Choose a base branch
from
Vicente-Cheng:add-get-namespaced-name
base: main
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,28 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ) | ||
|
|
||
| // GetNamespacedName returns the namespaced name of the object. | ||
| // If the object is not supported, it returns "NOSUPPORTED_OBJECT_TYPE". | ||
| func GetNamespacedName[T any](t T) (string, error) { | ||
| switch obj := any(t).(type) { | ||
| case metav1.Object: | ||
| if obj.GetNamespace() == "" { | ||
| return "", fmt.Errorf("input object type: %T should have namespaced", t) | ||
| } | ||
| if obj.GetName() == "" { | ||
| return "", fmt.Errorf("input object type: %T should have name", t) | ||
| } | ||
| return types.NamespacedName{ | ||
| Namespace: obj.GetNamespace(), | ||
| Name: obj.GetName(), | ||
| }.String(), nil | ||
| default: | ||
| return "", fmt.Errorf("unsupported object type: %T", t) | ||
| } | ||
| } | ||
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,84 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| type mockObject struct { | ||
| metav1.ObjectMeta | ||
| } | ||
|
|
||
| func TestGetNamespacedName(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| input any | ||
| expectedRet string | ||
| expectedErr bool | ||
| }{ | ||
| { | ||
| name: "valid object", | ||
| input: &mockObject{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-name", | ||
| Namespace: "test-ns", | ||
| }, | ||
| }, | ||
| expectedRet: "test-ns/test-name", | ||
| expectedErr: false, | ||
| }, | ||
| { | ||
| name: "nil object", | ||
| input: nil, | ||
| expectedRet: "", | ||
| expectedErr: true, | ||
| }, | ||
| { | ||
| name: "empty namespace", | ||
| input: &mockObject{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-name", | ||
| Namespace: "", | ||
| }, | ||
| }, | ||
| expectedRet: "", | ||
| expectedErr: true, | ||
| }, | ||
| { | ||
| name: "empty name", | ||
| input: &mockObject{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "", | ||
| Namespace: "test-ns", | ||
| }, | ||
| }, | ||
| expectedRet: "", | ||
| expectedErr: true, | ||
| }, | ||
| { | ||
| name: "empty name and namespace", | ||
| input: &mockObject{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "", | ||
| Namespace: "", | ||
| }, | ||
| }, | ||
| expectedRet: "", | ||
| expectedErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| result, err := GetNamespacedName(testCase.input) | ||
| if (err != nil) != testCase.expectedErr { | ||
| t.Errorf("GetNamespacedName() error = %v, wantErr %v", err, testCase.expectedErr) | ||
| return | ||
| } | ||
| if result != testCase.expectedRet { | ||
| t.Errorf("GetNamespacedName() = %v, want %v", result, testCase.expectedRet) | ||
| } | ||
| }) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,23 +1,43 @@ | ||
| module github.com/harvester/go-common | ||
|
|
||
| go 1.23 | ||
| go 1.24.1 | ||
|
|
||
| require ( | ||
| github.com/coreos/go-systemd/v22 v22.5.0 | ||
| github.com/fsnotify/fsnotify v1.6.0 | ||
| github.com/godbus/dbus/v5 v5.0.4 | ||
| github.com/sirupsen/logrus v1.9.2 | ||
| github.com/stretchr/testify v1.7.0 | ||
| github.com/stretchr/testify v1.9.0 | ||
| gopkg.in/yaml.v3 v3.0.1 | ||
| ) | ||
|
|
||
| require github.com/prometheus/procfs v0.15.1 | ||
|
|
||
| require github.com/pkg/errors v0.9.1 | ||
|
|
||
| require ( | ||
| github.com/fxamacker/cbor/v2 v2.7.0 // indirect | ||
| github.com/go-logr/logr v1.4.2 // indirect | ||
| github.com/gogo/protobuf v1.3.2 // indirect | ||
| github.com/google/gofuzz v1.2.0 // indirect | ||
| github.com/json-iterator/go v1.1.12 // indirect | ||
| github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
| github.com/modern-go/reflect2 v1.0.2 // indirect | ||
| github.com/x448/float16 v0.8.4 // indirect | ||
| golang.org/x/net v0.30.0 // indirect | ||
| golang.org/x/text v0.19.0 // indirect | ||
| gopkg.in/inf.v0 v0.9.1 // indirect | ||
| k8s.io/klog/v2 v2.130.1 // indirect | ||
| k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect | ||
| sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect | ||
| sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect | ||
| sigs.k8s.io/yaml v1.4.0 // indirect | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/Masterminds/semver/v3 v3.2.1 | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| golang.org/x/sys v0.20.0 // indirect | ||
| github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
| github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
| golang.org/x/sys v0.26.0 // indirect | ||
| k8s.io/apimachinery v0.32.3 | ||
| ) |
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
Oops, something went wrong.
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.
Doesn't an empty string mean the
defaultnamespace, per the k8s api doc?Uh oh!
There was an error while loading. Please reload this page.
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.
In our case, however, we should assume that we have a namespace, because otherwise the use of this helper function would make no sense. This is also implied by the function name. Therefore I think it is correct to throw an error.
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.
Thanks @ihcsim for pointing out this.
But I have the same point with @votdev. This is the internal helper function, and it is used for the namespaced object. So, I would like to return an error once the namespace is empty.
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.
My only hesitation is that how do we guarantee
defaultis always specified, considering Harvester is interacting with other components like kubevirt and LH. Will those components follow the same convention? Or will this function be used strictly for Harvester resources only?