/
composeservice.go
140 lines (110 loc) · 3.77 KB
/
composeservice.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
package composeservice
import (
"errors"
"fmt"
"github.com/microsoft/abstrakt/internal/buildmapservice"
"github.com/microsoft/abstrakt/internal/chartservice"
"github.com/microsoft/abstrakt/internal/dagconfigservice"
"helm.sh/helm/v3/pkg/chart"
)
//ComposeService takes maps and configs and builds out the helm chart
type ComposeService struct {
DagConfigService dagconfigservice.DagConfigService
BuildMapService buildmapservice.BuildMapService
}
//Compose takes the loaded DAG and maps and builds the Helm values and requirements documents
func (m *ComposeService) Compose(name string, dir string) (*chart.Chart, error) {
if m.DagConfigService.Name == "" || m.BuildMapService.Name == "" {
return nil, errors.New("Please initialise with LoadFromFile or LoadFromString")
}
newChart, err := chartservice.CreateChart(name, dir)
if err != nil {
return nil, err
}
serviceMap := make(map[string]int)
aliasMap := make(map[string]string)
deps := make([]*chart.Dependency, 0)
values := newChart.Values
for _, n := range m.DagConfigService.Services {
service := m.BuildMapService.FindByType(n.Type)
if service == nil {
return nil, fmt.Errorf("Could not find service: %v", service)
}
count := serviceMap[service.Type]
alias := service.ChartName
if count > 0 {
alias = fmt.Sprintf("%v%v", alias, count)
}
serviceMap[service.Type]++
dep := &chart.Dependency{
Name: service.ChartName, Version: service.Version, Repository: service.Location,
}
if count > 0 {
dep.Alias = alias
}
aliasMap[string(n.ID)] = alias
deps = append(deps, dep)
valMap := make(map[string]interface{})
values[alias] = &valMap
valMap["name"] = alias
valMap["type"] = service.Type
relationships := make(map[string]interface{})
valMap["relationships"] = &relationships
toRels := m.DagConfigService.FindRelationshipByToID(n.ID)
fromRels := m.DagConfigService.FindRelationshipByFromID(n.ID)
if toRels != nil {
toRelations := make(map[string]string)
relationships["input"] = &toRelations
//find the target service
foundService := m.DagConfigService.FindServiceByID(toRels.From)
toRelations["service"] = string(toRels.ID)
toRelations["type"] = foundService.Type
closure := func() { //ensure this only runs once all the counting is done
relAlias := aliasMap[string(foundService.ID)]
toRelations["name"] = relAlias
}
defer closure()
}
if fromRels != nil {
fromRelations := make(map[string]string)
relationships["output"] = &fromRelations
//find the target service
foundService := m.DagConfigService.FindServiceByID(fromRels.To)
fromRelations["service"] = string(fromRels.ID)
fromRelations["type"] = foundService.Type
closure := func() { //ensure this only runs once all the counting is done
relAlias := aliasMap[string(foundService.ID)]
fromRelations["name"] = relAlias
}
defer closure()
}
}
newChart.Values = values
newChart.Metadata.Dependencies = deps
return newChart, nil
}
//LoadFromFile takes a string dag and map and loads them
func (m *ComposeService) LoadFromFile(dagFile string, mapFile string) (err error) {
err = m.DagConfigService.LoadDagConfigFromFile(dagFile)
if err != nil {
return err
}
err = m.BuildMapService.LoadMapFromFile(mapFile)
return
}
//LoadFromString takes a string dag and map and loads them
func (m *ComposeService) LoadFromString(dagString string, mapString string) (err error) {
err = m.DagConfigService.LoadDagConfigFromString(dagString)
if err != nil {
return
}
err = m.BuildMapService.LoadMapFromString(mapString)
return
}
//NewComposeService constructs a new compose service
func NewComposeService() ComposeService {
s := ComposeService{}
s.DagConfigService = dagconfigservice.NewDagConfigService()
s.BuildMapService = buildmapservice.NewBuildMapService()
return s
}