Skip to content
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

kubernetes/unstructured: change the obj parameter of the ConvertToTyped function to a common interface #19

Merged
merged 1 commit into from
Sep 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion kubernetes/unstructured/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func ConvertToUnstructured(obj interface{}) (*unstructured.Unstructured, error)
// ConvertToTyped(object, node)
//
//nolint:gofmt,goimports
func ConvertToTyped(obj *unstructured.Unstructured, typedObj interface{}) error {
func ConvertToTyped(obj runtime.Unstructured, typedObj interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), typedObj)
}

Expand Down
30 changes: 20 additions & 10 deletions kubernetes/unstructured/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

func TestConvertToUnstructured(t *testing.T) {
Expand Down Expand Up @@ -58,27 +60,36 @@ metadata:
func TestConvertToTyped(t *testing.T) {
tests := []struct {
name string
obj interface{}
obj runtime.Unstructured
typedObj interface{}
isErr bool
}{
{
name: "obj test",
obj: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "test",
},
},
},
typedObj: &corev1.Namespace{},
isErr: false,
},
{
name: "list test",
obj: &corev1.NamespaceList{
Items: []corev1.Namespace{
obj: &unstructured.UnstructuredList{
Object: map[string]interface{}{
"kind": "List",
},
Items: []unstructured.Unstructured{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Object: map[string]interface{}{
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "test",
},
},
},
},
Expand All @@ -89,8 +100,7 @@ func TestConvertToTyped(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
unstructuredObj, _ := ConvertToUnstructured(tt.obj)
if err := ConvertToTyped(unstructuredObj, tt.typedObj); tt.isErr != (err != nil) {
if err := ConvertToTyped(tt.obj, tt.typedObj); tt.isErr != (err != nil) {
t.Errorf("%s ConvertToTyped() unexpected error: %v", tt.name, err)
}
})
Expand Down