Skip to content
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

feat: use live state for application version #341

Merged
merged 13 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 event_reporter/application/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (c *httpApplicationClient) GetManifests(ctx context.Context, in *appclient.
params := fmt.Sprintf("?appNamespace=%s&project=%s",
*in.AppNamespace,
*in.Project)
if in.Revision != nil {
params = fmt.Sprintf("%s&revision=%s", params, *in.Revision)
}
url := fmt.Sprintf("%s/api/v1/applications/%s/manifests%s", c.baseUrl, *in.Name, params)

manifest := &repoapiclient.ManifestResponse{}
Expand Down
44 changes: 30 additions & 14 deletions event_reporter/reporter/application_event_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ func (s *applicationEventReporter) shouldSendResourceEvent(a *appv1.Application,
return true
}

func (r *applicationEventReporter) getDesiredManifests(ctx context.Context, a *appv1.Application, logCtx *log.Entry) (*apiclient.ManifestResponse, error, bool) {
func (r *applicationEventReporter) getDesiredManifests(ctx context.Context, a *appv1.Application, logCtx *log.Entry) (*apiclient.ManifestResponse, bool) {
// get the desired state manifests of the application
project := a.Spec.GetProject()
desiredManifests, err := r.applicationServiceClient.GetManifests(ctx, &application.ApplicationManifestQuery{
Name: &a.Name,
AppNamespace: &a.Namespace,
Revision: &a.Status.Sync.Revision,
andrii-codefresh marked this conversation as resolved.
Show resolved Hide resolved
Project: &project,
})
if err != nil {
Expand All @@ -102,9 +101,26 @@ func (r *applicationEventReporter) getDesiredManifests(ctx context.Context, a *a
// each resource with empty desired state
logCtx.WithError(err).Warn("failed to get application desired state manifests, reporting actual state only")
desiredManifests = &apiclient.ManifestResponse{Manifests: []*apiclient.Manifest{}}
return desiredManifests, nil, true // will ignore requiresPruning=true to not delete resources with actual state
return desiredManifests, true // will ignore requiresPruning=true to not delete resources with actual state
}
return desiredManifests, nil, false
return desiredManifests, false
}

func (r *applicationEventReporter) getLiveManifests(ctx context.Context, a *appv1.Application, logCtx *log.Entry) *apiclient.ManifestResponse {
andrii-codefresh marked this conversation as resolved.
Show resolved Hide resolved
// get the live state manifests of the application
project := a.Spec.GetProject()
liveManifests, err := r.applicationServiceClient.GetManifests(ctx, &application.ApplicationManifestQuery{
Name: &a.Name,
AppNamespace: &a.Namespace,
Revision: &a.Status.Sync.Revision,
Project: &project,
})
if err != nil {
// if it's manifest generation error we need to return empty result
logCtx.WithError(err).Warn("failed to get application desired state manifests, reporting actual state only")
liveManifests = &apiclient.ManifestResponse{Manifests: []*apiclient.Manifest{}}
}
return liveManifests
}

func (s *applicationEventReporter) StreamApplicationEvents(
Expand Down Expand Up @@ -132,15 +148,18 @@ func (s *applicationEventReporter) StreamApplicationEvents(
}

// we still need process app even without tree, it is in case of app yaml originally contain error,
// we still want to show it the erorrs that related to it on codefresh ui
// we still want to show it the errors that related to it on codefresh ui
logCtx.WithError(err).Warn("failed to get application tree, resuming")
}

logCtx.Info("getting desired manifests")

desiredManifests, err, manifestGenErr := s.getDesiredManifests(ctx, a, logCtx)
if err != nil {
return err
desiredManifests, manifestGenErr := s.getDesiredManifests(ctx, a, logCtx)

var applicationVersions *apiclient.ApplicationVersions = nil
liveManifests := s.getLiveManifests(ctx, a, logCtx)
if liveManifests.ApplicationVersions != nil {
andrii-codefresh marked this conversation as resolved.
Show resolved Hide resolved
applicationVersions = liveManifests.ApplicationVersions
}

logCtx.Info("getting parent application name")
Expand All @@ -159,10 +178,7 @@ func (s *applicationEventReporter) StreamApplicationEvents(

rs := utils.GetAppAsResource(a)

parentDesiredManifests, err, manifestGenErr := s.getDesiredManifests(ctx, parentApplicationEntity, logCtx)
if err != nil {
logCtx.WithError(err).Warn("failed to get parent application's desired manifests, resuming")
}
parentDesiredManifests, manifestGenErr := s.getDesiredManifests(ctx, parentApplicationEntity, logCtx)

// helm app hasnt revision
// TODO: add check if it helm application
Expand All @@ -172,7 +188,7 @@ func (s *applicationEventReporter) StreamApplicationEvents(
}

utils.SetHealthStatusIfMissing(rs)
err = s.processResource(ctx, *rs, parentApplicationEntity, logCtx, ts, parentDesiredManifests, appTree, manifestGenErr, a, parentAppSyncRevisionsMetadata, appInstanceLabelKey, trackingMethod, desiredManifests.ApplicationVersions)
err = s.processResource(ctx, *rs, parentApplicationEntity, logCtx, ts, parentDesiredManifests, appTree, manifestGenErr, a, parentAppSyncRevisionsMetadata, appInstanceLabelKey, trackingMethod, applicationVersions)
if err != nil {
s.metricsServer.IncErroredEventsCounter(metrics.MetricChildAppEventType, metrics.MetricEventUnknownErrorType, a.Name)
return err
Expand All @@ -182,7 +198,7 @@ func (s *applicationEventReporter) StreamApplicationEvents(
} else {
logCtx.Info("processing as root application")
// will get here only for root applications (not managed as a resource by another application)
appEvent, err := s.getApplicationEventPayload(ctx, a, appTree, ts, appInstanceLabelKey, trackingMethod, desiredManifests.ApplicationVersions)
appEvent, err := s.getApplicationEventPayload(ctx, a, appTree, ts, appInstanceLabelKey, trackingMethod, applicationVersions)
if err != nil {
s.metricsServer.IncErroredEventsCounter(metrics.MetricParentAppEventType, metrics.MetricEventGetPayloadErrorType, a.Name)
return fmt.Errorf("failed to get application event: %w", err)
Expand Down
12 changes: 10 additions & 2 deletions reposerver/repository/app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ func getVersionFromFile(appPath, jsonPathExpression string) (*string, error) {
if err != nil {
return nil, err
}
appVersion, ok := versionValue.(string)
if !ok {

var appVersion string
var conversionSuccess bool
if versionArray, ok := versionValue.([]interface{}); ok && len(versionArray) > 0 {
andrii-codefresh marked this conversation as resolved.
Show resolved Hide resolved
appVersion, conversionSuccess = versionArray[0].(string)
} else {
appVersion, conversionSuccess = versionValue.(string)
}

if !conversionSuccess {
if versionValue == nil {
log.Info("Version value is not a string. Got: nil")
} else {
Expand Down
Loading