Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix chart download #83

Open
wants to merge 1 commit into
base: hotfix-v0.22.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions chart-sync/pkg/RepoManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ func (impl *HelmRepoManagerImpl) ValuesJson(repoUrl string, version *repo.ChartV
}

var byteBuffer *bytes.Buffer
if len(username) > 0 && len(password) > 0 {
byteBuffer, err = util.GetFromPrivateUrlWithRetry(repoUrl, absoluteChartURL, username, password, allowInsecureConnection)
} else {
byteBuffer, err = util.GetFromPublicUrlWithRetry(absoluteChartURL)
}

byteBuffer, err = util.GetFromUrlWithRetry(repoUrl, absoluteChartURL, username, password, allowInsecureConnection)
if err != nil {
fmt.Println("err", err)
return "", "", "", "", err
Expand Down
59 changes: 17 additions & 42 deletions chart-sync/util/HttpUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,47 @@ package util

import (
"bytes"
"fmt"
"github.com/devtron-labs/chart-sync/internals/sql"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)

func GetFromPublicUrlWithRetry(url string) (*bytes.Buffer, error) {
var (
err error
response *http.Response
retries = 3
)

for retries > 0 {
response, err = http.Get(url)
if err != nil {
retries -= 1
time.Sleep(1 * time.Second)
} else {
break
}
}
if response != nil {
defer response.Body.Close()
statusCode := response.StatusCode
if statusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("Error in getting content from url - %s. Status code : %s", url, strconv.Itoa(statusCode)))
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return bytes.NewBuffer(body), nil
}
return nil, err
}

func GetFromPrivateUrlWithRetry(baseurl string, absoluteUrl string, username string, password string, allowInsecureConnection bool) (*bytes.Buffer, error) {
func GetFromUrlWithRetry(baseurl string, absoluteUrl string, username string, password string, allowInsecureConnection bool) (*bytes.Buffer, error) {
var (
err, errInGetUrl error
response *bytes.Buffer
retries = 3
)
getters := getter.All(&cli.EnvSettings{})
u, err := url.Parse(baseurl)
u, err := url.Parse(absoluteUrl)
if err != nil {
return nil, errors.Errorf("invalid chart URL format: %s", baseurl)
}

client, err := getters.ByScheme(u.Scheme)

if err != nil {
return nil, errors.Errorf("could not find protocol handler for: %s", u.Scheme)
}

for retries > 0 {
response, errInGetUrl = client.Get(absoluteUrl,
getter.WithURL(baseurl),
getter.WithInsecureSkipVerifyTLS(allowInsecureConnection),
getter.WithBasicAuth(username, password),
)

var options []getter.Option

if allowInsecureConnection {
options = append(options, getter.WithInsecureSkipVerifyTLS(allowInsecureConnection))
}
if len(username) > 0 && len(password) > 0 {
options = append(options, getter.WithBasicAuth(username, password))
}
if len(baseurl) > 0 {
options = append(options, getter.WithURL(baseurl))
}

response, errInGetUrl = client.Get(absoluteUrl, options...)

if errInGetUrl != nil {
retries -= 1
Expand Down