Skip to content
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

Fix ConfigEntryResponse binary marshaller and ensure we watch the chan in ConfigEntry.Get even when no entry exists. #5773

Merged
merged 4 commits into from
May 2, 2019
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
2 changes: 1 addition & 1 deletion agent/consul/state/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func (s *Store) ConfigEntry(ws memdb.WatchSet, kind, name string) (uint64, struc
if err != nil {
return 0, nil, fmt.Errorf("failed config entry lookup: %s", err)
}
ws.Add(watchCh)
mkeeler marked this conversation as resolved.
Show resolved Hide resolved
if existing == nil {
return idx, nil, nil
mkeeler marked this conversation as resolved.
Show resolved Hide resolved
}
ws.Add(watchCh)

conf, ok := existing.(structs.ConfigEntry)
if !ok {
Expand Down
35 changes: 23 additions & 12 deletions agent/structs/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,19 @@ func (c *ConfigEntryResponse) MarshalBinary() (data []byte, err error) {
bs := make([]byte, 128)
enc := codec.NewEncoderBytes(&bs, msgpackHandle)

if err := enc.Encode(c.Entry.GetKind()); err != nil {
return nil, err
}
if err := enc.Encode(c.Entry); err != nil {
return nil, err
if c.Entry != nil {
rboyer marked this conversation as resolved.
Show resolved Hide resolved
if err := enc.Encode(c.Entry.GetKind()); err != nil {
return nil, err
}
if err := enc.Encode(c.Entry); err != nil {
return nil, err
}
} else {
if err := enc.Encode(""); err != nil {
mkeeler marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
}

if err := enc.Encode(c.QueryMeta); err != nil {
return nil, err
}
Expand All @@ -500,15 +507,19 @@ func (c *ConfigEntryResponse) UnmarshalBinary(data []byte) error {
return err
}

entry, err := MakeConfigEntry(kind, "")
if err != nil {
return err
}
if kind != "" {
entry, err := MakeConfigEntry(kind, "")
if err != nil {
return err
}

if err := dec.Decode(entry); err != nil {
return err
if err := dec.Decode(entry); err != nil {
return err
}
c.Entry = entry
} else {
c.Entry = nil
}
c.Entry = entry

if err := dec.Decode(&c.QueryMeta); err != nil {
return err
Expand Down
42 changes: 42 additions & 0 deletions agent/structs/config_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,45 @@ func TestServiceConfigResponse_MsgPack(t *testing.T) {

require.Equal(t, a, b)
}

func TestConfigEntryResponseMarshalling(t *testing.T) {
t.Parallel()

cases := map[string]ConfigEntryResponse{
"nil entry": ConfigEntryResponse{},
"proxy-default entry": ConfigEntryResponse{
Entry: &ProxyConfigEntry{
Kind: ProxyDefaults,
Name: ProxyConfigGlobal,
Config: map[string]interface{}{
"foo": "bar",
},
},
},
"service-default entry": ConfigEntryResponse{
Entry: &ServiceConfigEntry{
Kind: ServiceDefaults,
Name: "foo",
Protocol: "tcp",
// Connect: ConnectConfiguration{SideCarProxy: true},
},
},
}

for name, tcase := range cases {
name := name
tcase := tcase
t.Run(name, func(t *testing.T) {
t.Parallel()

data, err := tcase.MarshalBinary()
require.NoError(t, err)
require.NotEmpty(t, data)

var resp ConfigEntryResponse
require.NoError(t, resp.UnmarshalBinary(data))

require.Equal(t, tcase, resp)
})
}
}