Skip to content

Commit

Permalink
add previous & timestamps flags to log requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilken committed Sep 5, 2023
1 parent 73bec95 commit fe495ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
7 changes: 4 additions & 3 deletions Sources/SwiftkubeClient/Client/GenericKubernetesClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -427,9 +427,10 @@ public extension GenericKubernetesClient {
in namespace: NamespaceSelector,
name: String,
container: String?,
timestamps: Bool = false,
retryStrategy: RetryStrategy = RetryStrategy.never
) throws -> SwiftkubeClientTask<String> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
try super.follow(
in: namespace ?? .namespace(config.namespace),
name: name,
container: container,
timestamps: timestamps,
retryStrategy: retryStrategy
)
}
Expand All @@ -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
)
}
}
21 changes: 17 additions & 4 deletions Sources/SwiftkubeClient/Client/RequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -342,6 +347,14 @@ internal extension RequestBuilder {
if followFlag {
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))
Expand Down

0 comments on commit fe495ef

Please sign in to comment.