Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Karmada config page (fix date,monaco editor and other bug) #89

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 cmd/api/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/propagationpolicy"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/statefulset"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/unstructured"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/config"
)

// NewApiCommand creates a *cobra.Command object with default parameters
Expand Down
112 changes: 112 additions & 0 deletions cmd/api/app/routes/config/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package config

import (
"github.com/gin-gonic/gin"
"github.com/karmada-io/dashboard/cmd/api/app/router"
"github.com/karmada-io/dashboard/cmd/api/app/types/common"

)

func handleGetRunningapps(c *gin.Context) {
podLabels, err := GetRunningapps()
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"appLabels": podLabels})
}

func handleGetMetaData(c *gin.Context) {
appLabel := c.Param("appLabel")
podMetadataList, err := GetMetaData(appLabel)
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"pods": podMetadataList})
}

func handleCheckDeploymentStatus(c *gin.Context) {
podName := c.Param("podName")

status, err := GetDeploymentStatus(podName)
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"status": status})
}

func handleRestartDeployment(c *gin.Context) {
deploymentName := c.Param("podName")
status, err := RestartDeployment(deploymentName)
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"status": status})
}

func handleReinstallPod(c *gin.Context) {
podName := c.Param("podName")
status, err := DeletePod(podName)
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"status": status})
}

func handleGetPodLogs(c *gin.Context) {
podName := c.Param("podName")
logs, err := GetPodLogs(podName)
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"logs": logs})
}

func handlegetpodinfo(c *gin.Context) {
podName := c.Param("Name")

yamlData, err := GetPodYAML(podName)
if err != nil {
common.Fail(c, err)
return
}
c.String(200, yamlData)
}


func hadlleputpodinfo(c *gin.Context) {
podName := c.Param("Name")
var requestBody struct {
YamlContent string `json:"yamlContent"`
}
if err := c.ShouldBindJSON(&requestBody); err != nil {
common.Fail(c, err)
return
}

status, err := UpdatePodYAML(podName, requestBody.YamlContent)
if err != nil {
common.Fail(c, err)
return
}
c.JSON(200, gin.H{"code": 200,"message":status})

}

func init() {
r := router.V1()
r.GET("/config/running-apps", handleGetRunningapps)
r.GET("/config/apps/:appLabel", handleGetMetaData)
r.GET("/config/status/:podName", handleCheckDeploymentStatus)
r.GET("/config/restart/:podName", handleRestartDeployment)
r.DELETE("/config/reinstall/:podName", handleReinstallPod)
r.GET("/config/log/:podName", handleGetPodLogs)
r.GET("/config/podinfo/:Name", handlegetpodinfo)
r.PUT("/config/podinfo/:Name", hadlleputpodinfo)
}

188 changes: 188 additions & 0 deletions cmd/api/app/routes/config/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package config

import (

"context"
"fmt"
"log"
"bytes"
"io"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/karmada-io/dashboard/pkg/client"
v1 "github.com/karmada-io/dashboard/cmd/api/app/types/api/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
)

const (
namespace = "karmada-system"
)

func GetRunningapps() ([]string, error) {
kubeClient := client.InClusterClient()
pods, err := kubeClient.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("Error listing apps: %v", err)
return nil, fmt.Errorf("error listing apps")
}

labelSet := make(map[string]struct{}, len(pods.Items))
for _, pod := range pods.Items {
if appLabel, exists := pod.Labels["app"]; exists {
labelSet[appLabel] = struct{}{}
}
}

if len(labelSet) == 0 {
return nil, fmt.Errorf("no Apps found")
}

uniquePodLabels := make([]string, 0, len(labelSet))
for label := range labelSet {
uniquePodLabels = append(uniquePodLabels, label)
}

return uniquePodLabels, nil
}

func GetMetaData(appLabel string) ([]v1.PodMetadata, error) {
kubeClient := client.InClusterClient()
pods, err := kubeClient.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", appLabel),
})
if err != nil {
log.Printf("Error listing pods: %v", err)
return nil, fmt.Errorf("error listing pods")
}

var podMetadataList []v1.PodMetadata
for _, pod := range pods.Items {
podMetadata := v1.PodMetadata{
UID: string(pod.UID),
CreationTimestamp: pod.CreationTimestamp.String(),
GenerateName: pod.GenerateName,
Labels: pod.Labels,
Name: pod.Name,
}
podMetadataList = append(podMetadataList, podMetadata)
}

if len(podMetadataList) == 0 {
return nil, fmt.Errorf("no pods found with app label: %s", appLabel)
}

return podMetadataList, nil
}

func GetDeploymentStatus(deploymentName string) (string, error) {
kubeClient := client.InClusterClient()
deployment, err := kubeClient.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
log.Printf("Error getting deployment: %v", err)
return "", fmt.Errorf("error getting deployment")
}

if deployment.Status.Replicas == deployment.Status.AvailableReplicas {

return fmt.Sprintf("Deployment '%s' successfully rolled out", deploymentName), nil
}
return fmt.Sprintf("Deployment '%s' is not fully rolled out", deploymentName), nil
}

func RestartDeployment(deploymentName string) (string, error) {
kubeClient := client.InClusterClient()

deployment, err := kubeClient.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
log.Printf("Error getting deployment: %v", err)
return "", fmt.Errorf("error getting deployment")
}
_, err = kubeClient.AppsV1().Deployments(namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
if err != nil {
log.Printf("Error restarting deployment: %v", err)
return "", fmt.Errorf("error restarting deployment")
}

return fmt.Sprintf("Deployment '%s' restarted successfully", deploymentName), nil
}

func DeletePod(podName string) (string, error) {
kubeClient := client.InClusterClient()

err := kubeClient.CoreV1().Pods(namespace).Delete(context.TODO(), podName, metav1.DeleteOptions{})
if err != nil {
log.Printf("Error deleting pod: %v", err)
return "", fmt.Errorf("error deleting pod")
}

return fmt.Sprintf("Pod '%s' deleted successfully", podName), nil
}

func GetPodLogs(podName string) (string, error) {
kubeClient := client.InClusterClient()
logOptions := &corev1.PodLogOptions{}
req := kubeClient.CoreV1().Pods(namespace).GetLogs(podName, logOptions)
podLogs, err := req.Stream(context.TODO())
if err != nil {
return "", fmt.Errorf("error getting pod logs: %v", err)
}
defer podLogs.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return "", fmt.Errorf("error reading pod logs: %v", err)
}

return buf.String(), nil
}

func GetPodYAML(podName string) (string, error) {
kubeClient := client.InClusterClient()
pod, err := kubeClient.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
log.Printf("Error getting pod: %v", err)
return "", fmt.Errorf("error getting pod")
}
pod.TypeMeta.Kind = "Pod"
pod.TypeMeta.APIVersion = "v1"
scheme := runtime.NewScheme()
serializer := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme, scheme)
buf := new(bytes.Buffer)
err = serializer.Encode(pod, buf)
if err != nil {
return "", fmt.Errorf("error encoding pod to YAML: %v", err)
}

return buf.String(), nil
}


func UpdatePodYAML(podName string, yamlContent string) (string, error) {
ctx := context.TODO()
kubeClient := client.InClusterClient()

var newPod corev1.Pod
if err := yaml.Unmarshal([]byte(yamlContent), &newPod); err != nil {
return "", fmt.Errorf("failed to unmarshal Pod: %v", err)
}

oldPod, err := kubeClient.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get existing Pod: %v", err)
}
newPod.TypeMeta = oldPod.TypeMeta
newPod.ObjectMeta = oldPod.ObjectMeta

_, err = kubeClient.CoreV1().Pods(namespace).Update(ctx, &newPod, metav1.UpdateOptions{})
if err != nil {
return "", fmt.Errorf("failed to update Pod: %v", err)
}

// Return success message
successMessage := fmt.Sprintf("Pod '%s' in namespace '%s' successfully updated", podName, namespace)
fmt.Printf("Update successful: %s\n", successMessage)
return successMessage, nil
}
9 changes: 9 additions & 0 deletions cmd/api/app/types/api/v1/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package v1

type PodMetadata struct {
UID string `json:"uid"`
CreationTimestamp string `json:"creationTimestamp"`
GenerateName string `json:"generateName"`
Labels map[string]string `json:"labels"`
Name string `json:"name"`
}
1 change: 0 additions & 1 deletion ui/apps/dashboard/src/layout/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const Sidebar: FC<SidebarProps> = ({ collapsed }) => {
}
mode="inline"
items={menuItems}
inlineCollapsed={collapsed}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import { Tabs } from 'antd';

interface TerminalLogsProps {
logs: Record<string, string>;
}

const TerminalLogs: React.FC<TerminalLogsProps> = ({ logs }) => (
<Tabs
type="card"
items={Object.entries(logs).map(([podName, log]) => ({
label: podName,
key: podName,
children: (
<div
style={{
backgroundColor: '#1e1e1e',
borderRadius: '4px',
padding: '8px',
maxHeight: '400px',
overflowY: 'auto'
}}
>
<pre
style={{
color: '#00ff00',
margin: 0,
fontFamily: '"Consolas", "Courier New", monospace',
fontSize: '12px',
whiteSpace: 'pre-wrap',
wordBreak: 'break-all'
}}
>
{log}
</pre>
</div>
)
}))}
/>
);

export default TerminalLogs;
Loading