-
Notifications
You must be signed in to change notification settings - Fork 36
/
component_build_controller_common.go
472 lines (419 loc) · 16.6 KB
/
component_build_controller_common.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Copyright 2023 Red Hat, 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 controllers
import (
"context"
"encoding/json"
"fmt"
"net/url"
"path/filepath"
"sort"
"strings"
buildappstudiov1alpha1 "github.com/konflux-ci/build-service/api/v1alpha1"
"github.com/konflux-ci/build-service/pkg/boerrors"
. "github.com/konflux-ci/build-service/pkg/common"
l "github.com/konflux-ci/build-service/pkg/logs"
pipelineselector "github.com/konflux-ci/build-service/pkg/pipeline-selector"
appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
devfile "github.com/redhat-appstudio/application-service/cdq-analysis/pkg"
tektonapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
)
type BuildPipeline struct {
Name string `json:"name,omitempty"`
Bundle string `json:"bundle,omitempty"`
}
type pipelineConfig struct {
DefaultPipelineName string `yaml:"default-pipeline-name"`
Pipelines []BuildPipeline `yaml:"pipelines"`
}
// That way it can be mocked in tests
var DevfileSearchForDockerfile = devfile.SearchForDockerfile
// getGitProvider returns git provider name based on the repository url, e.g. github, gitlab, etc or git-privider annotation
func getGitProvider(component appstudiov1alpha1.Component) (string, error) {
allowedGitProviders := map[string]bool{"github": true, "gitlab": true, "bitbucket": true}
gitProvider := ""
if component.Spec.Source.GitSource == nil {
err := fmt.Errorf("git source URL is not set for %s Component in %s namespace", component.Name, component.Namespace)
return "", err
}
sourceUrl := component.Spec.Source.GitSource.URL
if strings.HasPrefix(sourceUrl, "git@") {
// git@github.com:redhat-appstudio/application-service.git
sourceUrl = strings.TrimPrefix(sourceUrl, "git@")
host := strings.Split(sourceUrl, ":")[0]
gitProvider = strings.Split(host, ".")[0]
} else {
// https://github.com/redhat-appstudio/application-service
u, err := url.Parse(sourceUrl)
if err != nil {
return "", err
}
uParts := strings.Split(u.Hostname(), ".")
if len(uParts) == 1 {
gitProvider = uParts[0]
} else {
gitProvider = uParts[len(uParts)-2]
}
}
var err error
if !allowedGitProviders[gitProvider] {
// Self-hosted git provider, check for git-provider annotation on the component
gitProviderAnnotationValue := component.GetAnnotations()[GitProviderAnnotationName]
if gitProviderAnnotationValue != "" {
if allowedGitProviders[gitProviderAnnotationValue] {
gitProvider = gitProviderAnnotationValue
} else {
err = fmt.Errorf("unsupported \"%s\" annotation value: %s", GitProviderAnnotationName, gitProviderAnnotationValue)
}
} else {
err = fmt.Errorf("self-hosted git provider is not specified via \"%s\" annotation in the component", GitProviderAnnotationName)
}
}
return gitProvider, err
}
// GetBuildPipelineFromComponentAnnotation parses pipeline annotation on component and returns build pipeline
func (r *ComponentBuildReconciler) GetBuildPipelineFromComponentAnnotation(ctx context.Context, component *appstudiov1alpha1.Component) (*tektonapi.PipelineRef, error) {
buildPipeline, err := readBuildPipelineAnnotation(component)
if err != nil {
return nil, err
}
if buildPipeline == nil {
return nil, nil
}
if buildPipeline.Bundle == "" || buildPipeline.Name == "" {
err = fmt.Errorf("missing name or bundle in pipeline annotation: name=%s bundle=%s", buildPipeline.Name, buildPipeline.Bundle)
return nil, boerrors.NewBuildOpError(boerrors.EWrongPipelineAnnotation, err)
}
finalBundle := buildPipeline.Bundle
if buildPipeline.Bundle == "latest" {
pipelinesConfigMap := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, types.NamespacedName{Name: buildPipelineConfigMapResourceName, Namespace: BuildServiceNamespaceName}, pipelinesConfigMap); err != nil {
if errors.IsNotFound(err) {
return nil, boerrors.NewBuildOpError(boerrors.EBuildPipelineConfigNotDefined, err)
}
return nil, err
}
buildPipelineData := &pipelineConfig{}
if err := yaml.Unmarshal([]byte(pipelinesConfigMap.Data[buildPipelineConfigName]), buildPipelineData); err != nil {
return nil, boerrors.NewBuildOpError(boerrors.EBuildPipelineConfigNotValid, err)
}
for _, pipeline := range buildPipelineData.Pipelines {
if pipeline.Name == buildPipeline.Name {
finalBundle = pipeline.Bundle
break
}
}
// requested pipeline was not found in configMap
if finalBundle == "latest" {
err = fmt.Errorf("invalid pipeline name in pipeline annotation: name=%s", buildPipeline.Name)
return nil, boerrors.NewBuildOpError(boerrors.EBuildPipelineInvalid, err)
}
}
// for now we will return PipelineRef format, because it is the same what methods which use build-selector are returning
pipelineRef := &tektonapi.PipelineRef{
ResolverRef: tektonapi.ResolverRef{
Resolver: "bundles",
Params: []tektonapi.Param{
{Name: "name", Value: *tektonapi.NewStructuredValues(buildPipeline.Name)},
{Name: "bundle", Value: *tektonapi.NewStructuredValues(finalBundle)},
},
},
}
return pipelineRef, nil
}
// GetPipelineForComponent searches for the build pipeline to use on the component.
func (r *ComponentBuildReconciler) GetPipelineForComponent(ctx context.Context, component *appstudiov1alpha1.Component) (*tektonapi.PipelineRef, []tektonapi.Param, error) {
var pipelineSelectors []buildappstudiov1alpha1.BuildPipelineSelector
pipelineSelector := &buildappstudiov1alpha1.BuildPipelineSelector{}
pipelineSelectorKeys := []types.NamespacedName{
// First try specific config for the application
{Namespace: component.Namespace, Name: component.Spec.Application},
// Second try namespaced config
{Namespace: component.Namespace, Name: buildPipelineSelectorResourceName},
// Finally try global config
{Namespace: BuildServiceNamespaceName, Name: buildPipelineSelectorResourceName},
}
for _, pipelineSelectorKey := range pipelineSelectorKeys {
if err := r.Client.Get(ctx, pipelineSelectorKey, pipelineSelector); err != nil {
if !errors.IsNotFound(err) {
return nil, nil, err
}
// The config is not found, try the next one in the hierarchy
} else {
pipelineSelectors = append(pipelineSelectors, *pipelineSelector)
}
}
if len(pipelineSelectors) > 0 {
pipelineRef, pipelineParams, err := pipelineselector.SelectPipelineForComponent(component, pipelineSelectors)
if err != nil {
return nil, nil, err
}
if pipelineRef == nil {
return nil, nil, boerrors.NewBuildOpError(boerrors.ENoPipelineIsSelected, nil)
}
return pipelineRef, pipelineParams, nil
}
return nil, nil, boerrors.NewBuildOpError(boerrors.EBuildPipelineSelectorNotDefined, nil)
}
func (r *ComponentBuildReconciler) ensurePipelineServiceAccount(ctx context.Context, namespace string) (*corev1.ServiceAccount, error) {
log := ctrllog.FromContext(ctx)
pipelinesServiceAccount := &corev1.ServiceAccount{}
err := r.Client.Get(ctx, types.NamespacedName{Name: buildPipelineServiceAccountName, Namespace: namespace}, pipelinesServiceAccount)
if err != nil {
if !errors.IsNotFound(err) {
log.Error(err, fmt.Sprintf("Failed to read service account %s in namespace %s", buildPipelineServiceAccountName, namespace), l.Action, l.ActionView)
return nil, err
}
// Create service account for the build pipeline
buildPipelineSA := corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: buildPipelineServiceAccountName,
Namespace: namespace,
},
}
if err := r.Client.Create(ctx, &buildPipelineSA); err != nil {
log.Error(err, fmt.Sprintf("Failed to create service account %s in namespace %s", buildPipelineServiceAccountName, namespace), l.Action, l.ActionAdd)
return nil, err
}
return r.ensurePipelineServiceAccount(ctx, namespace)
}
return pipelinesServiceAccount, nil
}
func (r *ComponentBuildReconciler) linkSecretToServiceAccount(ctx context.Context, secretName, serviceAccountName, namespace string, isPullSecret bool) (bool, error) {
log := ctrllog.FromContext(ctx)
if secretName == "" {
// The secret is empty, no updates needed
return false, nil
}
serviceAccount := &corev1.ServiceAccount{}
err := r.Client.Get(ctx, types.NamespacedName{Name: serviceAccountName, Namespace: namespace}, serviceAccount)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
log.Error(err, fmt.Sprintf("Failed to read service account %s in namespace %s", serviceAccountName, namespace), l.Action, l.ActionView)
return false, err
}
isNewSecretLinked := false
shouldAddLink := true
for _, secret := range serviceAccount.Secrets {
if secret.Name == secretName {
// The secret is present in the service account, no updates needed
shouldAddLink = false
break
}
}
if shouldAddLink {
serviceAccount.Secrets = append(serviceAccount.Secrets, corev1.ObjectReference{Name: secretName, Namespace: namespace})
}
isNewSecretLinked = shouldAddLink
if isPullSecret {
shouldAddLink = true
for _, secret := range serviceAccount.ImagePullSecrets {
if secret.Name == secretName {
// The secret is present in the service account, no updates needed
shouldAddLink = false
break
}
}
if shouldAddLink {
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, corev1.LocalObjectReference{Name: secretName})
}
isNewSecretLinked = isNewSecretLinked || shouldAddLink
}
// Update service account if needed
if isNewSecretLinked {
err := r.Client.Update(ctx, serviceAccount)
if err != nil {
log.Error(err, fmt.Sprintf("Unable to update service account %s", serviceAccount.Name), l.Action, l.ActionUpdate)
return false, err
}
log.Info(fmt.Sprintf("Service Account %s updated with secret %s", serviceAccount.Name, secretName), l.Action, l.ActionUpdate)
return true, nil
}
return false, nil
}
// unlinkSecretFromServiceAccount ensures that the given secret is not linked with the provided service account.
// Returns true if the secret was unlinked, false if the link didn't exist.
func (r *ComponentBuildReconciler) unlinkSecretFromServiceAccount(ctx context.Context, secretNameToRemove, serviceAccountName, namespace string) (bool, error) {
log := ctrllog.FromContext(ctx)
serviceAccount := &corev1.ServiceAccount{}
err := r.Client.Get(ctx, types.NamespacedName{Name: serviceAccountName, Namespace: namespace}, serviceAccount)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
log.Error(err, fmt.Sprintf("Failed to read service account %s in namespace %s", serviceAccountName, namespace), l.Action, l.ActionView)
return false, err
}
isSecretUnlinked := false
// Remove secret from secrets list
for index, credentialSecret := range serviceAccount.Secrets {
if credentialSecret.Name == secretNameToRemove {
secrets := make([]corev1.ObjectReference, 0, len(serviceAccount.Secrets)-1)
if len(serviceAccount.Secrets) != 1 {
secrets = append(secrets, serviceAccount.Secrets[:index]...)
secrets = append(secrets, serviceAccount.Secrets[index+1:]...)
}
serviceAccount.Secrets = secrets
isSecretUnlinked = true
break
}
}
// Remove secret from pull secrets list
for index, pullSecret := range serviceAccount.ImagePullSecrets {
if pullSecret.Name == secretNameToRemove {
secrets := make([]corev1.LocalObjectReference, 0, len(serviceAccount.ImagePullSecrets)-1)
if len(serviceAccount.ImagePullSecrets) != 1 {
secrets = append(secrets, serviceAccount.ImagePullSecrets[:index]...)
secrets = append(secrets, serviceAccount.ImagePullSecrets[index+1:]...)
}
serviceAccount.ImagePullSecrets = secrets
isSecretUnlinked = true
break
}
}
if isSecretUnlinked {
if err := r.Client.Update(ctx, serviceAccount); err != nil {
log.Error(err, fmt.Sprintf("Unable to update pipeline service account %v", serviceAccount), l.Action, l.ActionUpdate)
return false, err
}
log.Info(fmt.Sprintf("Removed %s secret link from %s service account", secretNameToRemove, serviceAccount.Name), l.Action, l.ActionUpdate)
}
return isSecretUnlinked, nil
}
func getContainerImageRepositoryForComponent(component *appstudiov1alpha1.Component) string {
if component.Spec.ContainerImage != "" {
return getContainerImageRepository(component.Spec.ContainerImage)
}
imageRepo, _, err := getComponentImageRepoAndSecretNameFromImageAnnotation(component)
if err == nil && imageRepo != "" {
return imageRepo
}
return ""
}
// getContainerImageRepository removes tag or SHA has from container image reference
func getContainerImageRepository(image string) string {
if strings.Contains(image, "@") {
// registry.io/user/image@sha256:586ab...d59a
return strings.Split(image, "@")[0]
}
// registry.io/user/image:tag
return strings.Split(image, ":")[0]
}
// getComponentImageRepoAndSecretNameFromImageAnnotation parses image.redhat.com/image annotation
// for image repository and secret name to access it.
// If image.redhat.com/image is not set, the procedure returns empty values.
func getComponentImageRepoAndSecretNameFromImageAnnotation(component *appstudiov1alpha1.Component) (string, string, error) {
type RepositoryInfo struct {
Image string `json:"image"`
Secret string `json:"secret"`
}
var repoInfo RepositoryInfo
if imageRepoDataJson, exists := component.Annotations[ImageRepoAnnotationName]; exists {
if err := json.Unmarshal([]byte(imageRepoDataJson), &repoInfo); err != nil {
return "", "", boerrors.NewBuildOpError(boerrors.EFailedToParseImageAnnotation, err)
}
return repoInfo.Image, repoInfo.Secret, nil
}
return "", "", nil
}
// mergeAndSortTektonParams merges additional params into existing params by adding new or replacing existing values.
func mergeAndSortTektonParams(existedParams, additionalParams []tektonapi.Param) []tektonapi.Param {
var params []tektonapi.Param
paramsMap := make(map[string]tektonapi.Param)
for _, p := range existedParams {
paramsMap[p.Name] = p
}
for _, p := range additionalParams {
paramsMap[p.Name] = p
}
for _, v := range paramsMap {
params = append(params, v)
}
sort.Slice(params, func(i, j int) bool {
return params[i].Name < params[j].Name
})
return params
}
func generateVolumeClaimTemplate() *corev1.PersistentVolumeClaim {
return &corev1.PersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
"ReadWriteOnce",
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"storage": resource.MustParse("1Gi"),
},
},
},
}
}
func getPathContext(gitContext, dockerfileContext string) string {
if gitContext == "" && dockerfileContext == "" {
return ""
}
separator := string(filepath.Separator)
path := filepath.Join(gitContext, dockerfileContext)
path = filepath.Clean(path)
path = strings.TrimPrefix(path, separator)
return path
}
func getPipelineNameAndBundle(pipelineRef *tektonapi.PipelineRef) (string, string, error) {
if pipelineRef.Resolver != "" && pipelineRef.Resolver != "bundles" {
return "", "", boerrors.NewBuildOpError(
boerrors.EUnsupportedPipelineRef,
fmt.Errorf("unsupported Tekton resolver %q", pipelineRef.Resolver),
)
}
name := pipelineRef.Name
var bundle string
for _, param := range pipelineRef.Params {
switch param.Name {
case "name":
name = param.Value.StringVal
case "bundle":
bundle = param.Value.StringVal
}
}
if name == "" || bundle == "" {
return "", "", boerrors.NewBuildOpError(
boerrors.EMissingParamsForBundleResolver,
fmt.Errorf("missing name or bundle in pipelineRef: name=%s bundle=%s", name, bundle),
)
}
return name, bundle, nil
}
func readBuildPipelineAnnotation(component *appstudiov1alpha1.Component) (*BuildPipeline, error) {
if component.Annotations == nil {
return nil, nil
}
requestedPipeline, requestedPipelineExists := component.Annotations[defaultBuildPipelineAnnotation]
if requestedPipelineExists && requestedPipeline != "" {
buildPipeline := &BuildPipeline{}
buildPipelineBytes := []byte(requestedPipeline)
if err := json.Unmarshal(buildPipelineBytes, buildPipeline); err != nil {
return nil, boerrors.NewBuildOpError(boerrors.EFailedToParsePipelineAnnotation, err)
}
return buildPipeline, nil
}
return nil, nil
}