Skip to content

Commit

Permalink
Merge pull request #17 from iawia002/unstructured
Browse files Browse the repository at this point in the history
kubernetes/unstructured: add a function to convert the object's YAML content into an unstructured object
  • Loading branch information
iawia002 authored Sep 7, 2023
2 parents c5fce15 + e4f5553 commit 6316719
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
k8s.io/client-go v0.28.0
k8s.io/klog/v2 v2.100.1
sigs.k8s.io/controller-runtime v0.16.0
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -50,5 +51,4 @@ require (
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
24 changes: 19 additions & 5 deletions kubernetes/unstructured/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@ package unstructured
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
)

// ConvertToUnstructured converts a typed object to an unstructured object.
func ConvertToUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
uncastObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: uncastObj}, nil
return &unstructured.Unstructured{Object: unstructuredObj}, nil
}

//nolint:gofmt,goimports
// ConvertToTyped converts an unstructured object to a typed object.
// Usage:
// node := &corev1.Node{}
// ConvertToTyped(object, node)
//
// node := &corev1.Node{}
// ConvertToTyped(object, node)
//
//nolint:gofmt,goimports
func ConvertToTyped(obj *unstructured.Unstructured, typedObj interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), typedObj)
}

// YAMLToUnstructured converts the object's YAML content into an unstructured object.
func YAMLToUnstructured(content []byte) (*unstructured.Unstructured, error) {
obj := make(map[string]interface{})
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
}
return &unstructured.Unstructured{
Object: obj,
}, nil
}
36 changes: 36 additions & 0 deletions kubernetes/unstructured/unstructured_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package unstructured

import (
"testing"
)

func TestYAMLToUnstructured(t *testing.T) {
tests := []struct {
name string
obj []byte
isErr bool
}{
{
name: "normal test",
obj: []byte(`
apiVersion: v1
kind: Namespace
metadata:
name: test
`),
isErr: false,
},
{
name: "error test",
obj: []byte(`aaa`),
isErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := YAMLToUnstructured(tt.obj); tt.isErr != (err != nil) {
t.Errorf("%s YAMLToUnstructured() unexpected error: %v", tt.name, err)
}
})
}
}

0 comments on commit 6316719

Please sign in to comment.