From 93c49f06ca32dbdbb01b152b6cd18d077da402e7 Mon Sep 17 00:00:00 2001 From: Elias Wilken Date: Wed, 23 Aug 2023 18:14:22 +0200 Subject: [PATCH] add `previous` & `timestamps` flags to log requests --- .../Client/GenericKubernetesClient.swift | 7 ++++--- ...amespacedGenericKubernetesClient+Pod.swift | 10 +++++++-- .../Client/RequestBuilder.swift | 21 +++++++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Sources/SwiftkubeClient/Client/GenericKubernetesClient.swift b/Sources/SwiftkubeClient/Client/GenericKubernetesClient.swift index 15dfd6e..8ca4ebe 100644 --- a/Sources/SwiftkubeClient/Client/GenericKubernetesClient.swift +++ b/Sources/SwiftkubeClient/Client/GenericKubernetesClient.swift @@ -259,10 +259,10 @@ internal extension GenericKubernetesClient { /// /// - Returns: The container logs as a single String. /// - Throws: An error of type ``SwiftkubeClientError``. - func logs(in namespace: NamespaceSelector, name: String, container: String?) async throws -> String { + func logs(in namespace: NamespaceSelector, name: String, container: String?, previous: Bool = false, timestamps: Bool = false) async throws -> String { let request = try makeRequest() .in(namespace) - .toLogs(pod: name, container: container) + .toLogs(pod: name, container: container, previous: previous, timestamps: timestamps) .subResource(.log) .build() @@ -427,9 +427,10 @@ public extension GenericKubernetesClient { in namespace: NamespaceSelector, name: String, container: String?, + timestamps: Bool = false, retryStrategy: RetryStrategy = RetryStrategy.never ) throws -> SwiftkubeClientTask { - let request = try makeRequest().in(namespace).toFollow(pod: name, container: container).build() + let request = try makeRequest().in(namespace).toFollow(pod: name, container: container, timestamps: timestamps).build() return SwiftkubeClientTask( client: httpClient, diff --git a/Sources/SwiftkubeClient/Client/NamespacedGenericKubernetesClient+Pod.swift b/Sources/SwiftkubeClient/Client/NamespacedGenericKubernetesClient+Pod.swift index e3b008d..33d7e39 100644 --- a/Sources/SwiftkubeClient/Client/NamespacedGenericKubernetesClient+Pod.swift +++ b/Sources/SwiftkubeClient/Client/NamespacedGenericKubernetesClient+Pod.swift @@ -70,12 +70,14 @@ public extension NamespacedGenericKubernetesClient where Resource == core.v1.Pod in namespace: NamespaceSelector? = nil, name: String, container: String? = nil, + timestamps: Bool = false, retryStrategy: RetryStrategy = RetryStrategy.never ) throws -> SwiftkubeClientTask { try super.follow( in: namespace ?? .namespace(config.namespace), name: name, container: container, + timestamps: timestamps, retryStrategy: retryStrategy ) } @@ -94,12 +96,16 @@ public extension NamespacedGenericKubernetesClient where Resource == core.v1.Pod func logs( in namespace: NamespaceSelector? = nil, name: String, - container: String? = nil + container: String? = nil, + previous: Bool = false, + timestamps: Bool = false ) async throws -> String { try await super.logs( in: namespace ?? .namespace(config.namespace), name: name, - container: container + container: container, + previous: previous, + timestamps: timestamps ) } } diff --git a/Sources/SwiftkubeClient/Client/RequestBuilder.swift b/Sources/SwiftkubeClient/Client/RequestBuilder.swift index f11a253..da5c643 100644 --- a/Sources/SwiftkubeClient/Client/RequestBuilder.swift +++ b/Sources/SwiftkubeClient/Client/RequestBuilder.swift @@ -50,11 +50,11 @@ internal protocol NamespaceStep { internal protocol MethodStep { func toGet() -> GetStep func toWatch() -> GetStep - func toFollow(pod: String, container: String?) -> GetStep + func toFollow(pod: String, container: String?, timestamps: Bool) -> GetStep func toPost() -> PostStep func toPut() -> PutStep func toDelete() -> DeleteStep - func toLogs(pod: String, container: String?) -> GetStep + func toLogs(pod: String, container: String?, previous: Bool, timestamps: Bool) -> GetStep } // MARK: - GetStep @@ -131,6 +131,8 @@ internal class RequestBuilder { var deleteOptions: meta.v1.DeleteOptions? var watchFlag = false var followFlag = false + var previousFlag = false + var timestampsFlag = false init(config: KubernetesClientConfig, gvr: GroupVersionResource) { self.config = config @@ -194,20 +196,23 @@ extension RequestBuilder: MethodStep { /// Set request method to GET and notice the pod and container to follow for the pending request /// - Returns:The builder instance as GetStep - func toFollow(pod: String, container: String?) -> GetStep { + func toFollow(pod: String, container: String?, timestamps: Bool = false) -> GetStep { method = .GET resourceName = pod containerName = container subResourceType = .log followFlag = true + timestampsFlag = timestamps return self as GetStep } - func toLogs(pod: String, container: String?) -> GetStep { + func toLogs(pod: String, container: String?, previous: Bool = false, timestamps: Bool = false) -> GetStep { method = .GET resourceName = pod containerName = container subResourceType = .log + previousFlag = previous + timestampsFlag = timestamps return self as GetStep } } @@ -343,6 +348,14 @@ internal extension RequestBuilder { add(queryItem: URLQueryItem(name: "follow", value: "true")) } + if previousFlag { + add(queryItem: URLQueryItem(name: "previous", value: "true")) + } + + if timestampsFlag { + add(queryItem: URLQueryItem(name: "timestamps", value: "true")) + } + if let container = containerName { add(queryItem: URLQueryItem(name: "container", value: container)) }