kubernetes / kubernetes

Production-Grade Container Scheduling and Management
https://kubernetes.io
Apache License 2.0
108.57k stars 38.95k forks source link

Codegen tools creates wrong conversion for struct if member order is not matching #125933

Open MartinWeindel opened 1 week ago

MartinWeindel commented 1 week ago

The conversion-gen tool produces wrong conversion code for a pointer to a struct, if source and destination struct have multiple members of the same type, but the member ordering is different.

Here is an example to show the issue for an extended struct from k8s.io/code-generator/examples/apiserver/apis/example:

Type example/v1:

package v1 // "k8s.io/code-generator/examples/apiserver/apis/example/v1"

type TestTypeStatus struct {
    Blah  string `json:"blah"`
    Times *Times `json:"times,omitempty"`
}

type Times struct {
    Start *metav1.Time `json:"start,omitempty"`
    End   *metav1.Time `json:"end,omitempty"`
}

and the internal type:

package example // "k8s.io/code-generator/examples/apiserver/apis/example"

type TestTypeStatus struct {
    Blah  string
    Times *Times
}

type Times struct {
    End   *metav1.Time
    Start *metav1.Time
}

Note, that the ordering of the members Start and End have been changed.

Converting example/v1 to v1 shows that the values have been swapped:

$ go run ./main.go
examplev1.TestTypeStatus:
blah: blah
times:
  end: "2023-12-31T23:00:00Z"
  start: "1993-12-31T23:00:00Z"

---
example.TestTypeStatus:
Blah: blah
Times:
  End: "1993-12-31T23:00:00Z"
  Start: "2023-12-31T23:00:00Z"

---conversion ok: false%

The complete example code can be found here

k8s-ci-robot commented 1 week ago

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
Adarsh-verma-14 commented 1 week ago

/sig api-machinery