Skip to content

Commit

Permalink
refactor discover to use K8s.Cluster.Discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
coryodaniel committed Jul 2, 2019
1 parent b90af2c commit 595e100
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 282 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -107,7 +107,7 @@ make get/${NEW_VERSION_NUMBER}
make test/${NEW_VERSION_NUMBER}
```

A [mock `Discovery`](.test/support/mock/discovery.ex) module exist populated by [this JSON config](./test/support/mock/data/groups.json) to simulate runtime API discovery.
Mock discovery [responses](.test/support/discovery) exist to simulate runtime API discovery using the [`FileDriver`](./lib/k8s/cluster/discover/file_driver.ex)

If new resources or APIs were added to kubernetes in the new version you will likely see one of these errors: `unsupported_group_version` and `unsupported_kind`.

Expand All @@ -117,7 +117,7 @@ This error occurs when a new API is added to kubernetes.

Example: `{:error, :unsupported_group_version, "scheduling.k8s.io/v1"}`

Add the `:unsupported_group_version` to the mock configuration and rerun the test suite.
Add the `:unsupported_group_version` to [this](test/support/discovery/resource_definitions.json) mock configuration and rerun the test suite.

A config entry looks like:

Expand Down
6 changes: 5 additions & 1 deletion config/test.exs
@@ -1,7 +1,11 @@
use Mix.Config

config :k8s,
discovery_provider: Mock.Discovery,
discovery_driver: K8s.Cluster.Discovery.FileDriver,
discovery_opts: %{
api_versions_path: "test/support/discovery/sample_api_versions.json",
resource_definitions_path: "test/support/discovery/resource_definitions.json"
},
http_provider: K8s.Client.DynamicHTTPProvider,
clusters: %{
test: %{
Expand Down
8 changes: 0 additions & 8 deletions lib/k8s/behaviours/discovery_provider.ex

This file was deleted.

12 changes: 6 additions & 6 deletions lib/k8s/cluster.ex
Expand Up @@ -3,7 +3,7 @@ defmodule K8s.Cluster do
Cluster configuration and API route store for `K8s.Client`
"""

@discovery Application.get_env(:k8s, :discovery_provider, K8s.Discovery)
alias K8s.Cluster.Discovery
@dialyzer {:no_return, register!: 2, auto_register_clusters!: 0}

defmodule RegistrationException do
Expand All @@ -29,8 +29,8 @@ defmodule K8s.Cluster do
@spec register(atom, K8s.Conf.t()) :: {:ok, atom()} | {:error, any()}
def register(cluster_name, conf) do
with true <- :ets.insert(K8s.Conf, {cluster_name, conf}),
{:ok, groups} <- @discovery.resource_definitions_by_group(cluster_name) do
insert_groups(cluster_name, groups)
{:ok, definitions} <- Discovery.resource_definitions(cluster_name) do
insert_definitions(cluster_name, definitions)
K8s.Sys.Event.cluster_registered(%{}, %{cluster: cluster_name})
{:ok, cluster_name}
end
Expand All @@ -57,9 +57,9 @@ defmodule K8s.Cluster do
end
end

@spec insert_groups(atom, list(map)) :: no_return
defp insert_groups(cluster_name, groups) do
Enum.each(groups, fn %{"groupVersion" => gv, "resources" => rs} ->
@spec insert_definitions(atom, list(map)) :: no_return
defp insert_definitions(cluster_name, definitions) do
Enum.each(definitions, fn %{"groupVersion" => gv, "resources" => rs} ->
cluster_group_key = K8s.Cluster.Group.cluster_key(cluster_name, gv)
:ets.insert(K8s.Cluster.Group, {cluster_group_key, gv, rs})
end)
Expand Down
3 changes: 1 addition & 2 deletions lib/k8s/cluster/discovery.ex
Expand Up @@ -13,8 +13,7 @@ defmodule K8s.Cluster.Discovery do
"""

@behaviour K8s.Cluster.Discovery.Driver
@driver K8s.Cluster.Discovery.FileDriver
# @driver Application.get_env(:k8s, :discovery_driver, K8s.Cluster.Discovery.HTTPDriver)
@driver Application.get_env(:k8s, :discovery_driver, K8s.Cluster.Discovery.HTTPDriver)

@typedoc """
Resource definition identifier. Format: `{groupVersion, kind, name}`
Expand Down
23 changes: 18 additions & 5 deletions lib/k8s/cluster/discovery/file_driver.ex
@@ -1,23 +1,36 @@
defmodule K8s.Cluster.Discovery.FileDriver do
@moduledoc """
`K8s.Cluster.Discovery.Driver` implementation that returns kubernetes API features from file
"""
@behaviour K8s.Cluster.Discovery.Driver

@api_versions_path "test/support/discovery_api_versions.json"
@resource_definitions_path "test/support/discovery_resource_definitions.json"

@impl true
def api_versions(_cluster, _opts \\ []), do: parse_json(@api_versions_path)
def api_versions(_cluster, opts \\ []) do
file = opts[:path] || path_for(:api_versions_path)
parse_json(file)
end

@impl true
def resource_definitions(_cluster, _opts \\ []), do: parse_json(@resource_definitions_path)
def resource_definitions(_cluster, opts \\ []) do
file = opts[:path] || path_for(:resource_definitions_path)
parse_json(file)
end

@spec parse_json(binary()) :: {:ok, list() | map()} | {:error, atom()}
defp parse_json(nil), do: {:error, :file_not_found}

defp parse_json(file) do
with {:ok, json} <- File.read(file),
{:ok, data} <- Jason.decode(json) do
{:ok, data}
end
end

@spec path_for(atom) :: nil | binary()
defp path_for(file) do
opts = Application.get_env(:k8s, :discovery_opts, %{})
opts[file]
end
end
97 changes: 0 additions & 97 deletions lib/k8s/discovery.ex

This file was deleted.

11 changes: 0 additions & 11 deletions lib/k8s/scratch.exs

This file was deleted.

9 changes: 6 additions & 3 deletions test/k8s/cluster/discovery_test.exs
Expand Up @@ -6,7 +6,8 @@ defmodule K8s.Cluster.DiscoveryTest do
describe "api_version/1" do
test "returns a list of API versions" do
cluster = :test
{:ok, api_versions} = Discovery.api_versions(cluster)
file = "test/support/discovery/sample_api_versions.json"
{:ok, api_versions} = Discovery.api_versions(cluster, path: file)

assert Enum.member?(api_versions, "v1")
assert Enum.member?(api_versions, "batch/v1")
Expand All @@ -16,7 +17,8 @@ defmodule K8s.Cluster.DiscoveryTest do
describe "resource_identifiers/1" do
test "returns a list of resource identifiers" do
cluster = :test
{:ok, resource_identifiers} = Discovery.resource_identifiers(cluster)
file = "test/support/discovery/sample_resource_definitions.json"
{:ok, resource_identifiers} = Discovery.resource_identifiers(cluster, path: file)

assert resource_identifiers == [
{"batch/v1", "Job", "jobs"},
Expand All @@ -31,7 +33,8 @@ defmodule K8s.Cluster.DiscoveryTest do
describe "resource_definitions/1" do
test "returns full resource definitions" do
cluster = :test
{:ok, resource_definitions} = Discovery.resource_definitions(cluster)
file = "test/support/discovery/sample_resource_definitions.json"
{:ok, resource_definitions} = Discovery.resource_definitions(cluster, path: file)

assert Enum.member?(resource_definitions, %{
"groupVersion" => "v1",
Expand Down
130 changes: 0 additions & 130 deletions test/k8s/discovery_test.exs

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 595e100

Please sign in to comment.