Normalise format of OVN agent heartbeat timestamp

A recent change [1] to show the real heartbeat timestamp from OVN agents
had a side effect of changing the timestamp format, which now includes a
timezone:

    +-------------------+----------------------------------+
    | Field             | Value                            |
    +-------------------+----------------------------------+
    | last_heartbeat_at | 2023-02-23 14:12:07.471000+00:00 |
    +-------------------+----------------------------------+

This unexpected format change causes some clients to fail to parse the
response to GET /v2.0/agents.

Normalise the format of the timestamp to remove timezone information.
Also remove the microsecond part, which was not done for OVN, but is
absent from other network agents.

[1] https://review.opendev.org/c/openstack/neutron/+/844179

Closes-Bug: #2008257
Change-Id: I75a37fb9b49a421e4524da6b56ef8362ceb6107b
This commit is contained in:
Pierre Riteau 2023-02-24 05:34:07 +01:00
parent aa40aef70f
commit 827fbd01c3
4 changed files with 26 additions and 2 deletions

View File

@ -81,7 +81,8 @@ class NeutronAgent(abc.ABC):
return {
'binary': self.binary,
'host': self.chassis.hostname,
'heartbeat_timestamp': self.updated_at,
'heartbeat_timestamp': timeutils.normalize_time(
self.updated_at.replace(microsecond=0)),
'availability_zone': ', '.join(
ovn_utils.get_chassis_availability_zones(self.chassis)),
'topic': 'n/a',

View File

@ -1148,7 +1148,7 @@ class TestAgentApi(base.TestOVNFunctionalBase):
'Chassis_Private', self.chassis, 'nb_cfg_timestamp'
).execute(check_error=True)
updated_at = datetime.datetime.fromtimestamp(
int(chassis_ts / 1000), datetime.timezone.utc)
int(chassis_ts / 1000))
# if table Chassis_Private present, agent.updated_at is
# Chassis_Private.nb_cfg_timestamp
self.assertEqual(updated_at, heartbeat_timestamp)

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from unittest import mock
import eventlet
@ -67,3 +68,19 @@ class AgentCacheTestCase(base.BaseTestCase):
agents = list(agents)
self.assertEqual(1, len(agents))
self.assertEqual('chassis5', agents[0].agent_id)
@mock.patch.object(neutron_agent.ControllerAgent, 'alive')
def test_heartbeat_timestamp_format(self, agent_alive):
chassis_private = fakes.FakeOvsdbRow.create_one_ovsdb_row(
attrs={'name': 'chassis5'})
agents = self.agent_cache.agents_by_chassis_private(chassis_private)
agent = list(agents)[0]
agent.chassis.hostname = 'fake-hostname'
agent.updated_at = datetime.datetime(
year=2023, month=2, day=23, hour=1, minute=2, second=3,
microsecond=456789).replace(tzinfo=datetime.timezone.utc)
agent_alive.return_value = True
# Verify that both microseconds and timezone are dropped
self.assertEqual(str(agent.as_dict()['heartbeat_timestamp']),
'2023-02-23 01:02:03')

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Normalise OVN agent heartbeat timestamp format to match other agent types.
This fixes parsing of ``GET /v2.0/agents`` for some clients, such as
gophercloud.