Skip to content

Commit

Permalink
APIGOV-29069 - additional check to re-initialize agent resource if no…
Browse files Browse the repository at this point in the history
…t set (#845)

* APIGOV-29069 - extra logging around status update

* APIGOV-29069 - additional check to reintialize agent resource if not set

* APIGOV-29069 - fix to reinitialize agent resource
  • Loading branch information
vivekschauhan authored Oct 25, 2024
1 parent 09c97ff commit 21e255a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ func UpdateStatusWithContext(ctx context.Context, status, prevStatus, descriptio
if err != nil {
logger.WithError(err).Warnf("could not update the agent status reference")
}
} else {
logger.WithField("status", agent.status).Trace("skipping status update, agent resource manager is not initialized")
}
}

Expand Down
24 changes: 20 additions & 4 deletions pkg/agent/resource/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (a *agentResourceManager) SetRebuildCacheFunc(rebuildCache EventSyncCache)
// FetchAgentResource - Gets the agent resource using API call to apiserver
func (a *agentResourceManager) FetchAgentResource() error {
if a.cfg.GetAgentName() == "" {
a.logger.Trace("skipping to fetch agent resource, agent name not configured")
return nil
}

Expand All @@ -130,15 +131,28 @@ func (a *agentResourceManager) FetchAgentResource() error {
// UpdateAgentStatus - Updates the agent status in agent resource
func (a *agentResourceManager) UpdateAgentStatus(status, prevStatus, message string) error {
if a.cfg == nil || a.cfg.GetAgentName() == "" {
a.logger.WithField("status", status).
WithField("previous-status", prevStatus).
Trace("skipping agent status update, agent name config not set")
return nil
}

agentInstance := a.getAgentResourceType()
if a.agentResource == nil && agentInstance != nil {
a.logger.Info("re-initializing agent resource")
err := a.FetchAgentResource()
if err != nil {
return err
}
}

if a.agentResource == nil {
a.logger.WithField("status", status).
WithField("previous-status", prevStatus).
Trace("skipping agent status update, agent resource not initialized")
return nil
}

agentInstance := a.getAgentResourceType()

statusSubResourceName := management.DiscoveryAgentStatusSubResourceName
// using discovery agent status here, but all agent status resources have the same structure
agentInstance.SubResources[statusSubResourceName] = management.DiscoveryAgentStatus{
Expand Down Expand Up @@ -183,17 +197,19 @@ func (a *agentResourceManager) shouldRebuildCache() (bool, error) {
} else {
value, exists := agentDetails.(map[string]interface{})["cacheUpdateTime"]
if value != nil {
logger := a.logger.WithField("cacheUpdateTime", value)
// get current cacheUpdateTime from x-agent-details
convToTimestamp, err := strconv.ParseInt(value.(string), 10, 64)
if err != nil {
logger.WithError(err).Error("unable to parse cache update time")
return false, err
}
currentCacheUpdateTime := time.Unix(0, convToTimestamp)
a.logger.Tracef("the current scheduled refresh cache date - %s", time.Unix(0, currentCacheUpdateTime.UnixNano()).Format("2006-01-02 15:04:05.000000"))
logger.Trace("the current scheduled refresh cache date - %s", time.Unix(0, currentCacheUpdateTime.UnixNano()).Format("2006-01-02 15:04:05.000000"))

// check to see if 7 days have passed since last refresh cache. currentCacheUpdateTime is the date at the time we rebuilt cache plus 7 days(in event sync - RebuildCache)
if a.getCurrentTime() > currentCacheUpdateTime.UnixNano() {
a.logger.Trace("the current date is greater than the current scheduled refresh date - time to rebuild cache")
logger.Trace("the current date is greater than the current scheduled refresh date - time to rebuild cache")
rebuildCache = true
}
} else {
Expand Down

0 comments on commit 21e255a

Please sign in to comment.