github.com/oam-dev/kubevela@v1.9.11/pkg/utils/helm/repo_index.go (about) 1 /* 2 Copyright 2023 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package helm 18 19 import ( 20 "bytes" 21 "context" 22 "fmt" 23 "net/url" 24 "strings" 25 "time" 26 27 "helm.sh/helm/v3/pkg/getter" 28 helmrepo "helm.sh/helm/v3/pkg/repo" 29 "sigs.k8s.io/yaml" 30 ) 31 32 // IndexYaml is the index.yaml of helm repo 33 const IndexYaml = "index.yaml" 34 35 // LoadRepoIndex load helm repo index 36 func LoadRepoIndex(_ context.Context, u string, cred *RepoCredential) (*helmrepo.IndexFile, error) { 37 38 if !strings.HasSuffix(u, "/") { 39 u = fmt.Sprintf("%s/%s", u, IndexYaml) 40 } else { 41 u = fmt.Sprintf("%s%s", u, IndexYaml) 42 } 43 44 resp, err := loadData(u, cred) 45 if err != nil { 46 return nil, err 47 } 48 49 indexFile, err := loadIndex(resp.Bytes()) 50 if err != nil { 51 return nil, err 52 } 53 54 return indexFile, nil 55 } 56 57 func loadData(u string, cred *RepoCredential) (*bytes.Buffer, error) { 58 parsedURL, err := url.Parse(u) 59 if err != nil { 60 return nil, err 61 } 62 var resp *bytes.Buffer 63 64 skipTLS := true 65 if cred.InsecureSkipTLSVerify != nil && !*cred.InsecureSkipTLSVerify { 66 skipTLS = false 67 } 68 69 indexURL := parsedURL.String() 70 // TODO add user-agent 71 g, _ := getter.NewHTTPGetter() 72 resp, err = g.Get(indexURL, 73 getter.WithTimeout(5*time.Minute), 74 getter.WithURL(u), 75 getter.WithInsecureSkipVerifyTLS(skipTLS), 76 getter.WithTLSClientConfig(cred.CertFile, cred.KeyFile, cred.CAFile), 77 getter.WithBasicAuth(cred.Username, cred.Password), 78 ) 79 if err != nil { 80 return nil, err 81 } 82 83 return resp, nil 84 } 85 86 // loadIndex loads an index file and does minimal validity checking. 87 // 88 // This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails. 89 func loadIndex(data []byte) (*helmrepo.IndexFile, error) { 90 i := &helmrepo.IndexFile{} 91 if err := yaml.Unmarshal(data, i); err != nil { 92 return i, err 93 } 94 i.SortEntries() 95 if i.APIVersion == "" { 96 return i, helmrepo.ErrNoAPIVersion 97 } 98 return i, nil 99 }