pythonkubernetesopenshiftpyyaml

How can I extract certain qualified nodes from a yaml document and add to a new document using PyYAML?


I'm new with both Python and YAML (great with Javascript and JSON) and I need to construct a subset of a YAML file. In particular, I want to generate a .yaml file that can be used to create a new deployment in openshift/kubernetes, gleaning the needed information from running pods/deployments. The YAML file will have separate sections for PVCs, Service, and Deployment.

I need to eliminate things like status, and a lot of the metadata that is generated when new pvcs, services, and deployments are created. The steps I want to use are:

It seems like an easy thing to do, but with my scarce experience with both Python and YAML, I can't seem to get it right. I can't even traverse all of the nodes of the YAML document and print out the qualified node names.

Edit: I know which oc/kubectl commands I need to get the YAML I need, I just need some help on taking only certain sections from those files and generating a new document by inserting those extracted sections,


Solution

  • Does your python script run on environment that has kubectl? You can try to create python script which does things like -

    1. Extract the data using kubectl commands using jsonpath filters; storing key information in variables
    2. Create Kubernetes deployment imperative commands passing relevant parameters and generating a source yaml with --dry-run=client and -oyaml
      This will ensure a proper kubernetes compliant yaml is generated.
    3. Execute the yaml using python script.

    P.S. These ideas might make things simple.