Skip to content

Commit

Permalink
add helper function for switching between pulp and aws repos (HMS-4121)
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 13, 2024
1 parent 97ee9c9 commit 744405a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
13 changes: 13 additions & 0 deletions pkg/models/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package models
import (
"errors"

feature "github.com/redhatinsights/edge-api/unleash/features"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -64,6 +65,18 @@ type Repo struct {
PulpStatus string `json:"pulp_repo_status"`
}

// 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 {
if feature.PulpIntegration.IsEnabled() && r.PulpStatus == RepoStatusSuccess {
return r.PulpURL
}

return r.URL
}

// Package represents the packages a Commit can have
type Package struct {
Model
Expand Down
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.URL == "" {
if image.Commit.Repo == nil || image.Commit.Repo.GetURL() == "" {
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.URL)
RepoURL, err := url2.Parse(image.Commit.Repo.GetURL())
if err != nil {
logger.WithFields(log.Fields{
"error": err.Error(),
"URL": image.Commit.Repo.URL,
"URL": image.Commit.Repo.GetURL(),
}).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.URL
image.Commit.OSTreeParentCommit = repo.GetURL()
}

if config.DistributionsRefs[previousSuccessfulImage.Distribution] != config.DistributionsRefs[image.Distribution] {
Expand Down
7 changes: 3 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.URL
update.Repo.URL = updateCommit.Repo.GetURL()
rb.log.WithField("update_transaction", update).Info("UPGRADE: point update to commit repo")
}

rb.log.WithField("repo", update.Repo.URL).Info("Update repo URL")
rb.log.WithField("repo", update.Repo.GetURL()).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 @@ -405,7 +405,6 @@ func (rb *RepoBuilder) StoreRepo(ctx context.Context, repo *models.Repo) (*model

// ImportRepo (unpack and upload) a single repo
func (rb *RepoBuilder) ImportRepo(r *models.Repo) (*models.Repo, error) {
// FIXME: delete after Pulp Store Repo is stable
var cmt models.Commit
cmtDB := db.DB.Where("repo_id = ?", r.ID).First(&cmt)
if cmtDB.Error != nil {
Expand Down Expand Up @@ -476,7 +475,7 @@ 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.URL)
redactedURL, _ := url.Parse(r.GetURL())
rb.log.WithField("repo_url", redactedURL.Redacted()).Info("Commit stored in AWS OSTree repo")

return r, nil
Expand Down
7 changes: 2 additions & 5 deletions pkg/services/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +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.URL
if feature.PulpIntegrationUpdateViaPulp.IsEnabled() {
updateURL = update.Repo.PulpURL
}
updateURL := update.Repo.GetURL()

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

0 comments on commit 744405a

Please sign in to comment.