Skip to content

Commit ea91c01

Browse files
authored
Merge pull request #47 from JuliaComputing/tan/misc
allow timeout to be set via KuberContext
2 parents e19f58f + 42b55bf commit ea91c01

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
A Julia Kubernetes Client.
66

7-
An easy to use API to access Kubernetes clusters from Julia. The `Kuber.Kubernetes` submodule has the complete set of low level APIs and entities.
7+
An easy to use API to access Kubernetes clusters from Julia. The `Kuber.ApiImpl.Kubernetes` submodule has the complete set of low level APIs and entities.
88

99
Most of the low level APIs fit into a common usage pattern. Kuber.jl makes it possible to use all of them with only a few intuitive verb based APIs. Verbs act on entities. Entities can be identified by names or selector patterns, or otherwise can apply to all entities of that class. Verbs can take additional parameters, e.g. when creating or updating entities.
1010

WalkThrough.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ Now that we have set up our connection properly, let's explore what we have in o
145145
julia> result = get(ctx, :ComponentStatus);
146146

147147
julia> typeof(result)
148-
Kuber.Kubernetes.IoK8sApiCoreV1ComponentStatusList
148+
Kuber.ApiImpl.Kubernetes.IoK8sApiCoreV1ComponentStatusList
149149
```
150150

151-
Note that we got back a Julia type `Kuber.Kubernetes.IoK8sApiCoreV1ComponentStatusList`. It represents a list of `ComponentStatus` entities that we asked for. It has been resolved to match the specific to the version of API we used - `CoreV1` in this case. We can display the entity in JSON form in the REPL, by simply `show`ing it.
151+
Note that we got back a Julia type `Kuber.ApiImpl.Kubernetes.IoK8sApiCoreV1ComponentStatusList`. It represents a list of `ComponentStatus` entities that we asked for. It has been resolved to match the specific to the version of API we used - `CoreV1` in this case. We can display the entity in JSON form in the REPL, by simply `show`ing it.
152152

153153
```julia
154154
julia> result
@@ -245,7 +245,7 @@ julia> nginx_pod = kuber_obj(ctx, """{
245245
}""");
246246

247247
julia> typeof(nginx_pod)
248-
Kuber.Kubernetes.IoK8sApiCoreV1Pod
248+
Kuber.ApiImpl.Kubernetes.IoK8sApiCoreV1Pod
249249

250250
julia> nginx_service = kuber_obj(ctx, """{
251251
"kind": "Service",
@@ -263,7 +263,7 @@ julia> nginx_service = kuber_obj(ctx, """{
263263
}""")
264264

265265
julia> typeof(nginx_service)
266-
Kuber.Kubernetes.IoK8sApiCoreV1Service
266+
Kuber.ApiImpl.Kubernetes.IoK8sApiCoreV1Service
267267
```
268268

269269
To create the pod in the cluster, use the `put!` API. And we should see it when we list the pods.

src/helpers.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ mutable struct KuberContext
4141
retry_all_apis::Bool
4242
initialized::Bool
4343

44-
function KuberContext(apimodule::Module=ApiImpl)
44+
function KuberContext(apimodule::Module=ApiImpl; kwargs...)
4545
kctx = new(apimodule)
4646

4747
rtfn = (default,data)->kuber_type(kctx, default, data)
48-
swaggerclient = Swagger.Client(DEFAULT_URI; get_return_type=rtfn)
48+
swaggerclient = Swagger.Client(DEFAULT_URI; get_return_type=rtfn, kwargs...)
4949
swaggerclient.headers["Connection"] = "close"
5050

5151
kctx.client = swaggerclient
@@ -85,6 +85,26 @@ end
8585
retries(ctx::KuberContext, mutating::Bool=true) = (mutating && !ctx.retry_all_apis) ? 1 : ctx.default_retries
8686
retries(watch::KuberWatchContext, mutating::Bool=true) = retries(watch.ctx, mutating)
8787

88+
get_client(ctx::KuberContext) = ctx.client
89+
get_client(ctx::KuberWatchContext) = ctx.ctx.client
90+
91+
get_timeout(ctx::Union{KuberContext,KuberWatchContext}) = get_client(ctx).timeout[]
92+
93+
function set_timeout(ctx::Union{KuberContext,KuberWatchContext}, timeout::Integer)
94+
Swagger.set_timeout(get_client(ctx), timeout)
95+
ctx
96+
end
97+
98+
function with_timeout(fn, ctx::Union{KuberContext,KuberWatchContext}, timeout::Integer)
99+
old_timeout = get_timeout(ctx)
100+
set_timeout(ctx, timeout)
101+
try
102+
fn(ctx)
103+
finally
104+
set_timeout(ctx, old_timeout)
105+
end
106+
end
107+
88108
convert(::Type{Vector{UInt8}}, s::T) where {T<:AbstractString} = collect(codeunits(s))
89109
convert(::Type{T}, json::String) where {T<:SwaggerModel} = convert(T, JSON.parse(json))
90110
convert(::Type{Dict{String,Any}}, model::T) where {T<:SwaggerModel} = JSON.parse(JSON.json(model))

test/runtests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Kuber
2+
using Swagger
23
using Test
34

45
const Typedefs = Kuber.ApiImpl.Typedefs
@@ -23,6 +24,23 @@ function init_context(override=nothing, verbose=true)
2324
ctx
2425
end
2526

27+
function test_set_timeout(ctx)
28+
@test Kuber.get_timeout(ctx) == Swagger.DEFAULT_TIMEOUT_SECS
29+
30+
Kuber.with_timeout(ctx, 10) do ctx
31+
@test Kuber.get_timeout(ctx) == 10
32+
end
33+
34+
wctx = Kuber.KuberWatchContext(ctx, Channel{Any}())
35+
@test Kuber.get_timeout(wctx) == Swagger.DEFAULT_TIMEOUT_SECS
36+
Kuber.with_timeout(wctx, 10) do wctx
37+
@test Kuber.get_timeout(wctx) == 10
38+
end
39+
@test Kuber.get_timeout(wctx) == Swagger.DEFAULT_TIMEOUT_SECS
40+
41+
@test Kuber.get_timeout(ctx) == Swagger.DEFAULT_TIMEOUT_SECS
42+
end
43+
2644
function list_cluster_components(ctx)
2745
@testset "List component status" begin
2846
res = get(ctx, :ComponentStatus)
@@ -299,6 +317,10 @@ function test_all()
299317
test_versioned(ctx, "1")
300318
end
301319

320+
@testset "Set Timeouts" begin
321+
test_set_timeout(ctx)
322+
end
323+
302324
@testset "Overridden API Versions" begin
303325
@test ctx.apis[:Apiregistration][1].api == Kubernetes.ApiregistrationV1Api
304326
@test ctx.apis[:Apps][1].api == Kubernetes.AppsV1Api

0 commit comments

Comments
 (0)