Skip to content

Commit

Permalink
version config
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-codefresh committed Jan 29, 2024
1 parent 46cf8f8 commit 1c5e5b7
Show file tree
Hide file tree
Showing 17 changed files with 646 additions and 410 deletions.
9 changes: 9 additions & 0 deletions cmd/argocd-repo-server/commands/argocd_repo_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/pkg/codefresh"
"github.com/argoproj/argo-cd/v2/reposerver"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/reposerver/askpass"
Expand Down Expand Up @@ -67,6 +68,8 @@ func NewCommand() *cobra.Command {
streamedManifestMaxExtractedSize string
helmManifestMaxExtractedSize string
disableManifestMaxExtractedSize bool
codefreshUrl string
codefreshToken string
)
var command = cobra.Command{
Use: cliName,
Expand Down Expand Up @@ -123,6 +126,10 @@ func NewCommand() *cobra.Command {
StreamedManifestMaxExtractedSize: streamedManifestMaxExtractedSizeQuantity.ToDec().Value(),
StreamedManifestMaxTarSize: streamedManifestMaxTarSizeQuantity.ToDec().Value(),
HelmManifestMaxExtractedSize: helmManifestMaxExtractedSizeQuantity.ToDec().Value(),
CodefreshConfig: codefresh.CodefreshConfig{
BaseURL: codefreshUrl,
AuthToken: codefreshToken,
},
}, askPassServer)
errors.CheckError(err)

Expand Down Expand Up @@ -203,6 +210,8 @@ func NewCommand() *cobra.Command {
command.Flags().BoolVar(&allowOutOfBoundsSymlinks, "allow-oob-symlinks", env.ParseBoolFromEnv("ARGOCD_REPO_SERVER_ALLOW_OUT_OF_BOUNDS_SYMLINKS", false), "Allow out-of-bounds symlinks in repositories (not recommended)")
command.Flags().StringVar(&streamedManifestMaxTarSize, "streamed-manifest-max-tar-size", env.StringFromEnv("ARGOCD_REPO_SERVER_STREAMED_MANIFEST_MAX_TAR_SIZE", "100M"), "Maximum size of streamed manifest archives")
command.Flags().StringVar(&streamedManifestMaxExtractedSize, "streamed-manifest-max-extracted-size", env.StringFromEnv("ARGOCD_REPO_SERVER_STREAMED_MANIFEST_MAX_EXTRACTED_SIZE", "1G"), "Maximum size of streamed manifest archives when extracted")
command.Flags().StringVar(&codefreshUrl, "codefresh-url", env.StringFromEnv("ARGOCD_REPO_SERVER_CODEFRESH_URL", "https://g.codefresh.io"), "Codefresh API URL")
command.Flags().StringVar(&codefreshToken, "codefresh-token", env.StringFromEnv("ARGOCD_REPO_SERVER_CODEFRESH_TOKEN", ""), "Codefresh token")
command.Flags().StringVar(&helmManifestMaxExtractedSize, "helm-manifest-max-extracted-size", env.StringFromEnv("ARGOCD_REPO_SERVER_HELM_MANIFEST_MAX_EXTRACTED_SIZE", "1G"), "Maximum size of helm manifest archives when extracted")
command.Flags().BoolVar(&disableManifestMaxExtractedSize, "disable-helm-manifest-max-extracted-size", env.ParseBoolFromEnv("ARGOCD_REPO_SERVER_DISABLE_HELM_MANIFEST_MAX_EXTRACTED_SIZE", false), "Disable maximum size of helm manifest archives when extracted")
tlsConfigCustomizerSrc = tls.AddTLSFlagsToCmd(&command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/argoproj/argo-cd/v2/event_reporter"
appclient "github.com/argoproj/argo-cd/v2/event_reporter/application"
"github.com/argoproj/argo-cd/v2/event_reporter/codefresh"
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/pkg/codefresh"

"github.com/argoproj/pkg/stats"
"github.com/redis/go-redis/v9"
Expand Down
10 changes: 8 additions & 2 deletions controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
v1 "k8s.io/api/core/v1"
"reflect"
"strings"
"time"

v1 "k8s.io/api/core/v1"

"github.com/argoproj/gitops-engine/pkg/diff"
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/argoproj/gitops-engine/pkg/sync"
Expand Down Expand Up @@ -207,7 +208,12 @@ func (m *appStateManager) getRepoObjs(app *v1alpha1.Application, sources []v1alp
RefSources: refSources,
ProjectName: proj.Name,
ProjectSourceRepos: proj.Spec.SourceRepos,
VersionConfig: apiclient.GetVersionConfig(),
ApplicationIdentity: &apiclient.ApplicationIdentity{
Runtime: "runtime",
Cluster: "cluster",
Namespace: app.Spec.Destination.Namespace,
Name: app.InstanceName(m.namespace),
},
})
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
78 changes: 0 additions & 78 deletions event_reporter/codefresh/client.go

This file was deleted.

2 changes: 1 addition & 1 deletion event_reporter/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"time"

argocommon "github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/event_reporter/codefresh"
"github.com/argoproj/argo-cd/v2/event_reporter/metrics"
"github.com/argoproj/argo-cd/v2/event_reporter/reporter"
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/pkg/codefresh"
servercache "github.com/argoproj/argo-cd/v2/server/cache"
argoutil "github.com/argoproj/argo-cd/v2/util/argo"
"github.com/argoproj/argo-cd/v2/util/env"
Expand Down
4 changes: 2 additions & 2 deletions event_reporter/reporter/application_event_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"time"

argocommon "github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/event_reporter/codefresh"
"github.com/argoproj/argo-cd/v2/event_reporter/metrics"
applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/pkg/codefresh"
servercache "github.com/argoproj/argo-cd/v2/server/cache"
"github.com/argoproj/argo-cd/v2/util/env"

Expand Down Expand Up @@ -46,7 +46,7 @@ type AppIdentity struct {

type applicationEventReporter struct {
cache *servercache.Cache
codefreshClient codefresh.CodefreshClient
codefreshClient *codefresh.CodefreshClient
appLister applisters.ApplicationLister
applicationServiceClient appclient.ApplicationClient
metricsServer *metrics.MetricsServer
Expand Down
2 changes: 1 addition & 1 deletion event_reporter/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"time"

"github.com/argoproj/argo-cd/v2/common"
codefresh "github.com/argoproj/argo-cd/v2/event_reporter/codefresh"
event_reporter "github.com/argoproj/argo-cd/v2/event_reporter/controller"
"github.com/argoproj/argo-cd/v2/event_reporter/handlers"
"github.com/argoproj/argo-cd/v2/event_reporter/metrics"
appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
appinformer "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions"
applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
codefresh "github.com/argoproj/argo-cd/v2/pkg/codefresh"
repoapiclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient"
servercache "github.com/argoproj/argo-cd/v2/server/cache"
"github.com/argoproj/argo-cd/v2/server/rbacpolicy"
Expand Down
168 changes: 168 additions & 0 deletions pkg/codefresh/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package codefresh

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/events"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

type CodefreshConfig struct {
BaseURL string
AuthToken string
}

type CodefreshClient struct {
cfConfig *CodefreshConfig
httpClient *http.Client
}

type CodefreshClientInterface interface {
Send(ctx context.Context, appName string, event *events.Event) error
GetApplicationConfiguration(app ApplicationIdentity) (*ApplicationConfiguration, error)
}

// VersionSource structure for the versionSource field
type VersionSource struct {
File string `json:"file"`
JsonPath string `json:"jsonPath"`
}

type ApplicationIdentity struct {
Runtime string
Cluster string
Namespace string
Name string
}

// ApplicationConfiguration structure for GraphQL response
type ApplicationConfiguration struct {
VersionSource VersionSource `json:"versionSource"`
}

// GraphQLQuery structure to form a GraphQL query
type GraphQLQuery struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}

func NewCodefreshClient(cfConfig *CodefreshConfig) *CodefreshClient {
return &CodefreshClient{
cfConfig: cfConfig,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}

func (cc *CodefreshClient) Send(ctx context.Context, appName string, event *events.Event) error {
return WithRetry(&DefaultBackoff, func() error {
url := cc.cfConfig.BaseURL + "/2.0/api/events"
log.Infof("Sending application event for %s", appName)

wrappedPayload := map[string]json.RawMessage{
"data": event.Payload,
}

newPayloadBytes, err := json.Marshal(wrappedPayload)
if err != nil {
return err
}

req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(newPayloadBytes))
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", cc.cfConfig.AuthToken)

res, err := cc.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed reporting to Codefresh, event: %s", string(event.Payload)))
}
defer res.Body.Close()

isStatusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !isStatusOK {
b, _ := io.ReadAll(res.Body)
return errors.Errorf("failed reporting to Codefresh, got response: status code %d and body %s, original request body: %s",
res.StatusCode, string(b), string(event.Payload))
}

log.Infof("Application event for %s successfully sent", appName)
return nil
})
}

// GetApplicationConfiguration method to get application configuration
func (client *CodefreshClient) GetApplicationConfiguration(app *ApplicationIdentity) (*ApplicationConfiguration, error) {
query := GraphQLQuery{
Query: `
query ($runtime: String!, $cluster: String!, $namespace: String!, $name: String!) {
applicationConfiguration(runtime: $runtime, cluster: $cluster, namespace: $namespace, name: $name) {
versionSource {
file
jsonPath
}
}
}
`,
Variables: map[string]interface{}{
"runtime": app.Runtime,
"cluster": app.Cluster,
"namespace": app.Namespace,
"name": app.Name,
},
}

responseData, err := sendGraphQLRequest(*client.cfConfig, query)
if err != nil {
return nil, err
}

return responseData.(*ApplicationConfiguration), nil
}

// sendGraphQLRequest function to send the GraphQL request and handle the response
func sendGraphQLRequest(config CodefreshConfig, query GraphQLQuery) (interface{}, error) {
queryJSON, err := json.Marshal(query)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", config.BaseURL+"/2.0/api/graphql", bytes.NewBuffer(queryJSON))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", config.AuthToken)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var responseStruct struct {
Data interface{} `json:"data"`
}
if err := json.Unmarshal(body, &responseStruct); err != nil {
return nil, err
}

return &responseStruct.Data, nil
}
File renamed without changes.
Loading

0 comments on commit 1c5e5b7

Please sign in to comment.