Skip to content

Commit

Permalink
fix: wrong error matching
Browse files Browse the repository at this point in the history
Signed-off-by: shiipou <[email protected]>
  • Loading branch information
shiipou committed Feb 6, 2024
1 parent 5f8d0ac commit 6871e98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions apps/lenra/lib/lenra/kubernetes/api_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Lenra.Kubernetes.ApiServices do
alias Lenra.Apps.Deployment
alias Lenra.Kubernetes.StatusDynSup
alias Lenra.Repo
alias Ecto
require Logger

@doc """
Expand Down Expand Up @@ -180,7 +181,7 @@ defmodule Lenra.Kubernetes.ApiServices do

defp response({:ok, %Finch.Response{status: status_code, body: body}}, :secret)
when status_code in [404] do
{:secret_not_exist, Jason.decode!(body)}
{:error, :secret_not_exist, Jason.decode!(body)}
end

defp response({:ok, %Finch.Response{status: status_code, body: body}}, :build)
Expand Down Expand Up @@ -222,8 +223,8 @@ defmodule Lenra.Kubernetes.ApiServices do
case secret_response do
{:ok, body} ->
%{"data" => secret_data} = body
Enum.into(Enum.map(secret_data, fn ({key, value}) -> {key, Base.decode64(value)} end), %{})
_ -> {:secret_not_found}
{:ok, Enum.into(Enum.map(secret_data, fn ({key, value}) -> {key, Base.decode64(value)} end), %{})}
{:error, error} -> { :error, error |> IO.inspect(label: "Fetch error") }
end
end

Expand Down Expand Up @@ -321,8 +322,8 @@ defmodule Lenra.Kubernetes.ApiServices do
kubernetes_apps_namespace = Application.fetch_env!(:lenra, :kubernetes_apps_namespace)
case get_k8s_secret(secret_name, kubernetes_apps_namespace) do
{:ok, secrets} -> {:ok, Enum.map(secrets, fn ({key, _value}) -> key end)}
{:secret_not_found} -> {:error, :secret_not_found}
_ -> {:error, :unexpected_response}
{:error, :secret_not_exist} -> {:error, :secret_not_found}
{:error, error} -> {:error, error}
end
end
def create_environment_secrets(service_name, env_id, secrets) do
Expand All @@ -331,7 +332,7 @@ defmodule Lenra.Kubernetes.ApiServices do
case create_k8s_secret(secret_name, kubernetes_apps_namespace, secrets) do
{:ok, secrets} ->
env = Apps.fetch_env(env_id)
|> Ecto.preload(deployment: [:build])
|> Repo.preload(deployment: [:build])
build_number = env.deployment.build.build_number
Lenra.OpenfaasServices.update_secrets(service_name, build_number, [])
{:ok, Enum.map(secrets, fn ({key, value}) -> key end)}
Expand Down Expand Up @@ -361,9 +362,10 @@ defmodule Lenra.Kubernetes.ApiServices do
case length(Map.keys(current_secrets)) do
len when len <= 1 ->
case Apps.fetch_env(env_id)
|> Ecto.preload(deployment: [:build]) do
|> Repo.preload(deployment: [:build]) do
%{ deployment: %{ build: build_number }} when not is_nil(build_number) ->
Lenra.OpenfaasServices.update_secrets(service_name, build_number, [secret_name])
{:ok, []} # TODO: Return all other secrets
_ -> {:error, :build_not_exist}
end
case delete_k8s_secret(secret_name, kubernetes_apps_namespace) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule LenraWeb.EnvsController do
{:ok, secrets} -> conn |> reply(secrets)
{:error, :secret_not_found} -> conn |> reply([])
{:error, :kubernetes_error} -> BusinessError.kubernetes_unexpected_response_tuple()
{:error, :unexpected_response} -> BusinessError.api_return_unexpected_response_tuple()
{:error, error} -> BusinessError.api_return_unexpected_response_tuple(error)
end
end
end
Expand All @@ -83,7 +83,7 @@ defmodule LenraWeb.EnvsController do
case ApiServices.get_environment_secrets(app.service_name, environment.id) do
{:ok, secrets} ->
case Enum.any?(secrets, fn (s) -> s == key end) do
false -> case ApiServices.update_environment_secrets(app.service_name, environment.id, Map.merge(secrets, %{key => value})) do
false -> case ApiServices.update_environment_secrets(app.service_name, environment.id, %{key => value}) do
{:ok, secrets} -> conn |> reply(secrets)
{:error, :secret_not_found} -> BusinessError.env_secret_not_found_tuple() # Should never happen
{:error, :kubernetes_error} -> BusinessError.kubernetes_unexpected_response_tuple()
Expand Down

0 comments on commit 6871e98

Please sign in to comment.