Skip to content

Commit

Permalink
RHINENG-9505: add call to candlepin into /templates/update
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMraka committed Jun 24, 2024
1 parent 1ce7b3b commit 0505352
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
18 changes: 18 additions & 0 deletions base/candlepin/candlepin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package candlepin

var APIPrefix = "/candlepin"

type EnvironmentsConsumersRequestGuestID struct {
ID string
GuestID string `json:"guestId"`
}

type EnvironmentsConsumersRequest struct {
ID string
UUID string
GuestIds []EnvironmentsConsumersRequestGuestID `json:"guestIds"`
}

type EnvironmentsConsumersResponse struct {
GuestIds []EnvironmentsConsumersRequestGuestID `json:"guestIds"`
}
2 changes: 2 additions & 0 deletions base/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type coreConfig struct {
// services
VmaasAddress string
RbacAddress string
CandlepinAddress string
ManagerPrivateAddress string
ListenerPrivateAddress string
EvaluatorUploadPrivateAddress string
Expand Down Expand Up @@ -154,6 +155,7 @@ func initTopicsFromEnv() {
func initServicesFromEnv() {
CoreCfg.VmaasAddress = Getenv("VMAAS_ADDRESS", CoreCfg.VmaasAddress)
CoreCfg.RbacAddress = Getenv("RBAC_ADDRESS", CoreCfg.RbacAddress)
CoreCfg.CandlepinAddress = Getenv("CANDLEPIN_ADDRESS", CoreCfg.CandlepinAddress)
}

func initDBFromClowder() {
Expand Down
1 change: 1 addition & 0 deletions conf/local.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ DB_SSLMODE=verify-full
DB_SSLROOTCERT=dev/database/secrets/pgca.crt

VMAAS_ADDRESS=http://localhost:9001
CANDLEPIN_ADDRESS=http://localhost:9001

#KAFKA_ADDRESS=localhost:29092
KAFKA_GROUP=patchman
Expand Down
9 changes: 9 additions & 0 deletions manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ var (
// Expose templates API (feature flag)
EnableTemplates = utils.PodConfig.GetBool("templates_api", true)

// Toggle compression when calling Candlepi API
CandlepinCallCmp = utils.PodConfig.GetBool("candlepin_call_compression", true)
// Number of retries on Candlepin API
CandlepinRetries = utils.PodConfig.GetInt("candlepin_retries", 5)
// Toggle exponential retries on Candlepin API
CandlepinExpRetries = utils.PodConfig.GetBool("candlepin_exp_retries", true)
// Full URL to Candlepin's Environment API
CandlepinEnvURL string // will be filled after reading core configuration
// Debug flag for API calls
DebugRequest = log.IsLevelEnabled(log.TraceLevel)
)
3 changes: 1 addition & 2 deletions manager/controllers/template_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func getTemplate(c *gin.Context, tx *gorm.DB, account int, uuid string) (*models
LogAndRespNotFound(c, err, err.Error())
return &template, err
}
err := tx.Model(&models.Template{}).
Where("rh_account_id = ? AND uuid = ?::uuid ", account, uuid).
err := tx.Where("rh_account_id = ? AND uuid = ?::uuid ", account, uuid).
// use Find() not First() otherwise it returns error "no rows found" if uuid is not present
Find(&template).Error
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions manager/controllers/template_systems_update.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package controllers

import (
"app/base"
"app/base/api"
"app/base/candlepin"
"app/base/database"
"app/base/models"
"app/base/utils"
"app/manager/config"
"app/manager/middlewares"
"context"
"fmt"
"net/http"

Expand All @@ -17,6 +21,12 @@ import (
"github.com/gin-gonic/gin"
)

var errCandlepin = errors.New("candlepin error")
var candlepinClient = api.Client{
HTTPClient: &http.Client{Transport: &http.Transport{DisableCompression: !config.CandlepinCallCmp}},
Debug: config.DebugRequest,
}

type TemplateSystemsUpdateRequest struct {
// List of inventory IDs to have templates removed
Systems []string `json:"systems" example:"system1-uuid, system2-uuid, ..."`
Expand Down Expand Up @@ -58,6 +68,11 @@ func TemplateSystemsUpdateHandler(c *gin.Context) {
return
}

err = assignCandlepinEnvironment(c, template.EnvironmentID, req.Systems)
if err != nil {
return
}

// TODO: re-evaluate systems added/removed from templates
// inventoryAIDs := kafka.InventoryIDs2InventoryAIDs(account, req.InventoryIDs)
// kafka.EvaluateBaselineSystems(inventoryAIDs)
Expand Down Expand Up @@ -155,3 +170,45 @@ func templateArchVersionMatch(
}
return err
}

func callCandlepin(ctx context.Context, env string, request *candlepin.EnvironmentsConsumersRequest) (
*candlepin.EnvironmentsConsumersResponse, error) {
candlepinEnvConsumersURL := config.CandlepinEnvURL + env + "/consumers"
candlepinFunc := func() (interface{}, *http.Response, error) {
utils.LogTrace("request", *request, "candlepin /environments/consumers request")
candlepinResp := candlepin.EnvironmentsConsumersResponse{}
resp, err := candlepinClient.Request(&ctx, http.MethodPost, candlepinEnvConsumersURL, request, &candlepinResp)
statusCode := utils.TryGetStatusCode(resp)
utils.LogDebug("status_code", statusCode, "candlepin /environments/consumers call")
utils.LogTrace("response", resp, "candlepin /environments/consumers response")
if err != nil && statusCode == 400 {
err = errors.Wrap(errCandlepin, err.Error())
}
return &candlepinResp, resp, err
}

candlepinRespPtr, err := utils.HTTPCallRetry(base.Context, candlepinFunc, config.CandlepinExpRetries,
config.CandlepinRetries, http.StatusServiceUnavailable)
if err != nil {
return nil, errors.Wrap(err, "candlepin /environments/consumers call failed")
}
return candlepinRespPtr.(*candlepin.EnvironmentsConsumersResponse), nil
}

func assignCandlepinEnvironment(c context.Context, env string, inventoryIDs []string) error {
guests := make([]candlepin.EnvironmentsConsumersRequestGuestID, 0, len(inventoryIDs))
for _, id := range inventoryIDs {
guests = append(guests, candlepin.EnvironmentsConsumersRequestGuestID{
ID: id,
GuestID: id,
})
}
req := candlepin.EnvironmentsConsumersRequest{
ID: env,
UUID: env,
GuestIds: guests,
}
_, err := callCandlepin(c, env, &req)
// check response
return err
}
3 changes: 3 additions & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manager

import (
"app/base"
"app/base/candlepin"
"app/base/core"
"app/base/mqueue"
"app/base/utils"
Expand Down Expand Up @@ -36,6 +37,8 @@ var basepaths = []string{"/api/patch/v3"}
// @BasePath /api/patch/v3
func RunManager() {
core.ConfigureApp()
config.CandlepinEnvURL = utils.FailIfEmpty(utils.CoreCfg.CandlepinAddress, "CANDLEPIN_ADDRESS") +
candlepin.APIPrefix + "/environments/"

port := utils.CoreCfg.PublicPort
utils.LogInfo("port", port, "Manager starting at port")
Expand Down

0 comments on commit 0505352

Please sign in to comment.