Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/console/deployments/kube_versions/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Console.Deployments.KubeVersions.Table do
defmodule Version do
@type t :: %__MODULE__{version: binary, extended: boolean}

@derive Jason.Encoder
defstruct [:version, :extended, :extended_from]

def new(attrs) do
Expand Down
21 changes: 21 additions & 0 deletions lib/console/deployments/pubsub/consumers/broadcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ defmodule Console.Deployments.PubSub.Broadcast do
end

defp do_broadcast(room, event, data) do
gql_broadcast(room, event, data)
Phoenix.Channel.Server.broadcast(Console.PubSub, room, event, data)
end

defp gql_broadcast("cluster:" <> cid, event, %{"id" => id} = data) do
case event_to_resource(event) do
:error -> :ok
resource ->
Absinthe.Subscription.publish(
ConsoleWeb.Endpoint,
%{resource: resource, resource_id: id, kick: data["kick"]},
[deploy_agent_notification: "deploy_agent_notifications:#{cid}"]
)
end
end
defp gql_broadcast(_, _, _), do: :ok

defp event_to_resource("gate" <> _), do: :gate
defp event_to_resource("agent" <> _), do: :agent_run
defp event_to_resource("stack" <> _), do: :stack_run
defp event_to_resource("service.manifests" <> _), do: :error
defp event_to_resource("service" <> _), do: :service
defp event_to_resource(_), do: :error
Comment thread
michaeljguarino marked this conversation as resolved.
end
3 changes: 3 additions & 0 deletions lib/console/external_graphql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ defmodule Console.ExternalGraphQl do
import_fields :public_sentinel_mutations
end

subscription do
import_fields :public_deployment_subscriptions
end

defp make_labels(nil), do: []
defp make_labels(map), do: Enum.map(map, fn {key, value} -> %{name: key, value: value} end)
Expand Down
23 changes: 23 additions & 0 deletions lib/console/graphql/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,24 @@
import_types Console.GraphQl.Deployments.Agent
import_types Console.GraphQl.Deployments.Integration

enum :agent_notification_resource do
value :service
value :stack_run
value :agent_run
value :gate
end

input_object :rbac_attributes do
field :read_bindings, list_of(:policy_binding_attributes)
field :write_bindings, list_of(:policy_binding_attributes)
end

object :deploy_agent_notification do
field :resource, non_null(:agent_notification_resource)
field :resource_id, non_null(:id)
field :kick, :boolean
end

object :deployment_queries do
import_fields :git_queries
import_fields :cluster_queries
Expand Down Expand Up @@ -130,4 +143,14 @@
import_fields :pipeline_subscriptions
import_fields :service_subscriptions
end

object :public_deployment_subscriptions do
field :deploy_agent_notification, :deploy_agent_notification do
config fn
_, %{context: %{cluster: %Console.Schema.Cluster{id: cid}}} ->
{:ok, topic: "deploy_agent_notifications:#{cid}"}
_, _ -> {:error, "unauthorized"}
end
end
end
end
22 changes: 22 additions & 0 deletions lib/console_web/channels/graphqlws_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,25 @@ defmodule ConsoleWeb.GraphqlWSSocket do
def fetch_token(%{"token" => token}), do: {:ok, token}
def fetch_token(_), do: {:error, :notoken}
end

defmodule ConsoleWeb.ExternalGraphqlWSSocket do
use Absinthe.GraphqlWS.Socket,
schema: Console.ExternalGraphQl
alias Console.Schema.Cluster
alias Console.Deployments.Clusters

@impl Absinthe.GraphqlWS.Socket
def handle_init(params, socket) do
with {:ok, token} <- fetch_token(params),
%Cluster{} = cluster <- Clusters.get_by_deploy_token(token) do
{:ok, %{name: cluster.handle}, assign_context(socket, cluster: cluster)}
else
_ -> {:error, %{}, socket}
end
end

def fetch_token(%{"Authorization" => "Bearer " <> token}), do: {:ok, token}
def fetch_token(%{"token" => "Bearer " <> token}), do: {:ok, token}
def fetch_token(%{"token" => token}), do: {:ok, token}
def fetch_token(_), do: {:error, :notoken}
end
3 changes: 3 additions & 0 deletions lib/console_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ defmodule ConsoleWeb.Endpoint do
websocket: [check_origin: false],
longpoll: false

socket "/ext/socket/gql-ws", ConsoleWeb.ExternalGraphqlWSSocket,
websocket: [path: "", subprotocols: ["graphql-transport-ws"], check_origin: false]

# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phx.digest
Expand Down
2 changes: 1 addition & 1 deletion priv/prompts/workbench/infrastructure/cluster.md.eex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Name: <%= @cluster.name %>
Handle: <%= @cluster.handle %>
Kubernetes Version: <%= @cluster.current_version %>
Kubernetes Distribution: <%= @cluster.distro %>
Extended Support: <%= Console.Deployments.Clusters.extended_support(@cluster) %>
Extended Support: <%= Console.Deployments.Clusters.extended_support(@cluster) |> Jason.encode!() %>
Metadata: <%= Jason.encode!(@cluster.metadata || %{}) %>
Tags: <%= Enum.map(@cluster.tags, & %{name: &1.name, value: &1.value}) |> Jason.encode!() %>
Project: <%= @cluster.project.name %>
Expand Down
Loading