Skip to content

Commit

Permalink
Update the JoinUrl function to handle urls without schema (vmware-tan…
Browse files Browse the repository at this point in the history
…zu#364) (vmware-tanzu#366)

(cherry picked from commit f1580b0)
  • Loading branch information
mpanchajanya committed Jun 20, 2023
1 parent c9851d2 commit 7d577b6
Show file tree
Hide file tree
Showing 3 changed files with 641 additions and 117 deletions.
17 changes: 14 additions & 3 deletions pkg/airgapped/plugin_bundle_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error {
// Iterate through all the images and publish them to the remote repository
for _, ic := range manifest.ImagesToCopy {
imageTar := filepath.Join(pluginBundleDir, ic.SourceTarFilePath)
repoImagePath := utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath)
repoImagePath, err := utils.JoinURL(o.DestinationRepo, ic.RelativeImagePath)
if err != nil {
return errors.Wrap(err, "error while constructing the repo image path")
}
log.Infof("---------------------------")
log.Infof("uploading image %q", repoImagePath)
err = o.ImageProcessor.CopyImageFromTar(imageTar, repoImagePath)
Expand All @@ -72,7 +75,10 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error {
// Publish plugin inventory metadata image after merging inventory metadata
log.Infof("publishing plugin inventory metadata image...")
bundledPluginInventoryMetadataDBFilePath := filepath.Join(pluginBundleDir, manifest.InventoryMetadataImage.SourceFilePath)
pluginInventoryMetadataImageWithTag := utils.JoinURL(o.DestinationRepo, manifest.InventoryMetadataImage.RelativeImagePathWithTag)
pluginInventoryMetadataImageWithTag, err := utils.JoinURL(o.DestinationRepo, manifest.InventoryMetadataImage.RelativeImagePathWithTag)
if err != nil {
return errors.Wrap(err, "error while constructing the plugin inventory metadata image with tag")
}
err = o.mergePluginInventoryMetadata(pluginInventoryMetadataImageWithTag, bundledPluginInventoryMetadataDBFilePath, tempDir)
if err != nil {
return errors.Wrap(err, "error while merging the plugin inventory metadata database before uploading metadata image")
Expand All @@ -85,7 +91,12 @@ func (o *UploadPluginBundleOptions) UploadPluginBundle() error {
}

log.Infof("---------------------------")
log.Infof("successfully published all plugin images to %q", utils.JoinURL(o.DestinationRepo, manifest.RelativeInventoryImagePathWithTag))

joinedURL, err := utils.JoinURL(o.DestinationRepo, manifest.RelativeInventoryImagePathWithTag)
if err != nil {
return errors.Wrap(err, "error while constructing the image URL")
}
log.Infof("successfully published all plugin images to %q", joinedURL)

return nil
}
Expand Down
40 changes: 22 additions & 18 deletions pkg/utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package utils

import (
"errors"
"net/url"
"path"
"strings"
)

Expand All @@ -20,30 +20,34 @@ import (
// Returns:
// A string that is the concatenation of baseURL and relativeURL, formatted correctly.
// An error if there was a problem parsing the base URL.
func JoinURL(baseURL, relativeURL string) string {
func JoinURL(baseURL, relativeURL string) (string, error) {
if baseURL == "" {
return "", errors.New("base url is empty")
}

var schemaNotPresent bool

// Check if the URL has a schema
if !strings.HasPrefix(baseURL, "http://") && !strings.HasPrefix(baseURL, "https://") {
schemaNotPresent = true
// If not, prepend "https://"
baseURL = "https://" + baseURL
}

// Parse the base URL into a *url.URL object
parsedBaseURL, err := url.Parse(baseURL)
if err != nil {
// If the base URL is not a valid URL, return empty string
return ""
return "", err
}

// Remove leading slash from relativeURL if it's there
relativeURL = strings.TrimPrefix(relativeURL, "/")

// Join the base URL path and the relative URL.
// path.Join() takes care of removing or adding any necessary slashes.
parsedBaseURL.Path = path.Join(parsedBaseURL.Path, relativeURL)

// If the Opaque field of the URL isn't empty, the URL path should be joined differently. This is because the net/url package in Go treats a specified port number as an opaque component, thus modifying the way the URL is formatted.
// 1. scheme:opaque?query#fragment
// 2. scheme://userinfo@host/path?query#fragment
// If u.Opaque is non-empty, String uses the first form; otherwise it uses the second form.
// Join the baseURL and relativeURL
parsedBaseURL = parsedBaseURL.JoinPath(relativeURL)

if parsedBaseURL.Opaque != "" {
return path.Join(parsedBaseURL.String(), parsedBaseURL.Path)
if schemaNotPresent {
// Replace the schema prefix with the original baseUrl and return the joined URL
return strings.TrimPrefix(parsedBaseURL.String(), "https://"), nil
}

// Return the joined URL as a string
return parsedBaseURL.String()
return parsedBaseURL.String(), nil
}
Loading

0 comments on commit 7d577b6

Please sign in to comment.