Skip to content

HMS-5913: add package dates from RHEL lifecycle #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Cache interface {

GetRoadmapAppstreams(ctx context.Context) ([]byte, error)
SetRoadmapAppstreams(ctx context.Context, roadmapAppstreamsResponse []byte)

GetRoadmapRhelLifecycle(ctx context.Context) ([]byte, error)
SetRoadmapRhelLifecycle(ctx context.Context, rhelLifecyleResponse []byte)
}

func Initialize() Cache {
Expand Down
35 changes: 35 additions & 0 deletions pkg/cache/cache_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/cache/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ func (c *noOpCache) GetRoadmapAppstreams(ctx context.Context) ([]byte, error) {
// SetRoadmapAppstreams a NoOp version to store cached roadmap appstreams check
func (c *noOpCache) SetRoadmapAppstreams(ctx context.Context, response []byte) {
}

// GetRoadmapAppstreams a NoOp version to fetch a cached roadmap rhel lifecycle check
Copy link
Preview

Copilot AI May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment for GetRoadmapRhelLifecycle incorrectly refers to 'Appstreams' instead of 'RhelLifecycle'. Please update the comment to accurately describe its purpose.

Suggested change
// GetRoadmapAppstreams a NoOp version to fetch a cached roadmap rhel lifecycle check
// GetRoadmapRhelLifecycle a NoOp version to fetch a cached roadmap rhel lifecycle check

Copilot uses AI. Check for mistakes.

func (c *noOpCache) GetRoadmapRhelLifecycle(ctx context.Context) ([]byte, error) {
return nil, NotFound
}

// SetRoadmapAppstreams a NoOp version to store cached roadmap rhel lifecycle check
Copy link
Preview

Copilot AI May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment for SetRoadmapRhelLifecycle seems to be a copy of the Appstreams comment; please update it to correctly refer to storing the RHEL lifecycle information.

Suggested change
// SetRoadmapAppstreams a NoOp version to store cached roadmap rhel lifecycle check
// SetRoadmapRhelLifecycle a NoOp version to store cached roadmap RHEL lifecycle information

Copilot uses AI. Check for mistakes.

func (c *noOpCache) SetRoadmapRhelLifecycle(ctx context.Context, response []byte) {
}
19 changes: 19 additions & 0 deletions pkg/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func roadmapAppstreamsKey(ctx context.Context) string {
return fmt.Sprintf("roadmap-appstreams:%v", identity.Identity.OrgID)
}

func roadmapRhelLifecycleKey(ctx context.Context) string {
identity := identity.GetIdentity(ctx)
return fmt.Sprintf("roadmap-rhel-lifecycle:%v", identity.Identity.OrgID)
}

// GetAccessList uses the request context to read user information, and then tries to retrieve the role AccessList from the cache
func (c *redisCache) GetAccessList(ctx context.Context) (rbac.AccessList, error) {
accessList := rbac.AccessList{}
Expand Down Expand Up @@ -187,6 +192,20 @@ func (c *redisCache) SetRoadmapAppstreams(ctx context.Context, response []byte)
c.client.Set(ctx, key, string(response), config.Get().Clients.Redis.Expiration.SubscriptionCheck)
}

func (c *redisCache) GetRoadmapRhelLifecycle(ctx context.Context) ([]byte, error) {
key := roadmapRhelLifecycleKey(ctx)
buf, err := c.get(ctx, key)
if err != nil {
return nil, fmt.Errorf("redis get error: %w", err)
}
return buf, nil
}

func (c *redisCache) SetRoadmapRhelLifecycle(ctx context.Context, response []byte) {
key := roadmapRhelLifecycleKey(ctx)
c.client.Set(ctx, key, string(response), config.Get().Clients.Redis.Expiration.SubscriptionCheck)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it probably makes sense to set this per service (for example subscription check should probably once an hour, but the roadmap expiration could be once per day). thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought subscription check is once a day, but I agree it should probably be separate. I'll fix that

}

func (c *redisCache) get(ctx context.Context, key string) ([]byte, error) {
cmd := c.client.Get(ctx, key)
if errors.Is(cmd.Err(), redis.Nil) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/clients/roadmap_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

type RoadmapClient interface {
GetAppstreams(ctx context.Context) (AppstreamsResponse, int, error)
GetRhelLifecycle(ctx context.Context) (LifecycleResponse, int, error)
GetRhelLifecycleForLatestMajorVersions(ctx context.Context) (map[int]LifecycleEntity, error)
}

type roadmapClient struct {
Expand Down
99 changes: 99 additions & 0 deletions pkg/clients/roadmap_client/roadmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/cache"
Expand Down Expand Up @@ -35,6 +36,18 @@ type AppstreamEntity struct {
Impl string `json:"impl"`
}

type LifecycleResponse struct {
Data []LifecycleEntity `json:"data"`
}

type LifecycleEntity struct {
Name string `json:"name"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
Major int `json:"major"`
Minor int `json:"minor"`
}

func encodedIdentity(ctx context.Context) (string, error) {
id := identity.GetIdentity(ctx)
jsonIdentity, err := json.Marshal(id)
Expand Down Expand Up @@ -107,3 +120,89 @@ func (rc roadmapClient) GetAppstreams(ctx context.Context) (AppstreamsResponse,

return appStreamResp, statusCode, nil
}

func (rc roadmapClient) GetRhelLifecycle(ctx context.Context) (LifecycleResponse, int, error) {
statusCode := http.StatusInternalServerError
server := config.Get().Clients.Roadmap.Server
var err error
var lifecycleResponse LifecycleResponse
var body []byte

appstreams, err := rc.cache.GetRoadmapRhelLifecycle(ctx)
if err != nil && !errors.Is(err, cache.NotFound) {
log.Error().Err(err).Msg("GetAppstreams - error reading from cache")
}
if appstreams != nil {
err = json.Unmarshal(appstreams, &lifecycleResponse)
if err != nil {
return LifecycleResponse{}, statusCode, fmt.Errorf("error during unmarshal response body: %w", err)
}
return lifecycleResponse, http.StatusOK, nil
}

fullPath := server + "/lifecycle/rhel"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fullPath, nil)
if err != nil {
return LifecycleResponse{}, statusCode, fmt.Errorf("error building request: %w", err)
}

if config.Get().Clients.Roadmap.Username != "" && config.Get().Clients.Roadmap.Password != "" {
req.SetBasicAuth(config.Get().Clients.Roadmap.Username, config.Get().Clients.Roadmap.Password)
}

encodedXRHID, err := encodedIdentity(ctx)
if err != nil {
return LifecycleResponse{}, statusCode, fmt.Errorf("error getting encoded XRHID: %w", err)
}
req.Header.Set(api.IdentityHeader, encodedXRHID)

resp, err := rc.client.Do(req)
if resp != nil {
defer resp.Body.Close()

body, err = io.ReadAll(resp.Body)
if err != nil {
return LifecycleResponse{}, statusCode, fmt.Errorf("error reading response body: %w", err)
}
if resp.StatusCode != 0 {
statusCode = resp.StatusCode
}
}
if err != nil {
return LifecycleResponse{}, statusCode, fmt.Errorf("error sending request: %w", err)
}
if statusCode < 200 || statusCode >= 300 {
return LifecycleResponse{}, statusCode, fmt.Errorf("unexpected status code with body: %s", string(body))
}

rc.cache.SetRoadmapRhelLifecycle(ctx, body)

err = json.Unmarshal(body, &lifecycleResponse)
if err != nil {
return LifecycleResponse{}, statusCode, fmt.Errorf("error during unmarshal response body: %w", err)
}

return lifecycleResponse, statusCode, nil
}

func (rc roadmapClient) GetRhelLifecycleForLatestMajorVersions(ctx context.Context) (map[int]LifecycleEntity, error) {
lifecycleResp, _, err := rc.GetRhelLifecycle(ctx)
if err != nil {
return nil, err
}

currentDate := time.Now().UTC().Truncate(24 * time.Hour)
rhelEolMap := make(map[int]LifecycleEntity)
for _, item := range lifecycleResp.Data {
startDate, err := time.Parse("2006-01-02", item.StartDate)
if err != nil {
return nil, err
}
if startDate.Before(currentDate) || startDate.Equal(currentDate) {
if existing, found := rhelEolMap[item.Major]; !found || (item.Minor > existing.Minor) {
rhelEolMap[item.Major] = item
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chatted privately, but we actually want to pick the earliest start date and latest end date for a given RHEL major release (9)

}
}
return rhelEolMap, nil
}
65 changes: 65 additions & 0 deletions pkg/clients/roadmap_client/roadmap_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading