/
installer.go
155 lines (137 loc) · 4.32 KB
/
installer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package app
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/RHEcosystemAppEng/SaaSi/exporter/exporter-lib/export/utils"
"github.com/sirupsen/logrus"
)
type Installer struct {
appContext *AppContext
sccToBeReplacedByNS map[string][]SccForSA
logger *logrus.Logger
}
type SccForSA struct {
serviceAccountName string
sccName string
}
func NewInstallerFromConfig(appContext *AppContext) *Installer {
installer := Installer{appContext: appContext, logger: appContext.Logger()}
installer.sccToBeReplacedByNS = make(map[string][]SccForSA)
return &installer
}
func (i *Installer) BuildKustomizeInstaller() error {
for _, ns := range i.appContext.AppConfig.Namespaces {
i.logger.Infof("Creating kustomize installer for NS %s", ns.Name)
outputFolder := i.appContext.OutputFolderForNS(ns.Name)
kustomizeFolder := i.appContext.BaseKustomizeFolderForNS(ns.Name)
kustomization := filepath.Join(kustomizeFolder, KustomizationFile)
os.Create(kustomization)
utils.AppendToFile(kustomization, "resources:")
filepath.WalkDir(outputFolder, func(path string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if !d.IsDir() && filepath.Ext(d.Name()) == ".yaml" {
i.logger.Debugf("Moving %s to %s", d.Name(), kustomizeFolder)
os.Rename(path, filepath.Join(kustomizeFolder, d.Name()))
utils.AppendToFile(kustomization, fmt.Sprintf("\n - %s", d.Name()))
}
return nil
})
}
err := i.createKustomizeTemplate()
return err
}
func (i *Installer) createKustomizeTemplate() error {
for _, ns := range i.appContext.AppConfig.Namespaces {
i.logger.Infof("Creating kustomize template for NS %s", ns.Name)
templateFolder := i.appContext.KustomizeTemplateFolderForNS(ns.Name)
paramsFolder := filepath.Join(templateFolder, ParamsFolder)
os.Rename(i.appContext.TmpParamsFolderForNS(ns.Name), paramsFolder)
secretsFolder := filepath.Join(templateFolder, SecretsFolder)
os.Rename(i.appContext.TmpSecretsFolderForNS(ns.Name), secretsFolder)
templateKustomization := i.appContext.KustomizationFileFrom(templateFolder)
os.Create(templateKustomization)
text := "resources:\n" +
" - ../base\n"
err := utils.AppendToFile(templateKustomization, text)
if err != nil {
return err
}
text = "generatorOptions:\n" +
" disableNameSuffixHash: true\n" +
"configMapGenerator:"
err = utils.AppendToFile(templateKustomization, text)
if err != nil {
return err
}
err = filepath.WalkDir(paramsFolder,
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
configMap := strings.Replace(d.Name(), ".env", "", 1)
i.logger.Infof("Creating configMapGenerator for %s", configMap)
text = "\n" +
"- name: %s\n" +
" behavior: merge\n" +
" envs:\n" +
" - %s/%s"
utils.AppendToFile(templateKustomization, text, configMap, ParamsFolder, d.Name())
}
return nil
})
if err != nil {
return err
}
text = "\nsecretGenerator:"
utils.AppendToFile(templateKustomization, text)
err = filepath.WalkDir(secretsFolder,
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
secret := strings.Replace(d.Name(), ".env", "", 1)
i.logger.Infof("Creating secretGenerator for %s", secret)
text = "\n" +
"- name: %s\n" +
" behavior: create\n" +
" envs:\n" +
" - %s/%s"
utils.AppendToFile(templateKustomization, text, secret, SecretsFolder, d.Name())
}
return nil
})
if err != nil {
i.logger.Errorf("Cannot create kustomize template: %s", err)
return err
}
if len(i.sccToBeReplacedByNS[ns.Name]) > 0 {
text := "\nreplacements:"
utils.AppendToFile(templateKustomization, text)
for _, sccForSA := range i.sccToBeReplacedByNS[ns.Name] {
text = "\n" +
"- source:\n" +
" kind: ServiceAccount\n" +
" name: %s\n" +
" fieldPath: metadata.namespace\n" +
" targets:\n" +
" - select:\n" +
" kind: SecurityContextConstraints\n" +
" name: %s\n" +
" fieldPaths:\n" +
" - users.*\n" +
" options:\n" +
" delimiter: \":\"\n" +
" index: 2\n"
utils.AppendToFile(templateKustomization, text, sccForSA.serviceAccountName, sccForSA.sccName)
}
}
}
return nil
}