-
Notifications
You must be signed in to change notification settings - Fork 15
/
registry.go
136 lines (110 loc) · 2.93 KB
/
registry.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
package registry
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/chartmuseum/helm-push/pkg/chartmuseum"
"github.com/chartmuseum/helm-push/pkg/helm"
"github.com/go-logr/logr"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/cli"
)
const (
defaultTargetDir = "/etc/orkestra/charts/pull/"
)
var (
errEmptyKey = errors.New("key cannot be an empty string")
errEmptyRegistries = errors.New("registries map cannot be nil or empty")
errRegistryNotFound = errors.New("registry entry not found in registries map")
)
// RegistryMap specifies a type alias for the registry configuration by repo key
type RegistryMap map[string]*Config //nolint:golint
func (rm RegistryMap) RegistryConfig(key string) (*Config, error) {
if key == "" {
return nil, errEmptyKey
}
if rm == nil || len(rm) == 0 {
return nil, errEmptyRegistries
}
v, ok := rm[key]
if !ok {
return nil, fmt.Errorf("registry with key %s not found : %w", key, errRegistryNotFound)
}
return v, nil
}
type helmActionConfig struct {
pull *action.Pull
push *chartmuseum.Client
}
type Client struct {
l logr.InfoLogger
// cfg stores the helm pull and push configurations
cfg helmActionConfig
// TargetDir is the location where the downloaded chart is saved
TargetDir string
registries RegistryMap
}
// NewClient is the constructor for the registry client
func NewClient(l logr.InfoLogger, registries map[string]*Config, opts ...Option) (*Client, error) {
cm, err := chartmuseum.NewClient()
if err != nil {
return nil, err
}
c := &Client{
l: l,
TargetDir: defaultTargetDir,
cfg: helmActionConfig{
pull: action.NewPull(),
push: cm,
},
registries: registries,
}
for _, opt := range opts {
opt(c)
}
err = c.init()
if err != nil {
return nil, err
}
return c, nil
}
func (c *Client) init() error {
// If no TargetDir option was passed, set to default location
if c.TargetDir == "" {
c.TargetDir = defaultTargetDir
}
// Set destination directory where we download the chart
c.cfg.pull.DestDir = c.TargetDir
// Initialize the pull and push clients
// Pull client config
actionCfg := new(action.Configuration)
settings := cli.New()
helmDriver := "memory"
if err := actionCfg.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, c.l.Info); err != nil {
return fmt.Errorf("unable to initialize action configuration: %w", err)
}
c.cfg.pull.Settings = settings
// Push Client
// no init required
return nil
}
func chartURL(repo, repoPath, chart, version string) string {
if repoPath != "" {
chart = strings.Trim(repoPath, "/") + "/" + chart
}
s := fmt.Sprintf("%s/%s-%s.tgz",
strings.Trim(repo, "/"),
strings.Trim(chart, "/"),
version,
)
// Validate the URL
if u, err := url.ParseRequestURI(s); u != nil || err != nil {
return s
}
return ""
}
func SaveChartPackage(ch *chart.Chart, dir string) (string, error) {
return helm.CreateChartPackage(&helm.Chart{V3: ch}, dir)
}