-
Notifications
You must be signed in to change notification settings - Fork 18
/
format.go
226 lines (200 loc) · 7.4 KB
/
format.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package localize
import (
"encoding/json"
"fmt"
"strings"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm"
v1 "github.com/open-component-model/ocm/v2/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/utils"
"github.com/open-component-model/ocm/v2/pkg/errors"
"github.com/open-component-model/ocm/v2/pkg/runtime"
)
// Definition of inbound substitution requests.
// - Localizations image locations substitution resolved using a component version
// - Configurations configuration substitution resolved by provided value data.
//
// Such requests can be given to merge externally provided data into
// some filesystem template.
// The evaluation of such requests results in a list
// of resolved substitution requests that can be applied without
// further value context to a filesystem structure.
// ImageMapping describes a dedicated substitution of parts
// of container image names based on a relative OCM resource reference.
type ImageMapping struct {
// The optional but unique(!) name of the mapping to support referencing mapping entries
Name string `json:"name,omitempty"`
// The resource reference used to resolve the substitution
v1.ResourceReference `json:",inline"`
// The optional variants for the value determination
// Path in target to substitute by the image tag/digest
Tag string `json:"tag,omitempty"`
// Path in target to substitute the image repository
Repository string `json:"repository,omitempty"`
// Path in target to substitute the complete image
Image string `json:"image,omitempty"`
}
type ImageMappings []ImageMapping
func (m *ImageMapping) Evaluate(idx int, cv ocm.ComponentVersionAccess, resolver ocm.ComponentVersionResolver) (ValueMappings, error) {
name := "image mapping"
if m.Name != "" {
name = fmt.Sprintf("%s %q", name, m.Name)
} else { //nolint: gocritic // yes
if idx >= 0 {
name = fmt.Sprintf("%s %d", name, idx+1)
}
}
acc, rcv, err := utils.ResolveResourceReference(cv, m.ResourceReference, resolver)
if err != nil {
return nil, errors.ErrNotFoundWrap(err, "mapping", fmt.Sprintf("%s (%s)", name, &m.ResourceReference))
}
rcv.Close()
ref, err := utils.GetOCIArtifactRef(cv.GetContext(), acc)
if err != nil {
return nil, errors.Wrapf(err, "mapping %s: cannot resolve resource %s to an OCI Reference", name, &m.ResourceReference)
}
ix := strings.Index(ref, ":")
if ix < 0 {
ix = strings.Index(ref, "@")
if ix < 0 {
return nil, errors.Wrapf(err, "mapping %s: image tag or digest missing (%s)", name, ref)
}
}
repo := ref[:ix]
tag := ref[ix+1:]
cnt := 0
if m.Repository != "" {
cnt++
}
if m.Tag != "" {
cnt++
}
if m.Image != "" {
cnt++
}
if cnt == 0 {
return nil, fmt.Errorf("no substitution target given for %s", name)
}
var result ValueMappings
var r *ValueMapping
if m.Repository != "" {
if r, err = NewValueMapping(substitutionName(name, "repository", cnt), m.Repository, repo); err != nil {
return nil, errors.Wrapf(err, "setting repository for %s", substitutionName(name, "repository", cnt))
}
result = append(result, *r)
}
if m.Tag != "" {
if r, err = NewValueMapping(substitutionName(name, "tag", cnt), m.Tag, tag); err != nil {
return nil, errors.Wrapf(err, "setting tag for %s", substitutionName(name, "tag", cnt))
}
result = append(result, *r)
}
if m.Image != "" {
if r, err = NewValueMapping(substitutionName(name, "image", cnt), m.Image, ref); err != nil {
return nil, errors.Wrapf(err, "setting image for %s", substitutionName(name, "image", cnt))
}
result = append(result, *r)
}
return result, nil
}
// Localization is a request to substitute an image location.
// The specification describes substitution targets given by the file path and
// the YAML/JSON value paths of the elements in this file.
// The substitution value is calculated
// from the access specification of the given resource provided by the actual
// component version.
type Localization struct {
// The path of the file for the substitution
FilePath string `json:"file"`
// The image mapping request
ImageMapping `json:",inline"`
}
// Configuration is a request to substitute a configuration value.
// The specification describes substitution targets given by the file path and
// the YAML/JSON value paths of the elements in this file.
// The substitution value is calculated
// by the value expression (spiff) based on given config data.
// It has the same structure as Substitution, but is a request based
// on external configuration data, while a Substitution describes a fixed target
// value.
type Configuration Substitution
type ValueMapping struct {
// The optional but unique(!) name of the mapping to support referencing mapping entries
Name string `json:"name,omitempty"`
// The target path for the value substitution
ValuePath string `json:"path"`
// The value to set
Value json.RawMessage `json:"value"`
}
func NewValueMapping(name, path string, value interface{}) (*ValueMapping, error) {
var (
v []byte
err error
)
if value != nil {
v, err = runtime.DefaultJSONEncoding.Marshal(value)
if err != nil {
return nil, fmt.Errorf("cannot marshal substitution value: %w", err)
}
}
return &ValueMapping{
Name: name,
ValuePath: path,
Value: v,
}, nil
}
type ValueMappings []ValueMapping
func (s *ValueMappings) Add(name, path string, value interface{}) error {
m, err := NewValueMapping(name, path, value)
if err != nil {
return err
}
*s = append(*s, *m)
return nil
}
// Here comes the structure used for resolved execution requests.
// They can be applied to a filesystem content without further external information.
// It basically has the same structure as the configuration request, but
// the given value is just the target value without any further interpretation.
// This way configuration requests could just provide dedicated values, also
// Substitution is a request to substitute the YAML/JSON
// element given by the value path in the given file path by the given
// direct value.
type Substitution struct {
// The path of the file for the substitution
FilePath string `json:"file"`
// The field mapping toapply to given file path
ValueMapping `json:",inline"`
}
func (s *Substitution) GetValue() (interface{}, error) {
var value interface{}
err := runtime.DefaultYAMLEncoding.Unmarshal(s.Value, &value)
return value, err
}
type Substitutions []Substitution
func (s *Substitutions) AddValueMapping(m *ValueMapping, file string) {
*s = append(*s, Substitution{
FilePath: file,
ValueMapping: *m,
})
}
func (s *Substitutions) Add(name, file, path string, value interface{}) error {
m, err := NewValueMapping(name, path, value)
if err != nil {
return err
}
s.AddValueMapping(m, file)
return nil
}
// InstantiationRules bundle the localization of a filesystem resource
// covering image localization and applying instance configuration.
type InstantiationRules struct {
Template v1.ResourceReference `json:"templateResource,omitempty"`
LocalizationRules []Localization `json:"localizationRules,omitempty"`
ConfigRules []Configuration `json:"configRules,omitempty"`
ConfigScheme json.RawMessage `json:"configScheme,omitempty"`
ConfigTemplate json.RawMessage `json:"configTemplate,omitempty"`
ConfigLibraries []v1.ResourceReference `json:"configLibraries,omitempty"`
}