Skip to content

Commit f8ba687

Browse files
authored
Workaround changed behavior in Protobuf's Message#to_h (#4401)
The behavior of 'to_h' for Protobuf objects changed [1]; unset and default values are now being omitted. Thus the update of Protobuf libraries [2] led to an incompatible change as the Cloud Controller returned "null" instead of "0" for the external and internal ports as part of the process stats endpoint. With this change the hash is created manually by accessing all fields in the Protobuf object(s) directly. [1] protocolbuffers/protobuf#15234 [2] #4359
1 parent 12fe390 commit f8ba687

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lib/cloud_controller/diego/reporters/instances_stats_reporter.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def build_info(state, actual_lrp, process, stats, quota_stats, log_cache_errors)
7878
host: actual_lrp.actual_lrp_net_info.address,
7979
instance_guid: actual_lrp.actual_lrp_instance_key.instance_guid,
8080
port: get_default_port(actual_lrp.actual_lrp_net_info),
81-
net_info: actual_lrp.actual_lrp_net_info.to_h,
81+
net_info: actual_lrp_net_info_to_hash(actual_lrp.actual_lrp_net_info),
8282
uptime: nanoseconds_to_seconds((Time.now.to_f * 1e9) - actual_lrp.since),
8383
fds_quota: process.file_descriptors
8484
}.merge(metrics_data_for_instance(stats, quota_stats, log_cache_errors, Time.now.to_datetime.rfc3339, actual_lrp.actual_lrp_key.index))
@@ -198,6 +198,22 @@ def get_default_port(net_info)
198198

199199
0
200200
end
201+
202+
def actual_lrp_net_info_to_hash(net_info)
203+
%i[address instance_address ports preferred_address].index_with do |field_name|
204+
if field_name == :ports
205+
net_info.ports.map(&method(:port_mapping_to_hash))
206+
else
207+
net_info.send(field_name)
208+
end
209+
end
210+
end
211+
212+
def port_mapping_to_hash(port_mapping)
213+
%i[container_port container_tls_proxy_port host_port host_tls_proxy_port].index_with do |field_name|
214+
port_mapping.send(field_name)
215+
end
216+
end
201217
end
202218
end
203219
end

spec/unit/lib/cloud_controller/diego/reporters/instances_stats_reporter_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
7878
[container_metric_batch]
7979
end
8080

81+
let(:expected_lrp_1_net_info) do
82+
{
83+
address: 'lrp-host',
84+
instance_address: '',
85+
ports: [
86+
{ container_port: DEFAULT_APP_PORT, container_tls_proxy_port: 0, host_port: 2222, host_tls_proxy_port: 0 },
87+
{ container_port: 1111, container_tls_proxy_port: 0, host_port: 0, host_tls_proxy_port: 0 }
88+
],
89+
preferred_address: :UNKNOWN
90+
}
91+
end
8192
let(:expected_stats_response) do
8293
{
8394
0 => {
@@ -90,7 +101,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
90101
host: 'lrp-host',
91102
instance_guid: 'instance-a',
92103
port: 2222,
93-
net_info: lrp_1_net_info.to_h,
104+
net_info: expected_lrp_1_net_info,
94105
uptime: two_days_in_seconds,
95106
mem_quota: 1234,
96107
disk_quota: 10_234,
@@ -217,7 +228,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
217228
host: 'lrp-host',
218229
instance_guid: 'instance-a',
219230
port: 2222,
220-
net_info: lrp_1_net_info.to_h,
231+
net_info: expected_lrp_1_net_info,
221232
uptime: two_days_in_seconds,
222233
mem_quota: nil,
223234
disk_quota: nil,
@@ -272,7 +283,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
272283
host: 'lrp-host',
273284
instance_guid: 'instance-a',
274285
port: 2222,
275-
net_info: lrp_1_net_info.to_h,
286+
net_info: expected_lrp_1_net_info,
276287
uptime: two_days_in_seconds,
277288
mem_quota: nil,
278289
disk_quota: nil,
@@ -519,7 +530,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
519530
host: 'lrp-host',
520531
instance_guid: 'instance-a',
521532
port: 2222,
522-
net_info: lrp_1_net_info.to_h,
533+
net_info: expected_lrp_1_net_info,
523534
uptime: two_days_in_seconds,
524535
mem_quota: nil,
525536
disk_quota: nil,
@@ -653,7 +664,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
653664
host: 'lrp-host',
654665
instance_guid: 'instance-a',
655666
port: 2222,
656-
net_info: lrp_1_net_info.to_h,
667+
net_info: expected_lrp_1_net_info,
657668
uptime: two_days_in_seconds,
658669
mem_quota: nil,
659670
disk_quota: nil,

0 commit comments

Comments
 (0)