-
Notifications
You must be signed in to change notification settings - Fork 34
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
func (c *noOpCache) GetRoadmapRhelLifecycle(ctx context.Context) ([]byte, error) { | ||||||
return nil, NotFound | ||||||
} | ||||||
|
||||||
// SetRoadmapAppstreams a NoOp version to store cached roadmap rhel lifecycle check | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
func (c *noOpCache) SetRoadmapRhelLifecycle(ctx context.Context, response []byte) { | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
|
@@ -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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.
Copilot uses AI. Check for mistakes.