Skip to content

fix panic when calling ToUnstructured on nil metav1.Time #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions value/reflectcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,18 @@ func (e TypeReflectCacheEntry) ToUnstructured(sv reflect.Value) (interface{}, er
// This is based on https://github.com/kubernetes/kubernetes/blob/82c9e5c814eb7acc6cc0a090c057294d0667ad66/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go#L505
// and is intended to replace it.

// Check if the object is a nil pointer.
if sv.Kind() == reflect.Ptr && sv.IsNil() {
// We're done - we don't need to store anything.
return nil, nil
}
// Check if the object has a custom string converter and use it if available, since it is much more efficient
// than round tripping through json.
if converter, ok := e.getUnstructuredConverter(sv); ok {
return converter.ToUnstructured(), nil
}
// Check if the object has a custom JSON marshaller/unmarshaller.
if marshaler, ok := e.getJsonMarshaler(sv); ok {
if sv.Kind() == reflect.Ptr && sv.IsNil() {
// We're done - we don't need to store anything.
return nil, nil
}

data, err := marshaler.MarshalJSON()
if err != nil {
return nil, err
Expand Down
49 changes: 49 additions & 0 deletions value/reflectcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package value
import (
"reflect"
"testing"
"time"
)

type CustomValue struct {
Expand All @@ -39,6 +40,21 @@ func (c *CustomPointer) MarshalJSON() ([]byte, error) {
return c.data, nil
}

// Mimics https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/time.go.
type Time struct {
time.Time
}

// ToUnstructured implements the value.UnstructuredConverter interface.
func (t Time) ToUnstructured() interface{} {
if t.IsZero() {
return nil
}
buf := make([]byte, 0, len(time.RFC3339))
buf = t.UTC().AppendFormat(buf, time.RFC3339)
return string(buf)
}

func TestToUnstructured(t *testing.T) {
testcases := []struct {
Data string
Expand Down Expand Up @@ -78,6 +94,39 @@ func TestToUnstructured(t *testing.T) {
}
}

func timePtr(t time.Time) *time.Time { return &t }

func TestTimeToUnstructured(t *testing.T) {
testcases := []struct {
Name string
Time *time.Time
Expected interface{}
}{
{Name: "nil", Time: nil, Expected: nil},
{Name: "zero", Time: &time.Time{}, Expected: nil},
{Name: "1", Time: timePtr(time.Time{}.Add(time.Second)), Expected: "0001-01-01T00:00:01Z"},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
var time *Time
rv := reflect.ValueOf(time)
if tc.Time != nil {
rv = reflect.ValueOf(Time{Time: *tc.Time})
}
result, err := TypeReflectEntryOf(rv.Type()).ToUnstructured(rv)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(result, tc.Expected) {
t.Errorf("expected %#v but got %#v", tc.Expected, result)
}
})
}
}

func TestTypeReflectEntryOf(t *testing.T) {
testString := ""
tests := map[string]struct {
Expand Down