added logs for projects and add ons

This commit is contained in:
Chris Zhu
2024-10-12 23:23:31 -07:00
parent 9bc1a8487d
commit 33d3c7df15
12 changed files with 104 additions and 21 deletions

View File

@@ -0,0 +1,9 @@
class AddOns::BaseController < ApplicationController
before_action :set_add_on
private
def set_add_on
@add_on = AddOn.find(params[:add_on_id])
end
end

View File

@@ -0,0 +1,17 @@
class AddOns::LogsController < AddOns::BaseController
def index
@pods = get_pods_for_add_on(@add_on)
end
def show
client = K8::Client.new(@add_on.cluster.kubeconfig)
@logs = client.get_pod_log(params[:id], @add_on.name)
end
private
def get_pods_for_add_on(add_on)
client = K8::Client.new(add_on.cluster.kubeconfig).client
client.get_pods(namespace: add_on.name)
end
end

View File

@@ -1,6 +1,6 @@
class AddOnsController < ApplicationController
include StorageHelper
before_action :set_add_on, only: [ :show, :edit, :update, :destroy, :logs ]
before_action :set_add_on, only: [:show, :edit, :update, :destroy]
# GET /add_ons
def index
@@ -22,9 +22,6 @@ class AddOnsController < ApplicationController
# authorize @add_on
end
def logs
end
# GET /add_ons/1/edit
def edit
end

View File

@@ -1,4 +1,5 @@
class StaticController < ApplicationController
layout "homepage"
def index
end
end

View File

@@ -0,0 +1,11 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["container"]
connect() {
// Scroll to the bottom of the container
this.containerTarget.scrollTop = this.containerTarget.scrollHeight;
console.log("Logs controller connected")
}
}

View File

@@ -4,7 +4,7 @@
Info
</div>
<% end %>
<%= link_to logs_add_on_url(add_on), class: "tab hover:bg-base-content/15 #{'tab-active' if current_page?(logs_add_on_url(add_on))}" do %>
<%= link_to add_on_logs_url(add_on), class: "tab hover:bg-base-content/15 #{'tab-active' if current_page?(add_on_logs_url(add_on))}" do %>
<div class="flex items-center gap-2">
Logs
</div>

View File

@@ -1,3 +0,0 @@
<%= add_on_layout(@add_on) do %>
<%= render "log_outputs/logs", loggable: @add_on %>
<% end %>

View File

@@ -0,0 +1,47 @@
<%= add_on_layout(@add_on) do %>
<%= turbo_frame_tag "pod_logs" do %>
<% if @pods.empty? %>
<div>
<p class="text-gray-500">Nothing running in this namespace</p>
</div>
<% else %>
<table class="table mt-2 rounded-box" data-component="table">
<thead>
<tr>
<th>
<span class="text-sm font-medium text-base-content/80">Pod Name</span>
</th>
<th>
<span class="text-sm font-medium text-base-content/80">
Status
</span>
</th>
</tr>
</thead>
<tbody>
<% @pods.each do |pod| %>
<tr class="cursor-pointer hover:bg-base-200/40">
<td>
<div class="flex items-center space-x-3 truncate">
<div class="font-medium">
<%= pod.metadata.name %>
</div>
</div>
</td>
<td>
<div class="font-medium">
<%= pod.status.phase %>
</div>
</td>
<td>
<div class="font-medium">
<%= link_to "Show Logs", add_on_log_path(@add_on, pod.metadata.name) %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %>
<% end %>

View File

@@ -0,0 +1,3 @@
<%= add_on_layout(@add_on) do %>
<%= render "log_outputs/pod_logs", logs: @logs, back_path: add_on_logs_path(@add_on) %>
<% end %>

View File

@@ -0,0 +1,11 @@
<%= turbo_frame_tag "pod_logs" do %>
<%= link_to back_path, class: "btn btn-ghost btn-sm" do %>
<iconify-icon icon="lucide:chevron-left"></iconify-icon> Back
<% end %>
<h4 class="text-md font-medium mb-2"><%= params[:id] %></h4>
<div class="bg-gray-900 text-gray-100 rounded-lg shadow-lg" data-controller="logs">
<div class="overflow-auto h-96 bg-gray-800 p-2 rounded" data-logs-target="container">
<pre class="text-sm font-mono whitespace-pre-wrap"><%= ansi_to_tailwind(logs).html_safe || "No logs yet..." %></pre>
</div>
</div>
<% end %>

View File

@@ -1,11 +1,3 @@
<%= turbo_frame_tag "pod_logs" do %>
<div class="flex">
<%= link_to "Back", project_logs_path(@project), class: "btn btn-sm btn-outline" %>
<h4 class="text-md font-medium mb-2"><%= params[:id] %></h4>
</div>
<div class="bg-gray-900 text-gray-100 rounded-lg shadow-lg" data-controller="logs">
<div class="overflow-auto h-96 bg-gray-800 p-2 rounded" data-logs-target="container">
<pre class="text-sm font-mono whitespace-pre-wrap"><%= ansi_to_tailwind(@logs).html_safe || "No logs yet..." %></pre>
</div>
</div>
<%= project_layout(@project) do %>
<%= render "log_outputs/pod_logs", logs: @logs, back_path: project_logs_path(@project) %>
<% end %>

View File

@@ -15,9 +15,7 @@ Rails.application.routes.draw do
# get "/dashboard", to: "dashboard#show", as: :user_root
end
resources :add_ons do
member do
get :logs, to: "add_ons#logs"
end
resources :logs, only: %i[index show], module: :add_ons
end
resources :projects do