blob: 0c9cf6b337302859ceef0aaa10c2a38cafb17a69 [file] [log] [blame]
giolekva8aa73e82022-07-09 11:34:39 +04001package installer
giolekva050609f2021-12-29 15:51:40 +04002
giolekva8aa73e82022-07-09 11:34:39 +04003import (
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +04004 "bytes"
gio0eaf2712024-04-14 13:08:46 +04005 _ "embed"
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +04006 "encoding/json"
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04007 "fmt"
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +04008 template "html/template"
gio3cdee592024-04-17 10:15:56 +04009 "net"
gioe72b54f2024-04-22 10:44:41 +040010 "net/netip"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040011 "strings"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040012
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +040013 "cuelang.org/go/cue"
gio308105e2024-04-19 13:12:13 +040014 "cuelang.org/go/cue/build"
15 "cuelang.org/go/cue/cuecontext"
16 "cuelang.org/go/cue/load"
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +040017 cueyaml "cuelang.org/go/encoding/yaml"
giof8843412024-05-22 16:38:05 +040018 helmv2 "github.com/fluxcd/helm-controller/api/v2"
giolekva8aa73e82022-07-09 11:34:39 +040019)
giolekva050609f2021-12-29 15:51:40 +040020
giof8843412024-05-22 16:38:05 +040021//go:embed app_configs/dodo_app.cue
22var dodoAppCue []byte
gio0eaf2712024-04-14 13:08:46 +040023
giof8843412024-05-22 16:38:05 +040024//go:embed app_configs/app_base.cue
25var cueBaseConfig []byte
gio0eaf2712024-04-14 13:08:46 +040026
giof8843412024-05-22 16:38:05 +040027//go:embed app_configs/app_global_env.cue
28var cueEnvAppGlobal []byte
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040029
giof8843412024-05-22 16:38:05 +040030//go:embed app_configs/app_global_infra.cue
31var cueInfraAppGlobal []byte
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040032
gioe72b54f2024-04-22 10:44:41 +040033type rendered struct {
giof8843412024-05-22 16:38:05 +040034 Name string
35 Readme string
36 Resources CueAppData
37 HelmCharts HelmCharts
38 ContainerImages map[string]ContainerImage
39 Ports []PortForward
40 Data CueAppData
41 URL string
42 Help []HelpDocument
43 Icon string
gio94904702024-07-26 16:58:34 +040044 Raw []byte
Davit Tabidze56f86a42024-04-09 19:15:25 +040045}
46
47type HelpDocument struct {
48 Title string
49 Contents string
50 Children []HelpDocument
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +040051}
52
giof8843412024-05-22 16:38:05 +040053type ContainerImage struct {
54 Registry string `json:"registry"`
55 Repository string `json:"repository"`
56 Name string `json:"name"`
57 Tag string `json:"tag"`
58}
59
60type helmChartRef struct {
61 Kind string `json:"kind"`
62}
63
64type HelmCharts struct {
65 Git map[string]HelmChartGitRepo
66}
67
68type HelmChartGitRepo struct {
69 Address string `json:"address"`
70 Branch string `json:"branch"`
71 Path string `json:"path"`
72}
73
gioe72b54f2024-04-22 10:44:41 +040074type EnvAppRendered struct {
75 rendered
76 Config AppInstanceConfig
77}
78
79type InfraAppRendered struct {
80 rendered
81 Config InfraAppInstanceConfig
82}
83
gio3cdee592024-04-17 10:15:56 +040084type PortForward struct {
85 Allocator string `json:"allocator"`
gioefa0ed42024-06-13 12:31:43 +040086 ReserveAddr string `json:"reservator"`
giocdfa3722024-06-13 20:10:14 +040087 RemoveAddr string `json:"deallocator"`
gio3cdee592024-04-17 10:15:56 +040088 Protocol string `json:"protocol"`
89 SourcePort int `json:"sourcePort"`
90 TargetService string `json:"targetService"`
91 TargetPort int `json:"targetPort"`
92}
93
94type AppType int
95
96const (
97 AppTypeInfra AppType = iota
98 AppTypeEnv
99)
100
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400101type App interface {
102 Name() string
gio44f621b2024-04-29 09:44:38 +0400103 Type() AppType
104 Slug() string
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400105 Description() string
106 Icon() template.HTML
107 Schema() Schema
gioef01fbb2024-04-12 16:52:59 +0400108 Namespace() string
gio3cdee592024-04-17 10:15:56 +0400109}
110
111type InfraConfig struct {
gioe72b54f2024-04-22 10:44:41 +0400112 Name string `json:"pcloudEnvName,omitempty"` // #TODO(gio): change to name
113 PublicIP []net.IP `json:"publicIP,omitempty"`
114 InfraNamespacePrefix string `json:"namespacePrefix,omitempty"`
115 InfraAdminPublicKey []byte `json:"infraAdminPublicKey,omitempty"`
gio3cdee592024-04-17 10:15:56 +0400116}
117
gio7841f4f2024-07-26 19:53:49 +0400118type InfraNetwork struct {
119 Name string `json:"name,omitempty"`
120 IngressClass string `json:"ingressClass,omitempty"`
121 CertificateIssuer string `json:"certificateIssuer,omitempty"`
122 AllocatePortAddr string `json:"allocatePortAddr,omitempty"`
123 ReservePortAddr string `json:"reservePortAddr,omitempty"`
124 DeallocatePortAddr string `json:"deallocatePortAddr,omitempty"`
125}
126
gio3cdee592024-04-17 10:15:56 +0400127type InfraApp interface {
128 App
gio7841f4f2024-07-26 19:53:49 +0400129 Render(release Release, infra InfraConfig, networks []InfraNetwork, values map[string]any, charts map[string]helmv2.HelmChartTemplateSpec) (InfraAppRendered, error)
gioe72b54f2024-04-22 10:44:41 +0400130}
131
132type EnvNetwork struct {
133 DNS net.IP `json:"dns,omitempty"`
134 DNSInClusterIP net.IP `json:"dnsInClusterIP,omitempty"`
135 Ingress net.IP `json:"ingress,omitempty"`
136 Headscale net.IP `json:"headscale,omitempty"`
137 ServicesFrom net.IP `json:"servicesFrom,omitempty"`
138 ServicesTo net.IP `json:"servicesTo,omitempty"`
139}
140
141func NewEnvNetwork(subnet net.IP) (EnvNetwork, error) {
142 addr, err := netip.ParseAddr(subnet.String())
143 if err != nil {
144 return EnvNetwork{}, err
145 }
146 if !addr.Is4() {
147 return EnvNetwork{}, fmt.Errorf("Expected IPv4, got %s instead", addr)
148 }
149 dns := addr.Next()
150 ingress := dns.Next()
151 headscale := ingress.Next()
152 b := addr.AsSlice()
153 if b[3] != 0 {
154 return EnvNetwork{}, fmt.Errorf("Expected last byte to be zero, got %d instead", b[3])
155 }
156 b[3] = 10
157 servicesFrom, ok := netip.AddrFromSlice(b)
158 if !ok {
159 return EnvNetwork{}, fmt.Errorf("Must not reach")
160 }
161 b[3] = 254
162 servicesTo, ok := netip.AddrFromSlice(b)
163 if !ok {
164 return EnvNetwork{}, fmt.Errorf("Must not reach")
165 }
166 b[3] = b[2]
167 b[2] = b[1]
168 b[0] = 10
169 b[1] = 44
170 dnsInClusterIP, ok := netip.AddrFromSlice(b)
171 if !ok {
172 return EnvNetwork{}, fmt.Errorf("Must not reach")
173 }
174 return EnvNetwork{
175 DNS: net.ParseIP(dns.String()),
176 DNSInClusterIP: net.ParseIP(dnsInClusterIP.String()),
177 Ingress: net.ParseIP(ingress.String()),
178 Headscale: net.ParseIP(headscale.String()),
179 ServicesFrom: net.ParseIP(servicesFrom.String()),
180 ServicesTo: net.ParseIP(servicesTo.String()),
181 }, nil
gio3cdee592024-04-17 10:15:56 +0400182}
183
gioe72b54f2024-04-22 10:44:41 +0400184type EnvConfig struct {
185 Id string `json:"id,omitempty"`
186 InfraName string `json:"pcloudEnvName,omitempty"`
187 Domain string `json:"domain,omitempty"`
188 PrivateDomain string `json:"privateDomain,omitempty"`
189 ContactEmail string `json:"contactEmail,omitempty"`
190 AdminPublicKey string `json:"adminPublicKey,omitempty"`
191 PublicIP []net.IP `json:"publicIP,omitempty"`
192 NameserverIP []net.IP `json:"nameserverIP,omitempty"`
193 NamespacePrefix string `json:"namespacePrefix,omitempty"`
194 Network EnvNetwork `json:"network,omitempty"`
gio3cdee592024-04-17 10:15:56 +0400195}
196
197type EnvApp interface {
198 App
giocb34ad22024-07-11 08:01:13 +0400199 Render(release Release, env EnvConfig, networks []Network, values map[string]any, charts map[string]helmv2.HelmChartTemplateSpec) (EnvAppRendered, error)
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400200}
201
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400202type cueApp struct {
203 name string
204 description string
205 icon template.HTML
206 namespace string
207 schema Schema
gio308105e2024-04-19 13:12:13 +0400208 cfg cue.Value
209 data CueAppData
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400210}
211
gio308105e2024-04-19 13:12:13 +0400212type CueAppData map[string][]byte
213
214func ParseCueAppConfig(data CueAppData) (cue.Value, error) {
215 ctx := cuecontext.New()
216 buildCtx := build.NewContext()
217 cfg := &load.Config{
218 Context: buildCtx,
219 Overlay: map[string]load.Source{},
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400220 }
gio308105e2024-04-19 13:12:13 +0400221 names := make([]string, 0)
222 for n, b := range data {
223 a := fmt.Sprintf("/%s", n)
224 names = append(names, a)
225 cfg.Overlay[a] = load.FromString("package main\n\n" + string(b))
226 }
227 instances := load.Instances(names, cfg)
228 for _, inst := range instances {
229 if inst.Err != nil {
230 return cue.Value{}, inst.Err
231 }
232 }
233 if len(instances) != 1 {
234 return cue.Value{}, fmt.Errorf("invalid")
235 }
236 ret := ctx.BuildInstance(instances[0])
237 if ret.Err() != nil {
238 return cue.Value{}, ret.Err()
239 }
240 if err := ret.Validate(); err != nil {
241 return cue.Value{}, err
242 }
243 return ret, nil
244}
245
246func newCueApp(config cue.Value, data CueAppData) (cueApp, error) {
gio3cdee592024-04-17 10:15:56 +0400247 cfg := struct {
248 Name string `json:"name"`
249 Namespace string `json:"namespace"`
250 Description string `json:"description"`
251 Icon string `json:"icon"`
252 }{}
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400253 if err := config.Decode(&cfg); err != nil {
254 return cueApp{}, err
255 }
gio44f621b2024-04-29 09:44:38 +0400256 schema, err := NewCueSchema("input", config.LookupPath(cue.ParsePath("input")))
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400257 if err != nil {
258 return cueApp{}, err
259 }
260 return cueApp{
261 name: cfg.Name,
262 description: cfg.Description,
263 icon: template.HTML(cfg.Icon),
264 namespace: cfg.Namespace,
265 schema: schema,
266 cfg: config,
gio308105e2024-04-19 13:12:13 +0400267 data: data,
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400268 }, nil
269}
270
gio308105e2024-04-19 13:12:13 +0400271func ParseAndCreateNewCueApp(data CueAppData) (cueApp, error) {
272 config, err := ParseCueAppConfig(data)
273 if err != nil {
274 return cueApp{}, err
275 }
276 return newCueApp(config, data)
277}
278
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400279func (a cueApp) Name() string {
280 return a.name
281}
282
gio44f621b2024-04-29 09:44:38 +0400283func (a cueApp) Slug() string {
284 return strings.ReplaceAll(strings.ToLower(a.name), " ", "-")
285}
286
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400287func (a cueApp) Description() string {
288 return a.description
289}
290
291func (a cueApp) Icon() template.HTML {
292 return a.icon
293}
294
295func (a cueApp) Schema() Schema {
296 return a.schema
297}
298
gioef01fbb2024-04-12 16:52:59 +0400299func (a cueApp) Namespace() string {
300 return a.namespace
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400301}
302
gioe72b54f2024-04-22 10:44:41 +0400303func (a cueApp) render(values map[string]any) (rendered, error) {
304 ret := rendered{
gio44f621b2024-04-29 09:44:38 +0400305 Name: a.Slug(),
gio308105e2024-04-19 13:12:13 +0400306 Resources: make(CueAppData),
giof8843412024-05-22 16:38:05 +0400307 HelmCharts: HelmCharts{
308 Git: make(map[string]HelmChartGitRepo),
309 },
310 ContainerImages: make(map[string]ContainerImage),
311 Ports: make([]PortForward, 0),
312 Data: a.data,
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400313 }
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400314 var buf bytes.Buffer
gio3cdee592024-04-17 10:15:56 +0400315 if err := json.NewEncoder(&buf).Encode(values); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400316 return rendered{}, err
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400317 }
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400318 ctx := a.cfg.Context()
319 d := ctx.CompileBytes(buf.Bytes())
320 res := a.cfg.Unify(d).Eval()
321 if err := res.Err(); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400322 return rendered{}, err
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400323 }
324 if err := res.Validate(); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400325 return rendered{}, err
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400326 }
gio308105e2024-04-19 13:12:13 +0400327 full, err := json.MarshalIndent(res, "", "\t")
328 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400329 return rendered{}, err
gio308105e2024-04-19 13:12:13 +0400330 }
gio94904702024-07-26 16:58:34 +0400331 ret.Raw = full
gio308105e2024-04-19 13:12:13 +0400332 ret.Data["rendered.json"] = full
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400333 readme, err := res.LookupPath(cue.ParsePath("readme")).String()
334 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400335 return rendered{}, err
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400336 }
337 ret.Readme = readme
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400338 if err := res.LookupPath(cue.ParsePath("portForward")).Decode(&ret.Ports); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400339 return rendered{}, err
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400340 }
gio0eaf2712024-04-14 13:08:46 +0400341 {
giof8843412024-05-22 16:38:05 +0400342 charts := res.LookupPath(cue.ParsePath("charts"))
343 i, err := charts.Fields()
344 if err != nil {
345 return rendered{}, err
346 }
347 for i.Next() {
348 var chartRef helmChartRef
349 if err := i.Value().Decode(&chartRef); err != nil {
350 return rendered{}, err
351 }
352 if chartRef.Kind == "GitRepository" {
353 var chart HelmChartGitRepo
354 if err := i.Value().Decode(&chart); err != nil {
355 return rendered{}, err
356 }
357 ret.HelmCharts.Git[cleanName(i.Selector().String())] = chart
358 }
359 }
360 }
361 {
362 images := res.LookupPath(cue.ParsePath("images"))
363 i, err := images.Fields()
364 if err != nil {
365 return rendered{}, err
366 }
367 for i.Next() {
368 var img ContainerImage
369 if err := i.Value().Decode(&img); err != nil {
370 return rendered{}, err
371 }
372 ret.ContainerImages[cleanName(i.Selector().String())] = img
373 }
374 }
375 {
gio0eaf2712024-04-14 13:08:46 +0400376 output := res.LookupPath(cue.ParsePath("output"))
377 i, err := output.Fields()
378 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400379 return rendered{}, err
gio0eaf2712024-04-14 13:08:46 +0400380 }
381 for i.Next() {
382 if contents, err := cueyaml.Encode(i.Value()); err != nil {
383 return rendered{}, err
384 } else {
385 name := fmt.Sprintf("%s.yaml", cleanName(i.Selector().String()))
386 ret.Resources[name] = contents
387 }
388 }
389 }
390 {
391 resources := res.LookupPath(cue.ParsePath("resources"))
392 i, err := resources.Fields()
393 if err != nil {
394 return rendered{}, err
395 }
396 for i.Next() {
397 if contents, err := cueyaml.Encode(i.Value()); err != nil {
398 return rendered{}, err
399 } else {
400 name := fmt.Sprintf("%s.yaml", cleanName(i.Selector().String()))
401 ret.Resources[name] = contents
402 }
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400403 }
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400404 }
Davit Tabidze56f86a42024-04-09 19:15:25 +0400405 helpValue := res.LookupPath(cue.ParsePath("help"))
406 if helpValue.Exists() {
407 if err := helpValue.Decode(&ret.Help); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400408 return rendered{}, err
Davit Tabidze56f86a42024-04-09 19:15:25 +0400409 }
410 }
411 url, err := res.LookupPath(cue.ParsePath("url")).String()
412 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400413 return rendered{}, err
Davit Tabidze56f86a42024-04-09 19:15:25 +0400414 }
gio09a3e5b2024-04-26 14:11:06 +0400415 ret.URL = url
Davit Tabidze56f86a42024-04-09 19:15:25 +0400416 icon, err := res.LookupPath(cue.ParsePath("icon")).String()
417 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400418 return rendered{}, err
Davit Tabidze56f86a42024-04-09 19:15:25 +0400419 }
420 ret.Icon = icon
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400421 return ret, nil
422}
423
gio3cdee592024-04-17 10:15:56 +0400424type cueEnvApp struct {
425 cueApp
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400426}
427
gio308105e2024-04-19 13:12:13 +0400428func NewCueEnvApp(data CueAppData) (EnvApp, error) {
429 app, err := ParseAndCreateNewCueApp(data)
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400430 if err != nil {
431 return nil, err
432 }
gio3cdee592024-04-17 10:15:56 +0400433 return cueEnvApp{app}, nil
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400434}
435
gio0eaf2712024-04-14 13:08:46 +0400436func NewDodoApp(appCfg []byte) (EnvApp, error) {
437 return NewCueEnvApp(CueAppData{
giofc9c4ea2024-06-26 13:46:53 +0400438 "app.cue": appCfg,
439 "base.cue": []byte(cueBaseConfig),
440 "dodo.cue": dodoAppCue,
441 "env.cue": []byte(cueEnvAppGlobal),
gio0eaf2712024-04-14 13:08:46 +0400442 })
443}
444
gio3cdee592024-04-17 10:15:56 +0400445func (a cueEnvApp) Type() AppType {
446 return AppTypeEnv
447}
448
giocb34ad22024-07-11 08:01:13 +0400449func (a cueEnvApp) Render(
450 release Release,
451 env EnvConfig,
452 networks []Network,
453 values map[string]any,
454 charts map[string]helmv2.HelmChartTemplateSpec,
455) (EnvAppRendered, error) {
gio3cdee592024-04-17 10:15:56 +0400456 derived, err := deriveValues(values, a.Schema(), networks)
457 if err != nil {
gioefa0ed42024-06-13 12:31:43 +0400458 return EnvAppRendered{}, err
gio3cdee592024-04-17 10:15:56 +0400459 }
giof8843412024-05-22 16:38:05 +0400460 if charts == nil {
461 charts = make(map[string]helmv2.HelmChartTemplateSpec)
462 }
gio3cdee592024-04-17 10:15:56 +0400463 ret, err := a.cueApp.render(map[string]any{
giof8843412024-05-22 16:38:05 +0400464 "global": env,
465 "release": release,
466 "input": derived,
467 "localCharts": charts,
gio5e49bb62024-07-20 10:43:19 +0400468 "networks": NetworkMap(networks),
gio3cdee592024-04-17 10:15:56 +0400469 })
470 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400471 return EnvAppRendered{}, err
gio3cdee592024-04-17 10:15:56 +0400472 }
gioe72b54f2024-04-22 10:44:41 +0400473 return EnvAppRendered{
474 rendered: ret,
475 Config: AppInstanceConfig{
gio44f621b2024-04-29 09:44:38 +0400476 AppId: a.Slug(),
gioe72b54f2024-04-22 10:44:41 +0400477 Env: env,
478 Release: release,
479 Values: values,
480 Input: derived,
gio09a3e5b2024-04-26 14:11:06 +0400481 URL: ret.URL,
gioe72b54f2024-04-22 10:44:41 +0400482 Help: ret.Help,
gio09a3e5b2024-04-26 14:11:06 +0400483 Icon: ret.Icon,
gioe72b54f2024-04-22 10:44:41 +0400484 },
485 }, nil
gio3cdee592024-04-17 10:15:56 +0400486}
487
488type cueInfraApp struct {
489 cueApp
490}
491
gio308105e2024-04-19 13:12:13 +0400492func NewCueInfraApp(data CueAppData) (InfraApp, error) {
493 app, err := ParseAndCreateNewCueApp(data)
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400494 if err != nil {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400495 return nil, err
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400496 }
gio3cdee592024-04-17 10:15:56 +0400497 return cueInfraApp{app}, nil
498}
499
500func (a cueInfraApp) Type() AppType {
501 return AppTypeInfra
502}
503
gio7841f4f2024-07-26 19:53:49 +0400504func (a cueInfraApp) Render(release Release, infra InfraConfig, networks []InfraNetwork, values map[string]any, charts map[string]helmv2.HelmChartTemplateSpec) (InfraAppRendered, error) {
giof8843412024-05-22 16:38:05 +0400505 if charts == nil {
506 charts = make(map[string]helmv2.HelmChartTemplateSpec)
507 }
gioe72b54f2024-04-22 10:44:41 +0400508 ret, err := a.cueApp.render(map[string]any{
giof8843412024-05-22 16:38:05 +0400509 "global": infra,
510 "release": release,
511 "input": values,
512 "localCharts": charts,
gio7841f4f2024-07-26 19:53:49 +0400513 "networks": InfraNetworkMap(networks),
gio3cdee592024-04-17 10:15:56 +0400514 })
gioe72b54f2024-04-22 10:44:41 +0400515 if err != nil {
516 return InfraAppRendered{}, err
517 }
518 return InfraAppRendered{
519 rendered: ret,
520 Config: InfraAppInstanceConfig{
gio44f621b2024-04-29 09:44:38 +0400521 AppId: a.Slug(),
gioe72b54f2024-04-22 10:44:41 +0400522 Infra: infra,
523 Release: release,
524 Values: values,
525 Input: values,
gio09a3e5b2024-04-26 14:11:06 +0400526 URL: ret.URL,
527 Help: ret.Help,
gioe72b54f2024-04-22 10:44:41 +0400528 },
529 }, nil
Giorgi Lekveishvilief21c132024-01-17 18:57:58 +0400530}
531
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400532func cleanName(s string) string {
533 return strings.ReplaceAll(strings.ReplaceAll(s, "\"", ""), "'", "")
Giorgi Lekveishvilief21c132024-01-17 18:57:58 +0400534}
gioe72b54f2024-04-22 10:44:41 +0400535
536func join[T fmt.Stringer](items []T, sep string) string {
537 var tmp []string
538 for _, i := range items {
539 tmp = append(tmp, i.String())
540 }
541 return strings.Join(tmp, ",")
542}
gio0eaf2712024-04-14 13:08:46 +0400543
gio5e49bb62024-07-20 10:43:19 +0400544func NetworkMap(networks []Network) map[string]Network {
gio0eaf2712024-04-14 13:08:46 +0400545 ret := make(map[string]Network)
546 for _, n := range networks {
547 ret[strings.ToLower(n.Name)] = n
548 }
549 return ret
550}
gio7841f4f2024-07-26 19:53:49 +0400551
552func InfraNetworkMap(networks []InfraNetwork) map[string]InfraNetwork {
553 ret := make(map[string]InfraNetwork)
554 for _, n := range networks {
555 ret[strings.ToLower(n.Name)] = n
556 }
557 return ret
558}