mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #53464 from juanvallejo/jvallejo/output-empty-creation-ts-as-null
Automatic merge from submit-queue (batch tested with PRs 55050, 53464, 54936, 55028, 54928). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. output empty creationTimestamp as null **Release note** ```release-note NONE ``` Updates the value of the `creationTimestamp` field to be `null` when empty, to keep parity between it and `deletionTimestamp`. Adds a round-trip test to ensure that unstructured objects containing empty metadata fields are able to be re-converted back into internal or external objects. Prior to the proposed patch in this PR, an unstructured object whose `.metadata.creationTimestamp` value had been set through the metadata accessor to an empty value (`metav1.Time{}` in this case), was unable to be re-converted to an internal or external type using the runtime decoder. Conversion would fail with the error: ``` unstructured_test.go:177: FromUnstructured failed: parsing time "" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "2006" ``` cc @liggitt @fabianofranz
This commit is contained in:
commit
1ad792f01d
@ -95,6 +95,7 @@ go_test(
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing/fuzzer:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/testing/roundtrip:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/conversion:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/conversion/unstructured:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
|
@ -25,7 +25,10 @@ import (
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/api/testing/fuzzer"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
metaunstruct "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/conversion/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
@ -131,6 +134,43 @@ func TestRoundTrip(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundTripWithEmptyCreationTimestamp(t *testing.T) {
|
||||
for groupKey, group := range testapi.Groups {
|
||||
for kind := range group.ExternalTypes() {
|
||||
if nonRoundTrippableTypes.Has(kind) {
|
||||
continue
|
||||
}
|
||||
item, err := legacyscheme.Scheme.New(group.GroupVersion().WithKind(kind))
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't create external object %v: %v", kind, err)
|
||||
}
|
||||
t.Logf("Testing: %v in %v", kind, groupKey)
|
||||
|
||||
unstrBody, err := unstructured.DefaultConverter.ToUnstructured(item)
|
||||
if err != nil {
|
||||
t.Fatalf("ToUnstructured failed: %v", err)
|
||||
}
|
||||
|
||||
unstructObj := &metaunstruct.Unstructured{}
|
||||
unstructObj.Object = unstrBody
|
||||
|
||||
if meta, err := meta.Accessor(unstructObj); err == nil {
|
||||
meta.SetCreationTimestamp(metav1.Time{})
|
||||
} else {
|
||||
t.Fatalf("Unable to set creation timestamp: %v", err)
|
||||
}
|
||||
|
||||
// attempt to re-convert unstructured object - conversion should not fail
|
||||
// based on empty metadata fields, such as creationTimestamp
|
||||
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
||||
err = unstructured.DefaultConverter.FromUnstructured(unstructObj.Object, newObj)
|
||||
if err != nil {
|
||||
t.Fatalf("FromUnstructured failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkToFromUnstructured(b *testing.B) {
|
||||
items := benchmarkItems(b)
|
||||
size := len(items)
|
||||
|
@ -258,6 +258,10 @@ func (u *Unstructured) GetCreationTimestamp() metav1.Time {
|
||||
|
||||
func (u *Unstructured) SetCreationTimestamp(timestamp metav1.Time) {
|
||||
ts, _ := timestamp.MarshalQueryParameter()
|
||||
if len(ts) == 0 || timestamp.Time.IsZero() {
|
||||
RemoveNestedField(u.Object, "metadata", "creationTimestamp")
|
||||
return
|
||||
}
|
||||
u.setNestedField(ts, "metadata", "creationTimestamp")
|
||||
}
|
||||
|
||||
|
@ -62,3 +62,24 @@ func TestNilDeletionTimestamp(t *testing.T) {
|
||||
_, ok = metadata["deletionTimestamp"]
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestEmptyCreationTimestampIsOmitted(t *testing.T) {
|
||||
var u Unstructured
|
||||
now := metav1.Now()
|
||||
|
||||
// set an initial creationTimestamp and ensure the field exists
|
||||
u.SetCreationTimestamp(now)
|
||||
metadata := u.Object["metadata"].(map[string]interface{})
|
||||
creationTimestamp, exists := metadata["creationTimestamp"]
|
||||
if !exists {
|
||||
t.Fatalf("unexpected missing creationTimestamp")
|
||||
}
|
||||
|
||||
// set an empty timestamp and ensure the field no longer exists
|
||||
u.SetCreationTimestamp(metav1.Time{})
|
||||
metadata = u.Object["metadata"].(map[string]interface{})
|
||||
creationTimestamp, exists = metadata["creationTimestamp"]
|
||||
if exists {
|
||||
t.Errorf("unexpected creation timestamp field: %q", creationTimestamp)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user