Skip to content

Commit

Permalink
report version info
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-codefresh committed Oct 16, 2023
1 parent 5a20eb6 commit 5ad33ef
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 113 deletions.
25 changes: 3 additions & 22 deletions controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/argoproj/argo-cd/v2/controller/metrics"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
"github.com/argoproj/argo-cd/v2/pkg/version_config_manager"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/util/argo"
argodiff "github.com/argoproj/argo-cd/v2/util/argo/diff"
Expand Down Expand Up @@ -107,26 +106,6 @@ type appStateManager struct {
persistResourceHealth bool
}

func getVersionConfig() *apiclient.VersionConfig {
versionConfigManager, err := version_config_manager.NewVersionConfigManager("ConfigMap", "some-product-cm")
if err != nil {
log.Fatalf("Failed to create VersionConfigManager: %v", err)
return nil
}

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

return &apiclient.VersionConfig{
ProductLabel: versionConfig.ProductLabel,
ResourceName: versionConfig.ResourceName,
JsonPath: versionConfig.JsonPath,
}
}

func (m *appStateManager) getRepoObjs(app *v1alpha1.Application, sources []v1alpha1.ApplicationSource, appLabelKey string, revisions []string, noCache, noRevisionCache, verifySignature bool, proj *v1alpha1.AppProject) ([]*unstructured.Unstructured, []*apiclient.ManifestResponse, error) {

ts := stats.NewTimingStats()
Expand Down Expand Up @@ -201,6 +180,8 @@ func (m *appStateManager) getRepoObjs(app *v1alpha1.Application, sources []v1alp

ts.AddCheckpoint("version_ms")
log.Debugf("Generating Manifest for source %s revision %s", source, revisions[i])

// versionConfig :=
manifestInfo, err := repoClient.GenerateManifest(context.Background(), &apiclient.ManifestRequest{
Repo: repo,
Repos: permittedHelmRepos,
Expand All @@ -221,7 +202,7 @@ func (m *appStateManager) getRepoObjs(app *v1alpha1.Application, sources []v1alp
HelmOptions: helmOptions,
HasMultipleSources: app.Spec.HasMultipleSources(),
RefSources: refSources,
VersionConfig: getVersionConfig(),
VersionConfig: apiclient.GetVersionConfig(),
})
if err != nil {
return nil, nil, fmt.Errorf("failed to generate manifest for source %d of %d: %w", i+1, len(sources), err)
Expand Down
364 changes: 304 additions & 60 deletions pkg/apiclient/events/events.pb.go

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions 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"
)

type VersionConfig struct {
Expand Down Expand Up @@ -62,3 +63,19 @@ func NewVersionConfigManager(providerType string, source string) (*VersionConfig
func (v *VersionConfigManager) ObtainConfig() (*VersionConfig, error) {
return v.provider.GetConfig()
}

func GetVersionConfig() *VersionConfig {
versionConfigManager, err := NewVersionConfigManager("ConfigMap", "some-product-cm")
if err != nil {
log.Printf("ERROR: Failed to create VersionConfigManager: %v", err)
return nil
}

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

return versionConfig
}
18 changes: 18 additions & 0 deletions reposerver/apiclient/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package apiclient

import (
"github.com/argoproj/argo-cd/v2/pkg/version_config_manager"
)

func (m *ManifestResponse) GetCompiledManifests() []string {
manifests := make([]string, len(m.Manifests))
for i, m := range m.Manifests {
manifests[i] = m.CompiledManifest
}
return manifests
}

func GetVersionConfig() *VersionConfig {
versionConfig := version_config_manager.GetVersionConfig()

if versionConfig == nil {
return nil
}

return &VersionConfig{
ProductLabel: versionConfig.ProductLabel,
ResourceName: versionConfig.ResourceName,
JsonPath: versionConfig.JsonPath,
}
}
10 changes: 2 additions & 8 deletions reposerver/repository/app_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import (
)

func getValueFromYAMLByJSONPath(appPath, jsonPathExpression string) (*string, error) {
// Чтение файла YAML
content, err := os.ReadFile(appPath)
if err != nil {
return nil, err
}

// Разбор YAML-файла
var obj interface{}
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
}

// Преобразование YAML в Map[interface{}]interface{} для работы jsonpath
// Convert YAML to Map[interface{}]interface{}
jsonObj, err := convertToJSONCompatible(obj)
if err != nil {
return nil, err
}

// Использование jsonpath для получения значения
jp := jsonpath.New("jsonpathParser")
jp.AllowMissingKeys(true)
if err := jp.Parse(jsonPathExpression); err != nil {
Expand Down Expand Up @@ -58,15 +55,12 @@ func convertToJSONCompatible(i interface{}) (interface{}, error) {
}

func getAppVersion(appPath string, resourceName string, jsonPathExpression string) (*string, error) {
// appPath = "example.yaml"
// jsonPathExpression = "{.some.json.path}"

value, err := getValueFromYAMLByJSONPath(appPath+"/"+resourceName, jsonPathExpression)
if err != nil {
return nil, err
}

fmt.Printf("Value: %v\n", *value)
fmt.Printf("appVersion value: %v\n", *value)

return value, nil
}
23 changes: 1 addition & 22 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import (
"github.com/argoproj/argo-cd/v2/util/settings"

applicationType "github.com/argoproj/argo-cd/v2/pkg/apis/application"
"github.com/argoproj/argo-cd/v2/pkg/version_config_manager"
)

type AppResourceTreeFn func(ctx context.Context, app *appv1.Application) (*appv1.ApplicationTree, error)
Expand Down Expand Up @@ -439,26 +438,6 @@ func (s *Server) queryRepoServer(ctx context.Context, a *appv1.Application, acti
return action(client, repo, permittedHelmRepos, permittedHelmCredentials, helmOptions, kustomizeOptions, enabledSourceTypes)
}

func getVersionConfig() *apiclient.VersionConfig {
versionConfigManager, err := version_config_manager.NewVersionConfigManager("ConfigMap", "some-product-cm")
if err != nil {
log.Fatalf("Failed to create VersionConfigManager: %v", err)
return nil
}

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

return &apiclient.VersionConfig{
ProductLabel: versionConfig.ProductLabel,
ResourceName: versionConfig.ResourceName,
JsonPath: versionConfig.JsonPath,
}
}

// GetManifests returns application manifests
func (s *Server) GetManifests(ctx context.Context, q *application.ApplicationManifestQuery) (*apiclient.ManifestResponse, error) {
if q.Name == nil || *q.Name == "" {
Expand Down Expand Up @@ -517,7 +496,7 @@ func (s *Server) GetManifests(ctx context.Context, q *application.ApplicationMan
HelmOptions: helmOptions,
TrackingMethod: string(argoutil.GetTrackingMethod(s.settingsMgr)),
EnabledSourceTypes: enableGenerateManifests,
VersionConfig: getVersionConfig(),
VersionConfig: apiclient.GetVersionConfig(),
})
if err != nil {
return fmt.Errorf("error generating manifests: %w", err)
Expand Down
19 changes: 18 additions & 1 deletion server/application/application_event_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/argoproj/argo-cd/v2/util/argo"
"reflect"
"strings"

"github.com/argoproj/argo-cd/v2/util/argo"

"github.com/argoproj/gitops-engine/pkg/health"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
"github.com/argoproj/gitops-engine/pkg/utils/text"
Expand Down Expand Up @@ -165,6 +166,13 @@ func (s *applicationEventReporter) streamApplicationEvents(
return err
}
} else {
appVersionJSON, err := json.Marshal(desiredManifests.ApplicationVersions)
if err == nil {
if a.Annotations == nil {
a.Annotations = make(map[string]string)
}
a.Annotations["codefresh.io/appVersion"] = string(appVersionJSON)
}
// will get here only for root applications (not managed as a resource by another application)
appEvent, err := s.getApplicationEventPayload(ctx, a, es, ts, appInstanceLabelKey, trackingMethod)
if err != nil {
Expand Down Expand Up @@ -294,6 +302,14 @@ func (s *applicationEventReporter) processResource(

if originalApplication != nil {
originalAppRevisionMetadata, _ = s.getApplicationRevisionDetails(ctx, originalApplication, getOperationRevision(originalApplication))

appVersionJSON, err := json.Marshal(desiredManifests.ApplicationVersions)
if err == nil {
if parentApplicationToReport.Annotations == nil {
parentApplicationToReport.Annotations = make(map[string]string)
}
parentApplicationToReport.Annotations["codefresh.io/appVersion"] = string(appVersionJSON)
}
}

ev, err := getResourceEventPayload(parentApplicationToReport, &rs, es, actualState, desiredState, appTree, manifestGenErr, ts, originalApplication, revisionMetadataToReport, originalAppRevisionMetadata, appInstanceLabelKey, trackingMethod)
Expand Down Expand Up @@ -687,6 +703,7 @@ func (s *applicationEventReporter) getApplicationEventPayload(
Cluster: a.Spec.Destination.Server,
AppInstanceLabelKey: appInstanceLabelKey,
TrackingMethod: string(trackingMethod),
AppVersions: nil,
}

payload := events.EventPayload{
Expand Down
8 changes: 8 additions & 0 deletions server/application/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ message EventPayload {
repeated ObjectError errors = 4;
}

/**
* Application versions
*/
message AppVersions {
optional string appVersion = 1 [(gogoproto.nullable) = false];
}

/**
* Holds information about the object source
*/
Expand Down Expand Up @@ -78,6 +85,7 @@ message ObjectSource {
optional string appNamespace = 21 [(gogoproto.nullable) = false];
optional string appInstanceLabelKey = 22 [(gogoproto.nullable) = false];
optional string trackingMethod = 23 [(gogoproto.nullable) = false];
optional AppVersions appVersions = 24;
}

/**
Expand Down
1 change: 1 addition & 0 deletions util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ func verifyGenerateManifests(
NoRevisionCache: true,
HasMultipleSources: hasMultipleSources,
RefSources: refSources,
VersionConfig: apiclient.GetVersionConfig(),
}
req.Repo.CopyCredentialsFromRepo(repoRes)
req.Repo.CopySettingsFrom(repoRes)
Expand Down

0 comments on commit 5ad33ef

Please sign in to comment.