kubernetes-sigs / kubernetes-sigs/controller-tools

controller-gen: Type aliases to array types don't work with +listType/+listMapKey markers

Open
#988 7 comments 1 reaction 0 assignees View on GitHub
kind/bug lifecycle/frozen
Dominant language
Go
Stars
868
Forks
482
Avg merge
1d 11h
Merged PRs (30d)
8

Description

## Summary

If Go type of a CRD has a field that uses `+listType` or `+listMapKey` markers, but the field (struct member) is a typedef to a slice type, the field is not detected as an "array" field, and these markers cannot be applied to it.

## Example

```go
// +k8s:deepcopy-gen=true
type Conditions []Condition

type FooStatus struct{
// +patchMergeKey=type
// +patchStrategy=merge
// +listType=map
// +listMapKey=type
Conditions Conditions `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}
```

Fails with error:

```
controller-gen crd paths=./api/... [...]

[...]/status_types.go:35:2: must apply listType to an array, found
[...]/status_types.go:35:2: must apply listMapKey to an array, found
```

## Details

This reproes in HEAD (as well as v0.14, v0.15).

If we do not use the declaration for the slice type (`type Conditions []Condition`) in the root object and instead use `[]Conditions` as the struct member type, it works as expected.

I think this is happening probably because the tool uses AST parsing, and `Conditions` struct member doesn't appear like an array [in this snippet](https://github.com/kubernetes-sigs/controller-tools/blob/master/pkg/crd/schema.go#L207), it calls `structToSchema` instead of `arrayToSchema`.

/kind bug

Contributor guide

Open the contributing guide

Research direction

Start in pkg/crd/schema.go around the referenced line 207 and trace how structToSchema and arrayToSchema classify field types. Reproduce the issue with the provided Conditions alias and controller-gen crd paths=./api/... command; done means +listType and +listMapKey work for slice type aliases without the reported errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.