func (r *RegistryCenterReconciler) updateIstioConfigMap(ctx context.Context) (ctrl.Result, error) {
rlog.Info("start update istio configmap")
cmList := &v1.ConfigMapList{}
selector := &client.MatchingLabelsSelector{
Selector: labels.SelectorFromSet(map[string]string{
"release": "istio",
}),
}
// 查询指定标签的cm
err := r.Client.List(ctx, cmList, &client.ListOptions{
Namespace: "istio-system",
LabelSelector: selector,
})
if err != nil {
rlog.Error("list istio configmap error", err)
}
for _, cm := range cmList.Items {
cmData := cm.Data
if cmData == nil {
break
}
meshData := cmData["mesh"]
if meshData == "" {
break
}
// 定义一个 map 来解析 YAML 数据
var data map[string]interface{}
// 将 YAML 字符串解析为 Go 对象
err := yaml.Unmarshal([]byte(meshData), &data)
if err != nil {
return ctrl.Result{}, err
}
data["configSources"] = []map[string]string{
{
"address": "k8s://localhost/",
},
}
// 将编辑后的数据转换回 YAML 字符串
newYAML, err := yaml.Marshal(data)
if err != nil {
rlog.Error("convert istio configmap error", err)
}
// 输出新的 YAML 字符串
fmt.Println("Modified YAML:")
fmt.Println(string(newYAML))
cmData["mesh"] = string(newYAML)
// 打印解析后的数据
fmt.Printf("Parsed YAML data: %+v\n", data)
err = r.Update(ctx, &cm)
if err != nil {
rlog.Error("update istio configmap error", err)
}
rlog.Info("update istio configmap success")
}
rlog.Info("istio configmap updated successfully")
return ctrl.Result{}, nil
}
go如何修改configmap
最新推荐文章于 2024-09-09 16:24:26 发布