0

我想自动化我的自定义资源的 kubedb API 版本。如何获取管理 bu 集群的 API 版本并将其传递到可以自动运行的代码中。我正在使用多个集群,每个集群都由不同的 Api 版本管理,例如我的开发集群由 kubedb.com/v1alpha1 管理,但我的生产集群由 kubedb.com/v1alpha2 管理。所以我想根据每个集群 API 版本自动化它。

{
        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",
            },
        }

我想在以下代码中更新以下 apiVersion

  "apiVersion": "kubedb.com/v1alpha1",
4

1 回答 1

0

这只是一张地图,所以你可以打电话

pg.Object["apiVersion"]

请参阅下面的完整示例

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"])
}
于 2020-11-20T19:07:53.013 回答