coryodaniel / bonny

The Elixir based Kubernetes Development Framework
MIT License
379 stars 27 forks source link

Reconciled deletes don't seem to happen easily #193

Closed edbonddevo closed 1 year ago

edbonddevo commented 1 year ago

Environment

Current behavior

With the finalizer changes, it looks like the best way to protect for delete if the system isn't up/crashed would be:

  step(Bonny.Pluggable.Finalizer,
    id:  @finalizer_id,
    impl: &__MODULE__.cleanup/1,
    add_to_resource: true
  )

  def cleanup(axn) do
    Logger.debug("TODO: add in delete / check delete logic")
    {:ok, axn}
  end

  # Reconcile events that we might have missed
  def handle_event(
        %Bonny.Axn{
          action: :reconcile,
          conn: _conn,
          resource: %{"metadata" => %{"deletionTimestamp" => _}}
        } = axn,
        _opts
      ) do
    Logger.debug("Reconcile event with a delete timestamp")

    new_finalizers_list =
      List.delete(axn.resource["metadata"]["finalizers"], @finalizer_id)

    patch_finalizers(axn, new_finalizers_list)
    Pluggable.Token.halt(axn)
  end

  defp patch_finalizers(%Bonny.Axn{resource: resource, conn: conn}, finalizers) do
    patch =
      ~y"""
      apiVersion: #{resource["apiVersion"]}
      kind: #{resource["kind"]}
      metadata:
        name: #{resource["metadata"]["name"]}
        namespace: #{resource["metadata"]["namespace"]}
      """
      |> put_in(~w(metadata finalizers), finalizers)

    patch
    |> K8s.Client.patch()
    |> K8s.Client.put_conn(conn)
    |> K8s.Client.run()
  end

I just want to make sure I am not missing something: the test case is something like:

terminal1# iex -S mix
terminal2# kubectl apply -f test.yaml
terminal1# kill the process
terminal2# kubectl delete -f test.yaml
terminal1# iex -S mix

#doesn't get deleted

Right now it's only doing it for :modify

https://github.com/coryodaniel/bonny/pull/189/files#diff-1ea35d2ed46d0b9f8eab3dfbb94494146ed4264c297b9d47e4a1d4d02630a084R94

Expected behavior

Is this the pattern we want for reconciled deletes?

mruoss commented 1 year ago

Yeah I fixed that and released in 1.1.1 yesterday. Can you check please?

edbonddevo commented 1 year ago

haha, yes it works.

When I pulled the latest for the Finalizers work the 1.1.1 wasn't out.

Sorry for the noise, I just assumed it hadn't been updated in 1 day lol

mruoss commented 1 year ago

No worries