/
templating.go
271 lines (250 loc) · 9.9 KB
/
templating.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
// Copyright 2022 Google LLC
//
// 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 krmtotf
import (
"fmt"
"regexp"
"strings"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
corekccv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/text"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var fieldRegex = regexp.MustCompile("{{([0-9A-Za-z_.?]+)}}")
func ResolveValueTemplate(template string, val string, r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (string, error) {
if template == "" {
// An empty template is defined as no expansion necessary.
return val, nil
}
ret := strings.ReplaceAll(template, "{{value}}", val)
// Check to see if there are any Terraform fields to expand. If so, send it through field
// expansion.
if fieldRegex.MatchString(ret) {
return expandTemplate(ret, r, c, smLoader)
}
return ret, nil
}
func resolveValueTemplateFromInterface(template string, val interface{}, r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (interface{}, error) {
if template == "" {
// An empty template is defined as no expansion necessary.
return val, nil
}
switch valAsType := val.(type) {
case string:
return ResolveValueTemplate(template, valAsType, r, c, smLoader)
default:
return nil, fmt.Errorf("cannot resolve value template for non-string type")
}
}
// returns true if the value could have been obtained from the template, i.e. if 'template' is "folders/{{value}}"
// - if 'value' is "folders/1234567" the result will be true
// - if 'value' is "organizations/1234567" the result will be false
func valueMatchesTemplate(template string, value string) bool {
if template == "" {
return true
}
template = strings.ReplaceAll(template, "{{value}}", ".*")
r := regexp.MustCompile(template)
return r.MatchString(value)
}
func expandTemplate(template string, r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (string, error) {
leftBracketIdx := strings.Index(template, "[")
if leftBracketIdx == -1 {
if isORTemplate(template) {
return expandOrTemplate(template, r, c, smLoader)
}
return expandFieldTemplate(template, r, c, smLoader)
}
rightBracketIdx := strings.LastIndex(template, "]")
if rightBracketIdx == -1 {
return "", fmt.Errorf("template '%v' has a left bracket but is missing corresponding right bracket", template)
}
subTemplate := template[leftBracketIdx+1 : rightBracketIdx]
result, err := expandTemplate(subTemplate, r, c, smLoader)
if err != nil {
return "", fmt.Errorf("error resolving sub-template: %w", err)
}
resolvedTemplate := fmt.Sprintf("%v%v%v", template[:leftBracketIdx], result, template[rightBracketIdx+1:])
return expandTemplate(resolvedTemplate, r, c, smLoader)
}
func isORTemplate(template string) bool {
return strings.Contains(template, "|")
}
// expandFieldTemplate expands the given template by replacing any occurrence of {{field_name}} with
// the value of that field. Note that this may trigger further recursive field expansions, if the
// field's value depends on further template expansion.
func expandFieldTemplate(template string, r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (string, error) {
var resolutionError error
resolveFunc := func(s string) string {
field := fieldRegex.FindStringSubmatch(s)[1]
isRequired := true
if strings.HasSuffix(field, "?") {
isRequired = false
field = strings.TrimSuffix(field, "?")
}
if isRef, refConfig := IsReferenceField(field, &r.ResourceConfig); isRef {
val, found, err := getValueFromReference(refConfig, r, c, smLoader)
if err != nil {
resolutionError = fmt.Errorf("error getting value from reference: %w", err)
return ""
}
if !found {
if isRequired {
resolutionError = fmt.Errorf("required reference '%v' could not be found in spec", field)
}
return ""
}
return val
}
if field == r.ResourceConfig.ResourceID.TargetField {
val, err := resolveResourceID(r, c, smLoader)
if err != nil {
resolutionError = fmt.Errorf("error resolving resource ID: %w", err)
return ""
}
return val
}
if field == r.ResourceConfig.MetadataMapping.Name {
val, err := resolveNameMetadataMapping(r, c, smLoader)
if err != nil {
resolutionError = fmt.Errorf("error resolving metadata name mapping value: %w", err)
return ""
}
return val
}
if field == r.ResourceConfig.MetadataMapping.Labels {
resolutionError = fmt.Errorf("cannot map labels (map[string]string) to string field '%v'", field)
return ""
}
if field == "region" && r.ResourceConfig.Locationality == gcp.Regional ||
field == "zone" && r.ResourceConfig.Locationality == gcp.Zonal {
if val, exists, _ := unstructured.NestedString(r.Spec, "location"); exists {
return val
}
}
if !SupportsHierarchicalReferences(&r.ResourceConfig) {
// TODO(b/193177782): Delete this if-block once all resources
// support hierarchical references.
for _, c := range r.ResourceConfig.Containers {
if field == c.TFField {
annotation := k8s.GetAnnotationForContainerType(c.Type)
val, ok := k8s.GetAnnotation(annotation, r)
if (!ok || val == "") && isRequired {
resolutionError = fmt.Errorf("no value found for annotation %v", annotation)
return ""
}
return val
}
}
}
for _, d := range r.ResourceConfig.Directives {
if field == d {
annotation := k8s.FormatAnnotation(text.SnakeCaseToKebabCase(d))
val, ok := k8s.GetAnnotation(annotation, r)
if (!ok || val == "") && isRequired {
resolutionError = fmt.Errorf("no value found for annotation %v", annotation)
return ""
}
return val
}
}
path := text.SnakeCaseToLowerCamelCase(field)
if val, exists, _ := unstructured.NestedString(r.Spec, strings.Split(path, ".")...); exists {
return val
}
if val, exists, _ := unstructured.NestedString(r.GetStatusOrObservedState(), strings.Split(path, ".")...); exists {
return val
}
if isRequired {
resolutionError = fmt.Errorf("unable to resolve missing value: %v", field)
}
return ""
}
return fieldRegex.ReplaceAllStringFunc(template, resolveFunc), resolutionError
}
func expandOrTemplate(template string, r *Resource, c client.Client, smLoader *servicemappingloader.ServiceMappingLoader) (string, error) {
templates := strings.Split(template, "|")
expectedCount := 2
if len(templates) != expectedCount {
return "", fmt.Errorf("unexpected template format: after splitting on '|' there are '%v' templates when %v were expected",
len(templates), expectedCount)
}
t1 := templates[0]
t2 := templates[1]
result, err := expandTemplate(t1, r, c, smLoader)
if err == nil {
return result, nil
}
result, err = expandTemplate(t2, r, c, smLoader)
if err != nil {
return "", fmt.Errorf("error resolving both sides of an '|' template: %w", err)
}
return result, nil
}
func getValueFromReference(refConfig *corekccv1alpha1.ReferenceConfig, r *Resource, c client.Client,
smLoader *servicemappingloader.ServiceMappingLoader) (val string, found bool, err error) {
pathToRef := getPathToReferenceKey(refConfig)
refObj, ok, err := unstructured.NestedMap(r.Spec, pathToRef...)
if err != nil {
return "", false, fmt.Errorf("error getting reference object '%v': %w", strings.Join(pathToRef, "."), err)
}
if !ok {
return "", false, nil
}
retRaw, err := ResolveReferenceObject(refObj, *refConfig, r, c, smLoader)
if err != nil {
return "", false, fmt.Errorf("error resolving reference field: %w", err)
}
ret, ok := retRaw.(string)
if !ok {
return "", false, fmt.Errorf("could not parse reference resolution value '%+v' as string", retRaw)
}
return ret, true, nil
}
func extractValueSegmentFromIDInStatus(idInStatus, template string) (string, error) {
if template == "" {
return idInStatus, nil
}
// Convert the template to be a compilable regular expression.
template = strings.ReplaceAll(template, "{{value}}", "([^/]+)")
// Template starting with the parent field value may contain additional
// slashes that are not in the template.
// E.g. DataCatalogPolicyTag has resource ID template:
// "{{taxonomy}}/policyTags/{{value}}", however the value of the parent
// field, 'spec.taxonomy', is "projects/test-project/locations/us/taxonomies/tid",
// which contains additional slashes that are not captured in the template.
if strings.HasPrefix(template, "{{") {
re := regexp.MustCompile(`^({{[a-z]([a-z_]*[a-z])*}})/.*$`)
matched := re.FindStringSubmatch(template)
if len(matched) == 0 {
return "", fmt.Errorf("error extracting the parent field name from resource ID template %v", template)
}
parentField := matched[1]
template = strings.ReplaceAll(template, parentField, "[^/](.*[^/])*")
}
template = fieldRegex.ReplaceAllString(template, "[^/]+")
template = fmt.Sprintf("%s%s%s", "^", template, "$")
// Extract out the resourceID from the value.
templateRegex := regexp.MustCompile(template)
subMatches := templateRegex.FindStringSubmatch(idInStatus)
if len(subMatches) < 2 {
return "", fmt.Errorf("error extracting out the value segment "+
"from the idInStatus template '%s' using template '%s'", idInStatus,
template)
}
return subMatches[len(subMatches)-1], nil
}