Skip to content

Commit 599fb4d

Browse files
ewilkeniabudiab
authored andcommitted
include tailLines flag on log requests
1 parent c497cd1 commit 599fb4d

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Sources/SwiftkubeClient/Client/GenericKubernetesClient.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,14 @@ internal extension GenericKubernetesClient {
259259
/// - container: The name of the container.
260260
/// - previous: Whether to request the logs of the previous instance of the container.
261261
/// - timestamps: Whether to include timestamps on the log lines.
262+
/// - tailLines: The number of log lines to load.
262263
///
263264
/// - Returns: The container logs as a single String.
264265
/// - Throws: An error of type ``SwiftkubeClientError``.
265-
func logs(in namespace: NamespaceSelector, name: String, container: String?, previous: Bool = false, timestamps: Bool = false) async throws -> String {
266+
func logs(in namespace: NamespaceSelector, name: String, container: String?, previous: Bool = false, timestamps: Bool = false, tailLines: Int? = nil) async throws -> String {
266267
let request = try makeRequest()
267268
.in(namespace)
268-
.toLogs(pod: name, container: container, previous: previous, timestamps: timestamps)
269+
.toLogs(pod: name, container: container, previous: previous, timestamps: timestamps, tailLines: tailLines)
269270
.subResource(.log)
270271
.build()
271272

@@ -424,6 +425,7 @@ public extension GenericKubernetesClient {
424425
/// - name: The name of the Pod.
425426
/// - container: The name of the container.
426427
/// - timestamps: Whether to include timestamps on the log lines.
428+
/// - tailLines: The number of log lines to load.
427429
/// - retryStrategy: An instance of a ``RetryStrategy`` configuration to use.
428430
///
429431
/// - Returns: A ``SwiftkubeClientTask`` instance, representing a streaming connection to the API server.
@@ -432,9 +434,10 @@ public extension GenericKubernetesClient {
432434
name: String,
433435
container: String?,
434436
timestamps: Bool = false,
437+
tailLines: Int? = nil,
435438
retryStrategy: RetryStrategy = RetryStrategy.never
436439
) async throws -> SwiftkubeClientTask<String> {
437-
let request = try makeRequest().in(namespace).toFollow(pod: name, container: container, timestamps: timestamps).build()
440+
let request = try makeRequest().in(namespace).toFollow(pod: name, container: container, timestamps: timestamps, tailLines: tailLines).build()
438441

439442
return SwiftkubeClientTask(
440443
client: httpClient,

Sources/SwiftkubeClient/Client/NamespacedGenericKubernetesClient+Pod.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public extension NamespacedGenericKubernetesClient where Resource == core.v1.Pod
6464
/// - name: The name of the Pod.
6565
/// - container: The name of the container.
6666
/// - timestamps: Whether to include timestamps on the log lines.
67+
/// - tailLines: The number of log lines to load.
6768
/// - retryStrategy: An instance of a ``RetryStrategy`` configuration to use.
6869
///
6970
/// - Returns: A ``SwiftkubeClientTask`` instance, representing a streaming connection to the API server.
@@ -72,13 +73,15 @@ public extension NamespacedGenericKubernetesClient where Resource == core.v1.Pod
7273
name: String,
7374
container: String? = nil,
7475
timestamps: Bool = false,
76+
tailLines: Int? = nil,
7577
retryStrategy: RetryStrategy = RetryStrategy.never
7678
) async throws -> SwiftkubeClientTask<String> {
7779
try await client.follow(
7880
in: namespace ?? .namespace(config.namespace),
7981
name: name,
8082
container: container,
8183
timestamps: timestamps,
84+
tailLines: tailLines,
8285
retryStrategy: retryStrategy
8386
)
8487
}
@@ -93,6 +96,7 @@ public extension NamespacedGenericKubernetesClient where Resource == core.v1.Pod
9396
/// - container: The name of the container.
9497
/// - previous: Whether to request the logs of the previous instance of the container.
9598
/// - timestamps: Whether to include timestamps on the log lines.
99+
/// - tailLines: The number of log lines to load.
96100
///
97101
/// - Returns: The container logs as a single String.
98102
/// - Throws: An error of type ``SwiftkubeClientError``.
@@ -101,14 +105,16 @@ public extension NamespacedGenericKubernetesClient where Resource == core.v1.Pod
101105
name: String,
102106
container: String? = nil,
103107
previous: Bool = false,
104-
timestamps: Bool = false
108+
timestamps: Bool = false,
109+
tailLines: Int? = nil
105110
) async throws -> String {
106111
try await client.logs(
107112
in: namespace ?? .namespace(config.namespace),
108113
name: name,
109114
container: container,
110115
previous: previous,
111-
timestamps: timestamps
116+
timestamps: timestamps,
117+
tailLines: tailLines
112118
)
113119
}
114120
}

Sources/SwiftkubeClient/Client/RequestBuilder.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ internal protocol NamespaceStep {
5050
internal protocol MethodStep {
5151
func toGet() -> GetStep
5252
func toWatch() -> GetStep
53-
func toFollow(pod: String, container: String?, timestamps: Bool) -> GetStep
53+
func toFollow(pod: String, container: String?, timestamps: Bool, tailLines: Int?) -> GetStep
5454
func toPost() -> PostStep
5555
func toPut() -> PutStep
5656
func toDelete() -> DeleteStep
57-
func toLogs(pod: String, container: String?, previous: Bool, timestamps: Bool) -> GetStep
57+
func toLogs(pod: String, container: String?, previous: Bool, timestamps: Bool, tailLines: Int?) -> GetStep
5858
}
5959

6060
// MARK: - GetStep
@@ -133,6 +133,7 @@ internal class RequestBuilder {
133133
var followFlag = false
134134
var previousFlag = false
135135
var timestampsFlag = false
136+
var tailLinesFlag: Int?
136137

137138
init(config: KubernetesClientConfig, gvr: GroupVersionResource) {
138139
self.config = config
@@ -196,23 +197,25 @@ extension RequestBuilder: MethodStep {
196197

197198
/// Set request method to GET and notice the pod and container to follow for the pending request
198199
/// - Returns:The builder instance as GetStep
199-
func toFollow(pod: String, container: String?, timestamps: Bool = false) -> GetStep {
200+
func toFollow(pod: String, container: String?, timestamps: Bool = false, tailLines: Int? = nil) -> GetStep {
200201
method = .GET
201202
resourceName = pod
202203
containerName = container
203204
subResourceType = .log
204205
followFlag = true
205206
timestampsFlag = timestamps
207+
tailLinesFlag = tailLines
206208
return self as GetStep
207209
}
208210

209-
func toLogs(pod: String, container: String?, previous: Bool = false, timestamps: Bool = false) -> GetStep {
211+
func toLogs(pod: String, container: String?, previous: Bool = false, timestamps: Bool = false, tailLines: Int? = nil) -> GetStep {
210212
method = .GET
211213
resourceName = pod
212214
containerName = container
213215
subResourceType = .log
214216
previousFlag = previous
215217
timestampsFlag = timestamps
218+
tailLinesFlag = tailLines
216219
return self as GetStep
217220
}
218221
}
@@ -356,6 +359,10 @@ internal extension RequestBuilder {
356359
add(queryItem: URLQueryItem(name: "timestamps", value: "true"))
357360
}
358361

362+
if let tailLinesFlag {
363+
add(queryItem: URLQueryItem(name: "tailLines", value: String(tailLinesFlag)))
364+
}
365+
359366
if let container = containerName {
360367
add(queryItem: URLQueryItem(name: "container", value: container))
361368
}

0 commit comments

Comments
 (0)