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
/
fetch.go
129 lines (112 loc) · 3.4 KB
/
fetch.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
package fetch
import (
"github.com/caos/orbos/internal/operator/boom/application"
"github.com/caos/orbos/internal/operator/boom/bundle/bundles"
"github.com/caos/orbos/internal/operator/boom/templator/helm/chart"
"github.com/caos/orbos/internal/operator/boom/templator/helm/helmcommand"
"github.com/caos/orbos/mntr"
)
type ChartKey struct {
Name string
Version string
}
type ChartInfo struct {
Name string
Version string
IndexName string
}
func All(monitor mntr.Monitor, basePath string, newVersions bool) error {
allApps := bundles.GetAll()
monitor.Info("Init Helm")
// helm init to create a HELMHOME
if err := helmcommand.Init(basePath); err != nil {
return err
}
//indexes in a map so that no doublicates exist
indexes := make(map[*ChartKey]*chart.Index, 0)
charts := make([]*ChartInfo, 0)
monitor.Info("Preparing lists of indexes and charts")
for _, appName := range allApps {
app := application.New(monitor, appName, "")
temp, ok := app.(application.HelmApplication)
// if application doenst implement helm interface then no charts are defined
if ok {
// get chartinfo from application
chart := temp.GetChartInfo()
// when no index defined then it the helm stable repository
var indexName string
if chart.Index != nil {
indexName = chart.Index.Name
indexes[&ChartKey{Name: chart.Name, Version: chart.Version}] = chart.Index
} else {
indexName = "stable"
}
// only add chart if chart is not used by another application, no doublicates
var found bool
found = false
for _, checkChart := range charts {
if checkChart.Name == chart.Name && checkChart.Version == chart.Version && checkChart.IndexName == indexName {
found = true
}
}
if !found {
charts = append(charts, &ChartInfo{Name: chart.Name, Version: chart.Version, IndexName: indexName})
}
} else {
logFields := map[string]interface{}{
"application": appName.String(),
}
monitor.WithFields(logFields).Info("Not helm templated")
}
}
monitor.Info("Adding all indexes")
// add all indexes in a map so that no dublicates exist
for _, v := range indexes {
if err := addIndex(monitor, basePath, v); err != nil {
return err
}
}
monitor.Info("Repo update")
if err := helmcommand.RepoUpdate(basePath); err != nil {
return err
}
if newVersions {
monitor.Info("Checking newer chart versions")
if err := CompareVersions(monitor, basePath, charts); err != nil {
return err
}
}
monitor.Info("Fetching all charts")
for _, chart := range charts {
if err := fetch(monitor, basePath, chart); err != nil {
return err
}
}
return nil
}
func fetch(monitor mntr.Monitor, basePath string, chart *ChartInfo) error {
logFields := map[string]interface{}{
"application": chart.Name,
"version": chart.Version,
}
logFields["indexname"] = chart.IndexName
monitor.WithFields(logFields).Info("Fetching chart")
return helmcommand.FetchChart(&helmcommand.FetchConfig{
TempFolderPath: basePath,
ChartName: chart.Name,
ChartVersion: chart.Version,
IndexName: chart.IndexName,
})
}
func addIndex(monitor mntr.Monitor, basePath string, index *chart.Index) error {
logFields := map[string]interface{}{
"index": index.Name,
"url": index.URL,
}
monitor.WithFields(logFields).Info("Adding index")
return helmcommand.AddIndex(&helmcommand.IndexConfig{
TempFolderPath: basePath,
IndexName: index.Name,
IndexURL: index.URL,
})
}