@@ -7,14 +7,19 @@ defmodule Exkube.Base do
7
7
use HTTPoison.Base
8
8
require Logger
9
9
10
+ @ kube_user Application . get_env ( :exkube , :kube_user )
11
+ @ kube_password Application . get_env ( :exkube , :kube_password )
12
+ @ insecure_ca Application . get_env ( :exkube , :insecure_ca )
13
+ @ endpoint Application . get_env ( :exkube , :api_url )
14
+
10
15
@ doc """
11
16
Checks the health endpoint of Kubernetes cluster
12
17
13
18
Returns `{:ok, "ok"}` if healthy or `{:error, %{status_code, body}}` if not
14
19
"""
15
20
def status ( api_url ) do
16
21
url = api_url <> "/healthz"
17
- result = HTTPoison . request! ( :get , url , "" , [ ] , hackney: [ Application . get_env ( :exkube , : insecure_ca) ] )
22
+ result = HTTPoison . request! ( :get , url , "" , [ ] , hackney: [ @ insecure_ca ] )
18
23
% HTTPoison.Response { status_code: status_code , body: body } = result
19
24
20
25
case status_code do
@@ -24,4 +29,47 @@ defmodule Exkube.Base do
24
29
{ :error , % { status_code: status_code , body: body } }
25
30
end
26
31
end
32
+
33
+ @ doc """
34
+ Processes the status code out of a response and returns a tuple
35
+ """
36
+ def process_response_status_code ( body , status_code ) do
37
+ case status_code do
38
+ 403 ->
39
+ { :error , :forbidden }
40
+ 404 ->
41
+ { :error , :not_found }
42
+ 200 ->
43
+ data = JSON . decode! ( body )
44
+ { :ok , data }
45
+ 201 ->
46
+ data = JSON . decode! ( body )
47
+ { :ok , data }
48
+ _ ->
49
+ data = JSON . decode! ( body )
50
+ { :error , status_code: status_code , body: data }
51
+ end
52
+ end
53
+
54
+ @ doc """
55
+ Builds up a URL
56
+ """
57
+ def process_url ( url ) do
58
+ @ endpoint <> url
59
+ end
60
+
61
+ @ doc """
62
+ Adds in the necessary auth headers if they exist
63
+ """
64
+ def process_request_options ( options ) do
65
+ options ++ [ hackney: [ @ insecure_ca , basic_auth: { @ kube_user , @ kube_password } ] ]
66
+ end
67
+
68
+ @ doc """
69
+ Adds in the necessary headers
70
+ """
71
+ def process_request_headers ( headers ) do
72
+ headers ++ [ { "Content-Type" , "application/json" } ]
73
+ end
74
+
27
75
end
0 commit comments