/
k8s.go
75 lines (60 loc) · 1.32 KB
/
k8s.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
package main
import (
"os"
"path"
"github.com/samber/lo"
"gopkg.in/yaml.v2"
)
type Images struct {
Images []*Image `yaml:"images"`
}
type Image struct {
OldName string `yaml:"old_image"`
NewName string `yaml:"new_image"`
}
func getKustomiztion(path string) (*Images, error) {
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
var i Images
err = yaml.Unmarshal(data, &i)
if err != nil {
return nil, err
}
return &i, nil
}
func updateK8sDeployment(namespace string, imagesChan chan *Image) error {
builtImages := []*Image{}
for image := range imagesChan {
builtImages = append(builtImages, image)
}
filePath := path.Join(".ippon", namespace+".yaml")
images, err := getKustomiztion(filePath)
if err != nil {
return err
}
currentImages := []*Image{}
if images != nil && images.Images != nil {
currentImages = images.Images
}
for _, image := range builtImages {
_, idx, ok := lo.FindIndexOf(currentImages, func(i *Image) bool {
return i.OldName == image.OldName
})
if !ok {
currentImages = append(currentImages, image)
} else {
currentImages[idx].NewName = image.NewName
}
}
images = &Images{Images: currentImages}
out, err := yaml.Marshal(images)
if err != nil {
return err
}
return os.WriteFile(filePath, out, 0644)
}