This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.go
88 lines (75 loc) · 2.22 KB
/
index.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
package fetch
import (
helper2 "github.com/caos/orbos/internal/utils/helper"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/caos/orbos/mntr"
"gopkg.in/yaml.v3"
)
type index struct {
APIVersion string `yaml:"apiVersion"`
Entries map[string][]entry `yaml:"entries"`
}
type entry struct {
Version string `yaml:"version"`
AppVersion string `yaml:"appVersion"`
}
func CompareVersions(monitor mntr.Monitor, basePath string, charts []*ChartInfo) error {
indexFolderPathAbs, err := helper2.GetAbsPath(basePath, "helm", "repository", "cache")
if err != nil {
return err
}
indexFiles := make(map[string]*index, 0)
err = filepath.Walk(indexFolderPathAbs, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var indexFile index
if err := yaml.Unmarshal(data, &indexFile); err != nil {
return err
}
indexFiles[strings.TrimSuffix(info.Name(), "-index.yaml")] = &indexFile
// for k, v := range indexFile.Entries {
// indexFiles[strings.TrimSuffix(info.Name(), "-index.yaml")].Entries[k] = v
// }
return nil
})
if err != nil {
return err
}
for _, chart := range charts {
indexFile := indexFiles[chart.IndexName]
for _, entry := range indexFile.Entries[chart.Name] {
entryParts := strings.Split(entry.Version, ".")
entryPartsInt := make([]int, 3)
for k, v := range entryParts {
entryPartsInt[k], _ = strconv.Atoi(v)
}
chartParts := strings.Split(chart.Version, ".")
chartPartsInt := make([]int, 3)
for k, v := range chartParts {
chartPartsInt[k], _ = strconv.Atoi(v)
}
if entryPartsInt[0] > chartPartsInt[0] ||
(entryPartsInt[0] == chartPartsInt[0] && entryPartsInt[1] > chartPartsInt[1]) ||
(entryPartsInt[0] == chartPartsInt[0] && entryPartsInt[1] == chartPartsInt[1] && entryPartsInt[2] > chartPartsInt[2]) {
logFields := map[string]interface{}{
"oldVersion": chart.Version,
"newVersion": entry.Version,
"index": chart.IndexName,
"chart": chart.Name,
"newAppVersion": entry.AppVersion,
}
monitor.WithFields(logFields).Info("There is a newer version")
}
}
}
return nil
}