Skip to content

Commit 132edd6

Browse files
committed
reconciler: Fix Status JSON marshalling
43587d4 broke JSON marshalling of Status by introducing a separate statusJSON type and Unmarshal{JSON,YAML} methods, but didn't introduce the Marshal{JSON,YAML} methods, which caused dictionary key mismatches as Status struct was used to marshal instead of statusJSON. Fix this by removing the whole statusJSON construct and add json and yaml tags to Status. There is no need for filling in the `id` field when unmarshalling Status as this is completely internal to the reconciler. Fixes: 43587d4 ("reconciler: Implement tests using scripttest") Signed-off-by: Jussi Maki <[email protected]>
1 parent e0d473d commit 132edd6

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

reconciler/status_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,57 @@ func TestStatusString(t *testing.T) {
4040
assert.Regexp(t, `Error: hey I'm an error \([0-9]+\.[0-9]+.+s ago\)`, s.String())
4141
}
4242

43+
func TestStatusJSON(t *testing.T) {
44+
testCases := []struct {
45+
s Status
46+
expected string
47+
}{
48+
{
49+
Status{
50+
Kind: StatusKindDone,
51+
UpdatedAt: time.Unix(1, 0).UTC(),
52+
Error: "",
53+
},
54+
`{"kind":"Done","updated-at":"1970-01-01T00:00:01Z"}`,
55+
},
56+
{
57+
Status{
58+
Kind: StatusKindPending,
59+
UpdatedAt: time.Unix(2, 0).UTC(),
60+
Error: "",
61+
},
62+
`{"kind":"Pending","updated-at":"1970-01-01T00:00:02Z"}`,
63+
},
64+
{
65+
Status{
66+
Kind: StatusKindError,
67+
UpdatedAt: time.Unix(3, 0).UTC(),
68+
Error: "some-error",
69+
},
70+
`{"kind":"Error","updated-at":"1970-01-01T00:00:03Z","error":"some-error"}`,
71+
},
72+
{
73+
Status{
74+
Kind: StatusKindRefreshing,
75+
UpdatedAt: time.Unix(4, 0).UTC(),
76+
Error: "",
77+
},
78+
`{"kind":"Refreshing","updated-at":"1970-01-01T00:00:04Z"}`,
79+
},
80+
}
81+
82+
for _, tc := range testCases {
83+
b, err := json.Marshal(tc.s)
84+
assert.NoError(t, err, "Marshal")
85+
assert.Equal(t, tc.expected, string(b))
86+
87+
var s Status
88+
assert.NoError(t, json.Unmarshal(b, &s), "Unmarshal")
89+
assert.Equal(t, tc.s, s)
90+
}
91+
92+
}
93+
4394
func sanitizeAgo(s string) string {
4495
r := regexp.MustCompile(`\(.* ago\)`)
4596
return string(r.ReplaceAll([]byte(s), []byte("(??? ago)")))

reconciler/types.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/cilium/statedb"
2121
"github.com/cilium/statedb/index"
2222
"github.com/cilium/statedb/internal"
23-
"gopkg.in/yaml.v3"
2423
)
2524

2625
type Reconciler[Obj any] interface {
@@ -132,9 +131,9 @@ func (s StatusKind) Key() index.Key {
132131
// the reconciler. Object may have multiple reconcilers and
133132
// multiple reconciliation statuses.
134133
type Status struct {
135-
Kind StatusKind
136-
UpdatedAt time.Time
137-
Error string
134+
Kind StatusKind `json:"kind" yaml:"kind"`
135+
UpdatedAt time.Time `json:"updated-at" yaml:"updated-at"`
136+
Error string `json:"error,omitempty" yaml:"error,omitempty"`
138137

139138
// id is a unique identifier for a pending object.
140139
// The reconciler uses this to compare whether the object
@@ -144,39 +143,6 @@ type Status struct {
144143
id uint64
145144
}
146145

147-
// statusJSON defines the JSON/YAML format for [Status]. Separate to
148-
// [Status] to allow custom unmarshalling that fills in [id].
149-
type statusJSON struct {
150-
Kind string `json:"kind" yaml:"kind"`
151-
UpdatedAt time.Time `json:"updated-at" yaml:"updated-at"`
152-
Error string `json:"error,omitempty" yaml:"error,omitempty"`
153-
}
154-
155-
func (sj *statusJSON) fill(s *Status) {
156-
s.Kind = StatusKind(sj.Kind)
157-
s.UpdatedAt = sj.UpdatedAt
158-
s.Error = sj.Error
159-
s.id = nextID()
160-
}
161-
162-
func (s *Status) UnmarshalYAML(value *yaml.Node) error {
163-
var sj statusJSON
164-
if err := value.Decode(&sj); err != nil {
165-
return err
166-
}
167-
sj.fill(s)
168-
return nil
169-
}
170-
171-
func (s *Status) UnmarshalJSON(data []byte) error {
172-
var sj statusJSON
173-
if err := json.Unmarshal(data, &sj); err != nil {
174-
return err
175-
}
176-
sj.fill(s)
177-
return nil
178-
}
179-
180146
func (s Status) IsPendingOrRefreshing() bool {
181147
return s.Kind == StatusKindPending || s.Kind == StatusKindRefreshing
182148
}

0 commit comments

Comments
 (0)