Skip to content

Commit

Permalink
next approach
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Apr 1, 2023
1 parent 5b08ee3 commit 696b3db
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 81 deletions.
12 changes: 8 additions & 4 deletions lib/bonny/admission_control/admission_review.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ defmodule Bonny.AdmissionControl.AdmissionReview do

require Logger

@derive Pluggable.Token

@type webhook_type :: :mutating | :validating
@type t :: %__MODULE__{
request: map(),
response: map(),
webhook_type: :mutating | :validating
webhook_type: webhook_type(),
halted: boolean(),
assigns: map()
}

@fields [:request, :response, :webhook_type]
@enforce_keys @fields
defstruct @fields
@enforce_keys [:request, :response, :webhook_type]
defstruct [:request, :response, :webhook_type, halted: false, assigns: %{}]

def new(%{"kind" => "AdmissionReview", "request" => request}, webhook_type) do
struct!(__MODULE__,
Expand Down
82 changes: 82 additions & 0 deletions lib/bonny/admission_control/plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
defmodule Bonny.AdmissionControl.Plug do
use Plug.Builder

alias Bonny.AdmissionControl.AdmissionReview

require Logger

@api_version "admission.k8s.io/v1"
@kind "AdmissionReview"

plug(Plug.Parsers,
parsers: [:urlencoded, :json],
json_decoder: Jason
)

@impl true
def init(opts) do
if opts[:webhook_type] not in [:mutating, :validating] do
raise(CompileError,
file: __ENV__.file,
line: __ENV__.line,
description:
"#{__MODULE__} requires you to define the :webhook_type option as :mutating or :validating when plugged."
)
end

webhook_handler =
case opts[:webhook_handler] do
{module, init_opts} ->
{module, module.init(init_opts)}

nil ->
raise(CompileError,
file: __ENV__.file,
line: __ENV__.line,
description: "#{__MODULE__} requires you to set the :webhook_handler option."
)

module ->
{module, []}
end

%{
webhook_type: opts[:webhook_type],
webhook_handler: webhook_handler
}
end

@doc false
@impl true
def call(conn, webhook_config) do
%{
webhook_type: webhook_type,
webhook_handler: {webhook_handler, opts}
} = webhook_config

conn
|> super([])
|> Map.get(:body_params)
|> AdmissionReview.new(webhook_type)
|> tap(fn review ->
Logger.debug("Processing Admission Review Request", library: :bonny, review: review)
end)
|> AdmissionReview.allow()
|> webhook_handler.call(opts)
|> encode_response()
|> send_response(conn)
end

defp send_response(response_body, conn) do
conn
|> put_resp_content_type("application/json")
|> send_resp(200, response_body)
end

@spec encode_response(AdmissionReview.t()) :: binary()
defp encode_response(admission_review) do
%{"apiVersion" => @api_version, "kind" => @kind}
|> Map.put("response", admission_review.response)
|> Jason.encode!()
end
end
77 changes: 0 additions & 77 deletions lib/bonny/admission_control/webhook_router.ex

This file was deleted.

0 comments on commit 696b3db

Please sign in to comment.