blob: 7274e03f99836cb23f8112da4877f914eca423b4 [file] [log] [blame]
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04001package installer
2
3import (
gio3af43942024-04-16 08:13:50 +04004 "bytes"
5 "encoding/json"
6 "errors"
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04007 "fmt"
gioefa0ed42024-06-13 12:31:43 +04008 "io"
gio3af43942024-04-16 08:13:50 +04009 "io/fs"
gio3af43942024-04-16 08:13:50 +040010 "net/http"
11 "path"
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040012 "path/filepath"
giof8843412024-05-22 16:38:05 +040013 "strings"
gioe72b54f2024-04-22 10:44:41 +040014
gioefa0ed42024-06-13 12:31:43 +040015 gio "github.com/giolekva/pcloud/core/installer/io"
gioe72b54f2024-04-22 10:44:41 +040016 "github.com/giolekva/pcloud/core/installer/soft"
gio778577f2024-04-29 09:44:38 +040017
giof8843412024-05-22 16:38:05 +040018 helmv2 "github.com/fluxcd/helm-controller/api/v2"
gio778577f2024-04-29 09:44:38 +040019 "sigs.k8s.io/yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040020)
21
gio5e49bb62024-07-20 10:43:19 +040022const (
23 configFileName = "config.yaml"
24 kustomizationFileName = "kustomization.yaml"
25 gitIgnoreFileName = ".gitignore"
26 includeEverything = "!*"
27)
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040028
gio778577f2024-04-29 09:44:38 +040029var ErrorNotFound = errors.New("not found")
30
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040031type AppManager struct {
gioe72b54f2024-04-22 10:44:41 +040032 repoIO soft.RepoIO
giof8843412024-05-22 16:38:05 +040033 nsc NamespaceCreator
34 jc JobCreator
35 hf HelmFetcher
gio308105e2024-04-19 13:12:13 +040036 appDirRoot string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040037}
38
giof8843412024-05-22 16:38:05 +040039func NewAppManager(
40 repoIO soft.RepoIO,
41 nsc NamespaceCreator,
42 jc JobCreator,
43 hf HelmFetcher,
44 appDirRoot string,
45) (*AppManager, error) {
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040046 return &AppManager{
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040047 repoIO,
giof8843412024-05-22 16:38:05 +040048 nsc,
49 jc,
50 hf,
gio308105e2024-04-19 13:12:13 +040051 appDirRoot,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040052 }, nil
53}
54
gioe72b54f2024-04-22 10:44:41 +040055func (m *AppManager) Config() (EnvConfig, error) {
56 var cfg EnvConfig
57 if err := soft.ReadYaml(m.repoIO, configFileName, &cfg); err != nil {
58 return EnvConfig{}, err
gio3af43942024-04-16 08:13:50 +040059 } else {
60 return cfg, nil
61 }
62}
63
gio3cdee592024-04-17 10:15:56 +040064func (m *AppManager) appConfig(path string) (AppInstanceConfig, error) {
65 var cfg AppInstanceConfig
gioe72b54f2024-04-22 10:44:41 +040066 if err := soft.ReadJson(m.repoIO, path, &cfg); err != nil {
gio3cdee592024-04-17 10:15:56 +040067 return AppInstanceConfig{}, err
gio3af43942024-04-16 08:13:50 +040068 } else {
69 return cfg, nil
70 }
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040071}
72
gio308105e2024-04-19 13:12:13 +040073func (m *AppManager) FindAllInstances() ([]AppInstanceConfig, error) {
gio09a3e5b2024-04-26 14:11:06 +040074 m.repoIO.Pull()
gioe72b54f2024-04-22 10:44:41 +040075 kust, err := soft.ReadKustomization(m.repoIO, filepath.Join(m.appDirRoot, "kustomization.yaml"))
gio3af43942024-04-16 08:13:50 +040076 if err != nil {
77 return nil, err
78 }
gio3cdee592024-04-17 10:15:56 +040079 ret := make([]AppInstanceConfig, 0)
gio3af43942024-04-16 08:13:50 +040080 for _, app := range kust.Resources {
gio308105e2024-04-19 13:12:13 +040081 cfg, err := m.appConfig(filepath.Join(m.appDirRoot, app, "config.json"))
82 if err != nil {
83 return nil, err
84 }
85 cfg.Id = app
86 ret = append(ret, cfg)
87 }
88 return ret, nil
89}
90
91func (m *AppManager) FindAllAppInstances(name string) ([]AppInstanceConfig, error) {
gioe72b54f2024-04-22 10:44:41 +040092 kust, err := soft.ReadKustomization(m.repoIO, filepath.Join(m.appDirRoot, "kustomization.yaml"))
gio308105e2024-04-19 13:12:13 +040093 if err != nil {
giocb34ad22024-07-11 08:01:13 +040094 if errors.Is(err, fs.ErrNotExist) {
95 return nil, nil
96 } else {
97 return nil, err
98 }
gio308105e2024-04-19 13:12:13 +040099 }
100 ret := make([]AppInstanceConfig, 0)
101 for _, app := range kust.Resources {
102 cfg, err := m.appConfig(filepath.Join(m.appDirRoot, app, "config.json"))
gio3af43942024-04-16 08:13:50 +0400103 if err != nil {
104 return nil, err
105 }
106 cfg.Id = app
107 if cfg.AppId == name {
108 ret = append(ret, cfg)
109 }
110 }
111 return ret, nil
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400112}
113
gio778577f2024-04-29 09:44:38 +0400114func (m *AppManager) FindInstance(id string) (*AppInstanceConfig, error) {
gioe72b54f2024-04-22 10:44:41 +0400115 kust, err := soft.ReadKustomization(m.repoIO, filepath.Join(m.appDirRoot, "kustomization.yaml"))
gio3af43942024-04-16 08:13:50 +0400116 if err != nil {
gio778577f2024-04-29 09:44:38 +0400117 return nil, err
gio3af43942024-04-16 08:13:50 +0400118 }
119 for _, app := range kust.Resources {
120 if app == id {
gio308105e2024-04-19 13:12:13 +0400121 cfg, err := m.appConfig(filepath.Join(m.appDirRoot, app, "config.json"))
gio3af43942024-04-16 08:13:50 +0400122 if err != nil {
gio778577f2024-04-29 09:44:38 +0400123 return nil, err
gio3af43942024-04-16 08:13:50 +0400124 }
125 cfg.Id = id
gio778577f2024-04-29 09:44:38 +0400126 return &cfg, nil
gio3af43942024-04-16 08:13:50 +0400127 }
128 }
gio778577f2024-04-29 09:44:38 +0400129 return nil, ErrorNotFound
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400130}
131
giof8843412024-05-22 16:38:05 +0400132func GetCueAppData(fs soft.RepoFS, dir string) (CueAppData, error) {
133 files, err := fs.ListDir(dir)
134 if err != nil {
135 return nil, err
136 }
137 cfg := CueAppData{}
138 for _, f := range files {
139 if !f.IsDir() && strings.HasSuffix(f.Name(), ".cue") {
140 contents, err := soft.ReadFile(fs, filepath.Join(dir, f.Name()))
141 if err != nil {
142 return nil, err
143 }
144 cfg[f.Name()] = contents
145 }
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +0400146 }
gio308105e2024-04-19 13:12:13 +0400147 return cfg, nil
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +0400148}
149
giof8843412024-05-22 16:38:05 +0400150func (m *AppManager) GetInstanceApp(id string) (EnvApp, error) {
151 cfg, err := GetCueAppData(m.repoIO, filepath.Join(m.appDirRoot, id))
152 if err != nil {
153 return nil, err
154 }
155 return NewCueEnvApp(cfg)
156}
157
gio3af43942024-04-16 08:13:50 +0400158type allocatePortReq struct {
159 Protocol string `json:"protocol"`
160 SourcePort int `json:"sourcePort"`
161 TargetService string `json:"targetService"`
162 TargetPort int `json:"targetPort"`
giocdfa3722024-06-13 20:10:14 +0400163 Secret string `json:"secret,omitempty"`
164}
165
166type removePortReq struct {
167 Protocol string `json:"protocol"`
168 SourcePort int `json:"sourcePort"`
169 TargetService string `json:"targetService"`
170 TargetPort int `json:"targetPort"`
gio3af43942024-04-16 08:13:50 +0400171}
172
gioefa0ed42024-06-13 12:31:43 +0400173type reservePortResp struct {
174 Port int `json:"port"`
175 Secret string `json:"secret"`
176}
177
178func reservePorts(ports map[string]string) (map[string]reservePortResp, error) {
179 ret := map[string]reservePortResp{}
180 for p, reserveAddr := range ports {
181 resp, err := http.Post(reserveAddr, "application/json", nil) // TODO(gio): address
182 if err != nil {
183 return nil, err
184 }
185 if resp.StatusCode != http.StatusOK {
186 var e bytes.Buffer
187 io.Copy(&e, resp.Body)
188 return nil, fmt.Errorf("Could not reserve port: %s", e.String())
189 }
190 var r reservePortResp
191 if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
192 return nil, err
193 }
194 ret[p] = r
195 }
196 return ret, nil
197}
198
199func openPorts(ports []PortForward, reservations map[string]reservePortResp, allocators map[string]string) error {
gio3af43942024-04-16 08:13:50 +0400200 for _, p := range ports {
201 var buf bytes.Buffer
202 req := allocatePortReq{
203 Protocol: p.Protocol,
204 SourcePort: p.SourcePort,
205 TargetService: p.TargetService,
206 TargetPort: p.TargetPort,
207 }
gioefa0ed42024-06-13 12:31:43 +0400208 allocator := ""
209 for n, r := range reservations {
210 if p.SourcePort == r.Port {
211 allocator = allocators[n]
giobd7ab0b2024-06-17 12:55:17 +0400212 req.Secret = r.Secret
gioefa0ed42024-06-13 12:31:43 +0400213 break
214 }
215 }
216 if allocator == "" {
217 return fmt.Errorf("Could not find allocator for: %d", p.SourcePort)
218 }
giobd7ab0b2024-06-17 12:55:17 +0400219 if err := json.NewEncoder(&buf).Encode(req); err != nil {
220 return err
221 }
gioefa0ed42024-06-13 12:31:43 +0400222 resp, err := http.Post(allocator, "application/json", &buf)
gio3af43942024-04-16 08:13:50 +0400223 if err != nil {
224 return err
225 }
226 if resp.StatusCode != http.StatusOK {
giocdfa3722024-06-13 20:10:14 +0400227 var r bytes.Buffer
228 io.Copy(&r, resp.Body)
229 return fmt.Errorf("Could not allocate port %d, status code %d, message: %s", p.SourcePort, resp.StatusCode, r.String())
gio3af43942024-04-16 08:13:50 +0400230 }
231 }
232 return nil
233}
234
giocdfa3722024-06-13 20:10:14 +0400235func closePorts(ports []PortForward) error {
236 var retErr error
237 for _, p := range ports {
238 var buf bytes.Buffer
239 req := removePortReq{
240 Protocol: p.Protocol,
241 SourcePort: p.SourcePort,
242 TargetService: p.TargetService,
243 TargetPort: p.TargetPort,
244 }
245 if err := json.NewEncoder(&buf).Encode(req); err != nil {
246 retErr = err
247 continue
248 }
249 resp, err := http.Post(p.RemoveAddr, "application/json", &buf)
250 if err != nil {
251 retErr = err
252 continue
253 }
254 if resp.StatusCode != http.StatusOK {
255 retErr = fmt.Errorf("Could not deallocate port %d, status code: %d", p.SourcePort, resp.StatusCode)
256 continue
257 }
258 }
259 return retErr
260}
261
gioe72b54f2024-04-22 10:44:41 +0400262func createKustomizationChain(r soft.RepoFS, path string) error {
gio3af43942024-04-16 08:13:50 +0400263 for p := filepath.Clean(path); p != "/"; {
264 parent, child := filepath.Split(p)
265 kustPath := filepath.Join(parent, "kustomization.yaml")
gioe72b54f2024-04-22 10:44:41 +0400266 kust, err := soft.ReadKustomization(r, kustPath)
gio3af43942024-04-16 08:13:50 +0400267 if err != nil {
268 if errors.Is(err, fs.ErrNotExist) {
gioefa0ed42024-06-13 12:31:43 +0400269 k := gio.NewKustomization()
gio3af43942024-04-16 08:13:50 +0400270 kust = &k
271 } else {
272 return err
273 }
274 }
275 kust.AddResources(child)
gioe72b54f2024-04-22 10:44:41 +0400276 if err := soft.WriteYaml(r, kustPath, kust); err != nil {
gio3af43942024-04-16 08:13:50 +0400277 return err
278 }
279 p = filepath.Clean(parent)
280 }
281 return nil
282}
283
gio778577f2024-04-29 09:44:38 +0400284type Resource struct {
285 Name string `json:"name"`
286 Namespace string `json:"namespace"`
giof9f0bee2024-06-11 20:10:05 +0400287 Info string `json:"info"`
gio778577f2024-04-29 09:44:38 +0400288}
289
290type ReleaseResources struct {
gio94904702024-07-26 16:58:34 +0400291 Release Release
292 Helm []Resource
293 RenderedRaw []byte
gio778577f2024-04-29 09:44:38 +0400294}
295
gio3cdee592024-04-17 10:15:56 +0400296// TODO(gio): rename to CommitApp
gio0eaf2712024-04-14 13:08:46 +0400297func installApp(
gioe72b54f2024-04-22 10:44:41 +0400298 repo soft.RepoIO,
299 appDir string,
300 name string,
301 config any,
gioe72b54f2024-04-22 10:44:41 +0400302 resources CueAppData,
303 data CueAppData,
giof8843412024-05-22 16:38:05 +0400304 opts ...InstallOption,
gio94904702024-07-26 16:58:34 +0400305) error {
giof8843412024-05-22 16:38:05 +0400306 var o installOptions
307 for _, i := range opts {
308 i(&o)
309 }
310 dopts := []soft.DoOption{}
311 if o.Branch != "" {
giof8843412024-05-22 16:38:05 +0400312 dopts = append(dopts, soft.WithCommitToBranch(o.Branch))
313 }
gio94904702024-07-26 16:58:34 +0400314 if o.NoPull {
315 dopts = append(dopts, soft.WithNoPull())
316 }
giof8843412024-05-22 16:38:05 +0400317 if o.NoPublish {
318 dopts = append(dopts, soft.WithNoCommit())
319 }
giof71a0832024-06-27 14:45:45 +0400320 if o.Force {
321 dopts = append(dopts, soft.WithForce())
322 }
gio9d66f322024-07-06 13:45:10 +0400323 if o.NoLock {
324 dopts = append(dopts, soft.WithNoLock())
325 }
gio94904702024-07-26 16:58:34 +0400326 return repo.Do(func(r soft.RepoFS) (string, error) {
gio308105e2024-04-19 13:12:13 +0400327 if err := r.RemoveDir(appDir); err != nil {
328 return "", err
329 }
330 resourcesDir := path.Join(appDir, "resources")
331 if err := r.CreateDir(resourcesDir); err != nil {
gio3af43942024-04-16 08:13:50 +0400332 return "", err
333 }
gio94904702024-07-26 16:58:34 +0400334 if err := func() error {
gio5e49bb62024-07-20 10:43:19 +0400335 if err := soft.WriteFile(r, path.Join(appDir, gitIgnoreFileName), includeEverything); err != nil {
gio94904702024-07-26 16:58:34 +0400336 return err
gio5e49bb62024-07-20 10:43:19 +0400337 }
gioe72b54f2024-04-22 10:44:41 +0400338 if err := soft.WriteYaml(r, path.Join(appDir, configFileName), config); err != nil {
gio94904702024-07-26 16:58:34 +0400339 return err
gio3af43942024-04-16 08:13:50 +0400340 }
gioe72b54f2024-04-22 10:44:41 +0400341 if err := soft.WriteJson(r, path.Join(appDir, "config.json"), config); err != nil {
gio94904702024-07-26 16:58:34 +0400342 return err
gio308105e2024-04-19 13:12:13 +0400343 }
gioe72b54f2024-04-22 10:44:41 +0400344 for name, contents := range data {
gio308105e2024-04-19 13:12:13 +0400345 if name == "config.json" || name == "kustomization.yaml" || name == "resources" {
gio94904702024-07-26 16:58:34 +0400346 return fmt.Errorf("%s is forbidden", name)
gio308105e2024-04-19 13:12:13 +0400347 }
348 w, err := r.Writer(path.Join(appDir, name))
gio3af43942024-04-16 08:13:50 +0400349 if err != nil {
gio94904702024-07-26 16:58:34 +0400350 return err
gio3af43942024-04-16 08:13:50 +0400351 }
gio308105e2024-04-19 13:12:13 +0400352 defer w.Close()
353 if _, err := w.Write(contents); err != nil {
gio94904702024-07-26 16:58:34 +0400354 return err
gio3af43942024-04-16 08:13:50 +0400355 }
356 }
gio94904702024-07-26 16:58:34 +0400357 return nil
358 }(); err != nil {
359 return "", err
gio308105e2024-04-19 13:12:13 +0400360 }
gio94904702024-07-26 16:58:34 +0400361 if err := func() error {
gio308105e2024-04-19 13:12:13 +0400362 if err := createKustomizationChain(r, resourcesDir); err != nil {
gio94904702024-07-26 16:58:34 +0400363 return err
gio308105e2024-04-19 13:12:13 +0400364 }
gioefa0ed42024-06-13 12:31:43 +0400365 appKust := gio.NewKustomization()
gioe72b54f2024-04-22 10:44:41 +0400366 for name, contents := range resources {
gio308105e2024-04-19 13:12:13 +0400367 appKust.AddResources(name)
368 w, err := r.Writer(path.Join(resourcesDir, name))
369 if err != nil {
gio94904702024-07-26 16:58:34 +0400370 return err
gio308105e2024-04-19 13:12:13 +0400371 }
372 defer w.Close()
373 if _, err := w.Write(contents); err != nil {
gio94904702024-07-26 16:58:34 +0400374 return err
gio308105e2024-04-19 13:12:13 +0400375 }
376 }
gioe72b54f2024-04-22 10:44:41 +0400377 if err := soft.WriteYaml(r, path.Join(resourcesDir, "kustomization.yaml"), appKust); err != nil {
gio94904702024-07-26 16:58:34 +0400378 return err
gio3af43942024-04-16 08:13:50 +0400379 }
gio94904702024-07-26 16:58:34 +0400380 return nil
381 }(); err != nil {
382 return "", err
gio3af43942024-04-16 08:13:50 +0400383 }
gioe72b54f2024-04-22 10:44:41 +0400384 return fmt.Sprintf("install: %s", name), nil
giof8843412024-05-22 16:38:05 +0400385 }, dopts...)
gio3af43942024-04-16 08:13:50 +0400386}
387
gio3cdee592024-04-17 10:15:56 +0400388// TODO(gio): commit instanceId -> appDir mapping as well
giof8843412024-05-22 16:38:05 +0400389func (m *AppManager) Install(
390 app EnvApp,
391 instanceId string,
392 appDir string,
393 namespace string,
394 values map[string]any,
395 opts ...InstallOption,
396) (ReleaseResources, error) {
gioefa0ed42024-06-13 12:31:43 +0400397 portFields := findPortFields(app.Schema())
398 fakeReservations := map[string]reservePortResp{}
399 for i, f := range portFields {
400 fakeReservations[f] = reservePortResp{Port: i}
401 }
402 if err := setPortFields(values, fakeReservations); err != nil {
403 return ReleaseResources{}, err
404 }
gio0eaf2712024-04-14 13:08:46 +0400405 o := &installOptions{}
406 for _, i := range opts {
407 i(o)
408 }
gio3af43942024-04-16 08:13:50 +0400409 appDir = filepath.Clean(appDir)
gio94904702024-07-26 16:58:34 +0400410 if !o.NoPull {
411 if err := m.repoIO.Pull(); err != nil {
412 return ReleaseResources{}, err
413 }
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400414 }
gio94904702024-07-26 16:58:34 +0400415 opts = append(opts, WithNoPull())
giof8843412024-05-22 16:38:05 +0400416 if err := m.nsc.Create(namespace); err != nil {
gio778577f2024-04-29 09:44:38 +0400417 return ReleaseResources{}, err
gio3cdee592024-04-17 10:15:56 +0400418 }
gio0eaf2712024-04-14 13:08:46 +0400419 var env EnvConfig
420 if o.Env != nil {
421 env = *o.Env
422 } else {
423 var err error
424 env, err = m.Config()
425 if err != nil {
426 return ReleaseResources{}, err
427 }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +0400428 }
giocb34ad22024-07-11 08:01:13 +0400429 var networks []Network
430 if o.Networks != nil {
431 networks = o.Networks
432 } else {
433 var err error
434 networks, err = m.CreateNetworks(env)
435 if err != nil {
436 return ReleaseResources{}, err
437 }
438 }
giof8843412024-05-22 16:38:05 +0400439 var lg LocalChartGenerator
440 if o.LG != nil {
441 lg = o.LG
442 } else {
443 lg = GitRepositoryLocalChartGenerator{env.Id, env.Id}
444 }
gio3cdee592024-04-17 10:15:56 +0400445 release := Release{
446 AppInstanceId: instanceId,
447 Namespace: namespace,
448 RepoAddr: m.repoIO.FullAddress(),
449 AppDir: appDir,
450 }
giocb34ad22024-07-11 08:01:13 +0400451 rendered, err := app.Render(release, env, networks, values, nil)
gioef01fbb2024-04-12 16:52:59 +0400452 if err != nil {
gio778577f2024-04-29 09:44:38 +0400453 return ReleaseResources{}, err
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400454 }
gioefa0ed42024-06-13 12:31:43 +0400455 reservators := map[string]string{}
456 allocators := map[string]string{}
457 for _, pf := range rendered.Ports {
458 reservators[portFields[pf.SourcePort]] = pf.ReserveAddr
459 allocators[portFields[pf.SourcePort]] = pf.Allocator
460 }
461 portReservations, err := reservePorts(reservators)
462 if err != nil {
463 return ReleaseResources{}, err
464 }
465 if err := setPortFields(values, portReservations); err != nil {
466 return ReleaseResources{}, err
467 }
giof8843412024-05-22 16:38:05 +0400468 imageRegistry := fmt.Sprintf("zot.%s", env.PrivateDomain)
469 if o.FetchContainerImages {
470 if err := pullContainerImages(instanceId, rendered.ContainerImages, imageRegistry, namespace, m.jc); err != nil {
471 return ReleaseResources{}, err
472 }
gio0eaf2712024-04-14 13:08:46 +0400473 }
giof71a0832024-06-27 14:45:45 +0400474 charts, err := pullHelmCharts(m.hf, rendered.HelmCharts, m.repoIO, "/helm-charts")
475 if err != nil {
giof8843412024-05-22 16:38:05 +0400476 return ReleaseResources{}, err
477 }
giof71a0832024-06-27 14:45:45 +0400478 localCharts := generateLocalCharts(lg, charts)
giof8843412024-05-22 16:38:05 +0400479 if o.FetchContainerImages {
480 release.ImageRegistry = imageRegistry
481 }
giocb34ad22024-07-11 08:01:13 +0400482 rendered, err = app.Render(release, env, networks, values, localCharts)
giof8843412024-05-22 16:38:05 +0400483 if err != nil {
484 return ReleaseResources{}, err
485 }
gio94904702024-07-26 16:58:34 +0400486 if err := installApp(m.repoIO, appDir, rendered.Name, rendered.Config, rendered.Resources, rendered.Data, opts...); err != nil {
gio778577f2024-04-29 09:44:38 +0400487 return ReleaseResources{}, err
488 }
gioff2a29a2024-05-01 17:06:42 +0400489 // TODO(gio): add ingress-nginx to release resources
gioefa0ed42024-06-13 12:31:43 +0400490 if err := openPorts(rendered.Ports, portReservations, allocators); err != nil {
gioff2a29a2024-05-01 17:06:42 +0400491 return ReleaseResources{}, err
492 }
gio778577f2024-04-29 09:44:38 +0400493 return ReleaseResources{
gio94904702024-07-26 16:58:34 +0400494 Release: rendered.Config.Release,
495 RenderedRaw: rendered.Raw,
496 Helm: extractHelm(rendered.Resources),
gio778577f2024-04-29 09:44:38 +0400497 }, nil
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400498}
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400499
gio778577f2024-04-29 09:44:38 +0400500type helmRelease struct {
giof9f0bee2024-06-11 20:10:05 +0400501 Metadata struct {
502 Name string `json:"name"`
503 Namespace string `json:"namespace"`
504 Annotations map[string]string `json:"annotations"`
505 } `json:"metadata"`
506 Kind string `json:"kind"`
507 Status struct {
gio778577f2024-04-29 09:44:38 +0400508 Conditions []struct {
509 Type string `json:"type"`
510 Status string `json:"status"`
511 } `json:"conditions"`
512 } `json:"status,omitempty"`
513}
514
515func extractHelm(resources CueAppData) []Resource {
516 ret := make([]Resource, 0, len(resources))
517 for _, contents := range resources {
518 var h helmRelease
519 if err := yaml.Unmarshal(contents, &h); err != nil {
520 panic(err) // TODO(gio): handle
521 }
gio0eaf2712024-04-14 13:08:46 +0400522 if h.Kind == "HelmRelease" {
giof9f0bee2024-06-11 20:10:05 +0400523 res := Resource{
524 Name: h.Metadata.Name,
525 Namespace: h.Metadata.Namespace,
526 Info: fmt.Sprintf("%s/%s", h.Metadata.Namespace, h.Metadata.Name),
527 }
528 if h.Metadata.Annotations != nil {
529 info, ok := h.Metadata.Annotations["dodo.cloud/installer-info"]
530 if ok && len(info) != 0 {
531 res.Info = info
532 }
533 }
534 ret = append(ret, res)
gio0eaf2712024-04-14 13:08:46 +0400535 }
gio778577f2024-04-29 09:44:38 +0400536 }
537 return ret
538}
539
giof8843412024-05-22 16:38:05 +0400540// TODO(gio): take app configuration from the repo
541func (m *AppManager) Update(
542 instanceId string,
543 values map[string]any,
544 opts ...InstallOption,
545) (ReleaseResources, error) {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400546 if err := m.repoIO.Pull(); err != nil {
gio778577f2024-04-29 09:44:38 +0400547 return ReleaseResources{}, err
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400548 }
gio3cdee592024-04-17 10:15:56 +0400549 env, err := m.Config()
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400550 if err != nil {
gio778577f2024-04-29 09:44:38 +0400551 return ReleaseResources{}, err
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400552 }
gio308105e2024-04-19 13:12:13 +0400553 instanceDir := filepath.Join(m.appDirRoot, instanceId)
giof8843412024-05-22 16:38:05 +0400554 app, err := m.GetInstanceApp(instanceId)
555 if err != nil {
556 return ReleaseResources{}, err
557 }
gio308105e2024-04-19 13:12:13 +0400558 instanceConfigPath := filepath.Join(instanceDir, "config.json")
gio3cdee592024-04-17 10:15:56 +0400559 config, err := m.appConfig(instanceConfigPath)
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400560 if err != nil {
gio778577f2024-04-29 09:44:38 +0400561 return ReleaseResources{}, err
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400562 }
giocdfa3722024-06-13 20:10:14 +0400563 renderedCfg, err := readRendered(m.repoIO, filepath.Join(instanceDir, "rendered.json"))
giof8843412024-05-22 16:38:05 +0400564 if err != nil {
565 return ReleaseResources{}, err
gio3cdee592024-04-17 10:15:56 +0400566 }
giocb34ad22024-07-11 08:01:13 +0400567 networks, err := m.CreateNetworks(env)
568 if err != nil {
569 return ReleaseResources{}, err
570 }
571 rendered, err := app.Render(config.Release, env, networks, values, renderedCfg.LocalCharts)
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400572 if err != nil {
gio778577f2024-04-29 09:44:38 +0400573 return ReleaseResources{}, err
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400574 }
gio94904702024-07-26 16:58:34 +0400575 if err := installApp(m.repoIO, instanceDir, rendered.Name, rendered.Config, rendered.Resources, rendered.Data, opts...); err != nil {
576 return ReleaseResources{}, err
577 }
578 return ReleaseResources{
579 Release: rendered.Config.Release,
580 RenderedRaw: rendered.Raw,
581 Helm: extractHelm(rendered.Resources),
582 }, nil
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400583}
584
585func (m *AppManager) Remove(instanceId string) error {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400586 if err := m.repoIO.Pull(); err != nil {
587 return err
588 }
giocdfa3722024-06-13 20:10:14 +0400589 var portForward []PortForward
590 if err := m.repoIO.Do(func(r soft.RepoFS) (string, error) {
591 instanceDir := filepath.Join(m.appDirRoot, instanceId)
592 renderedCfg, err := readRendered(m.repoIO, filepath.Join(instanceDir, "rendered.json"))
593 if err != nil {
594 return "", err
595 }
596 portForward = renderedCfg.PortForward
597 r.RemoveDir(instanceDir)
gio308105e2024-04-19 13:12:13 +0400598 kustPath := filepath.Join(m.appDirRoot, "kustomization.yaml")
gioe72b54f2024-04-22 10:44:41 +0400599 kust, err := soft.ReadKustomization(r, kustPath)
gio3af43942024-04-16 08:13:50 +0400600 if err != nil {
601 return "", err
602 }
603 kust.RemoveResources(instanceId)
gioe72b54f2024-04-22 10:44:41 +0400604 soft.WriteYaml(r, kustPath, kust)
gio3af43942024-04-16 08:13:50 +0400605 return fmt.Sprintf("uninstall: %s", instanceId), nil
giocdfa3722024-06-13 20:10:14 +0400606 }); err != nil {
607 return err
608 }
609 if err := closePorts(portForward); err != nil {
giocdfa3722024-06-13 20:10:14 +0400610 return err
611 }
612 return nil
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400613}
614
Giorgi Lekveishvili67383962024-03-22 19:27:34 +0400615// TODO(gio): deduplicate with cue definition in app.go, this one should be removed.
giocb34ad22024-07-11 08:01:13 +0400616func (m *AppManager) CreateNetworks(env EnvConfig) ([]Network, error) {
617 ret := []Network{
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400618 {
giocdfa3722024-06-13 20:10:14 +0400619 Name: "Public",
620 IngressClass: fmt.Sprintf("%s-ingress-public", env.InfraName),
621 CertificateIssuer: fmt.Sprintf("%s-public", env.Id),
622 Domain: env.Domain,
623 AllocatePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-public.svc.cluster.local/api/allocate", env.InfraName),
624 ReservePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-public.svc.cluster.local/api/reserve", env.InfraName),
625 DeallocatePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-public.svc.cluster.local/api/remove", env.InfraName),
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400626 },
627 {
giocdfa3722024-06-13 20:10:14 +0400628 Name: "Private",
629 IngressClass: fmt.Sprintf("%s-ingress-private", env.Id),
630 Domain: env.PrivateDomain,
631 AllocatePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-private.svc.cluster.local/api/allocate", env.Id),
632 ReservePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-private.svc.cluster.local/api/reserve", env.Id),
633 DeallocatePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-private.svc.cluster.local/api/remove", env.Id),
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400634 },
635 }
giocb34ad22024-07-11 08:01:13 +0400636 n, err := m.FindAllAppInstances("network")
637 if err != nil {
638 return nil, err
639 }
640 for _, a := range n {
641 ret = append(ret, Network{
642 Name: a.Input["name"].(string),
643 IngressClass: fmt.Sprintf("%s-ingress-public", env.InfraName),
644 CertificateIssuer: fmt.Sprintf("%s-public", env.Id),
645 Domain: a.Input["domain"].(string),
646 AllocatePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-public.svc.cluster.local/api/allocate", env.InfraName),
647 ReservePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-public.svc.cluster.local/api/reserve", env.InfraName),
648 DeallocatePortAddr: fmt.Sprintf("http://port-allocator.%s-ingress-public.svc.cluster.local/api/remove", env.InfraName),
649 })
650 }
651 return ret, nil
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400652}
gio3cdee592024-04-17 10:15:56 +0400653
gio0eaf2712024-04-14 13:08:46 +0400654type installOptions struct {
gio94904702024-07-26 16:58:34 +0400655 NoPull bool
giof8843412024-05-22 16:38:05 +0400656 NoPublish bool
657 Env *EnvConfig
giocb34ad22024-07-11 08:01:13 +0400658 Networks []Network
giof8843412024-05-22 16:38:05 +0400659 Branch string
660 LG LocalChartGenerator
661 FetchContainerImages bool
giof71a0832024-06-27 14:45:45 +0400662 Force bool
gio9d66f322024-07-06 13:45:10 +0400663 NoLock bool
gio0eaf2712024-04-14 13:08:46 +0400664}
665
666type InstallOption func(*installOptions)
667
668func WithConfig(env *EnvConfig) InstallOption {
669 return func(o *installOptions) {
670 o.Env = env
671 }
672}
673
giocb34ad22024-07-11 08:01:13 +0400674func WithNetworks(networks []Network) InstallOption {
675 return func(o *installOptions) {
676 o.Networks = networks
677 }
678}
679
gio23bdc1b2024-07-11 16:07:47 +0400680func WithNoNetworks() InstallOption {
681 return WithNetworks([]Network{})
682}
683
gio0eaf2712024-04-14 13:08:46 +0400684func WithBranch(branch string) InstallOption {
685 return func(o *installOptions) {
686 o.Branch = branch
687 }
688}
689
giof71a0832024-06-27 14:45:45 +0400690func WithForce() InstallOption {
691 return func(o *installOptions) {
692 o.Force = true
693 }
694}
695
giof8843412024-05-22 16:38:05 +0400696func WithLocalChartGenerator(lg LocalChartGenerator) InstallOption {
697 return func(o *installOptions) {
698 o.LG = lg
699 }
700}
701
702func WithFetchContainerImages() InstallOption {
703 return func(o *installOptions) {
704 o.FetchContainerImages = true
705 }
706}
707
708func WithNoPublish() InstallOption {
709 return func(o *installOptions) {
710 o.NoPublish = true
711 }
712}
713
gio94904702024-07-26 16:58:34 +0400714func WithNoPull() InstallOption {
715 return func(o *installOptions) {
716 o.NoPull = true
717 }
718}
719
gio9d66f322024-07-06 13:45:10 +0400720func WithNoLock() InstallOption {
721 return func(o *installOptions) {
722 o.NoLock = true
723 }
724}
725
giof8843412024-05-22 16:38:05 +0400726// InfraAppmanager
727
728type InfraAppManager struct {
729 repoIO soft.RepoIO
730 nsc NamespaceCreator
731 hf HelmFetcher
732 lg LocalChartGenerator
733}
734
735func NewInfraAppManager(
736 repoIO soft.RepoIO,
737 nsc NamespaceCreator,
738 hf HelmFetcher,
739 lg LocalChartGenerator,
740) (*InfraAppManager, error) {
gio3cdee592024-04-17 10:15:56 +0400741 return &InfraAppManager{
742 repoIO,
giof8843412024-05-22 16:38:05 +0400743 nsc,
744 hf,
745 lg,
gio3cdee592024-04-17 10:15:56 +0400746 }, nil
747}
748
749func (m *InfraAppManager) Config() (InfraConfig, error) {
750 var cfg InfraConfig
gioe72b54f2024-04-22 10:44:41 +0400751 if err := soft.ReadYaml(m.repoIO, configFileName, &cfg); err != nil {
gio3cdee592024-04-17 10:15:56 +0400752 return InfraConfig{}, err
753 } else {
754 return cfg, nil
755 }
756}
757
gioe72b54f2024-04-22 10:44:41 +0400758func (m *InfraAppManager) appConfig(path string) (InfraAppInstanceConfig, error) {
759 var cfg InfraAppInstanceConfig
760 if err := soft.ReadJson(m.repoIO, path, &cfg); err != nil {
761 return InfraAppInstanceConfig{}, err
762 } else {
763 return cfg, nil
764 }
765}
766
767func (m *InfraAppManager) FindInstance(id string) (InfraAppInstanceConfig, error) {
768 kust, err := soft.ReadKustomization(m.repoIO, filepath.Join("/infrastructure", "kustomization.yaml"))
769 if err != nil {
770 return InfraAppInstanceConfig{}, err
771 }
772 for _, app := range kust.Resources {
773 if app == id {
774 cfg, err := m.appConfig(filepath.Join("/infrastructure", app, "config.json"))
775 if err != nil {
776 return InfraAppInstanceConfig{}, err
777 }
778 cfg.Id = id
779 return cfg, nil
780 }
781 }
782 return InfraAppInstanceConfig{}, nil
783}
784
gio778577f2024-04-29 09:44:38 +0400785func (m *InfraAppManager) Install(app InfraApp, appDir string, namespace string, values map[string]any) (ReleaseResources, error) {
gio3cdee592024-04-17 10:15:56 +0400786 appDir = filepath.Clean(appDir)
787 if err := m.repoIO.Pull(); err != nil {
gio778577f2024-04-29 09:44:38 +0400788 return ReleaseResources{}, err
gio3cdee592024-04-17 10:15:56 +0400789 }
giof8843412024-05-22 16:38:05 +0400790 if err := m.nsc.Create(namespace); err != nil {
gio778577f2024-04-29 09:44:38 +0400791 return ReleaseResources{}, err
gio3cdee592024-04-17 10:15:56 +0400792 }
793 infra, err := m.Config()
794 if err != nil {
gio778577f2024-04-29 09:44:38 +0400795 return ReleaseResources{}, err
gio3cdee592024-04-17 10:15:56 +0400796 }
797 release := Release{
798 Namespace: namespace,
799 RepoAddr: m.repoIO.FullAddress(),
800 AppDir: appDir,
801 }
giof8843412024-05-22 16:38:05 +0400802 rendered, err := app.Render(release, infra, values, nil)
803 if err != nil {
804 return ReleaseResources{}, err
805 }
giof71a0832024-06-27 14:45:45 +0400806 charts, err := pullHelmCharts(m.hf, rendered.HelmCharts, m.repoIO, "/helm-charts")
807 if err != nil {
giof8843412024-05-22 16:38:05 +0400808 return ReleaseResources{}, err
809 }
giof71a0832024-06-27 14:45:45 +0400810 localCharts := generateLocalCharts(m.lg, charts)
giof8843412024-05-22 16:38:05 +0400811 rendered, err = app.Render(release, infra, values, localCharts)
gio3cdee592024-04-17 10:15:56 +0400812 if err != nil {
gio778577f2024-04-29 09:44:38 +0400813 return ReleaseResources{}, err
gio3cdee592024-04-17 10:15:56 +0400814 }
gio94904702024-07-26 16:58:34 +0400815 if err := installApp(m.repoIO, appDir, rendered.Name, rendered.Config, rendered.Resources, rendered.Data); err != nil {
816 return ReleaseResources{}, err
817 }
818 return ReleaseResources{
819 Release: rendered.Config.Release,
820 RenderedRaw: rendered.Raw,
821 Helm: extractHelm(rendered.Resources),
822 }, nil
gioe72b54f2024-04-22 10:44:41 +0400823}
824
giof8843412024-05-22 16:38:05 +0400825// TODO(gio): take app configuration from the repo
826func (m *InfraAppManager) Update(
827 instanceId string,
828 values map[string]any,
829 opts ...InstallOption,
830) (ReleaseResources, error) {
gioe72b54f2024-04-22 10:44:41 +0400831 if err := m.repoIO.Pull(); err != nil {
gio778577f2024-04-29 09:44:38 +0400832 return ReleaseResources{}, err
gioe72b54f2024-04-22 10:44:41 +0400833 }
834 env, err := m.Config()
835 if err != nil {
gio778577f2024-04-29 09:44:38 +0400836 return ReleaseResources{}, err
gioe72b54f2024-04-22 10:44:41 +0400837 }
838 instanceDir := filepath.Join("/infrastructure", instanceId)
giof8843412024-05-22 16:38:05 +0400839 appCfg, err := GetCueAppData(m.repoIO, instanceDir)
840 if err != nil {
841 return ReleaseResources{}, err
842 }
843 app, err := NewCueInfraApp(appCfg)
844 if err != nil {
845 return ReleaseResources{}, err
846 }
gioe72b54f2024-04-22 10:44:41 +0400847 instanceConfigPath := filepath.Join(instanceDir, "config.json")
848 config, err := m.appConfig(instanceConfigPath)
849 if err != nil {
gio778577f2024-04-29 09:44:38 +0400850 return ReleaseResources{}, err
gioe72b54f2024-04-22 10:44:41 +0400851 }
giocdfa3722024-06-13 20:10:14 +0400852 renderedCfg, err := readRendered(m.repoIO, filepath.Join(instanceDir, "rendered.json"))
giof8843412024-05-22 16:38:05 +0400853 if err != nil {
854 return ReleaseResources{}, err
gioe72b54f2024-04-22 10:44:41 +0400855 }
giocdfa3722024-06-13 20:10:14 +0400856 rendered, err := app.Render(config.Release, env, values, renderedCfg.LocalCharts)
gioe72b54f2024-04-22 10:44:41 +0400857 if err != nil {
gio778577f2024-04-29 09:44:38 +0400858 return ReleaseResources{}, err
gioe72b54f2024-04-22 10:44:41 +0400859 }
gio94904702024-07-26 16:58:34 +0400860 if err := installApp(m.repoIO, instanceDir, rendered.Name, rendered.Config, rendered.Resources, rendered.Data, opts...); err != nil {
861 return ReleaseResources{}, err
862 }
863 return ReleaseResources{
864 Release: rendered.Config.Release,
865 RenderedRaw: rendered.Raw,
866 Helm: extractHelm(rendered.Resources),
867 }, nil
gio3cdee592024-04-17 10:15:56 +0400868}
giof8843412024-05-22 16:38:05 +0400869
870func pullHelmCharts(hf HelmFetcher, charts HelmCharts, rfs soft.RepoFS, root string) (map[string]string, error) {
871 ret := make(map[string]string)
872 for name, chart := range charts.Git {
873 chartRoot := filepath.Join(root, name)
874 ret[name] = chartRoot
875 if err := hf.Pull(chart, rfs, chartRoot); err != nil {
876 return nil, err
877 }
878 }
879 return ret, nil
880}
881
882func generateLocalCharts(g LocalChartGenerator, charts map[string]string) map[string]helmv2.HelmChartTemplateSpec {
883 ret := make(map[string]helmv2.HelmChartTemplateSpec)
884 for name, path := range charts {
885 ret[name] = g.Generate(path)
886 }
887 return ret
888}
889
890func pullContainerImages(appName string, imgs map[string]ContainerImage, registry, namespace string, jc JobCreator) error {
891 for _, img := range imgs {
892 name := fmt.Sprintf("copy-image-%s-%s-%s-%s", appName, img.Repository, img.Name, img.Tag)
893 if err := jc.Create(name, namespace, "giolekva/skopeo:latest", []string{
894 "skopeo",
895 "--insecure-policy",
896 "copy",
897 "--dest-tls-verify=false", // TODO(gio): enable
898 "--multi-arch=all",
899 fmt.Sprintf("docker://%s/%s/%s:%s", img.Registry, img.Repository, img.Name, img.Tag),
900 fmt.Sprintf("docker://%s/%s/%s:%s", registry, img.Repository, img.Name, img.Tag),
901 }); err != nil {
902 return err
903 }
904 }
905 return nil
906}
907
908type renderedInstance struct {
909 LocalCharts map[string]helmv2.HelmChartTemplateSpec `json:"localCharts"`
giocdfa3722024-06-13 20:10:14 +0400910 PortForward []PortForward `json:"portForward"`
giof8843412024-05-22 16:38:05 +0400911}
912
giocdfa3722024-06-13 20:10:14 +0400913func readRendered(fs soft.RepoFS, path string) (renderedInstance, error) {
giof8843412024-05-22 16:38:05 +0400914 r, err := fs.Reader(path)
915 if err != nil {
giocdfa3722024-06-13 20:10:14 +0400916 return renderedInstance{}, err
giof8843412024-05-22 16:38:05 +0400917 }
918 defer r.Close()
919 var cfg renderedInstance
920 if err := json.NewDecoder(r).Decode(&cfg); err != nil {
giocdfa3722024-06-13 20:10:14 +0400921 return renderedInstance{}, err
giof8843412024-05-22 16:38:05 +0400922 }
giocdfa3722024-06-13 20:10:14 +0400923 return cfg, nil
giof8843412024-05-22 16:38:05 +0400924}
gioefa0ed42024-06-13 12:31:43 +0400925
926func findPortFields(scm Schema) []string {
927 switch scm.Kind() {
928 case KindBoolean:
929 return []string{}
930 case KindInt:
931 return []string{}
932 case KindString:
933 return []string{}
934 case KindStruct:
935 ret := []string{}
936 for _, f := range scm.Fields() {
937 for _, p := range findPortFields(f.Schema) {
938 if p == "" {
939 ret = append(ret, f.Name)
940 } else {
941 ret = append(ret, fmt.Sprintf("%s.%s", f.Name, p))
942 }
943 }
944 }
945 return ret
946 case KindNetwork:
947 return []string{}
gio4ece99c2024-07-18 11:05:50 +0400948 case KindMultiNetwork:
949 return []string{}
gioefa0ed42024-06-13 12:31:43 +0400950 case KindAuth:
951 return []string{}
952 case KindSSHKey:
953 return []string{}
954 case KindNumber:
955 return []string{}
956 case KindArrayString:
957 return []string{}
958 case KindPort:
959 return []string{""}
960 default:
961 panic("MUST NOT REACH!")
962 }
963}
964
965func setPortFields(values map[string]any, ports map[string]reservePortResp) error {
966 for p, r := range ports {
967 if err := setPortField(values, p, r.Port); err != nil {
968 return err
969 }
970 }
971 return nil
972}
973
974func setPortField(values map[string]any, field string, port int) error {
975 f := strings.SplitN(field, ".", 2)
976 if len(f) == 2 {
977 var sub map[string]any
978 if s, ok := values[f[0]]; ok {
979 sub, ok = s.(map[string]any)
980 if !ok {
981 return fmt.Errorf("expected map")
982 }
983 } else {
984 sub = map[string]any{}
985 values[f[0]] = sub
986 }
987 if err := setPortField(sub, f[1], port); err != nil {
988 return err
989 }
990 } else {
991 values[f[0]] = port
992 }
993 return nil
994}