Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Created March 28, 2023 18:56
Show Gist options
  • Save sorentwo/f5ba9048e1e91456d37a8c7d4b8e4d58 to your computer and use it in GitHub Desktop.
Save sorentwo/f5ba9048e1e91456d37a8c7d4b8e4d58 to your computer and use it in GitHub Desktop.
Kubernetes for Oban Auto Scaling
defmodule Oban.Pro.Clouds.Kubernetes do
@moduledoc false
@behaviour Oban.Pro.Cloud
@enforce_keys [:api_server, :bearer_token, :deployment, :namespace]
defstruct @enforce_keys
@impl Oban.Pro.Cloud
def init(opts) do
struct!(__MODULE__, opts)
end
@impl Oban.Pro.Cloud
def scale(quantity, %__MODULE__{} = conf) do
%{
api_server: api_server,
namespace: namespace,
deployment: deployment,
bearer_token: bearer_token
} = conf
url = "#{api_server}/apis/apps/v1/namespaces/#{namespace}/deployments/#{deployment}/scale"
headers = [
{"Content-Type", "application/strategic-merge-patch+json"},
{"Accept", "application/json"},
{"Authorization", "Bearer #{bearer_token}"}
]
Req.patch!(url, json: %{"spec" => %{"replicas" => quantity}}, headers: headers)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment