0

I want to automate kubedb API-version of my custom resource. How can I get the API version that manages bu cluster and pass it in a code that will work in an automatic way. I am using multiple clusters and every cluster is manage by different Api-versions such as my dev cluster is managing by kubedb.com/v1alpha1 but my cluster on production is managed by kubedb.com/v1alpha2. So I want to automate it according to every cluster API-versions.

{
        pg := &unstructured.Unstructured{}
        pg.Object = map[string]interface{}{
            "kind":       "Postgres",
            "apiVersion": "kubedb.com/v1alpha1",
            "metadata": map[string]interface{}{
                "name":      instance.Name + "-timescaledb",
                "namespace": instance.Namespace,
            },
            "spec": map[string]interface{}{
                "version":     "11.1-v1",
                "storageType": "Durable",
                "storage": map[string]interface{}{
                    "storageClassName": "standard",
                    "accessModes":      []string{"ReadWriteOnce"},
                    "resources": map[string]interface{}{
                        "requests": map[string]interface{}{
                            "storage": "50Gi",
                        },
                    },
                },
                "terminationPolicy": "DoNotTerminate",
            },
        }

I want to update this the following apiVersion in the following code

  "apiVersion": "kubedb.com/v1alpha1",
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ayesha kaleem
  • 77
  • 2
  • 7

1 Answers1

0

It's just a map so you can call

pg.Object["apiVersion"]

See below for a complete example

package main

import (
    "fmt"

    "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func main() {

    pg := &unstructured.Unstructured{}
    pg.Object = map[string]interface{}{
        "kind":       "Postgres",
        "apiVersion": "kubedb.com/v1alpha1",
        "metadata": map[string]interface{}{
            "name":      "instance.Name" + "-timescaledb",
            "namespace": "instance.Namespace",
        },
        "spec": map[string]interface{}{
            "version":     "11.1-v1",
            "storageType": "Durable",
            "storage": map[string]interface{}{
                "storageClassName": "standard",
                "accessModes":      []string{"ReadWriteOnce"},
                "resources": map[string]interface{}{
                    "requests": map[string]interface{}{
                        "storage": "50Gi",
                    },
                },
            },
            "terminationPolicy": "DoNotTerminate",
        },
    }
    // Print current apiVersion
    fmt.Println(pg.Object["apiVersion"])

    //change apiVersion
    pg.Object["apiVersion"] = "mynewVersion.Com/v1alpha4"

    // Print new apiVersion
    fmt.Println(pg.Object["apiVersion"])
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
David Walton
  • 321
  • 4
  • 10
  • That's not my concern. I am trying to automate APIVersion according to the environment. I would require client-go API that would provide current supportive Kubedb APIVersion in a k8s cluster. – ayesha kaleem Dec 01 '20 at 15:06
  • @ayeshakaleem - depends on your k8s setup, you can use a simple curl to either as a shell command or programatically with golang and check the kubernetes API for that info without the need of the client-go package – David Walton Dec 02 '20 at 16:26
  • I am working on Kubernetes operator, that builds pods and everything automatically, that's why I want to automate it in code. – ayesha kaleem Dec 08 '20 at 13:23