Containers and Kubernetes

Get Involved. Join the Conversation.

Topic

    enrique ortiz
    kubernetes get ddl
    Topic posted June 17, 2019 by enrique ortizRed Ribbon: 250+ Points, tagged Kubernetes, Tip 
    46 Views, 1 Comment
    Title:
    kubernetes get ddl
    Content:

    hi all 

    i want to know if there is a way to create an script ( terraform , ansible ) from a manually created kubernetes cluster in order to recreate it on another compartment 

     

    thanks

    Comment

     

    • Jon-Eric Eliker

      There may be some third-party tools that provide this end-to-end capability but I cannot say for certain. Otherwise let me describer what I have done to achieve what you as asking. I have used the Python SDK for OCI read the existing cluster information and write the relevant Terraform files.

      For a simple example using this method to create a compartment, consider this...

      import oci
      
      config = oci.config.from_file('~/.oci/config')
      iam_client = oci.identity.IdentityClient(config)
      comp = iam_client.get_compartment(compartment_id = "YOUR_COMP_ID").data
      with open("./comp.tf", "w") as f:
        f.write('resource "oci_identity_compartment" "comp1" {\n')
        f.write('  compartment_id = "{0}"\n'.format(comp.compartment_id)) #remember this is container of the compartment not compartment itself
        f.write('  description = "{0}"\n'.format(comp.description))
        f.write('  name = "{0}"\n'.format(comp.name))
        #I am ignoring tags here
        f.write('}\n')
      

      With a little bit more work you can make this accept YOUR_COMP_ID as a parameter to the script so you can reuse to "reverse engineer" any compartment.  Another option would be to loop through all compartments using something like this:

      import oci
      
      config = oci.config.from_file('~/.oci/config')
      iam_client = oci.identity.IdentityClient(config)
      comps = iam_client.list_compartments(compartment_id = "YOUR_TENANCY_ID").data #note I'm ignoring pagination so only 21 will be listed
      i = 1
      for comp in comps: 
        with open("./comp.tf", "w") as f:
          f.write('resource "oci_identity_compartment" "comp{0}" {{\n'.format(str(i)))
          f.write('  compartment_id = "{0}"\n'.format(comp.compartment_id)) #remember this is container of the compartment not compartment itself
          f.write('  description = "{0}"\n'.format(comp.description))
          f.write('  name = "{0}"\n'.format(comp.name))
          f.write('}\n\n')
        i = i + 1
      

      This same process can be used to create a Terraform file based on any OCI resource including k8s clusters.

      See this page for the OCI Python SDK documentation about k8s resources ("container engine" resources in OCI terms): https://oracle-cloud-infrastructure-python-sdk.readthedocs.io/en/latest/api/container_engine.html

      See these pages for Terraform documentation on k8s in OCI: https://www.terraform.io/docs/providers/oci/r/containerengine_cluster.html and https://www.terraform.io/docs/providers/oci/r/containerengine_node_pool.html

      Jon-Eric
      Mythics, Inc.