@@ -14,56 +14,71 @@ import (
14
14
"helm.sh/helm/v3/pkg/registry"
15
15
)
16
16
17
- func IsChart (ctx context.Context , chartUri string ) (chart , oci bool , err error ) {
18
- addr , err := url .Parse (chartUri )
17
+ type HelmCheckResponse struct {
18
+ // Chart returns true if helm chart
19
+ Chart bool
20
+ // Oci returns true if resource is stored
21
+ // in an OCI registry
22
+ Oci bool
23
+ }
24
+
25
+ func IsChart (ctx context.Context , chartURI string ) (HelmCheckResponse , error ) {
26
+ addr , err := url .Parse (chartURI )
19
27
if err != nil {
20
- return chart , oci , err
28
+ return HelmCheckResponse {} , err
21
29
}
22
30
23
31
if addr .Scheme != "" {
24
32
if ! strings .HasPrefix (addr .Scheme , "http" ) {
25
- err = fmt .Errorf ("unexpected Url scheme; %s\n " , addr .Scheme )
26
- return
33
+ return HelmCheckResponse {}, fmt .Errorf ("unexpected scheme; %s" , addr .Scheme )
27
34
}
28
35
29
- oci = false
30
36
helmchart , err := validateHelmChart (addr .String ())
31
37
if err != nil {
32
- chart = false
33
- return chart , oci , err
38
+ return HelmCheckResponse {}, err
34
39
}
35
40
36
41
if helmchart != nil &&
37
42
helmchart .Metadata != nil &&
38
43
helmchart .Metadata .Name != "" {
39
- chart = true
44
+ return HelmCheckResponse {
45
+ Chart : true ,
46
+ Oci : false ,
47
+ }, err
40
48
}
41
-
42
- return chart , oci , err
43
49
}
44
50
45
51
ociRe := regexp .MustCompile ("^(?P<host>[a-zA-Z0-9-_.:]+)([/]?)(?P<org>[a-zA-Z0-9-_/]+)?([/](?P<chart>[a-zA-Z0-9-_.:@]+))$" )
46
- if ociRe .MatchString (chartUri ) {
47
- oci = true
52
+ if ! ociRe .MatchString (chartURI ) {
53
+ return HelmCheckResponse {
54
+ Chart : false ,
55
+ Oci : false ,
56
+ }, fmt .Errorf ("does not conform to OCI url format" )
57
+ }
48
58
49
- chart , err = helmOciCheck (ctx , chartUri )
50
- if err != nil {
51
- return chart , oci , err
52
- }
59
+ ociCheck , err := helmOciCheck (ctx , chartURI )
60
+ if err != nil {
61
+ return HelmCheckResponse {
62
+ Chart : false ,
63
+ Oci : true ,
64
+ }, err
53
65
}
54
66
55
- return
67
+ return HelmCheckResponse {
68
+ Chart : ociCheck ,
69
+ Oci : true ,
70
+ }, nil
56
71
}
57
72
58
- // helmOciCheck() pull a helm chart using the provided chartUri from an
73
+ // helmOciCheck() pull a helm chart using the provided chartURI from an
59
74
// OCI registiry and inspects its media type to determine if a Helm chart
60
- func helmOciCheck (ctx context.Context , chartUri string ) (bool , error ) {
75
+ func helmOciCheck (_ context.Context , chartURI string ) (bool , error ) {
61
76
helmclient , err := registry .NewClient ()
62
77
if err != nil {
63
78
return false , err
64
79
}
65
80
66
- summary , err := helmclient .Pull (chartUri ,
81
+ summary , err := helmclient .Pull (chartURI ,
67
82
registry .PullOptWithProv (false ),
68
83
registry .PullOptWithChart (true ),
69
84
registry .PullOptIgnoreMissingProv (true ),
@@ -75,9 +90,14 @@ func helmOciCheck(ctx context.Context, chartUri string) (bool, error) {
75
90
return summary != nil && summary .Ref != "" , nil
76
91
}
77
92
78
- func validateHelmChart (chartUri string ) (* chart.Chart , error ) {
93
+ func validateHelmChart (chartURI string ) (* chart.Chart , error ) {
79
94
// Download helm chart from HTTP
80
- resp , err := http .Get (chartUri )
95
+ req , err := http .NewRequest (http .MethodGet , chartURI , nil )
96
+ if err != nil {
97
+ return nil , fmt .Errorf ("creating request failed; %w" , err )
98
+ }
99
+
100
+ resp , err := http .DefaultClient .Do (req )
81
101
if err != nil {
82
102
return nil , fmt .Errorf ("loading URL failed; %w" , err )
83
103
}
0 commit comments