blob: 19ec6c9463df371a3256b41d65e1b0128d338597 [file] [log] [blame]
giolekva8aa73e82022-07-09 11:34:39 +04001package main
2
3import (
4 "context"
giolekva8aa73e82022-07-09 11:34:39 +04005 _ "embed"
giolekva8aa73e82022-07-09 11:34:39 +04006 "fmt"
giolekva8aa73e82022-07-09 11:34:39 +04007 "log"
8 "os"
9 "path/filepath"
10 "time"
11
Giorgi Lekveishvili402fff92023-07-09 19:38:24 +040012 "github.com/cenkalti/backoff/v4"
giolekva8aa73e82022-07-09 11:34:39 +040013 "github.com/spf13/cobra"
14 "helm.sh/helm/v3/pkg/action"
15 "helm.sh/helm/v3/pkg/chart/loader"
16 "helm.sh/helm/v3/pkg/kube"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040017
18 "github.com/giolekva/pcloud/core/installer"
19 "github.com/giolekva/pcloud/core/installer/soft"
giolekva8aa73e82022-07-09 11:34:39 +040020)
21
22var bootstrapFlags struct {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040023 pcloudEnvName string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040024 chartsDir string
25 adminPubKey string
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040026 storageDir string
27 volumeDefaultReplicaCount int
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040028 softServeIP string // TODO(giolekva): reserve using metallb IPAddressPool
giolekva8aa73e82022-07-09 11:34:39 +040029}
30
31func bootstrapCmd() *cobra.Command {
32 cmd := &cobra.Command{
33 Use: "bootstrap",
34 RunE: bootstrapCmdRun,
35 }
36 cmd.Flags().StringVar(
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040037 &bootstrapFlags.pcloudEnvName,
38 "pcloud-env-name",
39 "pcloud",
40 "",
41 )
42 cmd.Flags().StringVar(
giolekva8aa73e82022-07-09 11:34:39 +040043 &bootstrapFlags.chartsDir,
44 "charts-dir",
45 "",
46 "",
47 )
48 cmd.Flags().StringVar(
49 &bootstrapFlags.adminPubKey,
50 "admin-pub-key",
51 "",
52 "",
53 )
54 cmd.Flags().StringVar(
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +040055 &bootstrapFlags.storageDir,
56 "storage-dir",
57 "",
58 "",
59 )
60 cmd.Flags().IntVar(
61 &bootstrapFlags.volumeDefaultReplicaCount,
62 "volume-default-replica-count",
63 3,
64 "",
65 )
66 cmd.Flags().StringVar(
67 &bootstrapFlags.softServeIP,
68 "soft-serve-ip",
69 "",
70 "",
71 )
giolekva8aa73e82022-07-09 11:34:39 +040072 return cmd
73}
74
75func bootstrapCmdRun(cmd *cobra.Command, args []string) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040076 adminPubKey, err := os.ReadFile(bootstrapFlags.adminPubKey)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040077 if err != nil {
78 return err
79 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040080 bootstrapJobKeys, err := installer.NewSSHKeyPair()
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040081 if err != nil {
82 return err
83 }
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040084 if err := installMetallb(); err != nil {
85 return err
86 }
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040087 if err := installLonghorn(); err != nil {
88 return err
89 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040090 time.Sleep(2 * time.Minute) // TODO(giolekva): implement proper wait
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040091 if err := installSoftServe(bootstrapJobKeys.Public); err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +040092 return err
93 }
Giorgi Lekveishvili402fff92023-07-09 19:38:24 +040094 var ss *soft.Client
95 err = backoff.Retry(func() error {
96 var err error
97 ss, err = soft.NewClient(bootstrapFlags.softServeIP, 22, []byte(bootstrapJobKeys.Private), log.Default())
98 return err
99 }, backoff.NewConstantBackOff(5*time.Second))
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400100 if err != nil {
101 return err
102 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400103 if ss.AddPublicKey("admin", string(adminPubKey)); err != nil {
104 return err
105 }
106 if err := installFluxcd(ss, bootstrapFlags.pcloudEnvName); err != nil {
107 return err
108 }
109 repo, err := ss.GetRepo(bootstrapFlags.pcloudEnvName)
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400110 if err != nil {
111 return err
112 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400113 repoIO := installer.NewRepoIO(repo, ss.Signer)
114 if err := configurePCloudRepo(repoIO); err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400115 return err
116 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400117 // TODO(giolekva): commit this to the repo above
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400118 global := installer.Values{
119 PCloudEnvName: bootstrapFlags.pcloudEnvName,
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400120 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400121 nsCreator, err := newNSCreator()
122 if err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400123 return err
124 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400125 nsGen := installer.NewPrefixGenerator("pcloud-")
126 if err := installInfrastructureServices(repoIO, nsGen, nsCreator, global); err != nil {
127 return err
128 }
129 if err := installEnvManager(ss, repoIO, nsGen, nsCreator, global); err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400130 return err
131 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400132 if ss.RemovePublicKey("admin", bootstrapJobKeys.Public); err != nil {
Giorgi Lekveishvili677b4572023-05-26 15:02:37 +0400133 return err
134 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400135 return nil
136}
137
138func installMetallb() error {
139 if err := installMetallbNamespace(); err != nil {
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400140 return err
141 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400142 if err := installMetallbService(); err != nil {
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400143 return err
144 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400145 if err := installMetallbConfig(); err != nil {
Giorgi Lekveishvilid6e80cc2023-06-09 17:38:49 +0400146 return err
147 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400148 return nil
149}
150
151func installMetallbNamespace() error {
152 fmt.Println("Installing metallb namespace")
153 // config, err := createActionConfig("default")
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400154 config, err := createActionConfig(bootstrapFlags.pcloudEnvName)
giolekva8aa73e82022-07-09 11:34:39 +0400155 if err != nil {
156 return err
157 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400158 chart, err := loader.Load(filepath.Join(bootstrapFlags.chartsDir, "namespace"))
giolekva8aa73e82022-07-09 11:34:39 +0400159 if err != nil {
160 return err
161 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400162 values := map[string]interface{}{
163 // "namespace": "pcloud-metallb",
164 "namespace": "metallb-system",
165 "labels": []string{
166 "pod-security.kubernetes.io/audit: privileged",
167 "pod-security.kubernetes.io/enforce: privileged",
168 "pod-security.kubernetes.io/warn: privileged",
169 },
170 }
171 installer := action.NewInstall(config)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400172 installer.Namespace = bootstrapFlags.pcloudEnvName
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400173 installer.ReleaseName = "metallb-ns"
174 installer.Wait = true
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400175 installer.WaitForJobs = true
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400176 if _, err := installer.RunWithContext(context.TODO(), chart, values); err != nil {
177 return err
178 }
179 return nil
180}
181
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400182func installMetallbService() error {
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400183 fmt.Println("Installing metallb")
184 // config, err := createActionConfig("default")
185 config, err := createActionConfig("metallb-system")
giolekva8aa73e82022-07-09 11:34:39 +0400186 if err != nil {
187 return err
188 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400189 chart, err := loader.Load(filepath.Join(bootstrapFlags.chartsDir, "metallb"))
giolekva8aa73e82022-07-09 11:34:39 +0400190 if err != nil {
191 return err
192 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400193 values := map[string]interface{}{ // TODO(giolekva): add loadBalancerClass?
194 "controller": map[string]interface{}{
195 "image": map[string]interface{}{
196 "repository": "quay.io/metallb/controller",
197 "tag": "v0.13.9",
198 "pullPolicy": "IfNotPresent",
199 },
200 "logLevel": "info",
201 },
202 "speaker": map[string]interface{}{
203 "image": map[string]interface{}{
204 "repository": "quay.io/metallb/speaker",
205 "tag": "v0.13.9",
206 "pullPolicy": "IfNotPresent",
207 },
208 "logLevel": "info",
209 },
210 }
211 installer := action.NewInstall(config)
212 installer.Namespace = "metallb-system" // "pcloud-metallb"
213 installer.CreateNamespace = true
214 installer.ReleaseName = "metallb"
215 installer.IncludeCRDs = true
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400216 installer.Wait = true
217 installer.WaitForJobs = true
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400218 installer.Timeout = 20 * time.Minute
219 if _, err := installer.RunWithContext(context.TODO(), chart, values); err != nil {
giolekva8aa73e82022-07-09 11:34:39 +0400220 return err
221 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400222 return nil
223}
224
225func installMetallbConfig() error {
226 fmt.Println("Installing metallb-config")
227 // config, err := createActionConfig("default")
228 config, err := createActionConfig("metallb-system")
229 if err != nil {
giolekva8aa73e82022-07-09 11:34:39 +0400230 return err
231 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400232 chart, err := loader.Load(filepath.Join(bootstrapFlags.chartsDir, "metallb-config"))
233 if err != nil {
giolekva8aa73e82022-07-09 11:34:39 +0400234 return err
235 }
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400236 values := map[string]interface{}{
237 "from": "192.168.0.210",
238 "to": "192.168.0.240",
239 }
240 installer := action.NewInstall(config)
241 installer.Namespace = "metallb-system" // "pcloud-metallb"
242 installer.CreateNamespace = true
243 installer.ReleaseName = "metallb-cfg"
244 installer.Wait = true
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400245 installer.WaitForJobs = true
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400246 installer.Timeout = 20 * time.Minute
247 if _, err := installer.RunWithContext(context.TODO(), chart, values); err != nil {
248 return err
249 }
250 return nil
251}
252
253func installLonghorn() error {
254 fmt.Println("Installing Longhorn")
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400255 config, err := createActionConfig(bootstrapFlags.pcloudEnvName)
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400256 if err != nil {
257 return err
258 }
259 chart, err := loader.Load(filepath.Join(bootstrapFlags.chartsDir, "longhorn"))
260 if err != nil {
261 return err
262 }
263 values := map[string]interface{}{
264 "defaultSettings": map[string]interface{}{
265 "defaultDataPath": bootstrapFlags.storageDir,
266 },
267 "persistence": map[string]interface{}{
268 "defaultClassReplicaCount": bootstrapFlags.volumeDefaultReplicaCount,
269 },
270 "service": map[string]interface{}{
271 "ui": map[string]interface{}{
272 "type": "LoadBalancer",
273 },
274 },
275 "ingress": map[string]interface{}{
276 "enabled": false,
277 },
278 }
279 installer := action.NewInstall(config)
280 installer.Namespace = "longhorn-system"
281 installer.CreateNamespace = true
282 installer.ReleaseName = "longhorn"
283 installer.Wait = true
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400284 installer.WaitForJobs = true
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400285 installer.Timeout = 20 * time.Minute
286 if _, err := installer.RunWithContext(context.TODO(), chart, values); err != nil {
giolekva8aa73e82022-07-09 11:34:39 +0400287 return err
288 }
289 return nil
290}
291
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400292func installSoftServe(adminPublicKey string) error {
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400293 fmt.Println("Installing SoftServe")
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400294 keys, err := installer.NewSSHKeyPair()
295 if err != nil {
296 return err
297 }
298 config, err := createActionConfig(bootstrapFlags.pcloudEnvName)
giolekva8aa73e82022-07-09 11:34:39 +0400299 if err != nil {
300 return err
301 }
302 chart, err := loader.Load(filepath.Join(bootstrapFlags.chartsDir, "soft-serve"))
303 if err != nil {
304 return err
305 }
Giorgi Lekveishvili25c705b2023-07-12 11:58:31 +0400306 values := map[string]any{
307 "image": map[string]any{
308 "repository": "charmcli/soft-serve",
309 "tag": "v0.5.4",
310 "pullPolicy": "IfNotPresent",
311 },
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400312 "privateKey": keys.Private,
313 "publicKey": keys.Public,
314 "adminKey": adminPublicKey,
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400315 "reservedIP": bootstrapFlags.softServeIP,
giolekva8aa73e82022-07-09 11:34:39 +0400316 }
317 installer := action.NewInstall(config)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400318 installer.Namespace = bootstrapFlags.pcloudEnvName
giolekva8aa73e82022-07-09 11:34:39 +0400319 installer.CreateNamespace = true
320 installer.ReleaseName = "soft-serve"
321 installer.Wait = true
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400322 installer.WaitForJobs = true
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400323 installer.Timeout = 20 * time.Minute
giolekva8aa73e82022-07-09 11:34:39 +0400324 if _, err := installer.RunWithContext(context.TODO(), chart, values); err != nil {
325 return err
326 }
327 return nil
328}
329
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400330func installFluxcd(ss *soft.Client, pcloudEnvName string) error {
331 keys, err := installer.NewSSHKeyPair()
332 if err != nil {
333 return err
334 }
335 if err := ss.AddUser("flux", keys.Public); err != nil {
336 return err
337 }
338 if err := ss.MakeUserAdmin("flux"); err != nil {
339 return err
340 }
341 fmt.Printf("Creating /%s repo", pcloudEnvName)
342 if err := ss.AddRepository(pcloudEnvName, "# PCloud Systems"); err != nil {
343 return err
344 }
345 fmt.Println("Installing Flux")
346 ssPublic, err := ss.GetPublicKey()
347 if err != nil {
348 return err
349 }
350 if err := installFluxBootstrap(
351 ss.GetRepoAddress(pcloudEnvName),
352 ss.IP,
353 string(ssPublic),
354 keys.Private,
355 ); err != nil {
356 return err
357 }
358 return nil
359}
360
361func installFluxBootstrap(repoAddr, repoHost, repoHostPubKey, privateKey string) error {
362 config, err := createActionConfig(bootstrapFlags.pcloudEnvName)
giolekva8aa73e82022-07-09 11:34:39 +0400363 if err != nil {
364 return err
365 }
366 chart, err := loader.Load(filepath.Join(bootstrapFlags.chartsDir, "flux-bootstrap"))
367 if err != nil {
368 return err
369 }
Giorgi Lekveishvili402fff92023-07-09 19:38:24 +0400370 values := map[string]any{
371 "image": map[string]any{
372 "repository": "giolekva/flux",
373 "tag": "2.0.0",
374 "pullPolicy": "IfNotPresent",
375 },
giolekva8aa73e82022-07-09 11:34:39 +0400376 "repositoryAddress": repoAddr,
377 "repositoryHost": repoHost,
378 "repositoryHostPublicKey": repoHostPubKey,
379 "privateKey": privateKey,
380 }
381 installer := action.NewInstall(config)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400382 installer.Namespace = bootstrapFlags.pcloudEnvName
giolekva8aa73e82022-07-09 11:34:39 +0400383 installer.CreateNamespace = true
384 installer.ReleaseName = "flux"
385 installer.Wait = true
386 installer.WaitForJobs = true
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400387 installer.Timeout = 20 * time.Minute
giolekva8aa73e82022-07-09 11:34:39 +0400388 if _, err := installer.RunWithContext(context.TODO(), chart, values); err != nil {
389 return err
390 }
391 return nil
392}
393
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400394func installInfrastructureServices(repo installer.RepoIO, nsGen installer.NamespaceGenerator, nsCreator installer.NamespaceCreator, global installer.Values) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400395 appRepo := installer.NewInMemoryAppRepository(installer.CreateAllApps())
396 install := func(name string) error {
397 app, err := appRepo.Find(name)
398 if err != nil {
399 return err
400 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400401 namespaces := make([]string, len(app.Namespaces))
402 for i, n := range app.Namespaces {
403 namespaces[i], err = nsGen.Generate(n)
404 if err != nil {
405 return err
406 }
407 }
408 for _, n := range namespaces {
409 if err := nsCreator.Create(n); err != nil {
410 return err
411 }
412 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400413 derived := installer.Derived{
414 Global: global,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400415 }
416 if len(namespaces) > 0 {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400417 derived.Release.Namespace = namespaces[0]
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400418 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400419 return repo.InstallApp(*app, filepath.Join("/infrastructure", app.Name), map[string]any{}, derived)
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400420 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400421 appsToInstall := []string{
422 "resource-renderer-controller",
423 "headscale-controller",
424 "csi-driver-smb",
425 "ingress-public",
426 "cert-manager",
427 "cert-manager-webhook-gandi",
428 "cert-manager-webhook-gandi-role",
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400429 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400430 for _, name := range appsToInstall {
431 if err := install(name); err != nil {
432 return err
433 }
Giorgi Lekveishvilid6e80cc2023-06-09 17:38:49 +0400434 }
435 return nil
436}
437
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400438func configurePCloudRepo(repo installer.RepoIO) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400439 {
440 kust := installer.NewKustomization()
441 kust.AddResources("pcloud-flux", "infrastructure", "environments")
442 if err := repo.WriteKustomization("kustomization.yaml", kust); err != nil {
443 return err
444 }
445 {
446 out, err := repo.Writer("infrastructure/pcloud-charts.yaml")
447 if err != nil {
448 return err
449 }
450 defer out.Close()
451 _, err = out.Write([]byte(`
Giorgi Lekveishvili25c705b2023-07-12 11:58:31 +0400452apiVersion: source.toolkit.fluxcd.io/v1
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400453kind: GitRepository
454metadata:
455 name: pcloud # TODO(giolekva): use more generic name
456 namespace: pcloud # TODO(giolekva): configurable
457spec:
458 interval: 1m0s
459 url: https://github.com/giolekva/pcloud
460 ref:
461 branch: main
462`))
463 if err != nil {
464 return err
465 }
466 }
467 infraKust := installer.NewKustomization()
468 infraKust.AddResources("pcloud-charts.yaml")
469 if err := repo.WriteKustomization("infrastructure/kustomization.yaml", infraKust); err != nil {
470 return err
471 }
472 if err := repo.WriteKustomization("environments/kustomization.yaml", installer.NewKustomization()); err != nil {
473 return err
474 }
475 if err := repo.CommitAndPush("initialize pcloud directory structure"); err != nil {
476 return err
477 }
478 }
479 return nil
480}
481
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400482func installEnvManager(ss *soft.Client, repo installer.RepoIO, nsGen installer.NamespaceGenerator, nsCreator installer.NamespaceCreator, global installer.Values) error {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400483 keys, err := installer.NewSSHKeyPair()
484 if err != nil {
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400485 return err
486 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400487 user := fmt.Sprintf("%s-env-manager", bootstrapFlags.pcloudEnvName)
488 if err := ss.AddUser(user, keys.Public); err != nil {
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400489 return err
490 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400491 if err := ss.MakeUserAdmin(user); err != nil {
492 return err
493 }
494 appRepo := installer.NewInMemoryAppRepository(installer.CreateAllApps())
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400495 app, err := appRepo.Find("env-manager")
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400496 if err != nil {
497 return err
498 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400499 namespaces := make([]string, len(app.Namespaces))
500 for i, n := range app.Namespaces {
501 namespaces[i], err = nsGen.Generate(n)
502 if err != nil {
503 return err
504 }
505 }
506 for _, n := range namespaces {
507 if err := nsCreator.Create(n); err != nil {
508 return err
509 }
510 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400511 derived := installer.Derived{
512 Global: global,
513 Values: map[string]any{
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400514 "RepoIP": bootstrapFlags.softServeIP,
515 "SSHPrivateKey": keys.Private,
516 },
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400517 }
518 if len(namespaces) > 0 {
519 derived.Release.Namespace = namespaces[0]
520 }
521 return repo.InstallApp(*app, filepath.Join("/infrastructure", app.Name), derived.Values, derived)
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400522}
523
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400524func createActionConfig(namespace string) (*action.Configuration, error) {
giolekva8aa73e82022-07-09 11:34:39 +0400525 config := new(action.Configuration)
526 if err := config.Init(
Giorgi Lekveishvili23ef7f82023-05-26 11:57:48 +0400527 kube.GetConfig(rootFlags.kubeConfig, "", namespace),
528 namespace,
giolekva8aa73e82022-07-09 11:34:39 +0400529 "",
530 func(fmtString string, args ...interface{}) {
531 fmt.Printf(fmtString, args...)
532 fmt.Println()
533 },
534 ); err != nil {
535 return nil, err
536 }
537 return config, nil
538}