-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm.go
169 lines (154 loc) · 4.41 KB
/
helm.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// helm.go
// Copyright (C) 2021 rmelo <Ricardo Melo <[email protected]>>
//
// Distributed under terms of the MIT license.
//
package main
import (
//"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"github.com/pkg/errors"
"go.uber.org/zap"
"helm.sh/helm/v3/cmd/helm/search"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/repo"
)
type releaseElement struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Revision string `json:"revision"`
Updated string `json:"updated"`
Status string `json:"status"`
ChartName string `json:"chart_name"`
ChartVersion string `json:"chart_version"`
AppVersion string `json:"app_version"`
}
type chartVersion struct {
ChartVersion string `json:"chart_version"`
AppVersion string `json:"app_version"`
}
type chartInfo struct {
ChartName string `json:"name"`
Versions []chartVersion `json:"versions"`
}
type releaseListWriter struct {
releases []releaseElement
}
func isNotExist(err error) bool {
return os.IsNotExist(errors.Cause(err))
}
func debug(format string, v ...interface{}) {
if settings.Debug {
format = fmt.Sprintf("[debug] %s\n", format)
log.Output(2, fmt.Sprintf(format, v...))
}
}
func warning(format string, v ...interface{}) {
format = fmt.Sprintf("WARNING: %s\n", format)
fmt.Fprintf(os.Stderr, format, v...)
}
func listCharts(logger *zap.Logger) ([]releaseElement, error) {
cfg := new(action.Configuration)
client := action.NewList(cfg)
if err := cfg.Init(settings.RESTClientGetter(), "", os.Getenv("HELM_DRIVER"), debug); err != nil {
logger.Error("failed to initialize helm configuration",
zap.Error(err),
zap.String("namespace", ""),
zap.String("driver", os.Getenv("HELM_DRIVER")))
return nil, &HelmError{
Op: "init_config",
Err: err,
Details: map[string]interface{}{
"driver": os.Getenv("HELM_DRIVER"),
},
}
}
client.SetStateMask()
results, err := client.Run()
if err != nil {
logger.Error("failed to list releases",
zap.Error(err))
return nil, &HelmError{
Op: "list_releases",
Err: err,
}
}
elements := make([]releaseElement, 0, len(results))
for _, r := range results {
element := releaseElement{
Name: r.Name,
Namespace: r.Namespace,
Revision: strconv.Itoa(r.Version),
Status: r.Info.Status.String(),
ChartName: r.Chart.Metadata.Name,
ChartVersion: r.Chart.Metadata.Version,
AppVersion: r.Chart.Metadata.AppVersion,
}
elements = append(elements, element)
logger.Debug("processed release",
zap.String("name", element.Name),
zap.String("namespace", element.Namespace),
zap.String("version", element.ChartVersion))
}
return elements, nil
}
// func searchChartVersions(chartName string) ([]chartInfo, error) {
func searchChartVersions(logger *zap.Logger, charts []string) ([]map[string][]chartVersion, error) {
logger.Debug("searching chart versions",
zap.Strings("charts", charts))
//Load repo file
rf, err := repo.LoadFile(settings.RepositoryConfig)
if isNotExist(err) || len(rf.Repositories) == 0 {
logger.Error("no repositories configured",
zap.String("config_path", settings.RepositoryConfig))
return nil, &RepositoryError{
Op: "load_repo_file",
Repo: settings.RepositoryConfig,
Err: errors.New("no repositories configured"),
}
}
chartList := make([]map[string][]chartVersion, len(charts))
//chart := make(map[string][]chartVersion)
i := search.NewIndex()
for _, re := range rf.Repositories {
n := re.Name
f := filepath.Join(settings.RepositoryCache, helmpath.CacheIndexFile(n))
ind, err := repo.LoadIndexFile(f)
if err != nil {
logger.Warn("repository is corrupt or missing",
zap.String("repo", re.Name),
zap.String("cache_file", f),
zap.Error(err))
continue
}
//should replace the condition
i.AddRepo(n, ind, true)
}
res := i.All()
//search.SortScore(res)
for i, chartTarget := range charts {
chartList[i] = map[string][]chartVersion{chartTarget: nil}
versions := []chartVersion{}
for _, tmp := range res {
if tmp.Chart.Name != chartTarget {
continue
}
version := chartVersion{
AppVersion: tmp.Chart.AppVersion,
ChartVersion: tmp.Chart.Version,
}
versions = append(versions, version)
}
chartList[i][chartTarget] = versions
logger.Debug("processed chart versions",
zap.String("chart", chartTarget),
zap.Int("version_count", len(versions)))
}
return chartList, nil
}