Skip to content
Merged
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
24 changes: 9 additions & 15 deletions pkg/strategy/openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func init() {

const (
prodHost = "developers.redhat.com"
stagingHost = "developers.qa.redhat.com"
fallbackVersion = "1.4.1"
fallbackVersion = "1.4.2"
)

var versionRegexp = regexp.MustCompile(`/RHTAS/[^/]+/`)
Expand All @@ -42,20 +41,14 @@ func download(ctx context.Context, client controller.Reader, cliName string) (st
if isTarGz(link) {
path, err := downloadTarGz(ctx, cliName, link)
if err != nil && strings.Contains(link, prodHost) {
stagingLink := strings.Replace(link, prodHost, stagingHost, 1)
logrus.Infof("Production download failed, falling back to staging: %s", stagingLink)
path, err = downloadTarGz(ctx, cliName, stagingLink)
if err != nil {
fallbackLink := versionRegexp.ReplaceAllString(link, "/RHTAS/"+fallbackVersion+"/")
logrus.Infof("Staging download failed, falling back to stable %s via CDN: %s", fallbackVersion, fallbackLink)
cdnLink, cdnErr := support.ResolveCDNLink(ctx, fallbackLink)
if cdnErr != nil {
return "", fmt.Errorf("all download attempts failed (prod, staging, CDN fallback): %w", cdnErr)
}
logrus.Infof("Resolved CDN link: %s", cdnLink)
return downloadTarGz(ctx, cliName, cdnLink)
fallbackLink := versionRegexp.ReplaceAllString(link, "/RHTAS/"+fallbackVersion+"/")
logrus.Infof("Download failed, falling back to stable %s via CDN: %s", fallbackVersion, fallbackLink)
cdnLink, cdnErr := support.ResolveCDNLink(ctx, fallbackLink)
if cdnErr != nil {
return "", fmt.Errorf("all download attempts failed (current version, CDN fallback %s): %w", fallbackVersion, cdnErr)
}
return path, nil
logrus.Infof("Resolved CDN link: %s", cdnLink)
return downloadTarGz(ctx, cliName, cdnLink)
}
return path, err
}
Expand All @@ -79,6 +72,7 @@ func downloadTarGz(ctx context.Context, cliName string, link string) (string, er
}

if err = support.DownloadAndUntarArchive(ctx, link, tmp); err != nil {
_ = os.RemoveAll(tmp)
return "", err
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/support/cgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ func ContentGatewayName(name string) string {
// FindBinary searches for a CLI binary in the given directory using candidate name patterns.
func FindBinary(dir, cliName, goos, goarch string) (string, error) {
cgwName := ContentGatewayName(cliName)
candidates := []string{
cliName,
candidates := []string{cliName}
if cgwName != cliName {
candidates = append(candidates, cgwName)
}
candidates = append(candidates,
fmt.Sprintf("%s_%s_%s", cgwName, goos, goarch),
fmt.Sprintf("%s_%s", cgwName, goos),
fmt.Sprintf("%s-%s-%s", cliName, goos, goarch),
}
)
if goos == "windows" {
for i, name := range candidates {
candidates[i] = name + ".exe"
Expand All @@ -38,6 +41,12 @@ func FindBinary(dir, cliName, goos, goarch string) (string, error) {
for _, name := range candidates {
path := filepath.Join(dir, name)
if _, err := os.Stat(path); err == nil {
if name != cliName {
link := filepath.Join(dir, cliName)
if err := os.Symlink(path, link); err == nil {
return link, nil
}
}
return path, nil
}
}
Expand Down
35 changes: 10 additions & 25 deletions pkg/support/testSupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func GitCloneWithAuth(url string, auth transport.AuthMethod) (string, *git.Repos

func DownloadAndUnzip(ctx context.Context, link string, writer io.Writer) error {
pr, pw := io.Pipe()
defer pr.Close()

go func() {
_, err := Download(ctx, link, pw)
Expand All @@ -75,6 +76,7 @@ func DownloadAndUnzip(ctx context.Context, link string, writer io.Writer) error

func DownloadAndUntarArchive(ctx context.Context, link string, dst string) error {
pr, pw := io.Pipe()
defer pr.Close()

go func() {
_, err := Download(ctx, link, pw)
Expand All @@ -83,30 +85,8 @@ func DownloadAndUntarArchive(ctx context.Context, link string, dst string) error
return UntarArchive(dst, pr)
}

const squidProxy = "http://squid.corp.redhat.com:3128"

var stagingProxySuffixes = []string{
".qa.redhat.com",
".dev.redhat.com",
".stage.redhat.com",
".preprod.redhat.com",
}

func proxyForStagingOnly(req *http.Request) (*url.URL, error) {
host := req.URL.Hostname()
for _, suffix := range stagingProxySuffixes {
if strings.HasSuffix(host, suffix) {
return url.Parse(squidProxy)
}
}
return nil, nil
}

func Download(ctx context.Context, link string, writer io.Writer) (int64, error) {
client := &http.Client{
Timeout: 2 * time.Minute, //nolint:mnd
Transport: &http.Transport{Proxy: proxyForStagingOnly},
}
client := &http.Client{Timeout: 2 * time.Minute} //nolint:mnd

const maxRetries = 5
var lastErr error
Expand Down Expand Up @@ -136,8 +116,13 @@ func Download(ctx context.Context, link string, writer io.Writer) (int64, error)
lastErr = fmt.Errorf("bad status: %s", resp.Status)
continue
}
defer resp.Body.Close()
return io.Copy(writer, resp.Body)
n, copyErr := io.Copy(writer, resp.Body)
resp.Body.Close()
if copyErr != nil {
lastErr = copyErr
continue
}
return n, nil
}
return 0, fmt.Errorf("download failed after %d attempts: %w", maxRetries, lastErr)
}
Expand Down
Loading