Skip to content
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
1 change: 0 additions & 1 deletion api/bean/GitOpsConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ type GitOpsConfigDto struct {
AzureProjectName string `json:"azureProjectName"`
BitBucketWorkspaceId string `json:"bitBucketWorkspaceId"`
BitBucketProjectKey string `json:"bitBucketProjectKey"`
AllowInsecureTLS bool `json:"allowInsecureTLS"`
UserId int32 `json:"-"`
}
1 change: 0 additions & 1 deletion internal/sql/repository/GitOpsConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type GitOpsConfig struct {
BitBucketWorkspaceId string `sql:"bitbucket_workspace_id"`
BitBucketProjectKey string `sql:"bitbucket_project_key"`
EmailId string `sql:"email_id"`
AllowInsecureTLS bool `sql:"allow_insecure_tls,notnull"`
sql.AuditLog
}

Expand Down
7 changes: 3 additions & 4 deletions internal/util/ChartService.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type ChartTemplateService interface {
GetGitOpsRepoName(appName string) string
GetGitOpsRepoNameFromUrl(gitRepoUrl string) string
CreateGitRepositoryForApp(gitOpsRepoName, baseTemplateName, version string, userId int32) (chartGitAttribute *ChartGitAttribute, err error)
RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context, allowInsecureTLS bool) error
RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context) error
BuildChartAndPushToGitRepo(chartMetaData *chart.Metadata, referenceTemplatePath string, gitOpsRepoName, referenceTemplate, version, repoUrl string, userId int32) error
GetByteArrayRefChart(chartMetaData *chart.Metadata, referenceTemplatePath string) ([]byte, error)
CreateReadmeInGitRepo(gitOpsRepoName string, userId int32) error
Expand Down Expand Up @@ -104,10 +104,9 @@ func NewChartTemplateServiceImpl(logger *zap.SugaredLogger,
repositoryService: repositoryService,
}
}
func (impl ChartTemplateServiceImpl) RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context, allowInsecureTLS bool) error {
func (impl ChartTemplateServiceImpl) RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context) error {
repo := &v1alpha1.Repository{
Repo: chartGitAttribute.RepoUrl,
Insecure: allowInsecureTLS,
Repo: chartGitAttribute.RepoUrl,
}
repo, err := impl.repositoryService.Create(ctx, &repository3.RepoCreateRequest{Repo: repo, Upsert: true})
if err != nil {
Expand Down
24 changes: 20 additions & 4 deletions internal/util/GitService.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"time"

Expand Down Expand Up @@ -98,7 +99,25 @@ func (factory *GitFactory) Reload() error {
}

func (factory *GitFactory) GetGitLabGroupPath(gitOpsConfig *bean2.GitOpsConfigDto) (string, error) {
gitLabClient, err := CreateGitlabClient(gitOpsConfig.Host, gitOpsConfig.Token, gitOpsConfig.AllowInsecureTLS)
var gitLabClient *gitlab.Client
var err error
if len(gitOpsConfig.Host) > 0 {
_, err = url.ParseRequestURI(gitOpsConfig.Host)
if err != nil {
return "", err
}
gitLabClient, err = gitlab.NewClient(gitOpsConfig.Token, gitlab.WithBaseURL(gitOpsConfig.Host))
if err != nil {
factory.logger.Errorw("error in getting new gitlab client", "err", err)
return "", err
}
} else {
gitLabClient, err = gitlab.NewClient(gitOpsConfig.Token)
if err != nil {
factory.logger.Errorw("error in getting new gitlab client", "err", err)
return "", err
}
}
group, _, err := gitLabClient.Groups.GetGroup(gitOpsConfig.GitLabGroupId, &gitlab.GetGroupOptions{})
if err != nil {
factory.logger.Errorw("error in fetching gitlab group name", "err", err, "gitLab groupID", gitOpsConfig.GitLabGroupId)
Expand All @@ -122,7 +141,6 @@ func (factory *GitFactory) NewClientForValidation(gitOpsConfig *bean2.GitOpsConf
GitHost: gitOpsConfig.Host,
AzureToken: gitOpsConfig.Token,
AzureProject: gitOpsConfig.AzureProjectName,
AllowInsecureTLS: gitOpsConfig.AllowInsecureTLS,
}
gitService := NewGitServiceImpl(cfg, logger, factory.gitCliUtil)
//factory.gitService = gitService
Expand Down Expand Up @@ -169,7 +187,6 @@ type GitConfig struct {
AzureProject string
BitbucketWorkspaceId string
BitbucketProjectKey string
AllowInsecureTLS bool
}

func GetGitConfig(gitOpsRepository repository.GitOpsConfigRepository) (*GitConfig, error) {
Expand Down Expand Up @@ -199,7 +216,6 @@ func GetGitConfig(gitOpsRepository repository.GitOpsConfigRepository) (*GitConfi
AzureProject: gitOpsConfig.AzureProject,
BitbucketWorkspaceId: gitOpsConfig.BitBucketWorkspaceId,
BitbucketProjectKey: gitOpsConfig.BitBucketProjectKey,
AllowInsecureTLS: gitOpsConfig.AllowInsecureTLS,
}
return cfg, err
}
Expand Down
48 changes: 18 additions & 30 deletions internal/util/GitServiceGitlab.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package util

import (
"crypto/tls"
"fmt"
"github.com/hashicorp/go-retryablehttp"
"github.com/xanzy/go-gitlab"
"go.uber.org/zap"
"net/http"
"net/url"
"path/filepath"
"strconv"
Expand All @@ -21,7 +18,24 @@ type GitLabClient struct {
}

func NewGitLabClient(config *GitConfig, logger *zap.SugaredLogger, gitService GitService) (GitClient, error) {
gitLabClient, err := CreateGitlabClient(config.GitHost, config.GitToken, config.AllowInsecureTLS)
var gitLabClient *gitlab.Client
var err error
if len(config.GitHost) > 0 {
_, err = url.ParseRequestURI(config.GitHost)
if err != nil {
return nil, err
}
gitLabClient, err = gitlab.NewClient(config.GitToken, gitlab.WithBaseURL(config.GitHost))
if err != nil {
return nil, err
}
} else {
gitLabClient, err = gitlab.NewClient(config.GitToken)
if err != nil {
return nil, err
}
}

gitlabGroupId := ""
if len(config.GitlabGroupId) > 0 {
if _, err := strconv.Atoi(config.GitlabGroupId); err == nil {
Expand Down Expand Up @@ -67,32 +81,6 @@ func NewGitLabClient(config *GitConfig, logger *zap.SugaredLogger, gitService Gi
}, nil
}

func CreateGitlabClient(host, token string, allowInsecureTLS bool) (*gitlab.Client, error) {
var gitLabClient *gitlab.Client
var err error
httpTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: allowInsecureTLS},
}
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = httpTransport
if len(host) > 0 {
_, err = url.ParseRequestURI(host)
if err != nil {
return nil, err
}
gitLabClient, err = gitlab.NewClient(token, gitlab.WithBaseURL(host), gitlab.WithHTTPClient(retryClient.HTTPClient))
if err != nil {
return nil, err
}
} else {
gitLabClient, err = gitlab.NewClient(token, gitlab.WithHTTPClient(retryClient.HTTPClient))
if err != nil {
return nil, err
}
}
return gitLabClient, err
}

func (impl GitLabClient) DeleteRepository(name string) error {
err := impl.DeleteProject(name)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package appStoreDeploymentFullMode
import (
"context"
"github.com/devtron-labs/devtron/client/argocdServer"
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
repository4 "github.com/devtron-labs/devtron/pkg/appStore/deployment/repository"
appStoreDiscoverRepository "github.com/devtron-labs/devtron/pkg/appStore/discover/repository"
Expand All @@ -36,6 +35,8 @@ import (
"time"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
repository2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
application2 "github.com/devtron-labs/devtron/client/argocdServer/application"
"github.com/devtron-labs/devtron/client/argocdServer/repository"
"github.com/devtron-labs/devtron/internal/util"
Expand All @@ -53,6 +54,7 @@ const (
type AppStoreDeploymentFullModeService interface {
AppStoreDeployOperationGIT(installAppVersionRequest *appStoreBean.InstallAppVersionDTO) (*appStoreBean.InstallAppVersionDTO, *util.ChartGitAttribute, error)
AppStoreDeployOperationACD(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, chartGitAttr *util.ChartGitAttribute, ctx context.Context) (*appStoreBean.InstallAppVersionDTO, error)
RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error
SyncACD(acdAppName string, ctx context.Context)
UpdateValuesYaml(installAppVersionRequest *appStoreBean.InstallAppVersionDTO) (*appStoreBean.InstallAppVersionDTO, error)
UpdateRequirementYaml(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, appStoreAppVersion *appStoreDiscoverRepository.AppStoreApplicationVersion) error
Expand All @@ -74,7 +76,6 @@ type AppStoreDeploymentFullModeServiceImpl struct {
installedAppRepository repository4.InstalledAppRepository
tokenCache *util2.TokenCache
argoUserService argo.ArgoUserService
gitOpsConfigRepository repository2.GitOpsConfigRepository
}

func NewAppStoreDeploymentFullModeServiceImpl(logger *zap.SugaredLogger,
Expand Down Expand Up @@ -236,13 +237,8 @@ func (impl AppStoreDeploymentFullModeServiceImpl) AppStoreDeployOperationGIT(ins
func (impl AppStoreDeploymentFullModeServiceImpl) AppStoreDeployOperationACD(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, chartGitAttr *util.ChartGitAttribute, ctx context.Context) (*appStoreBean.InstallAppVersionDTO, error) {
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()
gitOpsConfig, err := impl.gitOpsConfigRepository.GetGitOpsConfigActive()
if err != nil {
impl.logger.Errorw("error in getting active gitOps config", "err", err)
return nil, err
}
//STEP 4: registerInArgo
err = impl.chartTemplateService.RegisterInArgo(chartGitAttr, ctx, gitOpsConfig.AllowInsecureTLS)
err := impl.RegisterInArgo(chartGitAttr, ctx)
if err != nil {
impl.logger.Errorw("error in argo registry", "err", err)
return nil, err
Expand All @@ -259,6 +255,18 @@ func (impl AppStoreDeploymentFullModeServiceImpl) AppStoreDeployOperationACD(ins
return installAppVersionRequest, nil
}

func (impl AppStoreDeploymentFullModeServiceImpl) RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error {
repo := &v1alpha1.Repository{
Repo: chartGitAttribute.RepoUrl,
}
repo, err := impl.repositoryService.Create(ctx, &repository2.RepoCreateRequest{Repo: repo, Upsert: true})
if err != nil {
impl.logger.Errorw("error in creating argo Repository ", "err", err)
}
impl.logger.Debugw("repo registered in argo", "name", chartGitAttribute.RepoUrl)
return err
}

func (impl AppStoreDeploymentFullModeServiceImpl) SyncACD(acdAppName string, ctx context.Context) {
req := new(application.ApplicationSyncRequest)
req.Name = &acdAppName
Expand Down
10 changes: 10 additions & 0 deletions pkg/appStore/deployment/service/InstalledAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ func (impl InstalledAppServiceImpl) performDeployStageOnAcd(installedAppVersion
impl.logger.Errorw("fetching error", "err", err)
return nil, err
}
gitOpsConfigBitbucket, err := impl.gitOpsRepository.GetGitOpsConfigByProvider(util.BITBUCKET_PROVIDER)
if err != nil {
if err == pg.ErrNoRows {
gitOpsConfigBitbucket.BitBucketWorkspaceId = ""
gitOpsConfigBitbucket.BitBucketProjectKey = ""
} else {
return nil, err
}
}

repoUrl, err := impl.gitFactory.Client.GetRepoUrl(installedAppVersion.AppStoreName)
if err != nil {
//will allow to continue to persist status on next operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
application2 "github.com/devtron-labs/devtron/client/argocdServer/application"
"github.com/devtron-labs/devtron/internal/constants"
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/util"
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
appStoreDeploymentFullMode "github.com/devtron-labs/devtron/pkg/appStore/deployment/fullMode"
Expand Down Expand Up @@ -51,7 +50,6 @@ type AppStoreDeploymentArgoCdServiceImpl struct {
chartTemplateService util.ChartTemplateService
gitFactory *util.GitFactory
argoUserService argo.ArgoUserService
gitOpsConfigRepository repository2.GitOpsConfigRepository
}

func NewAppStoreDeploymentArgoCdServiceImpl(logger *zap.SugaredLogger, appStoreDeploymentFullModeService appStoreDeploymentFullMode.AppStoreDeploymentFullModeService,
Expand Down Expand Up @@ -400,13 +398,8 @@ func (impl AppStoreDeploymentArgoCdServiceImpl) UpdateInstalledApp(ctx context.C
func (impl AppStoreDeploymentArgoCdServiceImpl) patchAcdApp(ctx context.Context, installAppVersionRequest *appStoreBean.InstallAppVersionDTO, chartGitAttr *util.ChartGitAttribute) (*appStoreBean.InstallAppVersionDTO, error) {
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()
gitOpsConfig, err := impl.gitOpsConfigRepository.GetGitOpsConfigActive()
if err != nil {
impl.Logger.Errorw("error in getting active gitOps config", "err", err)
return nil, err
}
//registerInArgo
err = impl.chartTemplateService.RegisterInArgo(chartGitAttr, ctx, gitOpsConfig.AllowInsecureTLS)
err := impl.appStoreDeploymentFullModeService.RegisterInArgo(chartGitAttr, ctx)
if err != nil {
impl.Logger.Errorw("error in argo registry", "err", err)
return nil, err
Expand Down
15 changes: 15 additions & 0 deletions pkg/chart/ChartService.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (
"github.com/devtron-labs/devtron/pkg/sql"
dirCopy "github.com/otiai10/copy"

repository2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/devtron-labs/devtron/client/argocdServer/repository"
"github.com/devtron-labs/devtron/internal/sql/models"
repository3 "github.com/devtron-labs/devtron/internal/sql/repository"
Expand Down Expand Up @@ -139,6 +141,7 @@ type ChartService interface {
GetLocationFromChartNameAndVersion(chartName string, chartVersion string) string
ValidateUploadedFileFormat(fileName string) error
ReadChartMetaDataForLocation(chartDir string, fileName string) (*ChartYamlStruct, error)
RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error
FetchChartInfoByFlag(userUploaded bool) ([]*ChartDto, error)
CheckCustomChartByAppId(id int) (bool, error)
CheckCustomChartByChartId(id int) (bool, error)
Expand Down Expand Up @@ -579,6 +582,18 @@ func (impl ChartServiceImpl) CreateChartFromEnvOverride(templateRequest Template
return chartVal, err
}

func (impl ChartServiceImpl) RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error {
repo := &v1alpha1.Repository{
Repo: chartGitAttribute.RepoUrl,
}
repo, err := impl.repositoryService.Create(ctx, &repository2.RepoCreateRequest{Repo: repo, Upsert: true})
if err != nil {
impl.logger.Errorw("error in creating argo Repository ", "err", err)
}
impl.logger.Infow("repo registered in argo", "name", chartGitAttribute.RepoUrl)
return err
}

// converts db object to bean
func (impl ChartServiceImpl) chartAdaptor(chart *chartRepoRepository.Chart, appLevelMetrics *repository3.AppLevelMetrics) (*TemplateRequest, error) {
var appMetrics bool
Expand Down
4 changes: 1 addition & 3 deletions pkg/gitops/GitOpsConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ func (impl *GitOpsConfigServiceImpl) CreateGitOpsConfig(ctx context.Context, req
AzureProject: request.AzureProjectName,
BitBucketWorkspaceId: request.BitBucketWorkspaceId,
BitBucketProjectKey: request.BitBucketProjectKey,
AllowInsecureTLS: request.AllowInsecureTLS,
AuditLog: sql.AuditLog{CreatedBy: request.UserId, CreatedOn: time.Now(), UpdatedOn: time.Now(), UpdatedBy: request.UserId},
}
model, err = impl.gitOpsRepository.CreateGitOpsConfig(model, tx)
Expand Down Expand Up @@ -404,7 +403,6 @@ func (impl *GitOpsConfigServiceImpl) UpdateGitOpsConfig(request *bean2.GitOpsCon
model.AzureProject = request.AzureProjectName
model.BitBucketWorkspaceId = request.BitBucketWorkspaceId
model.BitBucketProjectKey = request.BitBucketProjectKey
model.AllowInsecureTLS = request.AllowInsecureTLS
err = impl.gitOpsRepository.UpdateGitOpsConfig(model, tx)
if err != nil {
impl.logger.Errorw("error in updating team", "data", model, "err", err)
Expand Down Expand Up @@ -701,7 +699,7 @@ func (impl *GitOpsConfigServiceImpl) GitOpsValidateDryRun(config *bean2.GitOpsCo
return detailedErrorGitOpsConfigResponse
}
appName := DryrunRepoName + util2.Generate(6)
//getting username & emailId for commit author data
//getting user name & emailId for commit author data
userEmailId, userName := impl.chartTemplateService.GetUserEmailIdAndNameForGitOpsCommit(config.UserId)
repoUrl, _, detailedErrorCreateRepo := client.CreateRepository(appName, "sample dry-run repo", userName, userEmailId)

Expand Down
Loading