Skip to content

Commit ae78751

Browse files
author
Oluwole Fadeyi
authored
Merge pull request #222 from jetstack/informer-dynamic-datagathers
Introduces informers in the dynamic datagatherers to find the fetch resources on events rather than polling resources.
2 parents bd337db + 7a14eda commit ae78751

File tree

20 files changed

+1285
-313
lines changed

20 files changed

+1285
-313
lines changed

api/datareading.go

Lines changed: 34 additions & 5 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 {
@@ -14,8 +17,34 @@ type DataReadingsPost struct {
1417
type DataReading struct {
1518
// ClusterID is optional as it can be infered from the agent
1619
// token when using basic authentication.
17-
ClusterID string `json:"cluster_id,omitempty"`
18-
DataGatherer string `json:"data-gatherer"`
19-
Timestamp Time `json:"timestamp"`
20-
Data interface{} `json:"data"`
20+
ClusterID string `json:"cluster_id,omitempty"`
21+
DataGatherer string `json:"data-gatherer"`
22+
Timestamp Time `json:"timestamp"`
23+
Data interface{} `json:"data"`
24+
SchemaVersion string `json:"schema_version"`
25+
}
26+
27+
// GatheredResource wraps the raw k8s resource that is sent to the jetstack secure backend
28+
type GatheredResource struct {
29+
// Resource is a reference to a k8s object that was found by the informer
30+
// should be of type unstructured.Unstructured, raw Object
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)
2150
}

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+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/kylelemons/godebug v1.1.0
2222
github.com/leodido/go-urn v1.2.0 // indirect
2323
github.com/pkg/errors v0.9.1
24+
github.com/pmylund/go-cache v2.1.0+incompatible
2425
github.com/sirupsen/logrus v1.7.0
2526
github.com/spf13/cobra v1.1.1
2627
github.com/spf13/pflag v1.0.5

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,9 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
779779
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
780780
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
781781
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
782+
github.com/pmylund/go-cache v1.0.0 h1:jbJMNhn4LhBfb3dRejPlnjxSiokDt4qO5NWt8mMi+UE=
783+
github.com/pmylund/go-cache v2.1.0+incompatible h1:n+7K51jLz6a3sCvff3BppuCAkixuDHuJ/C57Vw/XjTE=
784+
github.com/pmylund/go-cache v2.1.0+incompatible/go.mod h1:hmz95dGvINpbRZGsqPcd7B5xXY5+EKb5PpGhQY3NTHk=
782785
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
783786
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 h1:0XM1XL/OFFJjXsYXlG30spTkV/E9+gmd5GD1w2HE8xM=
784787
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=

pkg/agent/dummy_data_gatherer.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ type dummyDataGatherer struct {
2929
FailedAttempts int
3030
}
3131

32+
func (g *dummyDataGatherer) Run(stopCh <-chan struct{}) error {
33+
// no async functionality, see Fetch
34+
return nil
35+
}
36+
37+
func (g *dummyDataGatherer) WaitForCacheSync(stopCh <-chan struct{}) error {
38+
// no async functionality, see Fetch
39+
return nil
40+
}
41+
42+
func (g *dummyDataGatherer) Delete() error {
43+
// no async functionality, see Fetch
44+
return nil
45+
}
46+
3247
func (c *dummyDataGatherer) Fetch() (interface{}, error) {
3348
var err error
3449
if c.attemptNumber < c.FailedAttempts {

0 commit comments

Comments
 (0)