blob: 59d614b28ccd1f7d1eef828677f441952980d000 [file] [log] [blame]
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +04001package installer
2
3import (
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +04004 "bytes"
5 "encoding/json"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +04006 "errors"
7 "fmt"
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +04008 "io"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +04009 "io/fs"
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040010 "io/ioutil"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040011 "net"
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040012 "net/http"
13 "os"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040014 "path"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040015 "path/filepath"
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040016 "sync"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040017 "time"
18
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040019 "github.com/go-git/go-billy/v5/util"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040020 "github.com/go-git/go-git/v5"
21 "github.com/go-git/go-git/v5/plumbing/object"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040022 gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040023 "golang.org/x/crypto/ssh"
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040024 "sigs.k8s.io/yaml"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040025
26 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040027)
28
29type RepoIO interface {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040030 Addr() string
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040031 Pull() error
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040032 ReadConfig() (Config, error)
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040033 ReadAppConfig(path string) (AppConfig, error)
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040034 ReadKustomization(path string) (*Kustomization, error)
35 WriteKustomization(path string, kust Kustomization) error
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040036 ReadYaml(path string) (map[string]any, error)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040037 WriteYaml(path string, data any) error
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040038 CommitAndPush(message string) error
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040039 WriteCommitAndPush(path, contents, message string) error
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040040 Reader(path string) (io.ReadCloser, error)
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +040041 Writer(path string) (io.WriteCloser, error)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040042 CreateDir(path string) error
43 RemoveDir(path string) error
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040044 InstallApp(app App, path string, values map[string]any, derived Derived) error
45 RemoveApp(path string) error
46 FindAllInstances(root string, appId string) ([]AppConfig, error)
47 FindInstance(root string, id string) (AppConfig, error)
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040048}
49
50type repoIO struct {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040051 repo *soft.Repository
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040052 signer ssh.Signer
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040053 l sync.Locker
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040054}
55
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040056func NewRepoIO(repo *soft.Repository, signer ssh.Signer) RepoIO {
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040057 return &repoIO{
58 repo,
59 signer,
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +040060 &sync.Mutex{},
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040061 }
62}
63
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040064func (r *repoIO) Addr() string {
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040065 return r.repo.Addr.Addr
66}
67
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040068func (r *repoIO) Pull() error {
69 r.l.Lock()
70 defer r.l.Unlock()
71 return r.pullWithoutLock()
72}
73
74func (r *repoIO) pullWithoutLock() error {
75 wt, err := r.repo.Worktree()
76 if err != nil {
77 fmt.Printf("EEEER wt: %s\b", err)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040078 return nil
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +040079 }
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040080 err = wt.Pull(&git.PullOptions{
81 Auth: auth(r.signer),
82 Force: true,
83 })
84 // TODO(gio): propagate error
85 if err != nil {
86 fmt.Printf("EEEER: %s\b", err)
87 }
88 return nil
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +040089}
90
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040091func (r *repoIO) ReadConfig() (Config, error) {
92 configF, err := r.Reader(configFileName)
93 if err != nil {
94 return Config{}, err
95 }
96 defer configF.Close()
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040097 var cfg Config
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040098 if err := ReadYaml(configF, &cfg); err != nil {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +040099 return Config{}, err
100 } else {
101 return cfg, nil
102 }
103}
104
105func (r *repoIO) ReadAppConfig(path string) (AppConfig, error) {
106 configF, err := r.Reader(path)
107 if err != nil {
108 return AppConfig{}, err
109 }
110 defer configF.Close()
111 var cfg AppConfig
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +0400112 if err := ReadYaml(configF, &cfg); err != nil {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400113 return AppConfig{}, err
114 } else {
115 return cfg, nil
116 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400117}
118
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400119func (r *repoIO) ReadKustomization(path string) (*Kustomization, error) {
120 inp, err := r.Reader(path)
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400121 if err != nil {
122 return nil, err
123 }
124 defer inp.Close()
125 return ReadKustomization(inp)
126}
127
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400128func (r *repoIO) Reader(path string) (io.ReadCloser, error) {
129 wt, err := r.repo.Worktree()
130 if err != nil {
131 return nil, err
132 }
133 return wt.Filesystem.Open(path)
134}
135
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +0400136func (r *repoIO) Writer(path string) (io.WriteCloser, error) {
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400137 wt, err := r.repo.Worktree()
138 if err != nil {
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +0400139 return nil, err
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400140 }
141 if err := wt.Filesystem.MkdirAll(filepath.Dir(path), fs.ModePerm); err != nil {
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +0400142 return nil, err
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400143 }
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +0400144 return wt.Filesystem.Create(path)
145}
146
147func (r *repoIO) WriteKustomization(path string, kust Kustomization) error {
148 out, err := r.Writer(path)
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400149 if err != nil {
150 return err
151 }
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400152 return kust.Write(out)
153}
154
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400155func (r *repoIO) WriteYaml(path string, data any) error {
156 out, err := r.Writer(path)
157 if err != nil {
158 return err
159 }
160 serialized, err := yaml.Marshal(data)
161 if err != nil {
162 return err
163 }
164 if _, err := out.Write(serialized); err != nil {
165 return err
166 }
167 return nil
168}
169
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400170func (r *repoIO) ReadYaml(path string) (map[string]any, error) {
Giorgi Lekveishvili1506a4f2023-07-11 11:49:02 +0400171 inp, err := r.Reader(path)
172 if err != nil {
173 return nil, err
174 }
175 data := make(map[string]any)
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +0400176 if err := ReadYaml(inp, &data); err != nil {
Giorgi Lekveishvili1506a4f2023-07-11 11:49:02 +0400177 return nil, err
178 }
179 return data, err
180}
181
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400182func (r *repoIO) WriteCommitAndPush(path, contents, message string) error {
183 w, err := r.Writer(path)
184 if err != nil {
185 return err
186 }
187 defer w.Close()
188 if _, err := io.WriteString(w, contents); err != nil {
189 return err
190 }
191 return r.CommitAndPush(message)
192}
193
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400194func (r *repoIO) CommitAndPush(message string) error {
195 wt, err := r.repo.Worktree()
196 if err != nil {
197 return err
198 }
199 if err := wt.AddGlob("*"); err != nil {
200 return err
201 }
202 if _, err := wt.Commit(message, &git.CommitOptions{
203 Author: &object.Signature{
204 Name: "pcloud-installer",
205 When: time.Now(),
206 },
207 }); err != nil {
208 return err
209 }
210 return r.repo.Push(&git.PushOptions{
Giorgi Lekveishvili87be4ae2023-06-11 23:41:09 +0400211 RemoteName: "origin",
Giorgi Lekveishvili3550b432023-06-09 19:37:51 +0400212 Auth: auth(r.signer),
213 })
214}
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400215
216func (r *repoIO) CreateDir(path string) error {
217 wt, err := r.repo.Worktree()
218 if err != nil {
219 return err
220 }
221 return wt.Filesystem.MkdirAll(path, fs.ModePerm)
222}
223
224func (r *repoIO) RemoveDir(path string) error {
225 wt, err := r.repo.Worktree()
226 if err != nil {
227 return err
228 }
229 err = util.RemoveAll(wt.Filesystem, path)
230 if err == nil || errors.Is(err, fs.ErrNotExist) {
231 return nil
232 }
233 return err
234}
235
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400236type Release struct {
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400237 Namespace string `json:"namespace"`
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400238 RepoAddr string `json:"repoAddr"`
239 AppDir string `json:"appDir"`
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400240}
241
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400242type Derived struct {
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400243 Release Release `json:"release"`
244 Global Values `json:"global"`
245 Values map[string]any `json:"input"` // TODO(gio): rename to input
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400246}
247
248type AppConfig struct {
249 Id string `json:"id"`
250 AppId string `json:"appId"`
251 Config map[string]any `json:"config"`
252 Derived Derived `json:"derived"`
253}
254
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400255type allocatePortReq struct {
256 Protocol string `json:"protocol"`
257 SourcePort int `json:"sourcePort"`
258 TargetService string `json:"targetService"`
259 TargetPort int `json:"targetPort"`
260}
261
262// TODO(gio): most of this logic should move to AppManager
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400263func (r *repoIO) InstallApp(app App, appRootDir string, values map[string]any, derived Derived) error {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400264 r.l.Lock()
265 defer r.l.Unlock()
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400266 if err := r.pullWithoutLock(); err != nil {
267 return err
268 }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +0400269 if !filepath.IsAbs(appRootDir) {
270 return fmt.Errorf("Expected absolute path: %s", appRootDir)
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400271 }
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400272 derived.Release.RepoAddr = r.repo.Addr.FullAddress()
273 // TODO(gio): maybe client should populate this?
274 derived.Release.AppDir = appRootDir
275 rendered, err := app.Render(derived)
276 if err != nil {
277 return err
278 }
279 for _, p := range rendered.Ports {
280 var buf bytes.Buffer
281 req := allocatePortReq{
282 Protocol: p.Protocol,
283 SourcePort: p.SourcePort,
284 TargetService: p.TargetService,
285 TargetPort: p.TargetPort,
286 }
287 fmt.Printf("%+v\n", req)
288 if err := json.NewEncoder(&buf).Encode(req); err != nil {
289 return err
290 }
291 resp, err := http.Post(p.Allocator, "application/json", &buf)
292 if err != nil {
293 return err
294 }
295 if resp.StatusCode != http.StatusOK {
296 io.Copy(os.Stdout, resp.Body)
297 return fmt.Errorf("Could not allocate port %d, status code: %d", p.SourcePort, resp.StatusCode)
298 }
299 }
300 if err := r.pullWithoutLock(); err != nil {
301 return err
302 }
Giorgi Lekveishvili6e813182023-06-30 13:45:30 +0400303 appRootDir = filepath.Clean(appRootDir)
304 for p := appRootDir; p != "/"; {
305 parent, child := filepath.Split(p)
306 kustPath := filepath.Join(parent, "kustomization.yaml")
307 kust, err := r.ReadKustomization(kustPath)
308 if err != nil {
309 if errors.Is(err, fs.ErrNotExist) {
310 k := NewKustomization()
311 kust = &k
312 } else {
313 return err
314 }
315 }
316 kust.AddResources(child)
317 if err := r.WriteKustomization(kustPath, *kust); err != nil {
318 return err
319 }
320 p = filepath.Clean(parent)
321 }
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400322 {
323 if err := r.RemoveDir(appRootDir); err != nil {
324 return err
325 }
326 if err := r.CreateDir(appRootDir); err != nil {
327 return err
328 }
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400329 cfg := AppConfig{
Giorgi Lekveishvili08af67a2024-01-18 08:53:05 +0400330 AppId: app.Name(),
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400331 Config: values,
332 Derived: derived,
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400333 }
334 if err := r.WriteYaml(path.Join(appRootDir, configFileName), cfg); err != nil {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400335 return err
336 }
337 }
338 {
339 appKust := NewKustomization()
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400340 for name, contents := range rendered.Resources {
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400341 appKust.AddResources(name)
342 out, err := r.Writer(path.Join(appRootDir, name))
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400343 if err != nil {
344 return err
345 }
346 defer out.Close()
Giorgi Lekveishvili3f697b12024-01-04 00:56:06 +0400347 if _, err := out.Write(contents); err != nil {
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400348 return err
349 }
350 }
351 if err := r.WriteKustomization(path.Join(appRootDir, "kustomization.yaml"), appKust); err != nil {
352 return err
353 }
354 }
Giorgi Lekveishvili7c037392024-03-11 14:40:24 +0400355 return r.CommitAndPush(fmt.Sprintf("install: %s", app.Name()))
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400356}
357
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400358func (r *repoIO) RemoveApp(appRootDir string) error {
Giorgi Lekveishvili378ea882023-12-12 13:59:18 +0400359 r.l.Lock()
360 defer r.l.Unlock()
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400361 r.RemoveDir(appRootDir)
362 parent, child := filepath.Split(appRootDir)
363 kustPath := filepath.Join(parent, "kustomization.yaml")
364 kust, err := r.ReadKustomization(kustPath)
365 if err != nil {
366 return err
367 }
368 kust.RemoveResources(child)
369 r.WriteKustomization(kustPath, *kust)
370 return r.CommitAndPush(fmt.Sprintf("uninstall: %s", child))
371}
372
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400373func (r *repoIO) FindAllInstances(root string, name string) ([]AppConfig, error) {
374 if !filepath.IsAbs(root) {
375 return nil, fmt.Errorf("Expected absolute path: %s", root)
376 }
377 kust, err := r.ReadKustomization(filepath.Join(root, "kustomization.yaml"))
378 if err != nil {
379 return nil, err
380 }
381 ret := make([]AppConfig, 0)
382 for _, app := range kust.Resources {
383 cfg, err := r.ReadAppConfig(filepath.Join(root, app, "config.yaml"))
384 if err != nil {
385 return nil, err
386 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400387 cfg.Id = app
388 if cfg.AppId == name {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400389 ret = append(ret, cfg)
390 }
391 }
392 return ret, nil
393}
394
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400395func (r *repoIO) FindInstance(root string, id string) (AppConfig, error) {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400396 if !filepath.IsAbs(root) {
397 return AppConfig{}, fmt.Errorf("Expected absolute path: %s", root)
398 }
399 kust, err := r.ReadKustomization(filepath.Join(root, "kustomization.yaml"))
400 if err != nil {
401 return AppConfig{}, err
402 }
403 for _, app := range kust.Resources {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400404 if app == id {
405 cfg, err := r.ReadAppConfig(filepath.Join(root, app, "config.yaml"))
406 if err != nil {
407 return AppConfig{}, err
408 }
409 cfg.Id = id
410 return cfg, nil
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400411 }
412 }
413 return AppConfig{}, nil
414}
415
Giorgi Lekveishvili0ccd1482023-06-21 15:02:24 +0400416func auth(signer ssh.Signer) *gitssh.PublicKeys {
417 return &gitssh.PublicKeys{
418 Signer: signer,
419 HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
420 HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
421 // TODO(giolekva): verify server public key
422 // fmt.Printf("## %s || %s -- \n", serverPubKey, ssh.MarshalAuthorizedKey(key))
423 return nil
424 },
425 },
426 }
427}
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400428
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +0400429func ReadYaml[T any](r io.Reader, o *T) error {
Giorgi Lekveishvili76951482023-06-30 23:25:09 +0400430 if contents, err := ioutil.ReadAll(r); err != nil {
431 return err
432 } else {
433 return yaml.UnmarshalStrict(contents, o)
434 }
435}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400436
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400437func deriveValues(values any, schema Schema, networks []Network) (map[string]any, error) {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400438 ret := make(map[string]any)
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400439 for k, def := range schema.Fields() {
440 // TODO(gio): validate that it is map
441 v, ok := values.(map[string]any)[k]
442 // TODO(gio): if missing use default value
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400443 if !ok {
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400444 if def.Kind() == KindSSHKey {
445 key, err := NewECDSASSHKeyPair("tmp")
446 if err != nil {
447 return nil, err
448 }
449 ret[k] = map[string]string{
450 "public": string(key.RawAuthorizedKey()),
451 "private": string(key.RawPrivateKey()),
452 }
453 }
454 continue
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400455 }
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400456 switch def.Kind() {
457 case KindBoolean:
Giorgi Lekveishvili0ba5e402024-03-20 15:56:30 +0400458 ret[k] = v
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400459 case KindString:
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400460 ret[k] = v
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400461 case KindInt:
462 ret[k] = v
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400463 case KindNetwork:
464 n, err := findNetwork(networks, v.(string)) // TODO(giolekva): validate
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400465 if err != nil {
466 return nil, err
467 }
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400468 ret[k] = n
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400469 case KindAuth:
470 r, err := deriveValues(v, AuthSchema, networks)
471 if err != nil {
472 return nil, err
473 }
474 ret[k] = r
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400475 case KindSSHKey:
476 r, err := deriveValues(v, SSHKeySchema, networks)
477 if err != nil {
478 return nil, err
479 }
480 ret[k] = r
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +0400481 case KindStruct:
482 r, err := deriveValues(v, def, networks)
483 if err != nil {
484 return nil, err
485 }
486 ret[k] = r
487 default:
488 return nil, fmt.Errorf("Should not reach!")
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400489 }
490 }
491 return ret, nil
492}
493
494func findNetwork(networks []Network, name string) (Network, error) {
495 for _, n := range networks {
496 if n.Name == name {
497 return n, nil
498 }
499 }
500 return Network{}, fmt.Errorf("Network not found: %s", name)
501}
502
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400503type Network struct {
Giorgi Lekveishvili9b52ab92024-01-05 13:12:48 +0400504 Name string `json:"name,omitempty"`
505 IngressClass string `json:"ingressClass,omitempty"`
506 CertificateIssuer string `json:"certificateIssuer,omitempty"`
507 Domain string `json:"domain,omitempty"`
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +0400508 AllocatePortAddr string `json:"allocatePortAddr,omitempty"`
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400509}