我如何表示此结构的匹配标签部分以打印出来。我尝试以类似元数据的格式添加“NetworkPolicySpecPodSelector”结构,但我的格式不正确。
package main
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Metadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
type Spec struct {
PodSelector NetworkPolicySpecPodSelector `yaml:"Spec"`
}
type NetworkPolicySpecPodSelector struct {
MatchLabels map[string][]string `yaml:"matchLabels"`
}
type Ingress struct {
}
type NetworkPolicy struct {
ApiVersion string `yaml:"apiVersion`
Kind string `yaml:"kind"`
Metadata Metadata `yaml:"metadata"`
Spec struct {
PodSelector NetworkPolicySpecPodSelector
}
PolicyTypes []string `yaml:"policyTypes"`
}
func main() {
np := NetworkPolicy{
ApiVersion: "networking.k8s.io/v1",
Kind: "NetworkPolicy",
Metadata: Metadata{
Name: "allow-ingress",
Namespace: "default",
},
PolicyTypes: []string{"Ingress"},
}
yamlData, err := yaml.Marshal(&np)
if err != nil {
fmt.Printf("Error while Marshaling. %v", err)
}
fileName := "test.yaml"
err = ioutil.WriteFile(fileName, yamlData, 0644)
if err != nil {
panic("Unable to write data into the file")
}
}
我的输出在这里:
apiversion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-ingress
namespace: default
spec:
podselector:
matchLabels: {}
policyTypes:
- Ingress
期望的输出:
apiversion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-ingress
namespace: default
spec:
podselector:
matchLabels:
env:prod
policyTypes:
- Ingress
仍然需要成长,但我在这方面苦苦挣扎。该项目的目标是使用 YAML 输出 Kubernetes 网络策略。
拉莫斯之舞
相关分类