blob: d2feccc29f97b439954b6f8b1b2188b03d3b0d44 [file] [log] [blame]
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04001package installer
2
3import (
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +04004 "fmt"
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +04005 "io/ioutil"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04006
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +04007 "sigs.k8s.io/yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04008)
9
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040010const appDirName = "apps"
11const configFileName = "config.yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040012const kustomizationFileName = "kustomization.yaml"
13
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040014type AppManager struct {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040015 repoIO RepoIO
16 nsCreator NamespaceCreator
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040017}
18
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040019func NewAppManager(repoIO RepoIO, nsCreator NamespaceCreator) (*AppManager, error) {
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040020 return &AppManager{
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040021 repoIO,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040022 nsCreator,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040023 }, nil
24}
25
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040026func (m *AppManager) Config() (Config, error) {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040027 return m.repoIO.ReadConfig()
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040028}
29
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040030func (m *AppManager) AppConfig(name string) (map[string]any, error) {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040031 configF, err := m.repoIO.Reader(fmt.Sprintf("%s/%s/%s", appDirName, name, configFileName))
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040032 if err != nil {
33 return nil, err
34 }
35 defer configF.Close()
36 var cfg map[string]any
37 contents, err := ioutil.ReadAll(configF)
38 if err != nil {
39 return cfg, err
40 }
41 err = yaml.UnmarshalStrict(contents, &cfg)
42 return cfg, err
43}
44
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040045func (m *AppManager) Install(app App, ns NamespaceGenerator, config map[string]any) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040046 // if err := m.repoIO.Fetch(); err != nil {
47 // return err
48 // }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040049 namespaces := make([]string, len(app.Namespaces))
50 for i, n := range app.Namespaces {
51 var err error
52 namespaces[i], err = ns.Generate(n)
53 if err != nil {
54 return err
55 }
56 }
57 for _, n := range namespaces {
58 if err := m.nsCreator.Create(n); err != nil {
59 return err
60 }
61 }
62 globalConfig, err := m.repoIO.ReadConfig()
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040063 if err != nil {
64 return err
65 }
66 all := map[string]any{
67 "Global": globalConfig.Values,
68 "Values": config,
69 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040070 if len(namespaces) > 0 {
71 all["Release"] = map[string]any{
72 "Namespace": namespaces[0],
73 }
74 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040075 return m.repoIO.InstallApp(app, "apps", all)
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040076}