Skip to content

Commit

Permalink
Merge branch 'release-2.8' into fix/server-side-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pasha-codefresh authored Nov 1, 2023
2 parents 6be2148 + 5173f24 commit 1710612
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 54 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-20837
2.8.1-cap-CR-20837
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.

3 changes: 2 additions & 1 deletion pkg/version_config_manager/version_config_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version_config_manager

import (
"errors"

log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -78,7 +79,7 @@ func GetVersionConfig() *VersionConfig {

versionConfig, err := versionConfigManager.ObtainConfig()
if err != nil {
log.Printf("ERROR: Failed to obtain config: %v", err)
log.Printf("Failed to obtain config: %v", err)
return nil
}

Expand Down
60 changes: 19 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,35 +22,14 @@ 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 {
return nil, err
}

log.Infof("AppVersion source content: %s", string(content))

var obj interface{}
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
Expand All @@ -74,6 +54,7 @@ func getVersionFromYaml(appPath, jsonPathExpression string) (*string, error) {
}

appVersion := buf.String()
log.Infof("AppVersion source content appVersion: %s", appVersion)
return &appVersion, nil
}

Expand All @@ -89,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 @@ -103,30 +95,16 @@ func getAppVersions(appPath string, resourceName string, jsonPathExpression stri
if err != nil {
return nil, err
}
log.Printf("appVersion value: %v\n", *appVersion)
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
}
20 changes: 15 additions & 5 deletions server/application/application_event_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *applicationEventReporter) streamApplicationEvents(
logCtx.WithError(err).Warn("failed to get parent application's revision metadata, resuming")
}

err = s.processResource(ctx, *rs, parentApplicationEntity, logCtx, ts, parentDesiredManifests, stream, appTree, es, manifestGenErr, a, parentRevisionMetadata, true, appInstanceLabelKey, trackingMethod)
err = s.processResource(ctx, *rs, parentApplicationEntity, logCtx, ts, parentDesiredManifests, stream, appTree, es, manifestGenErr, a, parentRevisionMetadata, true, appInstanceLabelKey, trackingMethod, desiredManifests.ApplicationVersions)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *applicationEventReporter) streamApplicationEvents(
continue
}

err := s.processResource(ctx, rs, a, logCtx, ts, desiredManifests, stream, appTree, es, manifestGenErr, nil, revisionMetadata, ignoreResourceCache, appInstanceLabelKey, trackingMethod)
err := s.processResource(ctx, rs, a, logCtx, ts, desiredManifests, stream, appTree, es, manifestGenErr, nil, revisionMetadata, ignoreResourceCache, appInstanceLabelKey, trackingMethod, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -240,6 +240,7 @@ func (s *applicationEventReporter) processResource(
ignoreResourceCache bool,
appInstanceLabelKey string,
trackingMethod appv1.TrackingMethod,
applicationVersions *apiclient.ApplicationVersions,
) error {
logCtx = logCtx.WithFields(log.Fields{
"gvk": fmt.Sprintf("%s/%s/%s", rs.Group, rs.Version, rs.Kind),
Expand Down Expand Up @@ -297,7 +298,7 @@ func (s *applicationEventReporter) processResource(
originalAppRevisionMetadata, _ = s.getApplicationRevisionDetails(ctx, originalApplication, getOperationRevision(originalApplication))
}

ev, err := getResourceEventPayload(parentApplicationToReport, &rs, es, actualState, desiredState, appTree, manifestGenErr, ts, originalApplication, revisionMetadataToReport, originalAppRevisionMetadata, appInstanceLabelKey, trackingMethod, desiredManifests.ApplicationVersions)
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")
return nil
Expand Down Expand Up @@ -471,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 @@ -613,6 +612,8 @@ func getResourceEventPayload(
AppVersions: applicationVersionsEvents,
}

logCtx.Infof("AppVersion before encoding: %v", safeString(payload.AppVersions.AppVersion))

payloadBytes, err := json.Marshal(&payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload for resource %s/%s: %w", rs.Namespace, rs.Name, err)
Expand Down Expand Up @@ -720,6 +721,8 @@ func (s *applicationEventReporter) getApplicationEventPayload(
AppVersions: applicationVersionsEvents,
}

logCtx.Infof("AppVersion before encoding: %v", safeString(payload.AppVersions.AppVersion))

payloadBytes, err := json.Marshal(&payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload for resource %s/%s: %w", a.Namespace, a.Name, err)
Expand Down Expand Up @@ -799,3 +802,10 @@ func repoAppVersionsToEvent(applicationVersions *apiclient.ApplicationVersions)
}
return applicationVersionsEvents, nil
}

func safeString(s *string) string {
if s == nil {
return "<nil>"
}
return *s
}
6 changes: 3 additions & 3 deletions server/application/application_event_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGetResourceEventPayload(t *testing.T) {
Message: "some message",
}

event, err := getResourceEventPayload(&app, &rs, &es, &actualState, &desiredState, &appTree, true, "", nil, &revisionMetadata, nil, common.LabelKeyAppInstance, argo.TrackingMethodLabel, nil)
event, err := getResourceEventPayload(&app, &rs, &es, &actualState, &desiredState, &appTree, true, "", nil, &revisionMetadata, nil, common.LabelKeyAppInstance, argo.TrackingMethodLabel, &apiclient.ApplicationVersions{})
assert.NoError(t, err)

var eventPayload events.EventPayload
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestGetResourceEventPayload(t *testing.T) {
Message: "some message",
}

event, err := getResourceEventPayload(&app, &rs, &es, &actualState, &desiredState, &appTree, true, "", nil, &revisionMetadata, nil, common.LabelKeyAppInstance, argo.TrackingMethodLabel, nil)
event, err := getResourceEventPayload(&app, &rs, &es, &actualState, &desiredState, &appTree, true, "", nil, &revisionMetadata, nil, common.LabelKeyAppInstance, argo.TrackingMethodLabel, &apiclient.ApplicationVersions{})
assert.NoError(t, err)

var eventPayload events.EventPayload
Expand Down Expand Up @@ -375,7 +375,7 @@ func TestGetResourceEventPayloadWithoutRevision(t *testing.T) {
}
appTree := v1alpha1.ApplicationTree{}

_, err := getResourceEventPayload(&app, &rs, &es, &actualState, &desiredState, &appTree, true, "", nil, nil, nil, common.LabelKeyAppInstance, argo.TrackingMethodLabel, nil)
_, err := getResourceEventPayload(&app, &rs, &es, &actualState, &desiredState, &appTree, true, "", nil, nil, nil, common.LabelKeyAppInstance, argo.TrackingMethodLabel, &apiclient.ApplicationVersions{})
assert.NoError(t, err)

}
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 1710612

Please sign in to comment.