Skip to content

Commit

Permalink
add/update helpers for aws/pulp content and distribution urls
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Holloway <[email protected]>
  • Loading branch information
loadtheaccumulator authored and lzap committed Dec 16, 2024
1 parent c1be487 commit 0f88a47
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 28 deletions.
32 changes: 23 additions & 9 deletions pkg/models/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package models

import (
"errors"
"net/url"

"github.com/redhatinsights/edge-api/config"
feature "github.com/redhatinsights/edge-api/unleash/features"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
Expand Down Expand Up @@ -59,17 +61,29 @@ type Commit struct {
// Repo is the delivery mechanism of a Commit over HTTP
type Repo struct {
Model
URL string `json:"RepoURL"`
Status string `json:"RepoStatus"`
PulpURL string `json:"pulp_repo_url"`
PulpStatus string `json:"pulp_repo_status"`
URL string `json:"RepoURL"` // AWS repo URL
Status string `json:"RepoStatus"` // AWS repo upload status
PulpURL string `json:"pulp_repo_url"` // Distribution URL returned from Pulp
PulpStatus string `json:"pulp_repo_status"` // Status of Pulp repo import
}

// GetURL is a temporary helper to return the URL of the preferred repo store
//
// this avoids the feature flag everywhere repo.URL is used
// also using GetURL (not URL) to avoid changing the struct used by Gorm
func (r Repo) GetURL() string {
// ContentURL is the URL for internal and Image Builder access to the content in a Pulp repo
func (r Repo) ContentURL() string {
pulpConfig := config.Get().Pulp

if feature.PulpIntegration.IsEnabled() && r.PulpStatus == RepoStatusSuccess {
parsedURL, _ := url.Parse(r.PulpURL)
parsedConfigContentURL, _ := url.Parse(pulpConfig.ContentURL)
parsedURL.Host = parsedConfigContentURL.Host

return parsedURL.String()
}

return r.URL
}

// DistributionURL is the URL for external access to the content in a Pulp repo
func (r Repo) DistributionURL() string {
if feature.PulpIntegration.IsEnabled() && r.PulpStatus == RepoStatusSuccess {
return r.PulpURL
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/models/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
package models

import (
"os"
"testing"

"github.com/bxcodec/faker/v3"
"github.com/redhatinsights/edge-api/config"
"github.com/redhatinsights/edge-api/pkg/db"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -34,3 +36,65 @@ func TestCommitsBeforeCreate(t *testing.T) {
})
}
}

func TestDistributionURL(t *testing.T) {
var awsURL = "https://aws.repo.example.com/repo/is/here"
var pulpURL = "https://pulp.distribution.example.com/api/pulp-content/pulp/repo/is/here"
repo := Repo{
URL: awsURL,
Status: RepoStatusSuccess,
PulpURL: pulpURL,
PulpStatus: RepoStatusSuccess,
}

t.Run("return AWS URL", func(t *testing.T) {
defer config.Cleanup()

os.Unsetenv("FEATURE_PULP_INTEGRATION")
os.Unsetenv("PULP_CONTENT_URL")

assert.Equal(t, awsURL, repo.DistributionURL())
})

t.Run("return pulp distribution url", func(t *testing.T) {
defer config.Cleanup()

os.Setenv("FEATURE_PULP_INTEGRATION", "true")
os.Setenv("PULP_CONTENT_URL", "http://internal.repo.example.com:8080")

assert.Equal(t, pulpURL, repo.DistributionURL())
})
}

func TestContentURL(t *testing.T) {
var awsURL = "https://aws.repo.example.com/repo/is/here"
var pulpURL = "https://pulp.distribution.example.com:3030/api/pulp-content/pulp/repo/is/here"
repo := Repo{
URL: awsURL,
Status: RepoStatusSuccess,
PulpURL: pulpURL,
PulpStatus: RepoStatusSuccess,
}

t.Run("return pulp content url", func(t *testing.T) {
defer config.Cleanup()

os.Setenv("FEATURE_PULP_INTEGRATION", "true")
os.Setenv("PULP_CONTENT_URL", "http://internal.repo.example.com:8080")

var expectedURL = "https://internal.repo.example.com:8080/api/pulp-content/pulp/repo/is/here"

assert.Equal(t, expectedURL, repo.ContentURL())
})

t.Run("return aws content url", func(t *testing.T) {
defer config.Cleanup()

os.Unsetenv("FEATURE_PULP_INTEGRATION")
os.Unsetenv("PULP_CONTENT_URL")

var expectedURL = "https://aws.repo.example.com/repo/is/here"

assert.Equal(t, expectedURL, repo.ContentURL())
})
}
6 changes: 3 additions & 3 deletions pkg/routes/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,17 @@ func ValidateStorageImage(w http.ResponseWriter, r *http.Request) string {
return ""
}

if image.Commit.Repo == nil || image.Commit.Repo.GetURL() == "" {
if image.Commit.Repo == nil || image.Commit.Repo.DistributionURL() == "" {
logger.Error("image repository does not exist")
respondWithAPIError(w, logger, errors.NewNotFound("image repository does not exist"))
return ""
}

RepoURL, err := url2.Parse(image.Commit.Repo.GetURL())
RepoURL, err := url2.Parse(image.Commit.Repo.DistributionURL())
if err != nil {
logger.WithFields(log.Fields{
"error": err.Error(),
"URL": image.Commit.Repo.GetURL(),
"URL": image.Commit.Repo.DistributionURL(),
}).Error("error occurred when parsing repository url")
respondWithAPIError(w, logger, errors.NewBadRequest("bad image repository url"))
return ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func (s *ImageService) UpdateImage(image *models.Image, previousImage *models.Im
err := errors.NewBadRequest(fmt.Sprintf("Commit repo wasn't found in the database: #%v", image.Commit.ID))
return err
}
image.Commit.OSTreeParentCommit = repo.GetURL()
image.Commit.OSTreeParentCommit = repo.ContentURL()
}

if config.DistributionsRefs[previousSuccessfulImage.Distribution] != config.DistributionsRefs[image.Distribution] {
Expand Down
8 changes: 4 additions & 4 deletions pkg/services/repobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ func (rb *RepoBuilder) BuildUpdateRepo(id uint) (*models.UpdateTransaction, erro
if err != nil {
return nil, err
}
update.Repo.URL = updateCommit.Repo.GetURL()
update.Repo.URL = updateCommit.Repo.ContentURL()
rb.log.WithField("update_transaction", update).Info("UPGRADE: point update to commit repo")
}

rb.log.WithField("repo", update.Repo.GetURL()).Info("Update repo URL")
rb.log.WithField("repo", update.Repo.ContentURL()).Info("Update repo URL")
update.Repo.Status = models.RepoStatusSuccess
if err := db.DB.Omit("Devices.*").Save(&update).Error; err != nil {
return nil, err
Expand Down Expand Up @@ -475,8 +475,8 @@ func (rb *RepoBuilder) ImportRepo(r *models.Repo) (*models.Repo, error) {
return nil, fmt.Errorf("error saving status :: %s", result.Error.Error())
}

redactedURL, _ := url.Parse(r.GetURL())
rb.log.WithField("repo_url", redactedURL.Redacted()).Info("Commit stored in AWS OSTree repo")
logURL, _ := url.Parse(r.DistributionURL())
rb.log.WithField("repo_url", logURL.Redacted()).Info("Commit stored in AWS OSTree repo")

return r, nil
}
Expand Down
12 changes: 3 additions & 9 deletions pkg/services/repostore/pulpstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func fileRepoImport(ctx context.Context, pulpService *pulp.PulpService, sourceUR
return artifact, version, nil
}

// Import imports an artifact into a Pulp repo and deletes the tarfile artifact
// ostreeRepoImport imports an artifact into a Pulp ostree repo
func ostreeRepoImport(ctx context.Context, pulpService *pulp.PulpService, pulpHref string, pulpRepoName string,
distBaseURL string, fileRepoArtifact string) (string, error) {

Expand All @@ -213,16 +213,10 @@ func ostreeRepoImport(ctx context.Context, pulpService *pulp.PulpService, pulpHr
}
log.WithContext(ctx).WithField("repo_href", *repoImported.PulpHref).Info("Repository imported")

repoURL, err := distributionURL(ctx, pulpService.Domain(), pulpRepoName)
if err != nil {
log.WithContext(ctx).WithField("error", err.Error()).Error("Error getting distibution URL for Pulp repo")
return "", err
}

parsedURL, _ := url.Parse(repoURL)
parsedURL, _ := url.Parse(distBaseURL)
log.WithContext(ctx).WithFields(log.Fields{
"repo_distribution_url": parsedURL.Redacted(),
}).Debug("Repo import into Pulp complete")

return repoURL, nil
return distBaseURL, nil
}
4 changes: 2 additions & 2 deletions pkg/services/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (s *UpdateService) CreateUpdate(id uint) (*models.UpdateTransaction, error)
// NewTemplateRemoteInfo contains the info for the ostree remote file to be written to the system
func NewTemplateRemoteInfo(update *models.UpdateTransaction) TemplateRemoteInfo {

updateURL := update.Repo.GetURL()
updateURL := update.Repo.DistributionURL()

return TemplateRemoteInfo{
RemoteURL: updateURL,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ func (s *UpdateService) BuildUpdateTransactions(devicesUpdate *models.DevicesUpd
return nil, result.Error
}
s.log.WithFields(log.Fields{
"repoURL": repo.GetURL(),
"repoURL": repo.DistributionURL(),
"repoID": repo.ID,
}).Debug("Getting repo info")
}
Expand Down

0 comments on commit 0f88a47

Please sign in to comment.