1 回答
TA贡献1842条经验 获得超21个赞
我不太了解园丁的财产。但是您可以做的是为InfrastructureConfig下面的内容创建一个结构
type InfraConfig struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Networks struct {
CloudNAT struct {
MinPortsPerVM int `json:"minPortsPerVM"`
NatIPNames []struct {
Name string `json:"name"`
} `json:"natIPNames"`
} `json:"cloudNAT"`
} `json:"networks"`
}
并创建一个引用该对象的变量并将其解组Raw为如下所示。
var existingInfraConfig InfraConfig
err := json.Unmarshal(shoot.Spec.Provider.InfrastructureConfig.Raw, &existingInfraConfig)
然后您可以编辑名称(您可能想添加一些逻辑来验证切片以更新正确的索引)并将其编组并将其分配回 InfraConfig,如下所示。
existingInfraConfig.Networks.CloudNAT.NatIPNames[0].Name = "test2"
byteData, _ := json.Marshal(existingInfraConfig)
shoot.Spec.Provider.InfrastructureConfig = &runtime.RawExtension{
Raw: byteData,
Object: nil,
}
总的来说,就像
package main
import (
"encoding/json"
"fmt"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"io/ioutil"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
)
type InfraConfig struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Networks struct {
CloudNAT struct {
MinPortsPerVM int `json:"minPortsPerVM"`
NatIPNames []struct {
Name string `json:"name"`
} `json:"natIPNames"`
} `json:"cloudNAT"`
} `json:"networks"`
}
func main() {
shoot, e := parseShoot("test.yaml")
if e != nil {
fmt.Println(e)
}
var existingInfraConfig InfraConfig
err := json.Unmarshal(shoot.Spec.Provider.InfrastructureConfig.Raw, &existingInfraConfig)
fmt.Println(err)
existingInfraConfig.Networks.CloudNAT.NatIPNames[0].Name = "test2"
byteData, _ := json.Marshal(existingInfraConfig)
shoot.Spec.Provider.InfrastructureConfig = &runtime.RawExtension{
Raw: byteData,
Object: nil,
}
fmt.Println(string(shoot.Spec.Provider.InfrastructureConfig.Raw))
}
func parseShoot(path string) (*v1beta1.Shoot, error) {
var shootSpec *v1beta1.Shoot
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(bytes, &shootSpec)
if err != nil {
return nil, err
}
return shootSpec, nil
}
- 1 回答
- 0 关注
- 77 浏览
添加回答
举报