Skip to content

Commit 9ca5aae

Browse files
author
Fletcher Haynes
committed
Added a simple GenServer for monitoring the API health endpoint
1 parent dafdc4f commit 9ca5aae

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

lib/exkube.ex

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,5 @@ defmodule Exkube.Base do
2323
_ ->
2424
{:error, %{status_code: status_code, body: body}}
2525
end
26-
2726
end
28-
2927
end

lib/health_supervisor.ex

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule Exkube.HealthSupervisor do
2+
@moduledoc """
3+
Periodically pings the k8s API to check system health
4+
"""
5+
6+
use GenServer
7+
8+
def start_link(api_url, ok_callback, error_callback) do
9+
GenServer.start_link(__MODULE__, [api_url: api_url, ok_callback: ok_callback, error_callback: error_callback], [])
10+
end
11+
12+
def init([api_url, ok_callback, error_callback]) do
13+
schedule_work()
14+
{:ok, [api_url, ok_callback, error_callback]}
15+
end
16+
17+
def handle_info(:work, state) do
18+
state = do_work(state)
19+
schedule_work()
20+
{:noreply, state}
21+
end
22+
23+
defp do_work(state) do
24+
case Exkube.Base.status(state[:api_url]) do
25+
{:ok, _} ->
26+
state[:ok_callback].(state[:api_url])
27+
{:error, _} ->
28+
state[:error_callback].(state[:api_url])
29+
end
30+
state
31+
end
32+
33+
defp schedule_work do
34+
Process.send_after(self(), :work, 5_000)
35+
end
36+
37+
end

mix.exs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ defmodule Exkube.Mixfile do
33

44
def project do
55
[app: :exkube,
6-
version: "0.1.0",
6+
version: "0.1.1",
77
description: description(),
88
package: package(),
9-
links: links(),
109
elixir: "~> 1.4",
1110
build_embedded: Mix.env == :prod,
1211
start_permanent: Mix.env == :prod,
@@ -36,7 +35,8 @@ defmodule Exkube.Mixfile do
3635
defp deps do
3736
[
3837
{:httpoison, "~> 0.11.2"},
39-
{:poison, "~> 3.1.0"}
38+
{:poison, "~> 3.1.0"},
39+
{:ex_doc, ">= 0.0.0", only: :dev}
4040
]
4141
end
4242

@@ -51,12 +51,13 @@ defmodule Exkube.Mixfile do
5151
[
5252
name: :exkube,
5353
maintainers: ["Fletcher Haynes <[email protected]>"],
54-
licenses: ["Apache 2.0"]
54+
licenses: ["Apache 2.0"],
55+
links: links()
5556
]
5657
end
5758

5859
defp links do
59-
%{}
60+
%{"GitHub" => "https://github.com/fhaynes/exkube"}
6061
end
61-
62+
6263
end

mix.lock

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
%{"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], [], "hexpm"},
2+
"earmark": {:hex, :earmark, "1.2.2", "f718159d6b65068e8daeef709ccddae5f7fdc770707d82e7d126f584cd925b74", [:mix], [], "hexpm"},
3+
"ex_doc": {:hex, :ex_doc, "0.16.1", "b4b8a23602b4ce0e9a5a960a81260d1f7b29635b9652c67e95b0c2f7ccee5e81", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
24
"hackney": {:hex, :hackney, "1.8.6", "21a725db3569b3fb11a6af17d5c5f654052ce9624219f1317e8639183de4a423", [:rebar3], [{:certifi, "1.2.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.0.2", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
35
"httpoison": {:hex, :httpoison, "0.11.2", "9e59f17a473ef6948f63c51db07320477bad8ba88cf1df60a3eee01150306665", [:mix], [{:hackney, "~> 1.8.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
46
"idna": {:hex, :idna, "5.0.2", "ac203208ada855d95dc591a764b6e87259cb0e2a364218f215ad662daa8cd6b4", [:rebar3], [{:unicode_util_compat, "0.2.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},

test/exkube_test.exs

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
defmodule ExkubeTest do
22
use ExUnit.Case
33

4-
5-
test "get status" do
6-
result = Exkube.Base.status("https://api.kubernetes.ausw1a.int.uops.internal.unity3d.com")
7-
assert result == {:ok, "ok"}
8-
end
94
end

0 commit comments

Comments
 (0)