Skip to content

Commit be8d97d

Browse files
feat: Add GitHub API metrics (#404)
* added github api metrics Signed-off-by: reggie-k <regina.voloshin@codefresh.io> * fix(docs): fix applicationsetcontroller.enable.github.api.metrics to false in docs cm (argoproj#23516) Signed-off-by: reggie-k <regina.voloshin@codefresh.io> * fix: Account for batch event processing in e2e tests (argoproj#22356) Signed-off-by: Andrii Korotkov <andrii.korotkov@verkada.com> --------- Signed-off-by: reggie-k <regina.voloshin@codefresh.io> Signed-off-by: Andrii Korotkov <andrii.korotkov@verkada.com> Co-authored-by: Andrii Korotkov <137232734+andrii-korotkov-verkada@users.noreply.github.com>
1 parent 21a8655 commit be8d97d

60 files changed

Lines changed: 911 additions & 102 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ start-e2e-local: mod-vendor-local dep-ui-local cli-local
487487
ARGOCD_APPLICATIONSET_CONTROLLER_ALLOWED_SCM_PROVIDERS=http://127.0.0.1:8341,http://127.0.0.1:8342,http://127.0.0.1:8343,http://127.0.0.1:8344 \
488488
ARGOCD_E2E_TEST=true \
489489
ARGOCD_HYDRATOR_ENABLED=true \
490+
ARGOCD_CLUSTER_CACHE_EVENTS_PROCESSING_INTERVAL=1ms \
490491
goreman -f $(ARGOCD_PROCFILE) start ${ARGOCD_START}
491492
ls -lrt /tmp/coverage
492493

applicationset/controllers/requeue_after_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestRequeueAfter(t *testing.T) {
5656
},
5757
}
5858
fakeDynClient := dynfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), gvrToListKind, duckType)
59-
scmConfig := generators.NewSCMConfig("", []string{""}, true, nil, true)
59+
scmConfig := generators.NewSCMConfig("", []string{""}, true, true, nil, true)
6060
terminalGenerators := map[string]generators.Generator{
6161
"List": generators.NewListGenerator(),
6262
"Clusters": generators.NewClusterGenerator(ctx, k8sClient, appClientset, "argocd"),

applicationset/generators/pull_request.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net/http"
78
"strconv"
89
"time"
910

1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112

1213
"github.com/gosimple/slug"
1314

15+
"github.com/argoproj/argo-cd/v3/applicationset/services"
1416
pullrequest "github.com/argoproj/argo-cd/v3/applicationset/services/pull_request"
1517
"github.com/argoproj/argo-cd/v3/applicationset/utils"
1618
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
@@ -213,12 +215,27 @@ func (g *PullRequestGenerator) selectServiceProvider(ctx context.Context, genera
213215
}
214216

215217
func (g *PullRequestGenerator) github(ctx context.Context, cfg *argoprojiov1alpha1.PullRequestGeneratorGithub, applicationSetInfo *argoprojiov1alpha1.ApplicationSet) (pullrequest.PullRequestService, error) {
218+
var metricsCtx *services.MetricsContext
219+
var httpClient *http.Client
220+
221+
if g.enableGitHubAPIMetrics {
222+
metricsCtx = &services.MetricsContext{
223+
AppSetNamespace: applicationSetInfo.Namespace,
224+
AppSetName: applicationSetInfo.Name,
225+
}
226+
httpClient = services.NewGitHubMetricsClient(metricsCtx)
227+
}
228+
216229
// use an app if it was configured
217230
if cfg.AppSecretName != "" {
218231
auth, err := g.GitHubApps.GetAuthSecret(ctx, cfg.AppSecretName)
219232
if err != nil {
220233
return nil, fmt.Errorf("error getting GitHub App secret: %w", err)
221234
}
235+
236+
if g.enableGitHubAPIMetrics {
237+
return pullrequest.NewGithubAppService(*auth, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels, httpClient)
238+
}
222239
return pullrequest.NewGithubAppService(*auth, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels)
223240
}
224241

@@ -227,5 +244,9 @@ func (g *PullRequestGenerator) github(ctx context.Context, cfg *argoprojiov1alph
227244
if err != nil {
228245
return nil, fmt.Errorf("error fetching Secret token: %w", err)
229246
}
247+
248+
if g.enableGitHubAPIMetrics {
249+
return pullrequest.NewGithubService(token, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels, httpClient)
250+
}
230251
return pullrequest.NewGithubService(token, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels)
231252
}

applicationset/generators/pull_request_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func TestAllowedSCMProviderPullRequest(t *testing.T) {
325325
"gitea.myorg.com",
326326
"bitbucket.myorg.com",
327327
"azuredevops.myorg.com",
328-
}, true, nil, true))
328+
}, true, true, nil, true))
329329

330330
applicationSetInfo := argoprojiov1alpha1.ApplicationSet{
331331
ObjectMeta: metav1.ObjectMeta{
@@ -348,7 +348,7 @@ func TestAllowedSCMProviderPullRequest(t *testing.T) {
348348
}
349349

350350
func TestSCMProviderDisabled_PRGenerator(t *testing.T) {
351-
generator := NewPullRequestGenerator(nil, NewSCMConfig("", []string{}, false, nil, true))
351+
generator := NewPullRequestGenerator(nil, NewSCMConfig("", []string{}, false, true, nil, true))
352352

353353
applicationSetInfo := argoprojiov1alpha1.ApplicationSet{
354354
ObjectMeta: metav1.ObjectMeta{

applicationset/generators/scm_provider.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net/http"
78
"strings"
89
"time"
910

1011
"sigs.k8s.io/controller-runtime/pkg/client"
1112

1213
log "github.com/sirupsen/logrus"
1314

15+
"github.com/argoproj/argo-cd/v3/applicationset/services"
1416
"github.com/argoproj/argo-cd/v3/applicationset/services/github_app_auth"
1517
"github.com/argoproj/argo-cd/v3/applicationset/services/scm_provider"
1618
"github.com/argoproj/argo-cd/v3/applicationset/utils"
@@ -31,20 +33,22 @@ type SCMProviderGenerator struct {
3133
SCMConfig
3234
}
3335
type SCMConfig struct {
34-
scmRootCAPath string
35-
allowedSCMProviders []string
36-
enableSCMProviders bool
37-
GitHubApps github_app_auth.Credentials
38-
tokenRefStrictMode bool
36+
scmRootCAPath string
37+
allowedSCMProviders []string
38+
enableSCMProviders bool
39+
enableGitHubAPIMetrics bool
40+
GitHubApps github_app_auth.Credentials
41+
tokenRefStrictMode bool
3942
}
4043

41-
func NewSCMConfig(scmRootCAPath string, allowedSCMProviders []string, enableSCMProviders bool, gitHubApps github_app_auth.Credentials, tokenRefStrictMode bool) SCMConfig {
44+
func NewSCMConfig(scmRootCAPath string, allowedSCMProviders []string, enableSCMProviders bool, enableGitHubAPIMetrics bool, gitHubApps github_app_auth.Credentials, tokenRefStrictMode bool) SCMConfig {
4245
return SCMConfig{
43-
scmRootCAPath: scmRootCAPath,
44-
allowedSCMProviders: allowedSCMProviders,
45-
enableSCMProviders: enableSCMProviders,
46-
GitHubApps: gitHubApps,
47-
tokenRefStrictMode: tokenRefStrictMode,
46+
scmRootCAPath: scmRootCAPath,
47+
allowedSCMProviders: allowedSCMProviders,
48+
enableSCMProviders: enableSCMProviders,
49+
enableGitHubAPIMetrics: enableGitHubAPIMetrics,
50+
GitHubApps: gitHubApps,
51+
tokenRefStrictMode: tokenRefStrictMode,
4852
}
4953
}
5054

@@ -273,23 +277,36 @@ func (g *SCMProviderGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha
273277
}
274278

275279
func (g *SCMProviderGenerator) githubProvider(ctx context.Context, github *argoprojiov1alpha1.SCMProviderGeneratorGithub, applicationSetInfo *argoprojiov1alpha1.ApplicationSet) (scm_provider.SCMProviderService, error) {
280+
var metricsCtx *services.MetricsContext
281+
var httpClient *http.Client
282+
283+
if g.enableGitHubAPIMetrics {
284+
metricsCtx = &services.MetricsContext{
285+
AppSetNamespace: applicationSetInfo.Namespace,
286+
AppSetName: applicationSetInfo.Name,
287+
}
288+
httpClient = services.NewGitHubMetricsClient(metricsCtx)
289+
}
290+
276291
if github.AppSecretName != "" {
277292
auth, err := g.GitHubApps.GetAuthSecret(ctx, github.AppSecretName)
278293
if err != nil {
279294
return nil, fmt.Errorf("error fetching Github app secret: %w", err)
280295
}
281296

282-
return scm_provider.NewGithubAppProviderFor(
283-
*auth,
284-
github.Organization,
285-
github.API,
286-
github.AllBranches,
287-
)
297+
if g.enableGitHubAPIMetrics {
298+
return scm_provider.NewGithubAppProviderFor(*auth, github.Organization, github.API, github.AllBranches, httpClient)
299+
}
300+
return scm_provider.NewGithubAppProviderFor(*auth, github.Organization, github.API, github.AllBranches)
288301
}
289302

290303
token, err := utils.GetSecretRef(ctx, g.client, github.TokenRef, applicationSetInfo.Namespace, g.tokenRefStrictMode)
291304
if err != nil {
292305
return nil, fmt.Errorf("error fetching Github token: %w", err)
293306
}
307+
308+
if g.enableGitHubAPIMetrics {
309+
return scm_provider.NewGithubProvider(github.Organization, token, github.API, github.AllBranches, httpClient)
310+
}
294311
return scm_provider.NewGithubProvider(github.Organization, token, github.API, github.AllBranches)
295312
}

0 commit comments

Comments
 (0)