From e4f555362a6e426d38a43058eeba2ec29f833576 Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Thu, 7 Sep 2023 18:23:42 +0800 Subject: [PATCH] kubernetes/unstructured: add a function to convert the object's YAML content into an unstructured object --- go.mod | 2 +- kubernetes/unstructured/unstructured.go | 24 ++++++++++--- kubernetes/unstructured/unstructured_test.go | 36 ++++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 kubernetes/unstructured/unstructured_test.go diff --git a/go.mod b/go.mod index 6f3dc13..3005d90 100644 --- a/go.mod +++ b/go.mod @@ -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 ( @@ -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 ) diff --git a/kubernetes/unstructured/unstructured.go b/kubernetes/unstructured/unstructured.go index 3da2b7a..8cdd362 100644 --- a/kubernetes/unstructured/unstructured.go +++ b/kubernetes/unstructured/unstructured.go @@ -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 +} diff --git a/kubernetes/unstructured/unstructured_test.go b/kubernetes/unstructured/unstructured_test.go new file mode 100644 index 0000000..3ca292a --- /dev/null +++ b/kubernetes/unstructured/unstructured_test.go @@ -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) + } + }) + } +}