Skip to content

Commit

Permalink
feat: appVersions (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-codefresh authored Oct 30, 2023
1 parent f809bd1 commit 5173f24
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 86 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.8.1-cap-CR-app-version7
2.8.1-cap-CR-app-versions
2 changes: 1 addition & 1 deletion pkg/apiclient/events/events.pb.go

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

57 changes: 16 additions & 41 deletions reposerver/repository/app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"os"
"path/filepath"
"reflect"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -21,29 +22,6 @@ type Result struct {
Dependencies DependenciesMap `json:"dependencies"`
}

func getDependenciesFromChart(appPath string) (*string, error) {
content, err := os.ReadFile(appPath)
if err != nil {
return nil, err
}

var obj map[interface{}]interface{}
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
}

var dependenciesStr string
if obj["dependencies"] != nil {
dependencies, err := yaml.Marshal(&map[interface{}]interface{}{"dependencies": obj["dependencies"]})
if err != nil {
return nil, err
}
dependenciesStr = string(dependencies)
}

return &dependenciesStr, nil
}

func getVersionFromYaml(appPath, jsonPathExpression string) (*string, error) {
content, err := os.ReadFile(appPath)
if err != nil {
Expand Down Expand Up @@ -92,6 +70,17 @@ func convertToJSONCompatible(i interface{}) (interface{}, error) {
return obj, nil
}

func readFileContent(result *Result, appPath, fileName, fieldName string) {
content, err := os.ReadFile(filepath.Join(appPath, fileName))
if err == nil && content != nil {
v := reflect.ValueOf(result).Elem()
f := v.FieldByName("Dependencies").FieldByName(fieldName)
if f.IsValid() && f.CanSet() && f.Kind() == reflect.String {
f.SetString(string(content))
}
}
}

func getAppVersions(appPath string, resourceName string, jsonPathExpression string) (*Result, error) {
// Defaults
if resourceName == "" {
Expand All @@ -106,30 +95,16 @@ func getAppVersions(appPath string, resourceName string, jsonPathExpression stri
if err != nil {
return nil, err
}
log.Printf("appVersion value: %v (appPath=%s)", *appVersion, appPath)
log.Infof("appVersion value: %v (appPath=%s)", *appVersion, appPath)

result := &Result{
AppVersion: *appVersion,
Dependencies: DependenciesMap{},
}

// Get Chart.lock if exists
lock, err := os.ReadFile(filepath.Join(appPath, "Chart.lock"))
if err == nil && lock != nil {
result.Dependencies.Lock = string(lock)
}

// Get `dependencies` property from Chart.yaml if exists
deps, err := getDependenciesFromChart(filepath.Join(appPath, resourceName))
if err == nil && deps != nil {
result.Dependencies.Deps = *deps
}

// Get requirements.yaml if exists
requirements, err := os.ReadFile(filepath.Join(appPath, "requirements.yaml"))
if err == nil && requirements != nil {
result.Dependencies.Requirements = string(requirements)
}
readFileContent(result, appPath, "Chart.lock", "Lock")
readFileContent(result, appPath, "Chart.yaml", "Deps")
readFileContent(result, appPath, "requirements.yaml", "Requirements")

return result, nil
}
5 changes: 0 additions & 5 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,6 @@ func (s *Service) GenerateManifest(ctx context.Context, q *apiclient.ManifestReq
var err error
cacheFn := func(cacheKey string, refSourceCommitSHAs cache.ResolvedRevisions, firstInvocation bool) (bool, error) {
ok, resp, err := s.getManifestCacheEntry(cacheKey, q, refSourceCommitSHAs, firstInvocation)
if resp != nil {
log.Infof("appVersion from cache. App = %s, Version = %v", q.AppName, resp.ApplicationVersions)
}
res = resp
return ok, err
}
Expand Down Expand Up @@ -838,7 +835,6 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,
// Update the cache to include failure information
innerRes.NumberOfConsecutiveFailures++
innerRes.MostRecentError = err.Error()
log.Infof("Save App %s to cache 1. Versions: %v", q.AppName, innerRes.ManifestResponse.ApplicationVersions)
cacheErr = s.cache.SetManifests(cacheKey, appSourceCopy, q.RefSources, q, q.Namespace, q.TrackingMethod, q.AppLabelKey, q.AppName, innerRes, refSourceCommitSHAs)
if cacheErr != nil {
log.Warnf("manifest cache set error %s: %v", appSourceCopy.String(), cacheErr)
Expand All @@ -863,7 +859,6 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,
}
manifestGenResult.Revision = commitSHA
manifestGenResult.VerifyResult = opContext.verificationResult
log.Infof("Save App %s to cache 2. Versions: %v", q.AppName, manifestGenCacheEntry.ManifestResponse.ApplicationVersions)
err = s.cache.SetManifests(cacheKey, appSourceCopy, q.RefSources, q, q.Namespace, q.TrackingMethod, q.AppLabelKey, q.AppName, &manifestGenCacheEntry, refSourceCommitSHAs)
if err != nil {
log.Warnf("manifest cache set error %s/%s: %v", appSourceCopy.String(), cacheKey, err)
Expand Down
40 changes: 4 additions & 36 deletions server/application/application_event_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ func (s *applicationEventReporter) streamApplicationEvents(
}
} else {
// will get here only for root applications (not managed as a resource by another application)
log.Infof("getApplicationEventPayload 1 appVersions = %v", desiredManifests.ApplicationVersions)
log.Infof("getApplicationEventPayload 2 desiredManifests = %v", desiredManifests)
log.Infof("getApplicationEventPayload 3 application = %v", a)
appEvent, err := s.getApplicationEventPayload(ctx, a, es, ts, appInstanceLabelKey, trackingMethod, desiredManifests.ApplicationVersions)
if err != nil {
return fmt.Errorf("failed to get application event: %w", err)
Expand All @@ -180,7 +177,7 @@ func (s *applicationEventReporter) streamApplicationEvents(
return nil
}

logWithAppStatus(a, logCtx, ts).Infof("sending root application event %v", appEvent)
logWithAppStatus(a, logCtx, ts).Info("sending root application event")
if err := stream.Send(appEvent); err != nil {
return fmt.Errorf("failed to send event for root application %s/%s: %w", a.Namespace, a.Name, err)
}
Expand Down Expand Up @@ -301,13 +298,6 @@ func (s *applicationEventReporter) processResource(
originalAppRevisionMetadata, _ = s.getApplicationRevisionDetails(ctx, originalApplication, getOperationRevision(originalApplication))
}

log.Infof("getResourceEventPayload app = %s, appVersions = %v", parentApplicationToReport.Name, desiredManifests.ApplicationVersions)
log.Infof("getResourceEventPayload 1, parentApplicationToReport = %v", parentApplicationToReport)
log.Infof("getResourceEventPayload 2, actualState = %v", actualState)
log.Infof("getResourceEventPayload 3, desiredState = %v", desiredState)
log.Infof("getResourceEventPayload 4, originalApplication = %v", originalApplication)
log.Infof("getResourceEventPayload 5, revisionMetadataToReport = %v", revisionMetadataToReport)
log.Infof("getResourceEventPayload 6, originalAppRevisionMetadata = %v", originalAppRevisionMetadata)
ev, err := getResourceEventPayload(parentApplicationToReport, &rs, es, actualState, desiredState, appTree, manifestGenErr, ts, originalApplication, revisionMetadataToReport, originalAppRevisionMetadata, appInstanceLabelKey, trackingMethod, applicationVersions)
if err != nil {
logCtx.WithError(err).Warn("failed to get event payload, resuming")
Expand All @@ -316,7 +306,7 @@ func (s *applicationEventReporter) processResource(

appRes := appv1.Application{}
if isApp(rs) && actualState.Manifest != nil && json.Unmarshal([]byte(*actualState.Manifest), &appRes) == nil {
logWithAppStatus(&appRes, logCtx, ts).Infof("streaming resource event %v", ev)
logWithAppStatus(&appRes, logCtx, ts).Info("streaming resource event")
} else {
logWithResourceStatus(logCtx, rs).Info("streaming resource event")
}
Expand Down Expand Up @@ -482,8 +472,6 @@ func getResourceEventPayload(

if originalApplication != nil {
logCtx = log.WithField("application", originalApplication.Name)
} else if parentApplication != nil {
logCtx = log.WithField("application", parentApplication.Name)
} else {
logCtx = log.NewEntry(log.StandardLogger())
}
Expand Down Expand Up @@ -624,17 +612,7 @@ func getResourceEventPayload(
AppVersions: applicationVersionsEvents,
}

logCtx.Infof("AppVersion before encoding 1: %v", safeString(payload.AppVersions.AppVersion))
if payload.AppVersions.Dependencies == nil {
logCtx.Infof("AppVersion deps before encoding. Dependencies == nil")
} else {
logCtx.Infof(
"AppVersion deps before encoding: %v ||| %v ||| %v",
safeString(payload.AppVersions.Dependencies.Lock),
safeString(payload.AppVersions.Dependencies.Deps),
safeString(payload.AppVersions.Dependencies.Requirements),
)
}
logCtx.Infof("AppVersion before encoding: %v", safeString(payload.AppVersions.AppVersion))

payloadBytes, err := json.Marshal(&payload)
if err != nil {
Expand Down Expand Up @@ -743,17 +721,7 @@ func (s *applicationEventReporter) getApplicationEventPayload(
AppVersions: applicationVersionsEvents,
}

logCtx.Infof("AppVersion before encoding 2: %v", safeString(payload.AppVersions.AppVersion))
if payload.AppVersions.Dependencies == nil {
logCtx.Infof("AppVersion deps before encoding. Dependencies == nil")
} else {
logCtx.Infof(
"AppVersion deps before encoding: %v ||| %v ||| %v",
safeString(payload.AppVersions.Dependencies.Lock),
safeString(payload.AppVersions.Dependencies.Deps),
safeString(payload.AppVersions.Dependencies.Requirements),
)
}
logCtx.Infof("AppVersion before encoding: %v", safeString(payload.AppVersions.AppVersion))

payloadBytes, err := json.Marshal(&payload)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/application/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ message ObjectError {
optional string level = 2 [(gogoproto.nullable) = false];
optional string message = 3 [(gogoproto.nullable) = false];
optional k8s.io.apimachinery.pkg.apis.meta.v1.Time lastSeen = 4 [(gogoproto.nullable) = false];
}
}

message Dependencies {
// Content of Chart.lock
optional string lock = 1;
// Content of Cart.yaml/dependencies
// Content of Chart.yaml
optional string deps = 2;
// Content of requirements.yaml
optional string requirements = 3;
Expand Down

0 comments on commit 5173f24

Please sign in to comment.