blob: e57fee8bf81f80b6c5422decee570478900c48b4 [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
giof6ad2982024-08-23 17:42:49 +040036 Cluster string
37 Namespaces []Namespace
giof8843412024-05-22 16:38:05 +040038 Resources CueAppData
39 HelmCharts HelmCharts
40 ContainerImages map[string]ContainerImage
41 Ports []PortForward
giof6ad2982024-08-23 17:42:49 +040042 ClusterProxies map[string]ClusterProxy
giof8843412024-05-22 16:38:05 +040043 Data CueAppData
44 URL string
45 Help []HelpDocument
46 Icon string
gio94904702024-07-26 16:58:34 +040047 Raw []byte
Davit Tabidze56f86a42024-04-09 19:15:25 +040048}
49
giof6ad2982024-08-23 17:42:49 +040050type Namespace struct {
51 Name string `json:"name"`
52 Kubeconfig string `json:"kubeconfig,omitempty"`
53}
54
Davit Tabidze56f86a42024-04-09 19:15:25 +040055type HelpDocument struct {
56 Title string
57 Contents string
58 Children []HelpDocument
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +040059}
60
giof8843412024-05-22 16:38:05 +040061type ContainerImage struct {
62 Registry string `json:"registry"`
63 Repository string `json:"repository"`
64 Name string `json:"name"`
65 Tag string `json:"tag"`
66}
67
68type helmChartRef struct {
69 Kind string `json:"kind"`
70}
71
72type HelmCharts struct {
73 Git map[string]HelmChartGitRepo
74}
75
76type HelmChartGitRepo struct {
77 Address string `json:"address"`
78 Branch string `json:"branch"`
79 Path string `json:"path"`
80}
81
gioe72b54f2024-04-22 10:44:41 +040082type EnvAppRendered struct {
83 rendered
84 Config AppInstanceConfig
85}
86
87type InfraAppRendered struct {
88 rendered
89 Config InfraAppInstanceConfig
90}
91
giof6ad2982024-08-23 17:42:49 +040092type ClusterProxy struct {
93 From string `json:"from"`
94 To string `json:"to"`
95}
96
gio3cdee592024-04-17 10:15:56 +040097type PortForward struct {
gio721c0042025-04-03 11:56:36 +040098 Cluster string `json:"cluster,omitempty"`
gio802311e2024-11-04 08:37:34 +040099 Allocator string `json:"allocator"`
100 ReserveAddr string `json:"reservator"`
101 RemoveAddr string `json:"deallocator"`
102 Protocol string `json:"protocol"`
103 Port int `json:"port"`
104 Service struct {
105 Name string `json:"name"`
106 Port int `json:"port"`
107 } `json:"service"`
gio3cdee592024-04-17 10:15:56 +0400108}
109
110type AppType int
111
112const (
113 AppTypeInfra AppType = iota
114 AppTypeEnv
115)
116
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400117type App interface {
118 Name() string
gio44f621b2024-04-29 09:44:38 +0400119 Type() AppType
120 Slug() string
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400121 Description() string
122 Icon() template.HTML
123 Schema() Schema
gioef01fbb2024-04-12 16:52:59 +0400124 Namespace() string
gio3cdee592024-04-17 10:15:56 +0400125}
126
127type InfraConfig struct {
gioe72b54f2024-04-22 10:44:41 +0400128 Name string `json:"pcloudEnvName,omitempty"` // #TODO(gio): change to name
129 PublicIP []net.IP `json:"publicIP,omitempty"`
130 InfraNamespacePrefix string `json:"namespacePrefix,omitempty"`
131 InfraAdminPublicKey []byte `json:"infraAdminPublicKey,omitempty"`
gio3cdee592024-04-17 10:15:56 +0400132}
133
gio7841f4f2024-07-26 19:53:49 +0400134type InfraNetwork struct {
135 Name string `json:"name,omitempty"`
136 IngressClass string `json:"ingressClass,omitempty"`
137 CertificateIssuer string `json:"certificateIssuer,omitempty"`
138 AllocatePortAddr string `json:"allocatePortAddr,omitempty"`
139 ReservePortAddr string `json:"reservePortAddr,omitempty"`
140 DeallocatePortAddr string `json:"deallocatePortAddr,omitempty"`
141}
142
gio3cdee592024-04-17 10:15:56 +0400143type InfraApp interface {
144 App
gio7841f4f2024-07-26 19:53:49 +0400145 Render(release Release, infra InfraConfig, networks []InfraNetwork, values map[string]any, charts map[string]helmv2.HelmChartTemplateSpec) (InfraAppRendered, error)
gioe72b54f2024-04-22 10:44:41 +0400146}
147
148type EnvNetwork struct {
149 DNS net.IP `json:"dns,omitempty"`
150 DNSInClusterIP net.IP `json:"dnsInClusterIP,omitempty"`
151 Ingress net.IP `json:"ingress,omitempty"`
152 Headscale net.IP `json:"headscale,omitempty"`
153 ServicesFrom net.IP `json:"servicesFrom,omitempty"`
154 ServicesTo net.IP `json:"servicesTo,omitempty"`
155}
156
157func NewEnvNetwork(subnet net.IP) (EnvNetwork, error) {
158 addr, err := netip.ParseAddr(subnet.String())
159 if err != nil {
160 return EnvNetwork{}, err
161 }
162 if !addr.Is4() {
163 return EnvNetwork{}, fmt.Errorf("Expected IPv4, got %s instead", addr)
164 }
165 dns := addr.Next()
166 ingress := dns.Next()
167 headscale := ingress.Next()
168 b := addr.AsSlice()
169 if b[3] != 0 {
170 return EnvNetwork{}, fmt.Errorf("Expected last byte to be zero, got %d instead", b[3])
171 }
172 b[3] = 10
173 servicesFrom, ok := netip.AddrFromSlice(b)
174 if !ok {
175 return EnvNetwork{}, fmt.Errorf("Must not reach")
176 }
177 b[3] = 254
178 servicesTo, ok := netip.AddrFromSlice(b)
179 if !ok {
180 return EnvNetwork{}, fmt.Errorf("Must not reach")
181 }
182 b[3] = b[2]
183 b[2] = b[1]
184 b[0] = 10
185 b[1] = 44
186 dnsInClusterIP, ok := netip.AddrFromSlice(b)
187 if !ok {
188 return EnvNetwork{}, fmt.Errorf("Must not reach")
189 }
190 return EnvNetwork{
191 DNS: net.ParseIP(dns.String()),
192 DNSInClusterIP: net.ParseIP(dnsInClusterIP.String()),
193 Ingress: net.ParseIP(ingress.String()),
194 Headscale: net.ParseIP(headscale.String()),
195 ServicesFrom: net.ParseIP(servicesFrom.String()),
196 ServicesTo: net.ParseIP(servicesTo.String()),
197 }, nil
gio3cdee592024-04-17 10:15:56 +0400198}
199
gioe72b54f2024-04-22 10:44:41 +0400200type EnvConfig struct {
201 Id string `json:"id,omitempty"`
202 InfraName string `json:"pcloudEnvName,omitempty"`
203 Domain string `json:"domain,omitempty"`
204 PrivateDomain string `json:"privateDomain,omitempty"`
205 ContactEmail string `json:"contactEmail,omitempty"`
206 AdminPublicKey string `json:"adminPublicKey,omitempty"`
207 PublicIP []net.IP `json:"publicIP,omitempty"`
208 NameserverIP []net.IP `json:"nameserverIP,omitempty"`
209 NamespacePrefix string `json:"namespacePrefix,omitempty"`
210 Network EnvNetwork `json:"network,omitempty"`
gio3cdee592024-04-17 10:15:56 +0400211}
212
213type EnvApp interface {
214 App
gio36b23b32024-08-25 12:20:54 +0400215 Render(
216 release Release,
217 env EnvConfig,
218 networks []Network,
giof6ad2982024-08-23 17:42:49 +0400219 clusters []Cluster,
gio36b23b32024-08-25 12:20:54 +0400220 values map[string]any,
221 charts map[string]helmv2.HelmChartTemplateSpec,
gio864b4332024-09-05 13:56:47 +0400222 vpnKeyGen VPNAPIClient,
gio36b23b32024-08-25 12:20:54 +0400223 ) (EnvAppRendered, error)
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +0400224}
225
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400226type cueApp struct {
227 name string
228 description string
229 icon template.HTML
230 namespace string
231 schema Schema
gio308105e2024-04-19 13:12:13 +0400232 cfg cue.Value
233 data CueAppData
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400234}
235
gio308105e2024-04-19 13:12:13 +0400236type CueAppData map[string][]byte
237
238func ParseCueAppConfig(data CueAppData) (cue.Value, error) {
239 ctx := cuecontext.New()
240 buildCtx := build.NewContext()
241 cfg := &load.Config{
242 Context: buildCtx,
243 Overlay: map[string]load.Source{},
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400244 }
gio308105e2024-04-19 13:12:13 +0400245 names := make([]string, 0)
246 for n, b := range data {
247 a := fmt.Sprintf("/%s", n)
248 names = append(names, a)
249 cfg.Overlay[a] = load.FromString("package main\n\n" + string(b))
250 }
251 instances := load.Instances(names, cfg)
252 for _, inst := range instances {
253 if inst.Err != nil {
254 return cue.Value{}, inst.Err
255 }
256 }
257 if len(instances) != 1 {
258 return cue.Value{}, fmt.Errorf("invalid")
259 }
260 ret := ctx.BuildInstance(instances[0])
gioc81a8472024-09-24 13:06:19 +0200261 if err := ret.Err(); err != nil {
262 return cue.Value{}, err
gio308105e2024-04-19 13:12:13 +0400263 }
264 if err := ret.Validate(); err != nil {
265 return cue.Value{}, err
266 }
267 return ret, nil
268}
269
270func newCueApp(config cue.Value, data CueAppData) (cueApp, error) {
gio3cdee592024-04-17 10:15:56 +0400271 cfg := struct {
272 Name string `json:"name"`
273 Namespace string `json:"namespace"`
274 Description string `json:"description"`
275 Icon string `json:"icon"`
276 }{}
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400277 if err := config.Decode(&cfg); err != nil {
278 return cueApp{}, err
279 }
gio44f621b2024-04-29 09:44:38 +0400280 schema, err := NewCueSchema("input", config.LookupPath(cue.ParsePath("input")))
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400281 if err != nil {
282 return cueApp{}, err
283 }
284 return cueApp{
285 name: cfg.Name,
286 description: cfg.Description,
287 icon: template.HTML(cfg.Icon),
288 namespace: cfg.Namespace,
289 schema: schema,
290 cfg: config,
gio308105e2024-04-19 13:12:13 +0400291 data: data,
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400292 }, nil
293}
294
gio308105e2024-04-19 13:12:13 +0400295func ParseAndCreateNewCueApp(data CueAppData) (cueApp, error) {
296 config, err := ParseCueAppConfig(data)
297 if err != nil {
298 return cueApp{}, err
299 }
300 return newCueApp(config, data)
301}
302
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400303func (a cueApp) Name() string {
304 return a.name
305}
306
gio44f621b2024-04-29 09:44:38 +0400307func (a cueApp) Slug() string {
308 return strings.ReplaceAll(strings.ToLower(a.name), " ", "-")
309}
310
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400311func (a cueApp) Description() string {
312 return a.description
313}
314
315func (a cueApp) Icon() template.HTML {
316 return a.icon
317}
318
319func (a cueApp) Schema() Schema {
320 return a.schema
321}
322
gioef01fbb2024-04-12 16:52:59 +0400323func (a cueApp) Namespace() string {
324 return a.namespace
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400325}
326
gioe72b54f2024-04-22 10:44:41 +0400327func (a cueApp) render(values map[string]any) (rendered, error) {
328 ret := rendered{
gio44f621b2024-04-29 09:44:38 +0400329 Name: a.Slug(),
gio308105e2024-04-19 13:12:13 +0400330 Resources: make(CueAppData),
giof8843412024-05-22 16:38:05 +0400331 HelmCharts: HelmCharts{
332 Git: make(map[string]HelmChartGitRepo),
333 },
334 ContainerImages: make(map[string]ContainerImage),
335 Ports: make([]PortForward, 0),
336 Data: a.data,
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400337 }
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400338 var buf bytes.Buffer
gio3cdee592024-04-17 10:15:56 +0400339 if err := json.NewEncoder(&buf).Encode(values); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400340 return rendered{}, err
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400341 }
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400342 ctx := a.cfg.Context()
343 d := ctx.CompileBytes(buf.Bytes())
344 res := a.cfg.Unify(d).Eval()
345 if err := res.Err(); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400346 return rendered{}, err
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400347 }
348 if err := res.Validate(); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400349 return rendered{}, err
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400350 }
gio308105e2024-04-19 13:12:13 +0400351 full, err := json.MarshalIndent(res, "", "\t")
352 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400353 return rendered{}, err
gio308105e2024-04-19 13:12:13 +0400354 }
gio94904702024-07-26 16:58:34 +0400355 ret.Raw = full
gio308105e2024-04-19 13:12:13 +0400356 ret.Data["rendered.json"] = full
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400357 readme, err := res.LookupPath(cue.ParsePath("readme")).String()
358 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400359 return rendered{}, err
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400360 }
361 ret.Readme = readme
giof6ad2982024-08-23 17:42:49 +0400362 res.LookupPath(cue.ParsePath("input.cluster.name")).Decode(&ret.Cluster)
363 if err := res.LookupPath(cue.ParsePath("output.clusterProxy")).Decode(&ret.ClusterProxies); err != nil {
364 return rendered{}, err
365 }
366 if err := res.LookupPath(cue.ParsePath("namespaces")).Decode(&ret.Namespaces); err != nil {
367 return rendered{}, err
368 }
gio802311e2024-11-04 08:37:34 +0400369 if err := res.LookupPath(cue.ParsePath("output.openPort")).Decode(&ret.Ports); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400370 return rendered{}, err
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400371 }
gio0eaf2712024-04-14 13:08:46 +0400372 {
gio7fbd4ad2024-08-27 10:06:39 +0400373 charts := res.LookupPath(cue.ParsePath("output.charts"))
giof8843412024-05-22 16:38:05 +0400374 i, err := charts.Fields()
375 if err != nil {
376 return rendered{}, err
377 }
378 for i.Next() {
379 var chartRef helmChartRef
380 if err := i.Value().Decode(&chartRef); err != nil {
381 return rendered{}, err
382 }
383 if chartRef.Kind == "GitRepository" {
384 var chart HelmChartGitRepo
385 if err := i.Value().Decode(&chart); err != nil {
386 return rendered{}, err
387 }
388 ret.HelmCharts.Git[cleanName(i.Selector().String())] = chart
389 }
390 }
391 }
392 {
gio7fbd4ad2024-08-27 10:06:39 +0400393 images := res.LookupPath(cue.ParsePath("output.images"))
giof8843412024-05-22 16:38:05 +0400394 i, err := images.Fields()
395 if err != nil {
396 return rendered{}, err
397 }
398 for i.Next() {
399 var img ContainerImage
400 if err := i.Value().Decode(&img); err != nil {
401 return rendered{}, err
402 }
403 ret.ContainerImages[cleanName(i.Selector().String())] = img
404 }
405 }
406 {
gio7fbd4ad2024-08-27 10:06:39 +0400407 helm := res.LookupPath(cue.ParsePath("output.helm"))
408 i, err := helm.Fields()
gio0eaf2712024-04-14 13:08:46 +0400409 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400410 return rendered{}, err
gio0eaf2712024-04-14 13:08:46 +0400411 }
412 for i.Next() {
413 if contents, err := cueyaml.Encode(i.Value()); err != nil {
414 return rendered{}, err
415 } else {
416 name := fmt.Sprintf("%s.yaml", cleanName(i.Selector().String()))
417 ret.Resources[name] = contents
418 }
419 }
420 }
421 {
422 resources := res.LookupPath(cue.ParsePath("resources"))
423 i, err := resources.Fields()
424 if err != nil {
425 return rendered{}, err
426 }
427 for i.Next() {
428 if contents, err := cueyaml.Encode(i.Value()); err != nil {
429 return rendered{}, err
430 } else {
431 name := fmt.Sprintf("%s.yaml", cleanName(i.Selector().String()))
432 ret.Resources[name] = contents
433 }
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400434 }
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400435 }
Davit Tabidze56f86a42024-04-09 19:15:25 +0400436 helpValue := res.LookupPath(cue.ParsePath("help"))
437 if helpValue.Exists() {
438 if err := helpValue.Decode(&ret.Help); err != nil {
gioe72b54f2024-04-22 10:44:41 +0400439 return rendered{}, err
Davit Tabidze56f86a42024-04-09 19:15:25 +0400440 }
441 }
442 url, err := res.LookupPath(cue.ParsePath("url")).String()
443 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400444 return rendered{}, err
Davit Tabidze56f86a42024-04-09 19:15:25 +0400445 }
gio09a3e5b2024-04-26 14:11:06 +0400446 ret.URL = url
Davit Tabidze56f86a42024-04-09 19:15:25 +0400447 icon, err := res.LookupPath(cue.ParsePath("icon")).String()
448 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400449 return rendered{}, err
Davit Tabidze56f86a42024-04-09 19:15:25 +0400450 }
451 ret.Icon = icon
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400452 return ret, nil
453}
454
gio3cdee592024-04-17 10:15:56 +0400455type cueEnvApp struct {
456 cueApp
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +0400457}
458
gio308105e2024-04-19 13:12:13 +0400459func NewCueEnvApp(data CueAppData) (EnvApp, error) {
460 app, err := ParseAndCreateNewCueApp(data)
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400461 if err != nil {
462 return nil, err
463 }
gio3cdee592024-04-17 10:15:56 +0400464 return cueEnvApp{app}, nil
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400465}
466
gio0eaf2712024-04-14 13:08:46 +0400467func NewDodoApp(appCfg []byte) (EnvApp, error) {
468 return NewCueEnvApp(CueAppData{
giofc9c4ea2024-06-26 13:46:53 +0400469 "app.cue": appCfg,
470 "base.cue": []byte(cueBaseConfig),
471 "dodo.cue": dodoAppCue,
472 "env.cue": []byte(cueEnvAppGlobal),
gio0eaf2712024-04-14 13:08:46 +0400473 })
474}
475
gio3cdee592024-04-17 10:15:56 +0400476func (a cueEnvApp) Type() AppType {
477 return AppTypeEnv
478}
479
giofc441e32024-11-11 16:26:14 +0400480func merge(d map[string]any, v map[string]any) map[string]any {
481 ret := map[string]any{}
482 for k, val := range d {
483 if vv, ok := v[k]; ok && vv != nil {
484 if mv, ok := val.(map[string]any); ok {
485 // TODO(gio): check that it is actually map
486 fmt.Println(vv)
487 ret[k] = merge(mv, vv.(map[string]any))
488 } else {
489 ret[k] = vv
490 }
491 } else {
492 ret[k] = val
493 }
494 }
495 for k, v := range v {
496 if v != nil {
497 if _, ok := d[k]; !ok {
498 d[k] = v
499 }
500 }
501 }
502 return ret
503}
504
giocb34ad22024-07-11 08:01:13 +0400505func (a cueEnvApp) Render(
506 release Release,
507 env EnvConfig,
508 networks []Network,
giof6ad2982024-08-23 17:42:49 +0400509 clusters []Cluster,
giocb34ad22024-07-11 08:01:13 +0400510 values map[string]any,
511 charts map[string]helmv2.HelmChartTemplateSpec,
gio864b4332024-09-05 13:56:47 +0400512 vpnKeyGen VPNAPIClient,
giocb34ad22024-07-11 08:01:13 +0400513) (EnvAppRendered, error) {
giofc441e32024-11-11 16:26:14 +0400514 dv, err := ExtractDefaultValues(a.cueApp.cfg.LookupPath(cue.ParsePath("input")))
515 if err != nil {
516 return EnvAppRendered{}, err
517 }
518 mv := merge(dv.(map[string]any), values)
519 derived, err := deriveValues(mv, values, a.Schema(), networks, clusters, vpnKeyGen)
gio3cdee592024-04-17 10:15:56 +0400520 if err != nil {
gioefa0ed42024-06-13 12:31:43 +0400521 return EnvAppRendered{}, err
gio3cdee592024-04-17 10:15:56 +0400522 }
giof6ad2982024-08-23 17:42:49 +0400523 // return EnvAppRendered{}, fmt.Errorf("asdasd")
giof8843412024-05-22 16:38:05 +0400524 if charts == nil {
525 charts = make(map[string]helmv2.HelmChartTemplateSpec)
526 }
giof15b9da2024-09-19 06:59:16 +0400527 if clusters == nil {
528 clusters = []Cluster{}
529 }
gio3cdee592024-04-17 10:15:56 +0400530 ret, err := a.cueApp.render(map[string]any{
giof8843412024-05-22 16:38:05 +0400531 "global": env,
532 "release": release,
533 "input": derived,
534 "localCharts": charts,
gio5e49bb62024-07-20 10:43:19 +0400535 "networks": NetworkMap(networks),
giof15b9da2024-09-19 06:59:16 +0400536 "clusters": clusters,
gio3cdee592024-04-17 10:15:56 +0400537 })
538 if err != nil {
gioe72b54f2024-04-22 10:44:41 +0400539 return EnvAppRendered{}, err
gio3cdee592024-04-17 10:15:56 +0400540 }
gioe72b54f2024-04-22 10:44:41 +0400541 return EnvAppRendered{
542 rendered: ret,
543 Config: AppInstanceConfig{
gio44f621b2024-04-29 09:44:38 +0400544 AppId: a.Slug(),
gioe72b54f2024-04-22 10:44:41 +0400545 Env: env,
546 Release: release,
547 Values: values,
548 Input: derived,
gio09a3e5b2024-04-26 14:11:06 +0400549 URL: ret.URL,
gioe72b54f2024-04-22 10:44:41 +0400550 Help: ret.Help,
gio09a3e5b2024-04-26 14:11:06 +0400551 Icon: ret.Icon,
gioe72b54f2024-04-22 10:44:41 +0400552 },
553 }, nil
gio3cdee592024-04-17 10:15:56 +0400554}
555
556type cueInfraApp struct {
557 cueApp
558}
559
gio308105e2024-04-19 13:12:13 +0400560func NewCueInfraApp(data CueAppData) (InfraApp, error) {
561 app, err := ParseAndCreateNewCueApp(data)
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400562 if err != nil {
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400563 return nil, err
Giorgi Lekveishvili743fb432023-11-08 17:19:40 +0400564 }
gio3cdee592024-04-17 10:15:56 +0400565 return cueInfraApp{app}, nil
566}
567
568func (a cueInfraApp) Type() AppType {
569 return AppTypeInfra
570}
571
gio7841f4f2024-07-26 19:53:49 +0400572func (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 +0400573 if charts == nil {
574 charts = make(map[string]helmv2.HelmChartTemplateSpec)
575 }
gioe72b54f2024-04-22 10:44:41 +0400576 ret, err := a.cueApp.render(map[string]any{
giof8843412024-05-22 16:38:05 +0400577 "global": infra,
578 "release": release,
579 "input": values,
580 "localCharts": charts,
gio7841f4f2024-07-26 19:53:49 +0400581 "networks": InfraNetworkMap(networks),
gio3cdee592024-04-17 10:15:56 +0400582 })
gioe72b54f2024-04-22 10:44:41 +0400583 if err != nil {
584 return InfraAppRendered{}, err
585 }
586 return InfraAppRendered{
587 rendered: ret,
588 Config: InfraAppInstanceConfig{
gio44f621b2024-04-29 09:44:38 +0400589 AppId: a.Slug(),
gioe72b54f2024-04-22 10:44:41 +0400590 Infra: infra,
591 Release: release,
592 Values: values,
593 Input: values,
gio09a3e5b2024-04-26 14:11:06 +0400594 URL: ret.URL,
595 Help: ret.Help,
gioe72b54f2024-04-22 10:44:41 +0400596 },
597 }, nil
Giorgi Lekveishvilief21c132024-01-17 18:57:58 +0400598}
599
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400600func cleanName(s string) string {
601 return strings.ReplaceAll(strings.ReplaceAll(s, "\"", ""), "'", "")
Giorgi Lekveishvilief21c132024-01-17 18:57:58 +0400602}
gioe72b54f2024-04-22 10:44:41 +0400603
604func join[T fmt.Stringer](items []T, sep string) string {
605 var tmp []string
606 for _, i := range items {
607 tmp = append(tmp, i.String())
608 }
609 return strings.Join(tmp, ",")
610}
gio0eaf2712024-04-14 13:08:46 +0400611
gio5e49bb62024-07-20 10:43:19 +0400612func NetworkMap(networks []Network) map[string]Network {
gio0eaf2712024-04-14 13:08:46 +0400613 ret := make(map[string]Network)
614 for _, n := range networks {
615 ret[strings.ToLower(n.Name)] = n
616 }
617 return ret
618}
gio7841f4f2024-07-26 19:53:49 +0400619
620func InfraNetworkMap(networks []InfraNetwork) map[string]InfraNetwork {
621 ret := make(map[string]InfraNetwork)
622 for _, n := range networks {
623 ret[strings.ToLower(n.Name)] = n
624 }
625 return ret
626}