Skip to content

Commit

Permalink
Clean up Kube.Client (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Dec 2, 2022
1 parent f3acd0d commit cf565ae
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 200 deletions.
10 changes: 5 additions & 5 deletions lib/console/deployer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule Console.Deployer do

def ping(), do: GenServer.call(leader(), :ping)

def update(repo, content, tool), do: GenServer.call(leader(), {:update, repo, content, tool})
def update(repo, content, tool, msg \\ nil), do: GenServer.call(leader(), {:update, repo, content, tool, msg})

def exec(fun), do: GenServer.call(leader(), {:exec, fun})

Expand All @@ -70,8 +70,8 @@ defmodule Console.Deployer do
{:reply, :ok, state}
end

def handle_call({:update, repo, content, tool}, _, %State{storage: storage} = state) do
{:reply, update(storage, repo, content, tool), state}
def handle_call({:update, repo, content, tool, msg}, _, %State{storage: storage} = state) do
{:reply, update(storage, repo, content, tool, msg), state}
end

def handle_call({:exec, fun}, _, state) when is_function(fun) do
Expand Down Expand Up @@ -187,12 +187,12 @@ defmodule Console.Deployer do
], storage)
end

defp update(storage, repo, content, tool) do
defp update(storage, repo, content, tool, msg) do
Command.set_build(nil)
bot = Users.get_bot!("console")
with {:ok, _} <- storage.init(),
{:ok, res} <- Console.Services.Plural.update_configuration(repo, content, tool),
{:ok, _} <- storage.revise("updated configuration for #{tool} #{repo}"),
{:ok, _} <- storage.revise(msg || "updated configuration for #{tool} #{repo}"),
{:ok, _} <- storage.push(),
{:ok, _} <- Builds.create(%{
type: :deploy,
Expand Down
11 changes: 0 additions & 11 deletions lib/console/graphql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,6 @@ defmodule Console.GraphQl do
resolve safe_resolver(&Build.approve_build/2)
end

field :update_configuration, :configuration do
middleware Authenticated
middleware RequiresGit
arg :repository, non_null(:string)
arg :content, non_null(:string)
arg :tool, :tool

middleware Rbac, perm: :configure, arg: :repository
resolve safe_resolver(&Plural.update_configuration/2)
end

import_fields :user_mutations
import_fields :kubernetes_mutations
import_fields :plural_mutations
Expand Down
16 changes: 14 additions & 2 deletions lib/console/graphql/plural.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ defmodule Console.GraphQl.Plural do
arg :context, non_null(:map)
arg :oidc, :boolean

resolve &Plural.install_recipe/2
safe_resolve &Plural.install_recipe/2
end

field :update_smtp, :smtp do
Expand All @@ -168,7 +168,19 @@ defmodule Console.GraphQl.Plural do
middleware RequiresGit
arg :smtp, non_null(:smtp_input)

resolve &Plural.update_smtp/2
safe_resolve &Plural.update_smtp/2
end

field :update_configuration, :configuration do
middleware Authenticated
middleware RequiresGit
arg :repository, non_null(:string)
arg :content, non_null(:string)
arg :tool, :tool
arg :message, :string
middleware Rbac, perm: :configure, arg: :repository

safe_resolve &Plural.update_configuration/2
end
end
end
2 changes: 1 addition & 1 deletion lib/console/graphql/resolvers/plural.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule Console.GraphQl.Resolvers.Plural do

def update_configuration(%{repository: repo, content: content} = args, _) do
tool = args[:tool] || :helm
with {:ok, conf} <- Console.Deployer.update(repo, content, tool) do
with {:ok, conf} <- Console.Deployer.update(repo, content, tool, args[:message]) do
{:ok, %{configuration: %{tool => conf}}}
end
end
Expand Down
204 changes: 23 additions & 181 deletions lib/kube/client.ex
Original file line number Diff line number Diff line change
@@ -1,215 +1,57 @@
defmodule Kube.Client do
use Kube.Client.Base
alias Kube
alias Kazan.Models.Apimachinery.Meta.V1.{ObjectMeta}

def list_dashboards(ns) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/dashboards",
query_params: %{},
response_model: Kube.DashboardList
}
|> Kazan.run()
end

def list_log_filters(ns) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/logfilters",
query_params: %{},
response_model: Kube.LogFilterList
}
|> Kazan.run()
end
list_request :list_dashboards, Kube.DashboardList, "platform.plural.sh", "v1alpha1", "dashboards"
list_request :list_log_filters, Kube.LogFilterList, "platform.plural.sh", "v1alpha1", "logfilters"
list_request :list_configuration_overlays, Kube.ConfigurationOverlayList, "platform.plural.sh", "v1alpha1", "configurationoverlays"
list_request :list_runbooks, Kube.RunbookList, "platform.plural.sh", "v1alpha1", "runbooks"
list_request :list_vertical_pod_autoscalers, Kube.VerticalPodAutoscalerList, "autoscaling.k8s.io", "v1", "verticalpodautoscalers"

def list_configuration_overlays(ns) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/configurationoverlays",
query_params: %{},
response_model: Kube.ConfigurationOverlayList
}
|> Kazan.run()
end

def get_dashboard(ns, name) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/dashboards/#{name}",
query_params: %{},
response_model: Kube.Dashboard
}
|> Kazan.run()
end
get_request :get_dashboard, Kube.Dashboard, "platform.plural.sh", "v1alpha1", "dashboards"
get_request :get_slashcommand, Kube.SlashCommand, "platform.plural.sh", "v1alpha1", "slashcommands"
get_request :get_application, Kube.Application, "app.k8s.io", "v1", "applications"
get_request :get_certificate, Kube.Certificate, "cert-manager.io", "v1", "certificates"
get_request :get_runbook, Kube.Runbook, "platform.plural.sh", "v1alpha1", "runbooks"
get_request :get_statefulset_resize, Kube.StatefulSetResize, "platform.plural.sh", "v1alpha1", "statefulsetresizes"
get_request :get_vertical_pod_autoscaler, Kube.VerticalPodAutoscaler, "autoscaling.k8s.io", "v1", "verticalpodautoscalers"

def get_slashcommand(ns, name) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/slashcommands/#{name}",
query_params: %{},
response_model: Kube.SlashCommand
}
|> Kazan.run()
end
def get_application(name), do: get_application(name, name)

def list_slashcommands() do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/slashcommands",
query_params: %{},
response_model: Kube.SlashCommandList
}
|> Kazan.run()
make_request("/apis/platform.plural.sh/v1alpha1/slashcommands", "get", Kube.SlashCommandList)
end

def list_licenses() do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/licenses",
query_params: %{},
response_model: Kube.LicenseList
}
|> Kazan.run()
end

def get_application(name) do
%Kazan.Request{
method: "get",
path: "/apis/app.k8s.io/v1beta1/namespaces/#{Console.namespace(name)}/applications/#{name}",
query_params: %{},
response_model: Kube.Application
}
|> Kazan.run()
end

def get_certificate(ns, name) do
%Kazan.Request{
method: "get",
path: "/apis/cert-manager.io/v1/namespaces/#{ns}/certificates/#{name}",
query_params: %{},
response_model: Kube.Certificate
}
|> Kazan.run()
make_request("/apis/platform.plural.sh/v1alpha1/licenses", "get", Kube.LicenseList)
end

def list_applications() do
%Kazan.Request{
method: "get",
path: "/apis/app.k8s.io/v1beta1/applications",
query_params: %{},
response_model: Kube.ApplicationList
}
|> Kazan.run()
end

def get_application(name) do
ns = Console.namespace(name)
%Kazan.Request{
method: "get",
path: "/apis/app.k8s.io/v1beta1/namespaces/#{ns}/applications/#{name}",
query_params: %{},
response_model: Kube.Application
}
|> Kazan.run()
make_request("/apis/app.k8s.io/v1/applications", "get", Kube.ApplicationList)
end

def list_metrics() do
%Kazan.Request{
method: "get",
path: "/apis/metrics.k8s.io/v1beta1/nodes",
query_params: %{},
response_model: Kube.NodeMetricList
}
|> Kazan.run()
make_request("/apis/metrics.k8s.io/v1beta1/nodes", "get", Kube.NodeMetricList)
end

def get_metrics(node) do
%Kazan.Request{
method: "get",
path: "/apis/metrics.k8s.io/v1beta1/nodes/#{node}",
query_params: %{},
response_model: Kube.NodeMetric
}
|> Kazan.run()
end

def list_runbooks(ns, params \\ %{}) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/runbooks",
query_params: params,
response_model: Kube.RunbookList
}
|> Kazan.run()
end

def get_runbook(ns, name) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{Console.namespace(ns)}/runbooks/#{name}",
query_params: %{},
response_model: Kube.Runbook
}
|> Kazan.run()
make_request("/apis/metrics.k8s.io/v1beta1/nodes/#{node}", "get", Kube.NodeMetric)
end

def create_statefulset_resize(namespace, name, %Kube.StatefulSetResize{} = resize) do
resize = %{resize | metadata: %ObjectMeta{name: name, namespace: namespace}}
{:ok, encoded} = Kube.StatefulSetResize.encode(resize)

%Kazan.Request{
method: "post",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{namespace}/statefulsetresizes/#{name}",
query_params: %{},
body: Jason.encode!(encoded),
content_type: "application/json",
response_model: Kube.StatefulSetResize
}
|> Kazan.run()
end

def get_statefulset_resize(namespace, name) do
%Kazan.Request{
method: "get",
path: "/apis/platform.plural.sh/v1alpha1/namespaces/#{namespace}/statefulsetresizes/#{name}",
query_params: %{},
response_model: Kube.StatefulSetResize
}
|> Kazan.run()
end

def get_vertical_pod_autoscaler(namespace, name) do
%Kazan.Request{
method: "get",
path: "/apis/autoscaling.k8s.io/v1/namespaces/#{Console.namespace(namespace)}/verticalpodautoscalers/#{name}",
query_params: %{},
response_model: Kube.VerticalPodAutoscaler
}
|> Kazan.run()
end

def list_vertical_pod_autoscalers(namespace) do
%Kazan.Request{
method: "get",
path: "/apis/autoscaling.k8s.io/v1/namespaces/#{Console.namespace(namespace)}/verticalpodautoscalers",
query_params: %{},
response_model: Kube.VerticalPodAutoscaler
}
|> Kazan.run()
path_builder("platform.plural.sh", "v1alpha1", "statefulsetresizes", namespace, name)
|> make_request("post", Kube.StatefulSetResize, Jason.encode!(encoded))
end

def create_vertical_pod_autoscaler(namespace, name, %Kube.VerticalPodAutoscaler{} = vpa) do
resize = %{vpa | metadata: %ObjectMeta{name: name, namespace: namespace}}
{:ok, encoded} = Kube.VerticalPodAutoscaler.encode(resize)

%Kazan.Request{
method: "post",
path: "/apis/autoscaling.k8s.io/v1/namespaces/#{namespace}/verticalpodautoscalers/#{name}",
query_params: %{},
body: Jason.encode!(encoded),
content_type: "application/json",
response_model: Kube.VerticalPodAutoscaler
}
|> Kazan.run()
path_builder("autoscaling.k8s.io", "v1", "verticalpodautoscalers", namespace, name)
|> make_request("post", Kube.VerticalPodAutoscaler, Jason.encode!(encoded))
end
end
50 changes: 50 additions & 0 deletions lib/kube/client/base.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule Kube.Client.Base do
defmacro __using__(_) do
quote do
import Kube.Client.Base
end
end

def make_request(path, verb, model, body \\ "", params \\ %{}) do
%Kazan.Request{
method: verb,
path: path,
query_params: params,
body: body,
content_type: "application/json",
response_model: model
}
|> Kazan.run()
end

def path_builder(g, v, k, namespace), do: "/apis/#{g}/#{v}/namespaces/#{Console.namespace(namespace)}/#{k}"
def path_builder(g, v, k, namespace, name), do: "#{path_builder(g, v, k, namespace)}/#{name}"

defmacro get_request(name, model, g, v, k) do
quote do
def unquote(name)(namespace, name, params \\ %{}) do
%Kazan.Request{
method: "get",
path: path_builder(unquote(g), unquote(v), unquote(k), namespace, name),
query_params: params,
response_model: unquote(model)
}
|> Kazan.run()
end
end
end

defmacro list_request(name, model, g, v, k) do
quote do
def unquote(name)(namespace, params \\ %{}) do
%Kazan.Request{
method: "get",
path: path_builder(unquote(g), unquote(v), unquote(k), namespace),
query_params: params,
response_model: unquote(model)
}
|> Kazan.run()
end
end
end
end

0 comments on commit cf565ae

Please sign in to comment.