1 回答
TA贡献1820条经验 获得超10个赞
首先,您需要修复结构字段标签以反映您想要的结构。Spec在NetworkPolicy需要序列化为键spec及其字段PodSelector为podselector:
type NetworkPolicy struct {
ApiVersion string `yaml:"apiVersion`
Kind string `yaml:"kind"`
Metadata Metadata `yaml:"metadata"`
Spec Spec `yaml:"spec"`
PolicyTypes []string `yaml:"policyTypes"`
}
type Spec struct {
PodSelector NetworkPolicySpecPodSelector `yaml:"podselector"`
}
然后你可以简单地使用文字:
np := NetworkPolicy{
ApiVersion: "networking.k8s.io/v1",
Kind: "NetworkPolicy",
Metadata: Metadata{
Name: "allow-ingress",
Namespace: "default",
},
Spec: Spec{
PodSelector: NetworkPolicySpecPodSelector{
MatchLabels: map[string][]string{
"env": []string{"prod"},
},
},
},
PolicyTypes: []string{"Ingress"},
}
这是操场上的完整示例:https ://go.dev/play/p/xJ-mmCVcv2M
注意:在您的代码片段中,类型MatchLabels是map[string][]string. 我保持这样,虽然从示例中看起来像你想要map[string]string的。
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报