Skip to content

Commit

Permalink
Add mix kubegen.search
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Mar 26, 2024
1 parent a598a48 commit ea7378f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# Kubegen

**TODO: Add description**
Generate resource scoped Kubernetes clients with `Kubegen`.

[![Module Version](https://img.shields.io/hexpm/v/kubegen.svg)](https://hex.pm/packages/kubegen)
[![Last Updated](https://img.shields.io/github/last-commit/mruoss/kubegen.svg)](https://github.com/mruoss/kubegen/commits/main)

[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/kubegen/)
[![Total Download](https://img.shields.io/hexpm/dt/kubegen.svg)](https://hex.pm/packages/kubegen)
[![License](https://img.shields.io/hexpm/l/kubegen.svg)](https://github.com/mruoss/kubegen/blob/main/LICENSE)

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `kubegen` to your list of dependencies in `mix.exs`:
`kubegen` is a code generator. Add the package as dev dependency:

```elixir
def deps do
[
{:kubegen, "~> 0.1.0"}
{:kubegen, "~> 0.1.0", only: :dev, runtime: false}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/kubegen>.
The docs can be found at <https://hexdocs.pm/kubegen>.

## Usage
30 changes: 2 additions & 28 deletions lib/kubegen/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ defmodule Kubegen.Resource do
resource_definition
)

resource_list_path =
resource_list_path(
api_version,
resource_definition
)

generator_module =
if Map.fetch!(resource_definition, "namespaced"),
do: Kubegen.API.Namespaced,
Expand All @@ -63,17 +57,16 @@ defmodule Kubegen.Resource do
attributes =
quote do
@resource_path unquote(resource_path)
@resource_list_path unquote(resource_list_path)
end
|> Utils.flatten_blocks()
|> Utils.put_newlines()
|> List.wrap()

req_func =
quote do
defp req() do
Kubeconf.Default
|> Kubeconf.kubeconf()
|> Kubereq.new(@resource_path, @resource_list_path)
|> Kubereq.new(@resource_path)
end
end

Expand Down Expand Up @@ -197,23 +190,4 @@ defmodule Kubegen.Resource do
defp do_resource_path(api_version, %{"name" => resource_name, "namespaced" => false}) do
"#{api_version}/#{resource_name}/:name"
end

@spec resource_list_path(api_version :: String.t(), resource_definition :: map()) :: String.t()
defp resource_list_path(<<?v, _::integer>> = api_version, resource_definition) do
do_resource_list_path("api/#{api_version}", resource_definition)
end

defp resource_list_path(api_version, resource_definition) do
do_resource_list_path("apis/#{api_version}", resource_definition)
end

@spec do_resource_list_path(api_version :: String.t(), resource_definition :: map()) ::
String.t()
defp do_resource_list_path(api_version, %{"name" => resource_name, "namespaced" => true}) do
"#{api_version}/namespaces/:namespace/#{resource_name}"
end

defp do_resource_list_path(api_version, %{"name" => resource_name, "namespaced" => false}) do
"#{api_version}/#{resource_name}"
end
end
11 changes: 10 additions & 1 deletion lib/mix/tasks/kubegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ defmodule Mix.Tasks.Kubegen do
file_path |> Path.dirname() |> File.mkdir_p!()
File.write(file_path, rendered)

IO.puts("Generated module #{module_name} in file #{file_path}")
Owl.IO.puts([
"Generated module ",
IO.ANSI.green(),
"#{module_name}",
IO.ANSI.reset(),
" in file ",
IO.ANSI.yellow(),
file_path,
IO.ANSI.reset()
])
end
end

Expand Down
43 changes: 43 additions & 0 deletions lib/mix/tasks/kubegen.search.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Mix.Tasks.Kubegen.Search do
@moduledoc ~S"""
Search Group-Version-Kind (GVK) for Core Resources.
Kubegen requires you to pass GVK as keys in `config.exs`. This mix tasks
lets you search for GVK for a specific resource kind
"""
@shortdoc "Search Group-Version-Kind for Core Resources."

use Mix.Task

@impl Mix.Task
def run([input]) do
discovery = elem(Code.eval_file("build/discovery.ex"), 0)
gvks = Map.keys(discovery)

if input in gvks do
IO.puts(~s'"#{input} is a valid GVK."')
exit({:shutdown, 0})
end

list =
gvks
|> Enum.map(fn gvk -> {gvk, String.split(gvk, "/") |> Enum.reverse() |> List.first()} end)
|> Enum.map(fn {gvk, kind} -> {gvk, kind, String.jaro_distance(input, kind)} end)
|> Enum.filter(fn {_, _, score} -> score > 0.7 end)
|> Enum.sort_by(&elem(&1, 2), :desc)
|> Enum.map(&[IO.ANSI.green(), elem(&1, 0), IO.ANSI.reset(), "\n"])

case list do
[] ->
Owl.IO.puts([
IO.ANSI.red(),
"No resource matches your search term."
])

list ->
Owl.IO.puts([
"Did you mean one of these?\n\n",
list
])
end
end
end

0 comments on commit ea7378f

Please sign in to comment.