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

Merged
merged 1 commit into from
May 14, 2025
Merged
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
1 change: 1 addition & 0 deletions configs/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ clients:
rbac: 1m
pulp_content_path: 1h
subscription_check: 1h
roadmap: 24h
feature_service:
server: #https://feature.stage.api.redhat.com/features/v1
client_cert:
Expand Down
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) {
}

// GetRoadmapRhelLifecycle a NoOp version to fetch a cached roadmap rhel lifecycle check
func (c *noOpCache) GetRoadmapRhelLifecycle(ctx context.Context) ([]byte, error) {
return nil, NotFound
}

// SetRoadmapRhelLifecycle a NoOp version to store cached roadmap rhel lifecycle check
func (c *noOpCache) SetRoadmapRhelLifecycle(ctx context.Context, response []byte) {
}
21 changes: 20 additions & 1 deletion 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 @@ -184,7 +189,21 @@ func (c *redisCache) GetRoadmapAppstreams(ctx context.Context) ([]byte, error) {

func (c *redisCache) SetRoadmapAppstreams(ctx context.Context, response []byte) {
key := roadmapAppstreamsKey(ctx)
c.client.Set(ctx, key, string(response), config.Get().Clients.Redis.Expiration.SubscriptionCheck)
c.client.Set(ctx, key, string(response), config.Get().Clients.Redis.Expiration.Roadmap)
}

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.Roadmap)
}

func (c *redisCache) get(ctx context.Context, key string) ([]byte, error) {
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
91 changes: 91 additions & 0 deletions pkg/clients/roadmap_client/roadmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 +119,82 @@ 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
}

rhelEolMap := make(map[int]LifecycleEntity)
for _, item := range lifecycleResp.Data {
if existing, found := rhelEolMap[item.Major]; !found || (item.Minor > existing.Minor) {
rhelEolMap[item.Major] = item
}
}
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.

2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type Expiration struct {
Rbac time.Duration `mapstructure:"rbac"`
PulpContentPath time.Duration `mapstructure:"pulp_content_path"`
SubscriptionCheck time.Duration `mapstructure:"subscription_check"`
Roadmap time.Duration `mapstructure:"roadmap"`
}

type Sentry struct {
Expand Down Expand Up @@ -323,6 +324,7 @@ func setDefaults(v *viper.Viper) {
v.SetDefault("clients.redis.expiration.rbac", 1*time.Minute)
v.SetDefault("clients.redis.expiration.pulp_content_path", 1*time.Hour)
v.SetDefault("clients.redis.expiration.subscription_check", 1*time.Hour)
v.SetDefault("clients.redis.expiration.roadmap", 24*time.Hour)

v.SetDefault("clients.feature_service.server", "")
v.SetDefault("clients.feature_service.client_cert", "")
Expand Down
Loading
Loading