项目中用到需要使用client-go来读取并修改,删除k8s里configmap的内容,本文用来记录一下具体的使用及代码实例
1.configmap格式本例子所用到的configmap是一个map类型的数组,里面存储着每一块创建的硬盘的信息,具体格式如下:
apiVersion: v1
data:
config.json: '{"testpvs":[{"volumeid":"314f39fb-9dab-42fb-b755-7f0c5aab449e","status":"Delete","persistentVolumeReclaimPolicy":"Delete"},{"volumeid":"515d7c9c-03a6-4918-a611-0f0bad6636de","status":"Delete","persistentVolumeReclaimPolicy":"Delete"},{"volumeid":"71652d15-8b3a-4886-b5fc-2d1fa73df197","status":"Bound","persistentVolumeReclaimPolicy":"Delete"},{"volumeid":"8eba9774-9737-4a2d-bae9-a44ad307962a","status":"Delete","persistentVolumeReclaimPolicy":"Delete"}]}'
kind: ConfigMap
metadata:
creationTimestamp: "2022-03-09T02:21:18Z"
name: test-configmap
namespace: default
2.go解析
go语言解析的时候我们可以看到,存储在configmap中的是一个json,所以我们需要读取configmap的内容并解析json到一个map中,然后对map进行遍历、修改、删除等 *** 作。下面看一下具体代码:
//先定义两个结构体,一个表示具体的结构,一个是该结构的数组
type testPv struct {
VolumeID string `json:"volumeid"`
Status string `json:"status"`
PersistentVolumeReclaimPolicy string `json:"persistentVolumeReclaimPolicy"`
}
type testPvs struct {
Pvs []inDiskPv `json:"indiskpvs"`
}
下面是一个解析并在数组中新增一条数据的例子
func (p *cinderProvisioner) addToConfigmap(volumeID string, status string, pvrp string) error {
glog.Infof("addToConfigmap volumeID: %v, status: %v, pvrp: %v", volumeID, status, pvrp)
client := p.Client
configmap, err := client.CoreV1().ConfigMaps("default").Get("test-configmap", metav1.GetOptions{})
if err != nil {
glog.Errorf("Failed to get configmap : %v", err)
return fmt.Errorf("Failed to get configmap")
}
datamap, ok := configmap.Data["config.json"]
if !ok {
glog.Errorf("Failed to get configmap : %v", err)
return fmt.Errorf("Failed to get configmap")
}
testpvs := testPvs{}
err = json.Unmarshal([]byte(datamap), &testpvs)
if err != nil {
glog.Errorf("json decode failed : %v", err)
return fmt.Errorf("Failed to get configmap")
}
newpv := testPv{}
newpv.VolumeID = volumeID
newpv.Status = status
newpv.PersistentVolumeReclaimPolicy = pvrp
testpvs.Pvs = append(testpvs.Pvs, newpv)
pvsarr, err1 := json.Marshal(testpvs)
if err1 != nil {
glog.Errorf("Failed to get configmap : %v", err1)
return fmt.Errorf("Failed to get configmap")
}
configmap.Data["config.json"] = string(pvsarr)
configmap, err2 := client.CoreV1().ConfigMaps("default").Update(configmap)
if err2 != nil {
glog.Errorf("update configmap : %v", err2)
return fmt.Errorf("update configmap error")
}
glog.Infof("update configmap success: %v", configmap)
return nil
}
对应的删除 *** 作可以这样
index := -1
pvs := testpvs.Pvs
for i := 0; i < len(pvs); i++ {
if pvs[i].VolumeID == volumeID {
index = i
}
}
if index >= 0 {
pvs = append(pvs[:index], pvs[index+1:]...)
}
testpvs.Pvs = pvs
pvsarr, err1 := json.Marshal(testpvs)
if err1 != nil {
glog.Errorf("Failed to get configmap : %v", err1)
return fmt.Errorf("Failed to get configmap")
}
configmap.Data["config.json"] = string(pvsarr)
configmap, err2 := client.CoreV1().ConfigMaps("default").Update(configmap)
if err2 != nil {
glog.Errorf("update configmap : %v", err2)
return fmt.Errorf("update configmap error")
}
以上纯属个人理解 ,如有问题希望大家指正,一起交流学习!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)