Last active
August 3, 2021 17:12
-
-
Save aaronjwood/d0da96b9dd63e0206113578a041eb1a6 to your computer and use it in GitHub Desktop.
Parsing documents from a manifest
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
func ParseSearchConstruct(resources []*restmapper.APIGroupResources, dynamicClient dynamic.Interface, doc string) (*Resource, error) { | |
unstruct, gvk, err := decode(doc) | |
if err != nil { | |
return nil, err | |
} | |
ns, nsFound, err := search(unstruct, "metadata", "namespace") | |
if err != nil { | |
return nil, err | |
} | |
kind, kindFound, err := search(unstruct, "kind") | |
if err != nil { | |
return nil, err | |
} | |
// Not all resources will have or work with a namespace. | |
if !nsFound || (kindFound && (kind.(string) == "CustomResourceDefinition" || kind.(string) == "ClusterRole" || kind.(string) == "ClusterRoleBinding")) { | |
ns = "" | |
} | |
name, found, err := search(unstruct, "metadata", "name") | |
if err != nil { | |
return nil, err | |
} | |
if !found { | |
return nil, errors.New("Failed to find name field") | |
} | |
mapping, err := restMapping(resources, gvk) | |
if err != nil { | |
return nil, err | |
} | |
// If the namespace returned to us is empty then the action call will work in a non-namespaced context. | |
// Think of resources that are cluster-wide and not namespace specific. | |
return &Resource{ | |
Action: dynamicClient.Resource(mapping.Resource).Namespace(ns.(string)), | |
Mapping: mapping, | |
Ns: ns.(string), | |
Name: name.(string), | |
Unstruct: unstruct, | |
}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment