Skip to content

Instantly share code, notes, and snippets.

@obmarg
Last active October 19, 2018 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obmarg/c31d91b61a0ef8aff4f0428f7412d3a4 to your computer and use it in GitHub Desktop.
Save obmarg/c31d91b61a0ef8aff4f0428f7412d3a4 to your computer and use it in GitHub Desktop.
alias Kazan.Apis.Core.V1, as: Core
alias Kazan.Models.Apimachinery.Meta.V1, as: Meta
alias Kazan.Apis.Batch.V1, as: Batch
def deploy!(request, project, namespace, opts \\ []) do
server = server(opts)
Logger.debug("Creating job for request #{request.id}")
job =
request
|> build_job(project)
|> Batch.create_namespaced_job!(namespace)
|> Kazan.run!(server: server)
Logger.debug("Job created for request #{request.id}. Watching for updates")
namespace
|> Batch.watch_namespaced_job!(job.metadata.name)
|> Kazan.Watcher.start_link(
resource_version: job.metadata.resource_version,
send_to: self(),
server: server
)
|> await_done(job)
end
defp await_done({:ok, watcher}, job) do
receive do
%Meta.WatchEvent{object: %{status: status}, type: "MODIFIED"} ->
cond do
status.failed == 1 ->
Logger.warn("Job Failed")
Logger.warn(inspect(job))
{:error, :job_failed}
status.succeeded == 1 ->
Logger.info("Job Succeeded")
:ok
status.active == 1 ->
Logger.debug("Job in progress")
await_done({:ok, watcher}, job)
:otherwise ->
Logger.debug(
"K8s deploy received unknown status: #{inspect(status)}"
)
await_done({:ok, watcher}, job)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment