Skip to content

fix: incorrect gitops metrics #6444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/argocdServer/ArgoClientWrapperService.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (impl *ArgoClientWrapperServiceImpl) RegisterGitOpsRepoInArgoWithRetry(ctx
return nil
}

callback := func() error {
callback := func(int) error {
return impl.createRepoInArgoCd(ctx, grpcConfig, gitOpsRepoUrl)
}
argoCdErr := retryFunc.Retry(callback,
Expand Down
2 changes: 1 addition & 1 deletion cmd/external-app/wire_gen.go

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

31 changes: 7 additions & 24 deletions pkg/deployment/gitOps/git/GitOperationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type GitOperationService interface {
PushChartToGitOpsRepoForHelmApp(ctx context.Context, pushChartToGitRequest *bean.PushChartToGitRequestDTO, requirementsConfig, valuesConfig *ChartConfig) (*commonBean.ChartGitAttribute, string, error)

CreateRepository(ctx context.Context, dto *apiBean.GitOpsConfigDto, userId int32) (string, bool, bool, error)
GetRepoUrlByRepoName(repoName string) (string, error)

GetClonedDir(ctx context.Context, chartDir, repoUrl, targetRevision string) (string, error)
ReloadGitOpsProvider() error
Expand Down Expand Up @@ -170,7 +169,7 @@ func (impl *GitOperationServiceImpl) PushChartToGitRepo(ctx context.Context, git
commit, err := impl.gitFactory.GitOpsHelper.CommitAndPushAllChanges(newCtx, clonedDir, targetRevision, "first commit", userName, userEmailId)
if err != nil {
impl.logger.Errorw("error in pushing git", "err", err)
callback := func() error {
callback := func(int) error {
commit, err = impl.updateRepoAndPushAllChanges(newCtx, clonedDir, repoUrl, targetRevision,
tempReferenceTemplateDir, dir, userName, userEmailId, impl.gitFactory.GitOpsHelper)
return err
Expand Down Expand Up @@ -264,8 +263,12 @@ func (impl *GitOperationServiceImpl) CommitValues(ctx context.Context, chartGitA
return commitHash, commitTime, err
}
gitOpsConfig := &apiBean.GitOpsConfigDto{BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId}
callback := func() error {
commitHash, commitTime, err = impl.gitFactory.Client.CommitValues(newCtx, chartGitAttr, gitOpsConfig)
callback := func(retriesLeft int) error {
publishStatusConflictError := false
if retriesLeft <= 0 {
publishStatusConflictError = true
}
commitHash, commitTime, err = impl.gitFactory.Client.CommitValues(newCtx, chartGitAttr, gitOpsConfig, publishStatusConflictError)
return err
}
err = retryFunc.Retry(callback, impl.isRetryableGitCommitError,
Expand Down Expand Up @@ -303,26 +306,6 @@ func (impl *GitOperationServiceImpl) CreateRepository(ctx context.Context, dto *
return repoUrl, isNew, isEmpty, nil
}

func (impl *GitOperationServiceImpl) GetRepoUrlByRepoName(repoName string) (string, error) {
repoUrl := ""
bitbucketMetadata, err := impl.gitOpsConfigReadService.GetBitbucketMetadata()
if err != nil {
impl.logger.Errorw("error in getting bitbucket metadata", "err", err)
return repoUrl, err
}
dto := &apiBean.GitOpsConfigDto{
GitRepoName: repoName,
BitBucketWorkspaceId: bitbucketMetadata.BitBucketWorkspaceId,
BitBucketProjectKey: bitbucketMetadata.BitBucketProjectKey,
}
repoUrl, _, err = impl.gitFactory.Client.GetRepoUrl(dto)
if err != nil {
//will allow to continue to persist status on next operation
impl.logger.Errorw("error in getting repo url", "err", err, "repoName", repoName)
}
return repoUrl, nil
}

// PushChartToGitOpsRepoForHelmApp pushes built chart to GitOps repo (Specific implementation for Helm Apps)
// TODO refactoring: Make a common method for both PushChartToGitRepo and PushChartToGitOpsRepoForHelmApp
func (impl *GitOperationServiceImpl) PushChartToGitOpsRepoForHelmApp(ctx context.Context, pushChartToGitRequest *bean.PushChartToGitRequestDTO, requirementsConfig, valuesConfig *ChartConfig) (*commonBean.ChartGitAttribute, string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/deployment/gitOps/git/GitOpsClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

type GitOpsClient interface {
CreateRepository(ctx context.Context, config *gitOps.GitOpsConfigDto) (url string, isNew bool, isEmpty bool, detailedErrorGitOpsConfigActions DetailedErrorGitOpsConfigActions)
CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *gitOps.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error)
CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *gitOps.GitOpsConfigDto, publishStatusConflictError bool) (commitHash string, commitTime time.Time, err error)
GetRepoUrl(config *gitOps.GitOpsConfigDto) (repoUrl string, isRepoEmpty bool, err error)
DeleteRepository(config *gitOps.GitOpsConfigDto) error
CreateReadme(ctx context.Context, config *gitOps.GitOpsConfigDto) (string, error)
Expand Down
14 changes: 10 additions & 4 deletions pkg/deployment/gitOps/git/GitOpsHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ func (impl *GitOpsHelper) Clone(url, targetDir, targetRevision string) (clonedDi

func (impl *GitOpsHelper) Pull(repoRoot, targetRevision string) (err error) {
start := time.Now()
defer func() {
util.TriggerGitOpsMetrics("Pull", "GitService", start, err)
}()
ctx := git.BuildGitContext(context.Background()).WithCredentials(impl.Auth).
WithTLSData(impl.tlsConfig.CaData, impl.tlsConfig.TLSKeyData, impl.tlsConfig.TLSCertData, impl.isTlsEnabled)
return impl.gitCommandManager.Pull(ctx, targetRevision, repoRoot)
err = impl.gitCommandManager.Pull(ctx, targetRevision, repoRoot)
if err != nil {
util.TriggerGitOpsMetrics("Pull", "GitService", start, err)
return err
}
util.TriggerGitOpsMetrics("Pull", "GitService", start, nil)
return nil
}

const PushErrorMessage = "failed to push some refs"
Expand All @@ -129,11 +132,14 @@ func (impl *GitOpsHelper) pullFromBranch(ctx git.GitContext, rootDir, targetRevi
impl.logger.Warnw("no branch found in git repo", "rootDir", rootDir)
return "", "", err
}
start := time.Now()
response, errMsg, err := impl.gitCommandManager.PullCli(ctx, rootDir, branch)
if err != nil {
util.TriggerGitOpsMetrics("Pull", "GitCli", start, err)
impl.logger.Errorw("error on git pull", "branch", branch, "err", err)
return response, errMsg, err
}
util.TriggerGitOpsMetrics("Pull", "GitCli", start, nil)
return response, errMsg, err
}

Expand Down
30 changes: 23 additions & 7 deletions pkg/deployment/gitOps/git/GitServiceAzure.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,18 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
repoExists bool
)
start := time.Now()
defer func() {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
}()

detailedErrorGitOpsConfigActions.StageErrorMap = make(map[string]error)
url, repoExists, isEmpty, err = impl.repoExists(config.GitRepoName, impl.project)
if err != nil {
impl.logger.Errorw("error in communication with azure", "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[GetRepoUrlStage] = err
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
return "", false, isEmpty, detailedErrorGitOpsConfigActions
}
if repoExists {
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, GetRepoUrlStage)
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, nil)
return url, false, isEmpty, detailedErrorGitOpsConfigActions
}
gitRepositoryCreateOptions := git.GitRepositoryCreateOptions{
Expand All @@ -138,6 +137,7 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
impl.logger.Errorw("error in communication with azure", "err", err)
}
if err != nil || !repoExists {
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
}
Expand All @@ -146,11 +146,14 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
validated, err := impl.ensureProjectAvailabilityOnHttp(config.GitRepoName)
if err != nil {
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
err = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneHttpStage] = err
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneHttpStage)
Expand All @@ -159,6 +162,7 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
if err != nil {
impl.logger.Errorw("error in creating readme azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CreateReadmeStage] = err
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}
isEmpty = false //As we have created readme, repo is no longer empty
Expand All @@ -167,13 +171,17 @@ func (impl GitAzureClient) CreateRepository(ctx context.Context, config *bean2.G
if err != nil {
impl.logger.Errorw("error in ensuring project availability azure", "project", config.GitRepoName, "err", err)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}
if !validated {
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
err = fmt.Errorf("unable to validate project:%s in given time", config.GitRepoName)
detailedErrorGitOpsConfigActions.StageErrorMap[CloneSshStage] = err
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, err)
return "", true, isEmpty, detailedErrorGitOpsConfigActions
}
detailedErrorGitOpsConfigActions.SuccessfulStages = append(detailedErrorGitOpsConfigActions.SuccessfulStages, CloneSshStage)
globalUtil.TriggerGitOpsMetrics("CreateRepository", "GitAzureClient", start, nil)
return *operationReference.WebUrl, true, isEmpty, detailedErrorGitOpsConfigActions
}

Expand All @@ -196,14 +204,15 @@ func (impl GitAzureClient) CreateReadme(ctx context.Context, config *bean2.GitOp
UserName: config.Username,
UserEmailId: config.UserEmailId,
}
hash, _, err := impl.CommitValues(ctx, cfg, config)
hash, _, err := impl.CommitValues(ctx, cfg, config, true)
if err != nil {
impl.logger.Errorw("error in creating readme azure", "repo", config.GitRepoName, "err", err)
}
return hash, err
}

func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto) (commitHash string, commitTime time.Time, err error) {
func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig, gitOpsConfig *bean2.GitOpsConfigDto, publishStatusConflictError bool) (commitHash string, commitTime time.Time, err error) {
start := time.Now()
branch := config.TargetRevision
if len(branch) == 0 {
branch = globalUtil.GetDefaultTargetRevision()
Expand All @@ -229,13 +238,15 @@ func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig
if err != nil {
if e, ok := err.(azuredevops.WrappedError); !ok || *e.StatusCode >= 500 {
impl.logger.Errorw("error in fetching branch from azure devops", "err", err)
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitAzureClient", start, err)
return "", time.Time{}, err
}
} else if branchStat != nil {
oldObjId = *branchStat.Commit.CommitId
}
} else {
impl.logger.Errorw("error in fetching file from azure devops", "err", err)
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitAzureClient", start, err)
return "", time.Time{}, err
}
} else {
Expand Down Expand Up @@ -292,9 +303,13 @@ func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig
})
if e := (azuredevops.WrappedError{}); errors.As(err, &e) && e.StatusCode != nil && *e.StatusCode == http2.StatusConflict {
impl.logger.Warn("conflict found in commit azure", "err", err, "config", config)
if publishStatusConflictError {
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitAzureClient", start, err)
}
return "", time.Time{}, retryFunc.NewRetryableError(err)
} else if err != nil {
impl.logger.Errorw("error in commit azure", "err", err)
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitAzureClient", start, err)
return "", time.Time{}, err
}
//gitPush.Commits
Expand All @@ -305,6 +320,7 @@ func (impl GitAzureClient) CommitValues(ctx context.Context, config *ChartConfig
commitAuthorTime = (*push.Commits)[0].Author.Date.Time
}
// push.Commits[0].CommitId
globalUtil.TriggerGitOpsMetrics("CommitValues", "GitAzureClient", start, nil)
return commitId, commitAuthorTime, nil
}

Expand Down
Loading