github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/steps/kyma_template_common.go (about) 1 package steps 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "gopkg.in/yaml.v2" 8 9 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 10 "k8s.io/apimachinery/pkg/runtime" 11 k8syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml" 12 k8syamlutil "k8s.io/apimachinery/pkg/util/yaml" 13 ) 14 15 func DecodeKymaTemplate(kymaTemplate string) (*unstructured.Unstructured, error) { 16 tmpl := []byte(kymaTemplate) 17 18 decoder := k8syamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(tmpl), 512) 19 var rawObj runtime.RawExtension 20 if err := decoder.Decode(&rawObj); err != nil { 21 return nil, err 22 } 23 obj, _, err := k8syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, nil) 24 if err != nil { 25 return nil, err 26 } 27 28 unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj) 29 unstructuredObj := &unstructured.Unstructured{Object: unstructuredMap} 30 return unstructuredObj, err 31 } 32 33 func EncodeKymaTemplate(kymaTemplate *unstructured.Unstructured) (string, error) { 34 result, err := yaml.Marshal(kymaTemplate.Object) 35 if err != nil { 36 return "", fmt.Errorf("while marshal unstructured to yaml: %v", err) 37 } 38 return string(result), nil 39 }