-
Notifications
You must be signed in to change notification settings - Fork 475
/
pipelineStageVariableParser.go
212 lines (188 loc) · 8.55 KB
/
pipelineStageVariableParser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pipeline
import (
"errors"
"fmt"
dockerRegistryRepository "github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/pipeline/bean"
"github.com/devtron-labs/devtron/pkg/plugin"
"github.com/go-pg/pg"
errors1 "github.com/juju/errors"
"go.uber.org/zap"
"strings"
)
type copyContainerImagePluginInputVariable = string
type RefPluginName = string
const (
COPY_CONTAINER_IMAGE RefPluginName = "Copy container image"
EMPTY_STRING = " "
)
const (
DESTINATION_INFO copyContainerImagePluginInputVariable = "DESTINATION_INFO"
SOURCE_REGISTRY_CREDENTIALS_KEY = "SOURCE_REGISTRY_CREDENTIAL"
)
type PluginInputVariableParser interface {
HandleCopyContainerImagePluginInputVariables(inputVariables []*bean.VariableObject, dockerImageTag string, pluginTriggerImage string, sourceImageDockerRegistry string) (registryDestinationImageMap map[string][]string, registryCredentials map[string]plugin.RegistryCredentials, err error)
}
type PluginInputVariableParserImpl struct {
logger *zap.SugaredLogger
dockerRegistryConfig DockerRegistryConfig
customTagService CustomTagService
}
func NewPluginInputVariableParserImpl(
logger *zap.SugaredLogger,
dockerRegistryConfig DockerRegistryConfig,
customTagService CustomTagService,
) *PluginInputVariableParserImpl {
return &PluginInputVariableParserImpl{
logger: logger,
dockerRegistryConfig: dockerRegistryConfig,
customTagService: customTagService,
}
}
func (impl *PluginInputVariableParserImpl) HandleCopyContainerImagePluginInputVariables(inputVariables []*bean.VariableObject,
dockerImageTag string,
pluginTriggerImage string,
sourceImageDockerRegistry string) (registryDestinationImageMap map[string][]string, registryCredentials map[string]plugin.RegistryCredentials, err error) {
var DestinationInfo string
for _, ipVariable := range inputVariables {
if ipVariable.Name == DESTINATION_INFO {
DestinationInfo = ipVariable.Value
}
}
if len(pluginTriggerImage) == 0 {
return nil, nil, errors.New("no image provided during trigger time")
}
if len(DestinationInfo) == 0 {
return nil, nil, errors.New("destination info now")
}
if len(dockerImageTag) == 0 {
// case when custom tag is not configured - source image tag will be taken as docker image tag
pluginTriggerImageSplit := strings.Split(pluginTriggerImage, ":")
dockerImageTag = pluginTriggerImageSplit[len(pluginTriggerImageSplit)-1]
}
registryRepoMapping := impl.getRegistryRepoMapping(DestinationInfo)
registryCredentials, err = impl.getRegistryDetails(registryRepoMapping, sourceImageDockerRegistry)
if err != nil {
return nil, nil, err
}
registryDestinationImageMap = impl.getRegistryDestinationImageMapping(registryRepoMapping, dockerImageTag, registryCredentials)
err = impl.createEcrRepoIfRequired(registryCredentials, registryRepoMapping)
if err != nil {
impl.logger.Errorw("error in creating ecr repo", "err", err)
return registryDestinationImageMap, registryCredentials, err
}
return registryDestinationImageMap, registryCredentials, nil
}
func (impl *PluginInputVariableParserImpl) getRegistryRepoMapping(destinationInfo string) map[string][]string {
/*
creating map with registry as key and list of repositories in that registry where we need to copy image
destinationInfo format (each registry detail is separated by new line) :
<registryName1> | <comma separated repoNames>
<registryName2> | <comma separated repoNames>
*/
destinationRegistryRepositoryMap := make(map[string][]string)
destinationRegistryRepoDetails := strings.Split(destinationInfo, "\n")
for _, detail := range destinationRegistryRepoDetails {
registryRepoSplit := strings.Split(detail, "|")
registryName := strings.Trim(registryRepoSplit[0], EMPTY_STRING)
repositoryValuesSplit := strings.Split(registryRepoSplit[1], ",")
var repositories []string
for _, repositoryName := range repositoryValuesSplit {
repositoryName = strings.Trim(repositoryName, EMPTY_STRING)
repositories = append(repositories, repositoryName)
}
destinationRegistryRepositoryMap[registryName] = repositories
}
return destinationRegistryRepositoryMap
}
func (impl *PluginInputVariableParserImpl) getRegistryDetails(destinationRegistryRepositoryMap map[string][]string, sourceRegistry string) (map[string]plugin.RegistryCredentials, error) {
registryCredentialsMap := make(map[string]plugin.RegistryCredentials)
//saving source registry credentials
sourceRegistry = strings.Trim(sourceRegistry, " ")
sourceRegistryCredentials, err := impl.getPluginRegistryCredentialsByRegistryName(sourceRegistry)
if err != nil {
return nil, err
}
registryCredentialsMap[SOURCE_REGISTRY_CREDENTIALS_KEY] = *sourceRegistryCredentials
// saving destination registry credentials; destinationRegistryRepositoryMap -> map[registryName]= [<repo1>, <repo2>]
for registry, _ := range destinationRegistryRepositoryMap {
destinationRegistryCredential, err := impl.getPluginRegistryCredentialsByRegistryName(registry)
if err != nil {
return nil, err
}
registryCredentialsMap[registry] = *destinationRegistryCredential
}
return registryCredentialsMap, nil
}
func (impl *PluginInputVariableParserImpl) getPluginRegistryCredentialsByRegistryName(registryName string) (*plugin.RegistryCredentials, error) {
registryCredentials, err := impl.dockerRegistryConfig.FetchOneDockerAccount(registryName)
if err != nil {
impl.logger.Errorw("error in fetching registry details by registry name", "err", err)
if err == pg.ErrNoRows {
return nil, fmt.Errorf("invalid registry name: registry details not found in global container registries")
}
return nil, err
}
return &plugin.RegistryCredentials{
RegistryType: string(registryCredentials.RegistryType),
RegistryURL: registryCredentials.RegistryURL,
Username: registryCredentials.Username,
Password: registryCredentials.Password,
AWSRegion: registryCredentials.AWSRegion,
AWSSecretAccessKey: registryCredentials.AWSSecretAccessKey,
AWSAccessKeyId: registryCredentials.AWSAccessKeyId,
}, nil
}
func (impl *PluginInputVariableParserImpl) getRegistryDestinationImageMapping(
registryRepoMapping map[string][]string,
dockerImageTag string,
registryCredentials map[string]plugin.RegistryCredentials) map[string][]string {
// creating map with registry as key and list of destination images in that registry
registryDestinationImageMapping := make(map[string][]string)
for registry, destinationRepositories := range registryRepoMapping {
registryCredential := registryCredentials[registry]
var destinationImages []string
for _, repo := range destinationRepositories {
destinationImage := fmt.Sprintf("%s/%s:%s", registryCredential.RegistryURL, repo, dockerImageTag)
destinationImages = append(destinationImages, destinationImage)
}
registryDestinationImageMapping[registry] = destinationImages
}
return registryDestinationImageMapping
}
func (impl *PluginInputVariableParserImpl) createEcrRepoIfRequired(registryCredentials map[string]plugin.RegistryCredentials, registryRepoMapping map[string][]string) error {
for registry, registryCredential := range registryCredentials {
if registryCredential.RegistryType == dockerRegistryRepository.REGISTRYTYPE_ECR {
repositories := registryRepoMapping[registry]
for _, dockerRepo := range repositories {
err := util.CreateEcrRepo(dockerRepo, registryCredential.AWSRegion, registryCredential.AWSAccessKeyId, registryCredential.AWSSecretAccessKey)
if err != nil {
if errors1.IsAlreadyExists(err) {
impl.logger.Warnw("this repo already exists!!, skipping repo creation", "repo", dockerRepo)
} else {
impl.logger.Errorw("ecr repo creation failed, it might be due to authorization or any other external "+
"dependency. please create repo manually before triggering ci", "repo", dockerRepo, "err", err)
return err
}
}
}
}
}
return nil
}