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

Error during access to the item of the list

Open
#78 6 comments 0 reactions 0 assignees View on GitHub
bug good first issue
Dominant language
Go
Stars
100
Forks
65
Avg merge
2d 6h
Merged PRs (30d)
9

Description

### What happened?
During the access to the list item by index or by the `first` function inside the template, I got the error:
``` result: cannot execute template: template: manifests:10:18: executing "manifests"
at : error calling index: index of untyped nil
```

log data
```
2024-03-18T20:45:49.992Z DEBUG fn/fn.go:60 template {"template": "---\napiVersion: example.acme.io/v1beta1\nkind: Debug\nmetadata:\n name: test-debug\n annotations:\n gotemplating.fn.crossplane.io/composition-resource-name: test-debug\nspec:\n ip: {{ (first .observed.resources.pod.resource.status.atProvider.manifest.status.hostIPs).ip }}\n"}
2024-03-18T20:45:49.993Z DEBUG fn/fn.go:74 constructed request map {"request": {"context":{"apiextensions.crossplane.io/environment":{}},"desired":{"composite":{"resource":{"apiVersion":"example.acme.io/v1beta1","kind":"XR"}},"resources":{"pod":{"resource":{"apiVersion":"kubernetes.crossplane.io/v1alpha2","kind":"Object","spec":{"forProvider":{"manifest":{"apiVersion":"v1","kind":"Pod","metadata":{"name":"nginx","namespace":"default"},"spec":{"containers":[{"image":"nginx:1.14.0","name":"nginx"}]}}},"providerConfigRef":{"name":"kubernetes-provider"}}}}}},"input":{"apiVersion":"gotemplating.fn.crossplane.io/v1beta1","inline":{"template":"---\napiVersion: example.acme.io/v1beta1\nkind: Debug\nmetadata:\n name: test-debug\n annotations:\n gotemplating.fn.crossplane.io/composition-resource-name: test-debug\nspec:\n ip: {{ (first .observed.resources.pod.resource.status.atProvider.manifest.status.hostIPs).ip }}\n"},"kind":"GoTemplate","source":"Inline"},"observed":{"composite":{"resource":{"apiVersion":"example.acme.io/v1beta1","kind":"XR","metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"example.acme.io/v1beta1\",\"kind\":\"XR\",\"metadata\":{\"annotations\":{},\"name\":\"test\"},\"spec\":{\"data\":\"top-secret\"}}\n"},"creationTimestamp":"2024-03-18T20:44:44Z","finalizers":["composite.apiextensions.crossplane.io"],"generation":3,"labels":{"crossplane.io/composite":"test"},"managedFields":[{"apiVersion":"example.acme.io/v1beta1","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:finalizers":{".":{},"v:\"composite.apiextensions.crossplane.io\"":{}},"f:labels":{".":{},"f:crossplane.io/composite":{}}},"f:spec":{"f:compositionRef":{".":{},"f:name":{}},"f:compositionRevisionRef":{".":{},"f:name":{}}}},"manager":"crossplane","operation":"Update","time":"2024-03-18T20:44:44Z"},{"apiVersion":"example.acme.io/v1beta1","fieldsType":"FieldsV1","fieldsV1":{"f:status":{".":{},"f:conditions":{".":{},"k:{\"type\":\"Synced\"}":{".":{},"f:lastTransitionTime":{},"f:message":{},"f:reason":{},"f:status":{},"f:type":{}}}}},"manager":"crossplane","operation":"Update","subresource":"status","time":"2024-03-18T20:44:44Z"},{"apiVersion":"example.acme.io/v1beta1","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}}},"f:spec":{".":{},"f:compositionUpdatePolicy":{},"f:data":{}}},"manager":"kubectl-client-side-apply","operation":"Update","time":"2024-03-18T20:44:44Z"}],"name":"test","resourceVersion":"654357","uid":"1aa8b1d7-b30f-4d3b-be9f-05f8192232fa"},"spec":{"compositionRef":{"name":"function-go-template"},"compositionRevisionRef":{"name":"function-go-template-1737ec3"},"compositionUpdatePolicy":"Automatic","data":"top-secret"},"status":{"conditions":[{"lastTransitionTime":"2024-03-18T20:44:44Z","message":"cannot compose resources: pipeline step \"go-templating\" returned a fatal result: cannot execute template: template: manifests:9:10: executing \"manifests\" at : error calling first: runtime error: invalid memory address or nil pointer dereference","reason":"ReconcileError","status":"False","type":"Synced"}]}}}}}}
```

### How can we reproduce it?

composition.yaml

```yaml
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-go-template
spec:
compositeTypeRef:
apiVersion: example.acme.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: patch-and-transform
functionRef:
name: function-patch-and-transform
input:
apiVersion: pt.fn.crossplane.io/v1beta1
kind: Resources
resources:
- name: pod
base:
apiVersion: kubernetes.crossplane.io/v1alpha2
kind: Object
spec:
providerConfigRef:
name: kubernetes-provider
forProvider:
manifest:
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: default
spec:
containers:
- name: nginx
image: nginx:1.14.0
- step: go-templating
functionRef:
name: function-go-templating
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
source: Inline
inline:
template: |
---
apiVersion: example.acme.io/v1beta1
kind: Debug
metadata:
name: test-debug
annotations:
gotemplating.fn.crossplane.io/composition-resource-name: test-debug
spec:
ip: {{ (first .observed.resources.pod.resource.status.atProvider.manifest.status.hostIPs).ip }}

```

definition.yaml

```yaml
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: xrs.example.acme.io
spec:
group: example.acme.io
names:
kind: XR
plural: xrs
versions:
- name: v1beta1
served: true
referenceable: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
data:
type: string

---
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: debugs.example.acme.io
spec:
group: example.acme.io
names:
kind: Debug
plural: debugs
versions:
- name: v1beta1
served: true
referenceable: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
ip:
type: string
```

xr.yaml

```yaml
apiVersion: example.acme.io/v1beta1
kind: XR
metadata:
name: test
spec:
data: top-secret
```

### What environment did it happen in?
* function-go-templating **v0.4.1**
* Crossplane **v0.15.1**

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with the supplied composition.yaml, definition.yaml, and xr.yaml, then start at the template execution path shown in fn/fn.go. Trace how access to the missing status.hostIPs value is handled and establish the expected behavior for the template expression. Done should be defined by a reproducible result that no longer produces the reported nil-access failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, kubernetes
Domain
backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.