Skip to content

Commit b3af495

Browse files
authored
Merge pull request #319 from holyketzer/extend-pod-logs
Add timestamps and since_time to get_pod_log
2 parents 09e72a6 + 2c93569 commit b3af495

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,13 @@ client.get_pod_log('pod-name', 'default', previous: true)
514514
# => "..."
515515
```
516516

517+
Kubernetes can add timestamps to every log line or filter by lines time:
518+
```ruby
519+
client.get_pod_log('pod-name', 'default', timestamps: true, since_time: '2018-04-27T18:30:17.480321984Z')
520+
# => "..."
521+
```
522+
`since_time` can be a a `Time`, `DateTime` or `String` formatted according to RFC3339
523+
517524
You can also watch the logs of a pod to get a stream of data:
518525

519526
```ruby

lib/kubeclient/common.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,13 @@ def all_entities(options = {})
360360
end
361361
end
362362

363-
def get_pod_log(pod_name, namespace, container: nil, previous: false)
363+
def get_pod_log(pod_name, namespace,
364+
container: nil, previous: false, timestamps: false, since_time: nil)
364365
params = {}
365366
params[:previous] = true if previous
366367
params[:container] = container if container
368+
params[:timestamps] = timestamps if timestamps
369+
params[:sinceTime] = format_datetime(since_time) if since_time
367370

368371
ns = build_namespace_prefix(namespace)
369372
handle_exception do
@@ -427,6 +430,18 @@ def api
427430

428431
private
429432

433+
# Format ditetime according to RFC3339
434+
def format_datetime(value)
435+
case value
436+
when DateTime, Time
437+
value.strftime('%FT%T.%9N%:z')
438+
when String
439+
value
440+
else
441+
raise ArgumentError, "unsupported type '#{value.class}' of time value '#{value}'"
442+
end
443+
end
444+
430445
def format_response(as, body, list_type = nil)
431446
case as
432447
when :raw

test/test_common.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
# Unit tests for the common module
44
class CommonTest < MiniTest::Test
5+
class ClientStub
6+
include Kubeclient::ClientMixin
7+
end
8+
9+
def client
10+
@client ||= ClientStub.new
11+
end
12+
513
def test_underscore_entity
614
%w[
715
Pod pod
@@ -29,4 +37,22 @@ def test_underscore_entity
2937
assert_equal(Kubeclient::ClientMixin.underscore_entity(singular), plural)
3038
end
3139
end
40+
41+
def test_format_datetime_with_string
42+
value = '2018-04-27T18:30:17.480321984Z'
43+
formatted = client.send(:format_datetime, value)
44+
assert_equal(formatted, value)
45+
end
46+
47+
def test_format_datetime_with_datetime
48+
value = DateTime.new(2018, 4, 30, 19, 20, 33)
49+
formatted = client.send(:format_datetime, value)
50+
assert_equal(formatted, '2018-04-30T19:20:33.000000000+00:00')
51+
end
52+
53+
def test_format_datetime_with_time
54+
value = Time.new(2018, 4, 30, 19, 20, 33, 0)
55+
formatted = client.send(:format_datetime, value)
56+
assert_equal(formatted, '2018-04-30T19:20:33.000000000+00:00')
57+
end
3258
end

test/test_pod_log.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ def test_get_pod_log_container
3232
times: 1)
3333
end
3434

35+
def test_get_pod_log_since_time
36+
stub_request(:get, %r{/namespaces/default/pods/[a-z0-9-]+/log})
37+
.to_return(body: open_test_file('pod_log.txt'),
38+
status: 200)
39+
40+
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
41+
retrieved_log = client.get_pod_log('redis-master-pod',
42+
'default',
43+
timestamps: true,
44+
since_time: '2018-04-27T18:30:17.480321984Z')
45+
46+
assert_equal(open_test_file('pod_log.txt').read, retrieved_log)
47+
48+
assert_requested(:get,
49+
'http://localhost:8080/api/v1/namespaces/default/pods/redis-master-pod/log?sinceTime=2018-04-27T18:30:17.480321984Z&timestamps=true',
50+
times: 1)
51+
end
52+
3553
def test_watch_pod_log
3654
expected_lines = open_test_file('pod_log.txt').read.split("\n")
3755

0 commit comments

Comments
 (0)