Skip to content

Commit

Permalink
adding fix for oci sync
Browse files Browse the repository at this point in the history
  • Loading branch information
iamayushm committed Dec 16, 2024
1 parent 3345dfb commit 04bdd97
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
7 changes: 1 addition & 6 deletions chart-sync/pkg/RepoManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,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
2 changes: 1 addition & 1 deletion chart-sync/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04bdd97

Please sign in to comment.