sunguroku 5 лет назад
Родитель
Сommit
98fb8e2e2d

+ 18 - 9
dashboard/src/main/home/cluster-dashboard/expanded-chart/log/LogSection.tsx

@@ -5,26 +5,30 @@ import { ResourceType, ChartType } from '../../../../../shared/types';
 import Logs from './Logs';
 import { Context } from '../../../../../shared/Context';
 
+interface Pod {
+  namespace?: string;
+  name?: string;
+}
+
 type PropsType = {
   selectors: string[],
 };
 
 type StateType = {
   logs: string[]
-  pods: string[],
-  selectedPod: string,
+  pods: Pod[],
+  selectedPod: Pod,
 };
 
 export default class LogSection extends Component<PropsType, StateType> {
   state = {
     logs: [] as string[],
-    pods: [] as string[],
-    selectedPod: null as string,
-    matchingPods: [] as any[]
+    pods: [] as Pod[],
+    selectedPod: {} as Pod,
   }
 
   renderLogs = () => {
-    return <Logs key={this.state.selectedPod} selectedPod={this.state.selectedPod} />
+    return <Logs key={this.state.selectedPod.name} selectedPod={this.state.selectedPod} />
   }
 
   renderPodTabs = () => {
@@ -37,7 +41,7 @@ export default class LogSection extends Component<PropsType, StateType> {
           this.setState({selectedPod: pod})
           }
         }>
-          {pod}
+          {pod.name}
         </Tab>
       )
     })
@@ -54,8 +58,13 @@ export default class LogSection extends Component<PropsType, StateType> {
     }, {
       id: currentProject.id
     }, (err: any, res: any) => {
-      console.log("SELECTORS", selectors)
-      this.setState({pods: res.data, selectedPod: res.data[0]})
+      let pods = res?.data?.map((pod: any) => {
+        return {
+          namespace: pod.metadata.namespace, 
+          name: pod.metadata.name
+        }
+      })
+      this.setState({ pods , selectedPod: pods[0]})
     })
   }
 

+ 9 - 4
dashboard/src/main/home/cluster-dashboard/expanded-chart/log/Logs.tsx

@@ -2,8 +2,13 @@ import React, { Component } from 'react';
 import styled from 'styled-components';
 import { Context } from '../../../../../shared/Context';
 
+interface Pod {
+  namespace?: string;
+  name?: string;
+}
+
 type PropsType = {
-  selectedPod: string,
+  selectedPod: Pod,
 };
 
 type StateType = {
@@ -32,10 +37,11 @@ export default class Logs extends Component<PropsType, StateType> {
 
   componentDidMount() {
     let { currentCluster, currentProject } = this.context;
+    let { selectedPod } = this.props;
     if (!this.props.selectedPod) return
 
-    let ws = new WebSocket(`ws://localhost:8080/api/projects/${currentProject.id}/k8s/default/pod/${this.props.selectedPod}/logs?cluster_id=${currentCluster.id}&service_account_id=${currentCluster.service_account_id}`)
-    // let ws = new WebSocket(`ws://localhost:8080/api/projects/${currentProject.id}/k8s/deployment/status?cluster_id=${currentCluster.id}&service_account_id=${currentCluster.service_account_id}`)
+    let ws = new WebSocket(`ws://localhost:8080/api/projects/${currentProject.id}/k8s/${selectedPod.namespace}/pod/${selectedPod.name}/logs?cluster_id=${currentCluster.id}&service_account_id=${currentCluster.service_account_id}`)
+
     this.setState({ ws }, () => {
       if (!this.state.ws) return;
   
@@ -57,7 +63,6 @@ export default class Logs extends Component<PropsType, StateType> {
 
   componentWillUnmount() {
     if (this.state.ws) {
-      console.log('unmounting')
       this.state.ws.close()
     }
   }

+ 2 - 3
internal/kubernetes/agent.go

@@ -74,11 +74,11 @@ func (a *Agent) GetPodLogs(namespace string, name string, conn *websocket.Conn)
 		for {
 			select {
 			case <-errorchan:
+				defer close(errorchan)
 				return
 			default:
 			}
 			bytes, err := r.ReadBytes('\n')
-			fmt.Println("BYTES", bytes)
 			if writeErr := conn.WriteMessage(websocket.TextMessage, bytes); writeErr != nil {
 				errorchan <- writeErr
 				return
@@ -104,7 +104,6 @@ func (a *Agent) GetPodLogs(namespace string, name string, conn *websocket.Conn)
 
 // StreamDeploymentStatus streams deployment status.
 func (a *Agent) StreamDeploymentStatus(conn *websocket.Conn) error {
-	fmt.Println("===========================streaming dep status============================")
 
 	factory := informers.NewSharedInformerFactory(a.Clientset, 0)
 	informer := factory.Apps().V1().Deployments().Informer()
@@ -131,6 +130,6 @@ func (a *Agent) StreamDeploymentStatus(conn *websocket.Conn) error {
 		},
 	})
 
-	informer.Run(stopper)
+	go informer.Run(stopper)
 	return nil
 }

+ 3 - 2
server/api/k8s_handler.go

@@ -9,6 +9,7 @@ import (
 	"github.com/go-chi/chi"
 	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/models"
+	v1 "k8s.io/api/core/v1"
 
 	"github.com/gorilla/websocket"
 	"github.com/porter-dev/porter/internal/forms"
@@ -189,7 +190,7 @@ func (app *App) HandleListPods(w http.ResponseWriter, r *http.Request) {
 		agent, err = kubernetes.GetAgentOutOfClusterConfig(form.OutOfClusterConfig)
 	}
 
-	pods := []string{}
+	pods := []v1.Pod{}
 	for _, selector := range vals["selectors"] {
 		podsList, err := agent.GetPodsByLabel(selector)
 
@@ -199,7 +200,7 @@ func (app *App) HandleListPods(w http.ResponseWriter, r *http.Request) {
 		}
 
 		for _, pod := range podsList.Items {
-			pods = append(pods, pod.ObjectMeta.Name)
+			pods = append(pods, pod)
 		}
 	}