blob: fccf0b2ddcaa9774ec5e87aada39cb1723343eb1 [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 Lekveishvili6e813182023-06-30 13:45:30 +04006 "path/filepath"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04007
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +04008 "sigs.k8s.io/yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +04009)
10
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040011const appDirName = "apps"
12const configFileName = "config.yaml"
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040013const kustomizationFileName = "kustomization.yaml"
14
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040015type AppManager struct {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040016 repoIO RepoIO
17 nsCreator NamespaceCreator
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040018}
19
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040020func NewAppManager(repoIO RepoIO, nsCreator NamespaceCreator) (*AppManager, error) {
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040021 return &AppManager{
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040022 repoIO,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040023 nsCreator,
Giorgi Lekveishvilibd6be7f2023-05-26 15:51:28 +040024 }, nil
25}
26
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040027func (m *AppManager) Config() (Config, error) {
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040028 return m.repoIO.ReadConfig()
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040029}
30
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040031func (m *AppManager) AppConfig(name string) (map[string]any, error) {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040032 configF, err := m.repoIO.Reader(fmt.Sprintf("%s/%s/%s", appDirName, name, configFileName))
Giorgi Lekveishvili03ee5852023-05-30 13:20:10 +040033 if err != nil {
34 return nil, err
35 }
36 defer configF.Close()
37 var cfg map[string]any
38 contents, err := ioutil.ReadAll(configF)
39 if err != nil {
40 return cfg, err
41 }
42 err = yaml.UnmarshalStrict(contents, &cfg)
43 return cfg, err
44}
45
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040046func (m *AppManager) Install(app App, ns NamespaceGenerator, suffixGen SuffixGenerator, config map[string]any) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040047 // if err := m.repoIO.Fetch(); err != nil {
48 // return err
49 // }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040050 suffix, err := suffixGen.Generate()
51 if err != nil {
52 return err
53 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040054 namespaces := make([]string, len(app.Namespaces))
55 for i, n := range app.Namespaces {
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040056 ns, err := ns.Generate(n)
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040057 if err != nil {
58 return err
59 }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040060 namespaces[i] = ns + suffix
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040061 }
62 for _, n := range namespaces {
63 if err := m.nsCreator.Create(n); err != nil {
64 return err
65 }
66 }
67 globalConfig, err := m.repoIO.ReadConfig()
Giorgi Lekveishvili7efe22f2023-05-30 13:01:53 +040068 if err != nil {
69 return err
70 }
71 all := map[string]any{
72 "Global": globalConfig.Values,
73 "Values": config,
74 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040075 if len(namespaces) > 0 {
76 all["Release"] = map[string]any{
77 "Namespace": namespaces[0],
78 }
79 }
Giorgi Lekveishvili27b2b572023-06-30 10:44:45 +040080 // TODO(giolekva): use ns suffix for app directory
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +040081 return m.repoIO.InstallApp(
82 app,
83 filepath.Join("/apps", app.Name+suffix),
84 all)
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040085}