Skip to content

Commit 7b91e40

Browse files
authored
Merge pull request #6231 from devtron-labs/registry-fix
misc: GetDockerRegistryIdForCiPipeline
2 parents 1da9a50 + 626ee68 commit 7b91e40

File tree

5 files changed

+109
-82
lines changed

5 files changed

+109
-82
lines changed

cmd/external-app/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/app/AppListingService.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ package app
1919
import (
2020
"context"
2121
"fmt"
22-
argoApplication "github.com/devtron-labs/devtron/client/argocdServer/bean"
23-
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
24-
repository2 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
25-
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deployedAppMetrics"
26-
util2 "github.com/devtron-labs/devtron/util"
27-
"net/http"
28-
"strconv"
29-
"strings"
30-
"time"
31-
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
3222
"github.com/devtron-labs/common-lib/utils/k8s/health"
23+
argoApplication "github.com/devtron-labs/devtron/client/argocdServer/bean"
3324
"github.com/devtron-labs/devtron/internal/middleware"
3425
"github.com/devtron-labs/devtron/internal/sql/repository/app"
26+
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
3527
userrepository "github.com/devtron-labs/devtron/pkg/auth/user/repository"
28+
ciConfig "github.com/devtron-labs/devtron/pkg/build/pipeline/read"
3629
chartRepoRepository "github.com/devtron-labs/devtron/pkg/chartRepo/repository"
30+
repository2 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
31+
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deployedAppMetrics"
32+
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate/read"
3733
"github.com/devtron-labs/devtron/pkg/dockerRegistry"
34+
"github.com/devtron-labs/devtron/pkg/pipeline/constants"
35+
util2 "github.com/devtron-labs/devtron/util"
3836
"github.com/devtron-labs/devtron/util/argo"
3937
errors2 "github.com/juju/errors"
4038
"go.opentelemetry.io/otel"
4139
"golang.org/x/exp/slices"
40+
"net/http"
41+
"strconv"
42+
"strings"
43+
"time"
4244

4345
"github.com/devtron-labs/devtron/api/bean"
4446
application2 "github.com/devtron-labs/devtron/client/argocdServer/application"
@@ -145,6 +147,7 @@ type AppListingServiceImpl struct {
145147
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService
146148
ciArtifactRepository repository.CiArtifactRepository
147149
envConfigOverrideReadService read.EnvConfigOverrideService
150+
ciPipelineConfigReadService ciConfig.CiPipelineConfigReadService
148151
}
149152

150153
func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository repository.AppListingRepository,
@@ -156,7 +159,8 @@ func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository re
156159
chartRepository chartRepoRepository.ChartRepository, ciPipelineRepository pipelineConfig.CiPipelineRepository,
157160
dockerRegistryIpsConfigService dockerRegistry.DockerRegistryIpsConfigService, userRepository userrepository.UserRepository,
158161
deployedAppMetricsService deployedAppMetrics.DeployedAppMetricsService, ciArtifactRepository repository.CiArtifactRepository,
159-
envConfigOverrideReadService read.EnvConfigOverrideService) *AppListingServiceImpl {
162+
envConfigOverrideReadService read.EnvConfigOverrideService,
163+
ciPipelineConfigReadService ciConfig.CiPipelineConfigReadService) *AppListingServiceImpl {
160164
serviceImpl := &AppListingServiceImpl{
161165
Logger: Logger,
162166
appListingRepository: appListingRepository,
@@ -176,6 +180,7 @@ func NewAppListingServiceImpl(Logger *zap.SugaredLogger, appListingRepository re
176180
deployedAppMetricsService: deployedAppMetricsService,
177181
ciArtifactRepository: ciArtifactRepository,
178182
envConfigOverrideReadService: envConfigOverrideReadService,
183+
ciPipelineConfigReadService: ciPipelineConfigReadService,
179184
}
180185
return serviceImpl
181186
}
@@ -780,11 +785,28 @@ func (impl AppListingServiceImpl) setIpAccessProvidedData(ctx context.Context, a
780785
}
781786

782787
if ciPipeline != nil && ciPipeline.CiTemplate != nil && len(*ciPipeline.CiTemplate.DockerRegistryId) > 0 {
783-
dockerRegistryId := ciPipeline.CiTemplate.DockerRegistryId
784-
appDetailContainer.DockerRegistryId = *dockerRegistryId
785-
if !ciPipeline.IsExternal || ciPipeline.ParentCiPipeline != 0 {
788+
if !ciPipeline.IsExternal || ciPipeline.ParentCiPipeline != 0 && ciPipeline.PipelineType != string(constants.LINKED_CD) {
786789
appDetailContainer.IsExternalCi = false
787790
}
791+
// get dockerRegistryId starts
792+
artifact, err := impl.ciArtifactRepository.Get(appDetailContainer.CiArtifactId)
793+
if err != nil {
794+
impl.Logger.Errorw("error in fetching ci artifact", "ciArtifactId", appDetailContainer.CiArtifactId, "error", err)
795+
return bean.AppDetailContainer{}, err
796+
}
797+
dockerRegistryId, err := impl.ciPipelineConfigReadService.GetDockerRegistryIdForCiPipeline(ciPipelineId, artifact)
798+
if err != nil {
799+
impl.Logger.Errorw("error in fetching docker registry id", "ciPipelineId", ciPipelineId, "error", err)
800+
return bean.AppDetailContainer{}, err
801+
}
802+
803+
if dockerRegistryId == nil {
804+
impl.Logger.Errorw("docker registry id not found", "ciPipelineId", ciPipelineId)
805+
return appDetailContainer, nil
806+
}
807+
// get dockerRegistryId ends
808+
appDetailContainer.DockerRegistryId = *dockerRegistryId
809+
788810
_, span = otel.Tracer("orchestrator").Start(ctx, "dockerRegistryIpsConfigService.IsImagePullSecretAccessProvided")
789811
// check ips access provided to this docker registry for that cluster
790812
ipsAccessProvided, err := impl.dockerRegistryIpsConfigService.IsImagePullSecretAccessProvided(*dockerRegistryId, clusterId, isVirtualEnv)

pkg/build/pipeline/read/CiPipelineReadService.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package read
1818

1919
import (
2020
"errors"
21+
"github.com/devtron-labs/devtron/internal/sql/repository"
2122
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
2223
"github.com/devtron-labs/devtron/pkg/build/pipeline/read/adapter"
2324
"github.com/devtron-labs/devtron/pkg/build/pipeline/read/bean"
@@ -32,20 +33,24 @@ type CiPipelineConfigReadService interface {
3233
FindAllDeletedPipelineCountInLast24Hour() (pipelineCount int, err error)
3334
GetChildrenCiCount(parentCiPipelineId int) (int, error)
3435
GetCiPipelineById(ciPipelineId int) (*bean.CiPipelineMin, error)
36+
GetDockerRegistryIdForCiPipeline(ciPipelineId int, artifact *repository.CiArtifact) (*string, error)
3537
}
3638

3739
type CiPipelineConfigReadServiceImpl struct {
38-
logger *zap.SugaredLogger
39-
ciPipelineRepository pipelineConfig.CiPipelineRepository
40+
logger *zap.SugaredLogger
41+
ciPipelineRepository pipelineConfig.CiPipelineRepository
42+
ciTemplateOverrideRepository pipelineConfig.CiTemplateOverrideRepository
4043
}
4144

4245
func NewCiPipelineConfigReadServiceImpl(
4346
logger *zap.SugaredLogger,
4447
ciPipelineRepository pipelineConfig.CiPipelineRepository,
48+
ciTemplateOverrideRepository pipelineConfig.CiTemplateOverrideRepository,
4549
) *CiPipelineConfigReadServiceImpl {
4650
return &CiPipelineConfigReadServiceImpl{
47-
logger: logger,
48-
ciPipelineRepository: ciPipelineRepository,
51+
logger: logger,
52+
ciPipelineRepository: ciPipelineRepository,
53+
ciTemplateOverrideRepository: ciTemplateOverrideRepository,
4954
}
5055
}
5156

@@ -85,3 +90,50 @@ func (impl *CiPipelineConfigReadServiceImpl) GetCiPipelineById(ciPipelineId int)
8590
}
8691
return adapter.NewCiPipelineMin(ciPipeline)
8792
}
93+
94+
func (impl *CiPipelineConfigReadServiceImpl) GetDockerRegistryIdForCiPipeline(ciPipelineId int, artifact *repository.CiArtifact) (*string, error) {
95+
ciPipeline, err := impl.ciPipelineRepository.FindById(ciPipelineId)
96+
if err != nil {
97+
impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciPipelineId, "error", err)
98+
return nil, err
99+
}
100+
101+
if ciPipeline.IsExternal && ciPipeline.ParentCiPipeline == 0 {
102+
impl.logger.Warn("Ignoring for external ci")
103+
return nil, nil
104+
}
105+
106+
if ciPipeline.CiTemplate == nil {
107+
impl.logger.Warn("returning as ciPipeline.CiTemplate is found nil")
108+
return nil, nil
109+
}
110+
var dockerRegistryId string
111+
if artifact.DataSource == repository.POST_CI || artifact.DataSource == repository.PRE_CD || artifact.DataSource == repository.POST_CD {
112+
// if image is generated by plugin at these stages
113+
if artifact.CredentialsSourceType == repository.GLOBAL_CONTAINER_REGISTRY {
114+
dockerRegistryId = artifact.CredentialSourceValue
115+
}
116+
} else if artifact.DataSource == repository.CI_RUNNER {
117+
// if image is created by ci build
118+
dockerRegistryId = *ciPipeline.CiTemplate.DockerRegistryId
119+
if len(dockerRegistryId) == 0 {
120+
impl.logger.Warn("returning as dockerRegistryId is found empty")
121+
return nil, nil
122+
}
123+
124+
if ciPipeline.IsDockerConfigOverridden {
125+
//set dockerRegistryId value with the DockerRegistryId of the overridden dockerRegistry
126+
ciPipId := ciPipelineId
127+
if ciPipeline.ParentCiPipeline != 0 {
128+
ciPipId = ciPipeline.ParentCiPipeline
129+
}
130+
ciTemplateOverride, err := impl.ciTemplateOverrideRepository.FindByCiPipelineId(ciPipId)
131+
if err != nil {
132+
impl.logger.Errorw("error in getting ciTemplateOverride by ciPipelineId", "ciPipelineId", ciPipelineId, "error", err)
133+
return nil, err
134+
}
135+
dockerRegistryId = ciTemplateOverride.DockerRegistryId
136+
}
137+
}
138+
return &dockerRegistryId, nil
139+
}

pkg/dockerRegistry/DockerRegistryIpsConfigService.go

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/devtron-labs/common-lib/utils/k8s"
2323
repository3 "github.com/devtron-labs/devtron/internal/sql/repository"
2424
"github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
25-
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
2625
util2 "github.com/devtron-labs/devtron/internal/util"
26+
ciConfig "github.com/devtron-labs/devtron/pkg/build/pipeline/read"
2727
"github.com/devtron-labs/devtron/pkg/cluster"
2828
repository2 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
2929
"github.com/go-pg/pg"
@@ -46,22 +46,21 @@ type DockerRegistryIpsConfigServiceImpl struct {
4646
dockerRegistryIpsConfigRepository repository.DockerRegistryIpsConfigRepository
4747
k8sUtil *k8s.K8sServiceImpl
4848
clusterService cluster.ClusterService
49-
ciPipelineRepository pipelineConfig.CiPipelineRepository
5049
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository
51-
ciTemplateOverrideRepository pipelineConfig.CiTemplateOverrideRepository
50+
ciPipelineConfigReadService ciConfig.CiPipelineConfigReadService
5251
}
5352

5453
func NewDockerRegistryIpsConfigServiceImpl(logger *zap.SugaredLogger, dockerRegistryIpsConfigRepository repository.DockerRegistryIpsConfigRepository,
55-
k8sUtil *k8s.K8sServiceImpl, clusterService cluster.ClusterService, ciPipelineRepository pipelineConfig.CiPipelineRepository,
56-
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository, ciTemplateOverrideRepository pipelineConfig.CiTemplateOverrideRepository) *DockerRegistryIpsConfigServiceImpl {
54+
k8sUtil *k8s.K8sServiceImpl, clusterService cluster.ClusterService,
55+
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository,
56+
ciPipelineConfigReadService ciConfig.CiPipelineConfigReadService) *DockerRegistryIpsConfigServiceImpl {
5757
return &DockerRegistryIpsConfigServiceImpl{
5858
logger: logger,
5959
dockerRegistryIpsConfigRepository: dockerRegistryIpsConfigRepository,
6060
k8sUtil: k8sUtil,
6161
clusterService: clusterService,
62-
ciPipelineRepository: ciPipelineRepository,
6362
dockerArtifactStoreRepository: dockerArtifactStoreRepository,
64-
ciTemplateOverrideRepository: ciTemplateOverrideRepository,
63+
ciPipelineConfigReadService: ciPipelineConfigReadService,
6564
}
6665
}
6766

@@ -91,7 +90,7 @@ func (impl DockerRegistryIpsConfigServiceImpl) HandleImagePullSecretOnApplicatio
9190
return valuesFileContent, nil
9291
}
9392

94-
dockerRegistryId, err := impl.getDockerRegistryIdForCiPipeline(ciPipelineId, artifact)
93+
dockerRegistryId, err := impl.ciPipelineConfigReadService.GetDockerRegistryIdForCiPipeline(ciPipelineId, artifact)
9594
if err != nil {
9695
impl.logger.Errorw("error in getting docker registry", "dockerRegistryId", dockerRegistryId, "error", err)
9796
return valuesFileContent, err
@@ -144,52 +143,6 @@ func (impl DockerRegistryIpsConfigServiceImpl) HandleImagePullSecretOnApplicatio
144143
return updatedValuesFileContent, nil
145144
}
146145

147-
func (impl DockerRegistryIpsConfigServiceImpl) getDockerRegistryIdForCiPipeline(ciPipelineId int, artifact *repository3.CiArtifact) (*string, error) {
148-
ciPipeline, err := impl.ciPipelineRepository.FindById(ciPipelineId)
149-
if err != nil {
150-
impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciPipelineId, "error", err)
151-
return nil, err
152-
}
153-
154-
if ciPipeline.IsExternal && ciPipeline.ParentCiPipeline == 0 {
155-
impl.logger.Warn("Ignoring for external ci")
156-
return nil, nil
157-
}
158-
159-
if ciPipeline.CiTemplate == nil {
160-
impl.logger.Warn("returning as ciPipeline.CiTemplate is found nil")
161-
return nil, nil
162-
}
163-
var dockerRegistryId string
164-
if artifact.DataSource == repository3.POST_CI || artifact.DataSource == repository3.PRE_CD || artifact.DataSource == repository3.POST_CD {
165-
// if image is generated by plugin at these stages
166-
if artifact.CredentialsSourceType == repository3.GLOBAL_CONTAINER_REGISTRY {
167-
dockerRegistryId = artifact.CredentialSourceValue
168-
}
169-
} else if artifact.DataSource == repository3.CI_RUNNER {
170-
// if image is created by ci build
171-
dockerRegistryId = *ciPipeline.CiTemplate.DockerRegistryId
172-
if len(dockerRegistryId) == 0 {
173-
impl.logger.Warn("returning as dockerRegistryId is found empty")
174-
return nil, nil
175-
}
176-
177-
if ciPipeline.IsDockerConfigOverridden {
178-
//set dockerRegistryId value with the DockerRegistryId of the overridden dockerRegistry
179-
ciPipId := ciPipelineId
180-
if ciPipeline.ParentCiPipeline != 0 {
181-
ciPipId = ciPipeline.ParentCiPipeline
182-
}
183-
ciTemplateOverride, err := impl.ciTemplateOverrideRepository.FindByCiPipelineId(ciPipId)
184-
if err != nil {
185-
impl.logger.Errorw("error in getting ciTemplateOverride by ciPipelineId", "ciPipelineId", ciPipelineId, "error", err)
186-
return nil, err
187-
}
188-
dockerRegistryId = ciTemplateOverride.DockerRegistryId
189-
}
190-
}
191-
return &dockerRegistryId, nil
192-
}
193146
func (impl DockerRegistryIpsConfigServiceImpl) createOrUpdateDockerRegistryImagePullSecret(clusterId int, namespace string, ipsName string, dockerRegistryBean *repository.DockerArtifactStore) error {
194147
impl.logger.Infow("creating/updating ips", "ipsName", ipsName, "clusterId", clusterId)
195148

0 commit comments

Comments
 (0)