Skip to content

Commit 56aa65a

Browse files
committed
Rename deleted_at case, drop field if not set
Signed-off-by: Charlie Egan <[email protected]>
1 parent 0985b13 commit 56aa65a

File tree

5 files changed

+89
-21
lines changed

5 files changed

+89
-21
lines changed

api/datareading.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package api
22

3-
import "time"
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
47

58
// DataReadingsPost is the payload in the upload request.
69
type DataReadingsPost struct {
@@ -25,6 +28,23 @@ type DataReading struct {
2528
type GatheredResource struct {
2629
// Resource is a reference to a k8s object that was found by the informer
2730
// should be of type unstructured.Unstructured, raw Object
28-
Resource interface{} `json:"resource"`
29-
DeletedAt *Time `json:"deletedAt,omitempty"`
31+
Resource interface{}
32+
DeletedAt Time
33+
}
34+
35+
func (v GatheredResource) MarshalJSON() ([]byte, error) {
36+
dateString := ""
37+
if !v.DeletedAt.IsZero() {
38+
dateString = v.DeletedAt.Format(TimeFormat)
39+
}
40+
41+
data := struct {
42+
Resource interface{} `json:"resource"`
43+
DeletedAt string `json:"deleted_at,omitempty"`
44+
}{
45+
Resource: v.Resource,
46+
DeletedAt: dateString,
47+
}
48+
49+
return json.Marshal(data)
3050
}

api/datareading_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestJSONGatheredResourceDropsEmptyTime(t *testing.T) {
10+
var resource GatheredResource
11+
bytes, err := json.Marshal(resource)
12+
if err != nil {
13+
t.Fatalf("failed to marshal %s", err)
14+
}
15+
16+
expected := `{"resource":null}`
17+
18+
if string(bytes) != expected {
19+
t.Fatalf("unexpected json \ngot %s\nwant %s", string(bytes), expected)
20+
}
21+
}
22+
23+
func TestJSONGatheredResourceSetsTimeWhenPresent(t *testing.T) {
24+
var resource GatheredResource
25+
resource.DeletedAt = Time{time.Date(2021, 3, 29, 0, 0, 0, 0, time.UTC)}
26+
bytes, err := json.Marshal(resource)
27+
if err != nil {
28+
t.Fatalf("failed to marshal %s", err)
29+
}
30+
31+
expected := `{"resource":null,"deleted_at":"2021-03-29T00:00:00Z"}`
32+
33+
if string(bytes) != expected {
34+
t.Fatalf("unexpected json \ngot %s\nwant %s", string(bytes), expected)
35+
}
36+
}

pkg/datagatherer/k8s/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func onDelete(obj interface{}, dgCache *cache.Cache) {
7070
data := metadata.(map[string]interface{})
7171
if uid, ok := data["uid"]; ok {
7272
cacheObject := updateCacheGatheredResource(uid.(string), obj, dgCache)
73-
cacheObject.DeletedAt = &api.Time{Time: clock.now()}
73+
cacheObject.DeletedAt = api.Time{Time: clock.now()}
7474
dgCache.Set(uid.(string), cacheObject, cache.DefaultExpiration)
7575
} else {
7676
log.Printf("could not %q resource %q to the cache, missing uid field", "delete", data["name"].(string))
@@ -92,7 +92,7 @@ func updateCacheGatheredResource(cacheKey string, resource interface{},
9292
// update the object's properties, if it's already in the cache
9393
if o, ok := dgCache.Get(cacheKey); ok {
9494
deletedAt := o.(*api.GatheredResource).DeletedAt
95-
if deletedAt != nil {
95+
if deletedAt.IsZero() && !deletedAt.IsZero() {
9696
cacheObject.DeletedAt = deletedAt
9797
}
9898
}

pkg/datagatherer/k8s/cache_test.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"k8s.io/apimachinery/pkg/runtime"
1212
)
1313

14-
func makeGatheredResource(obj runtime.Object, deletedAt *api.Time) *api.GatheredResource {
14+
func makeGatheredResource(obj runtime.Object, deletedAt api.Time) *api.GatheredResource {
1515
return &api.GatheredResource{
1616
Resource: obj,
1717
DeletedAt: deletedAt,
@@ -32,9 +32,9 @@ func TestOnAddCache(t *testing.T) {
3232
getObject("foobar/v1", "NotFoo", "notfoo", "testns", false),
3333
},
3434
expected: []*api.GatheredResource{
35-
makeGatheredResource(getObject("foobar/v1", "Foo", "testfoo", "testns", false), nil),
36-
makeGatheredResource(getObject("v1", "Service", "testservice", "testns", false), nil),
37-
makeGatheredResource(getObject("foobar/v1", "NotFoo", "notfoo", "testns", false), nil),
35+
makeGatheredResource(getObject("foobar/v1", "Foo", "testfoo", "testns", false), api.Time{}),
36+
makeGatheredResource(getObject("v1", "Service", "testservice", "testns", false), api.Time{}),
37+
makeGatheredResource(getObject("foobar/v1", "NotFoo", "notfoo", "testns", false), api.Time{}),
3838
},
3939
},
4040
"delete all objects. All objects should have the deletedAt flag": {
@@ -51,14 +51,17 @@ func TestOnAddCache(t *testing.T) {
5151
},
5252
eventFunc: func(old, new interface{}, dgCache *cache.Cache) { onDelete(old, dgCache) },
5353
expected: []*api.GatheredResource{
54-
makeGatheredResource(getObject("foobar/v1", "Foo", "testfoo", "testns", false),
55-
&api.Time{Time: clock.now()},
54+
makeGatheredResource(
55+
getObject("foobar/v1", "Foo", "testfoo", "testns", false),
56+
api.Time{Time: clock.now()},
5657
),
57-
makeGatheredResource(getObject("v1", "Service", "testservice", "testns", false),
58-
&api.Time{Time: clock.now()},
58+
makeGatheredResource(
59+
getObject("v1", "Service", "testservice", "testns", false),
60+
api.Time{Time: clock.now()},
5961
),
60-
makeGatheredResource(getObject("foobar/v1", "NotFoo", "notfoo", "testns", false),
61-
&api.Time{Time: clock.now()},
62+
makeGatheredResource(
63+
getObject("foobar/v1", "NotFoo", "notfoo", "testns", false),
64+
api.Time{Time: clock.now()},
6265
),
6366
},
6467
},
@@ -76,9 +79,18 @@ func TestOnAddCache(t *testing.T) {
7679
},
7780
eventFunc: onUpdate,
7881
expected: []*api.GatheredResource{
79-
makeGatheredResource(getObject("foobar/v1", "Foo", "testfoo", "testns1", false), nil),
80-
makeGatheredResource(getObject("v1", "Service", "testservice", "testns1", false), nil),
81-
makeGatheredResource(getObject("foobar/v1", "NotFoo", "notfoo", "testns1", false), nil),
82+
makeGatheredResource(
83+
getObject("foobar/v1", "Foo", "testfoo", "testns1", false),
84+
api.Time{},
85+
),
86+
makeGatheredResource(
87+
getObject("v1", "Service", "testservice", "testns1", false),
88+
api.Time{},
89+
),
90+
makeGatheredResource(
91+
getObject("foobar/v1", "NotFoo", "notfoo", "testns1", false),
92+
api.Time{},
93+
),
8294
},
8395
},
8496
}

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
345345
expected: []*api.GatheredResource{
346346
{
347347
Resource: getObject("foobar/v1", "Foo", "testfoo", "testns", false),
348-
DeletedAt: &api.Time{Time: clock.now()},
348+
DeletedAt: api.Time{Time: clock.now()},
349349
},
350350
},
351351
},
@@ -416,11 +416,11 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
416416
expected: []*api.GatheredResource{
417417
{
418418
Resource: getObject("foobar/v1", "Foo", "testfoo1", "testns1", false),
419-
DeletedAt: &api.Time{Time: clock.now()},
419+
DeletedAt: api.Time{Time: clock.now()},
420420
},
421421
{
422422
Resource: getObject("foobar/v1", "Foo", "testfoo2", "testns2", false),
423-
DeletedAt: &api.Time{Time: clock.now()},
423+
DeletedAt: api.Time{Time: clock.now()},
424424
},
425425
},
426426
},

0 commit comments

Comments
 (0)