-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (51 loc) · 1.4 KB
/
Copy pathmain.go
File metadata and controls
60 lines (51 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"fmt"
"strings"
"gopkg.in/yaml.v2"
)
type Metadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
type Spec struct {
PodSelector NetworkPolicySpecPodSelector `yaml:"podSelector"`
PolicyTypes []string `yaml:"policyTypes"`
}
type NetworkPolicySpecPodSelector struct {
MatchLabels map[string]string `yaml:"matchLabels"`
}
type NetworkPolicy struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata Metadata `yaml:"metadata"`
Spec Spec `yaml:"spec"`
}
func main() {
np := NetworkPolicy{
ApiVersion: "networking.k8s.io/v1",
Kind: "NetworkPolicy",
Metadata: Metadata{
Name: "allow-ingress",
Namespace: "default",
},
Spec: Spec{
PodSelector: NetworkPolicySpecPodSelector{
MatchLabels: map[string]string{},
},
PolicyTypes: []string{"Ingress", "Egress"},
},
}
flag.StringVar(&np.Metadata.Name, "name", "", "Choose a Name for the Network Policy")
flag.StringVar(&np.Metadata.Namespace, "namespace", "", "Specify Namespace for the Network Policy")
flag.Func("labels", "Specify a key value pair for policy labels using =. app=dev", func(flagValue string) error {
s := strings.Split(flagValue, "=")
np.Spec.PodSelector.MatchLabels[(s[0])] = s[1]
return nil
},
)
flag.Parse()
yamlData, _ := yaml.Marshal(&np)
fmt.Println(string(yamlData))
}