登录
首页 >  Golang >  Go问答

从Golang编写的应用程序中,通过OpenShift API获取和更新持久卷的YAML配置文件

来源:stackoverflow

时间:2024-03-28 18:00:26 436浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《从Golang编写的应用程序中,通过OpenShift API获取和更新持久卷的YAML配置文件》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

我正在尝试通过 go(lang) 中的 api 修改 openshiftpersistent 卷 go(lang) 文件,我有以下内容

pv, err := clientset.corev1().persistentvolumes().list(metav1.listoptions{})


    for _, persv := range pv.items {
        // check status persistent volume
        pvstatus, err := clientset.corev1().persistentvolumes().get(persv.name, metav1.getoptions{})
        if err != nil {
            panic(err.error())
        }

        patch := []byte(`{"spec":{"template":{"spec":{"containers":[{"persistentvolumereclaimpolicy":"retain"}]}}}}`)
        a := fmt.sprintf("%s", patch)
        fmt.println(a)
        _, err = clientset.corev1().persistentvolumes().patch(persv.name, types.strategicmergepatchtype, patch)
}

我的持久卷 yaml

apiVersion: v1
kind: PersistentVolume
metadata:
...
...
...
persistentVolumeReclaimPolicy: Retain
status:
  phase: Released

如何获取 yaml 文件并根据实际的 pvstatus 对其进行修改? 我想将 persistentvolumereclaimpolicy: retain 更改为 persistentvolumereclaimpolicy: delete


解决方案


基本上我的错误是构造 patch []byte 值,脚本应该如下所示

pv, err := clientset.CoreV1().PersistentVolumes().List(metav1.ListOptions{})


    for _, persV := range pv.Items {
        // Check status persistent volume
        pvStatus, err := clientset.CoreV1().PersistentVolumes().Get(persV.Name, metav1.GetOptions{})
        if err != nil {
            panic(err.Error())
        }

        patch := []byte(`{"spec": {"persistentVolumeReclaimPolicy": "Delete"}}`)
        _, err = clientset.CoreV1().PersistentVolumes().Patch(persV.Name, types.StrategicMergePatchType, patch)

本篇关于《从Golang编写的应用程序中,通过OpenShift API获取和更新持久卷的YAML配置文件》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>