-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (130 loc) · 2.97 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
type Appspec struct {
Image string `yaml:"image" json:"image"`
Port int `yaml:"port" json:"port"`
Domain string `yaml:"value" json:"value"`
}
type App struct {
Metadata struct {
Name string `yaml:"name"`
} `yaml:"metadata"`
Spec Appspec `yaml:"spec" json:"spec"`
}
// Generate Deployment yaml
func generateDeployment(name string, image string) (*yaml.RNode, error) {
d, err := yaml.Parse(fmt.Sprintf(`apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: %s
name: %s
spec:
replicas: 1
selector:
matchLabels:
app: %s
template:
metadata:
labels:
app: %s
spec:
containers:
- image: %s
name: %s
`, name, name, name, name, image, name))
if err != nil {
return nil, err
}
return d, nil
}
// Generate Service yaml
func generateService(name string, sourcePort int, targetPort int) (*yaml.RNode, error) {
serviceName := name + "-svc"
svc, err := yaml.Parse(fmt.Sprintf(`apiVersion: v1
kind: Service
metadata:
labels:
app: %s
name: %s
spec:
selector:
app: %s
ports:
- name: http
port: %d
protocol: TCP
targetPort: %d
`, name, serviceName, name, sourcePort, targetPort))
if err != nil {
return nil, err
}
return svc, nil
}
func generateIngress(name string, domain string, sourcePort int) (*yaml.RNode, error) {
serviceName := name + "-svc"
ing, err := yaml.Parse(fmt.Sprintf(`apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: %s
namespace: monitoring
spec:
rules:
- host: %s
http:
paths:
- pathType: Prefix
backend:
service:
name: %s
port:
number: %d
path: /
`, name, domain, serviceName, sourcePort))
if err != nil {
return nil, err
}
return ing, nil
}
func main() {
config := new(App)
fn := func(items []*yaml.RNode) ([]*yaml.RNode, error) {
var newNodes []*yaml.RNode
resourceName := config.Metadata.Name
for _ = range items {
// get generated Deployment
deploy, err := generateDeployment(resourceName, config.Spec.Image)
if err != nil {
return nil, err
}
newNodes = append(newNodes, deploy)
// get generated Service
service, err := generateService(resourceName, config.Spec.Port, config.Spec.Port)
if err != nil {
return nil, err
}
newNodes = append(newNodes, service)
// get generated Ingress
ingress, err := generateIngress(resourceName, config.Spec.Domain, config.Spec.Port)
if err != nil {
return nil, err
}
newNodes = append(newNodes, ingress)
}
items = newNodes
return items, nil
}
p := framework.SimpleProcessor{Config: config, Filter: kio.FilterFunc(fn)}
cmd := command.Build(p, command.StandaloneDisabled, false)
command.AddGenerateDockerfile(cmd)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}