crossplane-contrib / crossplane-contrib/function-go-templating

ExtraResources.matchName doesn't find my XR but matchLabels does

Open
#501 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
100
Forks
65
Avg merge
2d 6h
Merged PRs (30d)
9

Description

### What happened?

With Crossplane 2 I'm trying to get a reference to an existing instance of a custom XR. I'm trying to reference it like so:

```
apiVersion: meta.gotemplating.fn.crossplane.io/v1alpha1
kind: ExtraResources
requirements:
serviceStacks:
apiVersion: stacks.example.acme.com/v1alpha1
kind: ServiceStack
matchName: {{ .observed.composite.resource.spec.stackRef.name }}
```

But the slice, referenced through `getExtraResources . "serviceStacks"`, is always empty. However, if I query labels added by Crossplane the slice is not empty, like so:

```
apiVersion: meta.gotemplating.fn.crossplane.io/v1alpha1
kind: ExtraResources
requirements:
serviceStacks:
apiVersion: stacks.example.acme.com/v1alpha2
kind: ServiceStack
matchLabels:
crossplane.io/composite: {{ .observed.composite.resource.spec.stackRef.name }}
```

The slice contains one element. **However**, and this makes no sense to me, I can't get at it through a slice index. I have no issues seeing the reference if I iterate the slice like so:

```
---
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
{{ setResourceNameAnnotation "debug-cm" }}
data:
{{- $someExtraResources := getExtraResources . "serviceStacks" }}
{{- range $i, $extraResource := $someExtraResources }}
item-{{ $i }}: "{{ $extraResource.resource.metadata.name }}"
{{- end }}
```

The resulting `ConfigMap` looks like this:

```
apiVersion: v1
kind: ConfigMap
data:
item-0: acme-example-service
```

But using `index` like so:

```
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
{{ setResourceNameAnnotation "debug-cm" }}
data:
{{- $someExtraResources := getExtraResources . "serviceStacks" }}
{{- $extraResource := (index $someExtraResources 0) }}
item: "{{ $extraResource.resource.metadata.name }}"
```

I always get an out of range error:

```
cannot compose resources: pipeline step "render-templates" returned a fatal result: cannot execute template: template: manifests:50:25: executing "manifests" at : error calling index: reflect: slice index out of range
```

What on earth am I misunderstanding?

Is this maybe a side effect of the underlying Crossplane SDK in the function pinned to a 1.X version? Perhaps similar to #483?

### What environment did it happen in?

Crossplane 2. Latest version of function.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the ExtraResources requirement using matchName, then compare it with matchLabels and the getExtraResources calls shown in the issue. Investigate the function's Crossplane SDK version and the behavior discussed in #483; done means explaining both the empty matchName result and the inconsistent slice indexing, with a regression test if the repository has coverage for this path.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.