@@ -14,56 +14,71 @@ import (
1414 "helm.sh/helm/v3/pkg/registry"
1515)
1616
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 )
1927 if err != nil {
20- return chart , oci , err
28+ return HelmCheckResponse {} , err
2129 }
2230
2331 if addr .Scheme != "" {
2432 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 )
2734 }
2835
29- oci = false
3036 helmchart , err := validateHelmChart (addr .String ())
3137 if err != nil {
32- chart = false
33- return chart , oci , err
38+ return HelmCheckResponse {}, err
3439 }
3540
3641 if helmchart != nil &&
3742 helmchart .Metadata != nil &&
3843 helmchart .Metadata .Name != "" {
39- chart = true
44+ return HelmCheckResponse {
45+ Chart : true ,
46+ Oci : false ,
47+ }, err
4048 }
41-
42- return chart , oci , err
4349 }
4450
4551 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+ }
4858
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
5365 }
5466
55- return
67+ return HelmCheckResponse {
68+ Chart : ociCheck ,
69+ Oci : true ,
70+ }, nil
5671}
5772
58- // helmOciCheck() pull a helm chart using the provided chartUri from an
73+ // helmOciCheck() pull a helm chart using the provided chartURI from an
5974// 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 ) {
6176 helmclient , err := registry .NewClient ()
6277 if err != nil {
6378 return false , err
6479 }
6580
66- summary , err := helmclient .Pull (chartUri ,
81+ summary , err := helmclient .Pull (chartURI ,
6782 registry .PullOptWithProv (false ),
6883 registry .PullOptWithChart (true ),
6984 registry .PullOptIgnoreMissingProv (true ),
@@ -75,9 +90,14 @@ func helmOciCheck(ctx context.Context, chartUri string) (bool, error) {
7590 return summary != nil && summary .Ref != "" , nil
7691}
7792
78- func validateHelmChart (chartUri string ) (* chart.Chart , error ) {
93+ func validateHelmChart (chartURI string ) (* chart.Chart , error ) {
7994 // 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 )
81101 if err != nil {
82102 return nil , fmt .Errorf ("loading URL failed; %w" , err )
83103 }
0 commit comments