forked from opskumu/helm-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistries.go
More file actions
124 lines (103 loc) · 3.22 KB
/
registries.go
File metadata and controls
124 lines (103 loc) · 3.22 KB
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
package main
import (
"fmt"
"os"
"strings"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
)
type RegistryConfig struct {
Host string
Username string
Password string
CertFile string
KeyFile string
CAFile string
}
func repoEntryToRegistryConfig(repoEntry *repo.Entry) (*RegistryConfig, error) {
if repoEntry.URL == "" || !strings.HasPrefix(repoEntry.URL, "oci://") {
return nil, fmt.Errorf("Invalid OCI registry URL: %s", repoEntry.URL)
}
hostParts := strings.Split(repoEntry.URL, "oci://")
if len(hostParts) != 2 {
return nil, fmt.Errorf("Invalid OCI registry URL: %s", repoEntry.URL)
}
registryConfig := RegistryConfig{
Host: hostParts[1],
Username: repoEntry.Username,
Password: repoEntry.Password,
CertFile: repoEntry.CertFile,
KeyFile: repoEntry.KeyFile,
CAFile: repoEntry.CAFile,
}
return ®istryConfig, nil
}
func chartPathOptionsToRegistryConfig(aimChart *string, chartPathOptions *action.ChartPathOptions) (*RegistryConfig, error) {
if !strings.HasPrefix(*aimChart, "oci://") {
return nil, fmt.Errorf("Invalid OCI chart url: %s", aimChart)
}
chartUrlParts := strings.Split(*aimChart, "oci://")
if len(chartUrlParts) != 2 {
return nil, fmt.Errorf("Invalid OCI chart url: %s", aimChart)
}
hostParts := strings.Split(chartUrlParts[1], "/")
if len(hostParts) < 2 {
return nil, fmt.Errorf("Invalid OCI chart url: %s", aimChart)
}
registryConfig := RegistryConfig{
Host: hostParts[0],
Username: chartPathOptions.Username,
Password: chartPathOptions.Password,
CertFile: chartPathOptions.CertFile,
KeyFile: chartPathOptions.KeyFile,
CAFile: chartPathOptions.CaFile,
}
return ®istryConfig, nil
}
func createOCIRegistryClient(registryConfig *RegistryConfig) (*registry.Client, error) {
opts := []registry.ClientOption{
registry.ClientOptDebug(true),
registry.ClientOptEnableCache(true),
registry.ClientOptWriter(os.Stdout),
registry.ClientOptCredentialsFile(settings.RegistryConfig),
}
registryClient, err := registry.NewClient(opts...)
if err != nil {
return nil, fmt.Errorf("Failed to create registry client: %s", err)
}
if registryConfig.Username != "" && registryConfig.Password != "" {
registryClient.Login(
registryConfig.Host,
registry.LoginOptBasicAuth(
registryConfig.Username,
registryConfig.Password,
),
registry.LoginOptInsecure(false),
registry.LoginOptTLSClientConfig(
registryConfig.CertFile,
registryConfig.KeyFile,
registryConfig.CAFile,
),
)
}
return registryClient, nil
}
func initRegistry(c *repo.Entry) (error) {
registryConfig, err := repoEntryToRegistryConfig(c)
if err != nil {
return fmt.Errorf("Failed to convert repo entry to registry config: %s", err)
}
_, err = createOCIRegistryClient(registryConfig)
return err
}
func createOCIRegistryClientForChartPathOptions(aimChart *string, chartPathOptions *action.ChartPathOptions) (*registry.Client, error) {
if strings.HasPrefix(*aimChart, "oci://") {
registryConfig, err := chartPathOptionsToRegistryConfig(aimChart, chartPathOptions)
if err != nil {
return nil, fmt.Errorf("Failed to convert chart path options to registry config: %s", err)
}
return createOCIRegistryClient(registryConfig)
}
return nil, nil
}