Created
March 25, 2022 18:59
-
-
Save jeesmon/945b0a142228aede796e439c088ce9b3 to your computer and use it in GitHub Desktop.
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
var decUnstructured = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) | |
func DecodePodSpec(spec corev1.PodSpec) (*unstructured.Unstructured, error) { | |
pod := &corev1.Pod{ | |
ObjectMeta: v1.ObjectMeta{ | |
Name: "name", | |
Namespace: "namespace", | |
}, | |
Spec: *spec.DeepCopy(), | |
} | |
scheme := runtime.NewScheme() | |
corev1.AddToScheme(scheme) | |
codec := serializer.NewCodecFactory(scheme).LegacyCodec(corev1.SchemeGroupVersion) | |
bytes, err := runtime.Encode(codec, pod) | |
if err != nil { | |
return nil, err | |
} | |
resource := &unstructured.Unstructured{} | |
_, _, err = decUnstructured.Decode(bytes, nil, resource) | |
if err != nil { | |
return nil, err | |
} | |
return resource, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment