This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
inputmapper.go
101 lines (82 loc) · 2.58 KB
/
inputmapper.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
package inputmapper
import (
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/db"
"github.com/concourse/atc/db/algorithm"
"github.com/concourse/atc/scheduler/inputmapper/inputconfig"
)
//go:generate counterfeiter . InputMapper
type InputMapper interface {
SaveNextInputMapping(
logger lager.Logger,
versions *algorithm.VersionsDB,
job db.Job,
resources db.Resources,
) (algorithm.InputMapping, error)
}
func NewInputMapper(pipeline db.Pipeline, transformer inputconfig.Transformer) InputMapper {
return &inputMapper{pipeline: pipeline, transformer: transformer}
}
type inputMapper struct {
pipeline db.Pipeline
transformer inputconfig.Transformer
}
func (i *inputMapper) SaveNextInputMapping(
logger lager.Logger,
versions *algorithm.VersionsDB,
job db.Job,
resources db.Resources,
) (algorithm.InputMapping, error) {
logger = logger.Session("save-next-input-mapping")
inputConfigs := job.Config().Inputs()
for i, inputConfig := range inputConfigs {
resource, found := resources.Lookup(inputConfig.Resource)
if !found {
logger.Debug("failed-to-find-resource")
continue
}
if len(resource.PinnedVersion()) != 0 {
inputConfigs[i].Version = &atc.VersionConfig{Pinned: resource.PinnedVersion()}
}
}
algorithmInputConfigs, err := i.transformer.TransformInputConfigs(versions, job.Name(), inputConfigs)
if err != nil {
logger.Error("failed-to-get-algorithm-input-configs", err)
return nil, err
}
independentMapping := algorithm.InputMapping{}
for _, inputConfig := range algorithmInputConfigs {
singletonMapping, ok := algorithm.InputConfigs{inputConfig}.Resolve(versions)
if ok {
independentMapping[inputConfig.Name] = singletonMapping[inputConfig.Name]
}
}
err = job.SaveIndependentInputMapping(independentMapping)
if err != nil {
logger.Error("failed-to-save-independent-input-mapping", err)
return nil, err
}
if len(independentMapping) < len(inputConfigs) {
// this is necessary to prevent builds from running with missing pinned versions
err := job.DeleteNextInputMapping()
if err != nil {
logger.Error("failed-to-delete-next-input-mapping-after-missing-pending", err)
}
return nil, err
}
resolvedMapping, ok := algorithmInputConfigs.Resolve(versions)
if !ok {
err := job.DeleteNextInputMapping()
if err != nil {
logger.Error("failed-to-delete-next-input-mapping-after-failed-resolve", err)
}
return nil, err
}
err = job.SaveNextInputMapping(resolvedMapping)
if err != nil {
logger.Error("failed-to-save-next-input-mapping", err)
return nil, err
}
return resolvedMapping, nil
}