Skip to content

Commit

Permalink
[m3admin] non-strict jsonpb unmarshalling (#117)
Browse files Browse the repository at this point in the history
When we unmarshal responses from the m3coordinator, the default jsonpb
behavior is to not allow unknown fields. This means that new fields
added by m3db to the proto can break the operator. This fixes that and
future-proofs us against new fields.
  • Loading branch information
schallert authored Apr 8, 2019
1 parent e1e7579 commit 61d54d0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 48 deletions.
44 changes: 0 additions & 44 deletions pkg/controller/config.go

This file was deleted.

6 changes: 5 additions & 1 deletion pkg/m3admin/namespace/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func (n *namespaceClient) List() (*admin.NamespaceGetResponse, error) {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}()
if err := jsonpb.Unmarshal(resp.Body, data); err != nil {

um := &jsonpb.Unmarshaler{
AllowUnknownFields: true,
}
if err := um.Unmarshal(resp.Body, data); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/m3admin/namespace/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ func TestCreateErr(t *testing.T) {
func TestGet(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(`{"registry":{"namespaces":{"default":{"bootstrapEnabled":true,"flushEnabled":true,"writesToCommitLog":true,"cleanupEnabled":true,"repairEnabled":false,"retentionOptions":{"retentionPeriodNanos":"172800000000000","blockSizeNanos":"7200000000000","bufferFutureNanos":"600000000000","bufferPastNanos":"600000000000","blockDataExpiry":true,"blockDataExpiryAfterNotAccessPeriodNanos":"300000000000"},"snapshotEnabled":false,"indexOptions":{"enabled":true,"blockSizeNanos":"7200000000000"}},"m3db-cluster":{"bootstrapEnabled":true,"flushEnabled":true,"writesToCommitLog":true,"cleanupEnabled":true,"repairEnabled":false,"retentionOptions":{"retentionPeriodNanos":"172800000000000","blockSizeNanos":"7200000000000","bufferFutureNanos":"600000000000","bufferPastNanos":"600000000000","blockDataExpiry":true,"blockDataExpiryAfterNotAccessPeriodNanos":"300000000000"},"snapshotEnabled":false,"indexOptions":{"enabled":true,"blockSizeNanos":"7200000000000"}}}}}`))
w.Write([]byte(`{"registry":{"namespaces":{"default":{"bootstrapEnabled":true,"flushEnabled":true,"writesToCommitLog":true,"cleanupEnabled":true,"repairEnabled":false,"retentionOptions":{"retentionPeriodNanos":"172800000000000","blockSizeNanos":"7200000000000","bufferFutureNanos":"600000000000","bufferPastNanos":"600000000000","blockDataExpiry":true,"blockDataExpiryAfterNotAccessPeriodNanos":"300000000000"},"snapshotEnabled":false,"indexOptions":{"enabled":true,"blockSizeNanos":"7200000000000"}},"m3db-cluster":{"bootstrapEnabled":true,"flushEnabled":true,"writesToCommitLog":true,"cleanupEnabled":true,"repairEnabled":false,"retentionOptions":{"retentionPeriodNanos":"172800000000000","blockSizeNanos":"7200000000000","bufferFutureNanos":"600000000000","bufferPastNanos":"600000000000","blockDataExpiry":true,"blockDataExpiryAfterNotAccessPeriodNanos":"300000000000"},"snapshotEnabled":false,"schemaOptions":{},"indexOptions":{"enabled":true,"blockSizeNanos":"7200000000000"}}}}}`))
}))
defer s.Close()
client := newNamespaceClient(t, s.URL)

resp, err := client.List()
require.NotNil(t, resp)
require.Nil(t, err)
require.NoError(t, err)
}

func TestGet_Nil(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/m3admin/placement/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func (p *placementClient) Get() (m3placement.Placement, error) {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}()
if err := jsonpb.Unmarshal(resp.Body, data); err != nil {

um := &jsonpb.Unmarshaler{
AllowUnknownFields: true,
}
if err := um.Unmarshal(resp.Body, data); err != nil {
return nil, err
}
if data.Placement == nil {
Expand Down

0 comments on commit 61d54d0

Please sign in to comment.