Skip to content

Commit bb77716

Browse files
authored
Merge pull request #174 from tv2-oss/feature/155-loops-and-multiple-resources-in-templates
Support multiple resources per template, aka. loops and conditions
2 parents 9dc5ea2 + 0117473 commit bb77716

File tree

10 files changed

+293
-110
lines changed

10 files changed

+293
-110
lines changed

blueprints/aws-alb-crossplane/gatewayclassblueprint-aws-alb-crossplane.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ spec:
3333
template: |
3434
addresses:
3535
- type: Hostname
36-
value: {{ .Resources.LB.status.atProvider.dnsName }}
36+
value: {{ (index .Resources.LB 0).status.atProvider.dnsName }}
3737
resourceTemplates:
3838
childGateway: |
3939
apiVersion: gateway.networking.k8s.io/v1beta1
@@ -153,7 +153,7 @@ spec:
153153
{{- toYaml .Values.tags | nindent 4 }}
154154
{{ end }}
155155
spec:
156-
targetGroupARN: {{ .Resources.LBTargetGroup.status.atProvider.arn }}
156+
targetGroupARN: {{ (index .Resources.LBTargetGroup 0).status.atProvider.arn }}
157157
targetType: ip
158158
serviceRef:
159159
name: {{ .Gateway.metadata.name }}-child

blueprints/contour-istio/gatewayclassblueprint-contour-istio-cert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spec:
99
status:
1010
template: |
1111
addresses:
12-
{{ range .Resources.loadBalancer.status.loadBalancer.ingress }}
12+
{{ range (index .Resources.loadBalancer 0).status.loadBalancer.ingress }}
1313
- type: IPAddress
1414
value: {{ .ip }}
1515
{{ end }}

controllers/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ spec:
173173
name: default
174174
`
175175

176-
var _ = Describe("Common functions", func() {
176+
var _ = Describe("Attached policies and value precedence", func() {
177177

178178
const (
179179
timeout = time.Second * 10

controllers/gateway_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
186186
if tmpl, errs := parseSingleTemplate("status", tmplStr); errs != nil {
187187
logger.Info("unable to parse status template", "temporary error", errs)
188188
} else {
189-
if statusMap, errs := template2map(tmpl, &templateValues); errs != nil {
189+
if statusMap, errs := template2maps(tmpl, &templateValues); errs != nil {
190190
logger.Info("unable to render status template", "temporary error", errs, "template", tmplStr, "values", templateValues)
191191
} else {
192192
gw.Status.Addresses = []gatewayapi.GatewayAddress{}
193-
_, found := statusMap["addresses"]
193+
_, found := statusMap[0]["addresses"] // FIXME, more addresses?
194194
if found {
195-
addresses := statusMap["addresses"]
195+
addresses := statusMap[0]["addresses"]
196196
if errs := mapstructure.Decode(addresses, &gw.Status.Addresses); errs != nil {
197197
// This is probably not a temporary error
198198
logger.Error(errs, "unable to decode status data")

controllers/gateway_controller_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ kind: GatewayClassBlueprint
8181
metadata:
8282
name: default-gateway-class
8383
spec:
84+
values:
85+
default:
86+
configmap2SuffixData:
87+
- one
88+
- two
89+
- three
8490
gatewayTemplate:
8591
status:
8692
template: |
8793
addresses:
88-
{{ toYaml .Resources.childGateway.status.addresses | nindent 2}}
94+
{{ toYaml (index .Resources.childGateway 0).status.addresses | nindent 2}}
8995
resourceTemplates:
9096
childGateway: |
9197
apiVersion: gateway.networking.k8s.io/v1beta1
@@ -109,14 +115,25 @@ spec:
109115
data:
110116
valueToRead1: Hello
111117
valueToRead2: World
112-
configMapTestIntermediate: |
118+
configMapTestIntermediate1: |
113119
apiVersion: v1
114120
kind: ConfigMap
115121
metadata:
116-
name: intermediate-configmap
122+
name: intermediate1-configmap
117123
namespace: {{ .Gateway.metadata.namespace }}
118124
data:
119-
valueIntermediate: {{ .Resources.configMapTestSource.data.valueToRead1 }}
125+
valueIntermediate: {{ (index .Resources.configMapTestSource 0).data.valueToRead1 }}
126+
configMapTestIntermediate2: |
127+
{{ range $idx,$suffix := .Values.configmap2SuffixData }}
128+
apiVersion: v1
129+
kind: ConfigMap
130+
metadata:
131+
name: intermediate2-configmap-{{ $idx }}
132+
namespace: {{ $.Gateway.metadata.namespace }}
133+
data:
134+
valueIntermediate: {{ (index $.Resources.configMapTestSource 0).data.valueToRead1 }}-{{ $suffix }}
135+
---
136+
{{ end }}
120137
# Use references to multiple resources coupled with template pipeline and functions
121138
configMapTestDestination: |
122139
apiVersion: v1
@@ -125,7 +142,8 @@ spec:
125142
name: dst-configmap
126143
namespace: {{ .Gateway.metadata.namespace }}
127144
data:
128-
valueRead: {{ printf "%s, %s" .Resources.configMapTestIntermediate.data.valueIntermediate .Resources.configMapTestSource.data.valueToRead2 | upper }}
145+
valueRead: {{ printf "%s, %s" (index .Resources.configMapTestIntermediate1 0).data.valueIntermediate (index .Resources.configMapTestSource 0).data.valueToRead2 | upper }}
146+
valueRead2: {{ printf "Testing, one two %s" (index .Resources.configMapTestIntermediate2 2).data.valueIntermediate | upper }}
129147
httpRouteTemplate:
130148
resourceTemplates:
131149
shadowHttproute: |
@@ -270,6 +288,7 @@ var _ = Describe("Gateway controller", func() {
270288

271289
By("Setting the content of the destination configmap")
272290
Expect(cm.Data["valueRead"]).To(Equal("HELLO, WORLD"))
291+
Expect(cm.Data["valueRead2"]).To(Equal("TESTING, ONE TWO HELLO-THREE"))
273292
})
274293
})
275294
})

controllers/statuscheck.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,40 @@ limitations under the License.
3232
package controllers
3333

3434
import (
35+
"fmt"
36+
3537
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
3638
)
3739

3840
// Given a slice of template states, compute the overall
3941
// health/readiness status. The general approach is to test for a
4042
// `Ready` status condition, which is implemented through kstatus.
41-
func statusIsReady(templates []*TemplateResource) (bool, error) {
42-
for _, tmplRes := range templates {
43-
if tmplRes.Current == nil {
44-
return false, nil
45-
}
46-
res, err := status.Compute(tmplRes.Current)
47-
if err != nil {
48-
return false, err
49-
}
50-
if res.Status != status.CurrentStatus {
51-
return false, nil
43+
func statusIsReady(templates []*ResourceTemplateState) (bool, error) {
44+
for _, tmpl := range templates {
45+
for _, res := range tmpl.NewResources {
46+
if res.Current == nil {
47+
return false, nil
48+
}
49+
res, err := status.Compute(res.Current)
50+
if err != nil {
51+
return false, err
52+
}
53+
if res.Status != status.CurrentStatus {
54+
return false, nil
55+
}
5256
}
5357
}
5458
return true, nil
5559
}
5660

5761
// Build a list of template names which are not yet reconciled. Useful for status reporting
58-
func statusExistingTemplates(templates []*TemplateResource) []string {
62+
func statusExistingTemplates(templates []*ResourceTemplateState) []string {
5963
var missing []string
60-
for _, tmplRes := range templates {
61-
if tmplRes.Current == nil {
62-
missing = append(missing, tmplRes.TemplateName)
64+
for _, tmpl := range templates {
65+
for resIdx, res := range tmpl.NewResources {
66+
if res.Current == nil {
67+
missing = append(missing, fmt.Sprintf("%s[%d]", tmpl.TemplateName, resIdx))
68+
}
6369
}
6470
}
6571
return missing

0 commit comments

Comments
 (0)