kubernetes / kubernetes

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

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

Open MartinWeindel opened 4 months ago

MartinWeindel commented 4 months 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

Adarsh-verma-14 commented 4 months ago

/sig api-machinery

cici37 commented 4 months ago

/triage accepted