fix: YAML查看器按钮文案 '清理并编辑' → '编辑',统一简洁命名
This commit is contained in:
1 parent
a8b4af4409
commit
4bd9fa629b
14 files changed
+2146
-400
No files matched your search
@@ -1678,22 +1678,40 @@ func (h *KubernetesHandler) listServicesWithNS(c *gin.Context, ns string) {
|
||||
if err != nil { c.JSON(500, models.Response{Code: 500, Message: err.Error()}); return }
|
||||
svcs, err := client.ListServices()
|
||||
if err != nil { c.JSON(500, models.Response{Code: 500, Message: "K8s API error: " + err.Error()}); return }
|
||||
type SvcPortDetail struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Port int `json:"port"`
|
||||
TargetPort interface{} `json:"targetPort"`
|
||||
NodePort int `json:"nodePort,omitempty"`
|
||||
Protocol string `json:"protocol,omitempty"`
|
||||
}
|
||||
type SvcSummary struct {
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
ClusterIP string `json:"cluster_ip"`
|
||||
Ports int `json:"ports"`
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
ClusterIP string `json:"cluster_ip"`
|
||||
Ports int `json:"ports"`
|
||||
PortList []SvcPortDetail `json:"portList,omitempty"`
|
||||
Selector map[string]string `json:"selector,omitempty"`
|
||||
}
|
||||
result := []SvcSummary{}
|
||||
for _, s := range svcs.Items {
|
||||
if s.Metadata.Namespace != ns { continue }
|
||||
ports := []SvcPortDetail{}
|
||||
for _, p := range s.Spec.Ports {
|
||||
ports = append(ports, SvcPortDetail{
|
||||
Name: p.Name, Port: p.Port, TargetPort: p.TargetPort,
|
||||
NodePort: p.NodePort, Protocol: p.Protocol,
|
||||
})
|
||||
}
|
||||
result = append(result, SvcSummary{
|
||||
Namespace: s.Metadata.Namespace,
|
||||
Name: s.Metadata.Name,
|
||||
Type: s.Spec.Type,
|
||||
ClusterIP: s.Spec.ClusterIP,
|
||||
Ports: len(s.Spec.Ports),
|
||||
Namespace: s.Metadata.Namespace,
|
||||
Name: s.Metadata.Name,
|
||||
Type: s.Spec.Type,
|
||||
ClusterIP: s.Spec.ClusterIP,
|
||||
Ports: len(s.Spec.Ports),
|
||||
PortList: ports,
|
||||
Selector: s.Spec.Selector,
|
||||
})
|
||||
}
|
||||
c.JSON(200, models.Response{Code: 200, Data: result})
|
||||
@@ -1705,22 +1723,57 @@ func (h *KubernetesHandler) listIngressesWithNS(c *gin.Context, ns string) {
|
||||
if err != nil { c.JSON(500, models.Response{Code: 500, Message: err.Error()}); return }
|
||||
ings, err := client.ListIngresses()
|
||||
if err != nil { c.JSON(500, models.Response{Code: 500, Message: "K8s API error: " + err.Error()}); return }
|
||||
type PathDetail struct {
|
||||
Path string `json:"path"`
|
||||
PathType string `json:"pathType,omitempty"`
|
||||
BackendSvc string `json:"backendService,omitempty"`
|
||||
BackendPort string `json:"backendPort,omitempty"`
|
||||
}
|
||||
type RuleDetail struct {
|
||||
Host string `json:"host"`
|
||||
Paths []PathDetail `json:"paths"`
|
||||
}
|
||||
type IngSummary struct {
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Hosts []string `json:"hosts"`
|
||||
TLS bool `json:"tls"`
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"name"`
|
||||
Hosts []string `json:"hosts"`
|
||||
TLS bool `json:"tls"`
|
||||
IngressClass string `json:"ingressClass,omitempty"`
|
||||
Rules []RuleDetail `json:"rules,omitempty"`
|
||||
}
|
||||
result := []IngSummary{}
|
||||
for _, ing := range ings.Items {
|
||||
if ing.Metadata.Namespace != ns { continue }
|
||||
hosts := []string{}
|
||||
for _, rule := range ing.Spec.Rules { if rule.Host != "" { hosts = append(hosts, rule.Host) } }
|
||||
rules := []RuleDetail{}
|
||||
for _, rule := range ing.Spec.Rules {
|
||||
if rule.Host != "" { hosts = append(hosts, rule.Host) }
|
||||
rd := RuleDetail{Host: rule.Host, Paths: []PathDetail{}}
|
||||
if rule.HTTP != nil {
|
||||
for _, p := range rule.HTTP.Paths {
|
||||
pd := PathDetail{Path: p.Path, PathType: p.PathType, BackendSvc: p.Backend.Service.Name}
|
||||
if p.Backend.Service.Port.Number > 0 {
|
||||
pd.BackendPort = strconv.Itoa(p.Backend.Service.Port.Number)
|
||||
} else if p.Backend.Service.Port.Name != "" {
|
||||
pd.BackendPort = p.Backend.Service.Port.Name
|
||||
}
|
||||
rd.Paths = append(rd.Paths, pd)
|
||||
}
|
||||
}
|
||||
rules = append(rules, rd)
|
||||
}
|
||||
// ingressClassName from annotations
|
||||
ingClass := ""
|
||||
if ing.Metadata.Annotations != nil {
|
||||
ingClass = ing.Metadata.Annotations["kubernetes.io/ingress.class"]
|
||||
}
|
||||
result = append(result, IngSummary{
|
||||
Namespace: ing.Metadata.Namespace,
|
||||
Name: ing.Metadata.Name,
|
||||
Hosts: hosts,
|
||||
TLS: len(ing.Spec.TLS) > 0,
|
||||
Namespace: ing.Metadata.Namespace,
|
||||
Name: ing.Metadata.Name,
|
||||
Hosts: hosts,
|
||||
TLS: len(ing.Spec.TLS) > 0,
|
||||
IngressClass: ingClass,
|
||||
Rules: rules,
|
||||
})
|
||||
}
|
||||
c.JSON(200, models.Response{Code: 200, Data: result})
|
||||
@@ -2243,6 +2296,55 @@ func (h *KubernetesHandler) UpdateResourceYAMLNS(c *gin.Context) {
|
||||
c.JSON(200, models.Response{Code: 200, Message: "YAML已应用"})
|
||||
}
|
||||
|
||||
// resourceAPIPath builds the K8s API path for common resource types (shared by Get/Update/Delete)
|
||||
func resourceAPIPath(ns, resource, name string) (string, error) {
|
||||
switch resource {
|
||||
case "deployments", "deployment":
|
||||
return fmt.Sprintf("/apis/apps/v1/namespaces/%s/deployments/%s", ns, name), nil
|
||||
case "statefulsets", "statefulset":
|
||||
return fmt.Sprintf("/apis/apps/v1/namespaces/%s/statefulsets/%s", ns, name), nil
|
||||
case "daemonsets", "daemonset":
|
||||
return fmt.Sprintf("/apis/apps/v1/namespaces/%s/daemonsets/%s", ns, name), nil
|
||||
case "replicasets", "replicaset":
|
||||
return fmt.Sprintf("/apis/apps/v1/namespaces/%s/replicasets/%s", ns, name), nil
|
||||
case "pods", "pod":
|
||||
return fmt.Sprintf("/api/v1/namespaces/%s/pods/%s", ns, name), nil
|
||||
case "services", "service":
|
||||
return fmt.Sprintf("/api/v1/namespaces/%s/services/%s", ns, name), nil
|
||||
case "ingresses", "ingress":
|
||||
return fmt.Sprintf("/apis/networking.k8s.io/v1/namespaces/%s/ingresses/%s", ns, name), nil
|
||||
case "configmaps", "configmap":
|
||||
return fmt.Sprintf("/api/v1/namespaces/%s/configmaps/%s", ns, name), nil
|
||||
case "secrets", "secret":
|
||||
return fmt.Sprintf("/api/v1/namespaces/%s/secrets/%s", ns, name), nil
|
||||
case "pvcs", "persistentvolumeclaim":
|
||||
return fmt.Sprintf("/api/v1/namespaces/%s/persistentvolumeclaims/%s", ns, name), nil
|
||||
case "hpas", "hpa", "horizontalpodautoscaler":
|
||||
return fmt.Sprintf("/apis/autoscaling/v2/namespaces/%s/horizontalpodautoscalers/%s", ns, name), nil
|
||||
case "cronjobs", "cronjob":
|
||||
return fmt.Sprintf("/apis/batch/v1/namespaces/%s/cronjobs/%s", ns, name), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported resource type: %s", resource)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteResourceYAMLNS deletes a K8s resource (Service/Ingress/ConfigMap etc.)
|
||||
func (h *KubernetesHandler) DeleteResourceYAMLNS(c *gin.Context) {
|
||||
clusterID := h.resolveClusterCode(c.Param("clusterCode"))
|
||||
if clusterID == 0 { c.JSON(500, models.Response{Code: 500, Message: "cluster not found"}); return }
|
||||
client, _, err := h.getK8sClient(clusterID)
|
||||
if err != nil { c.JSON(500, models.Response{Code: 500, Message: err.Error()}); return }
|
||||
ns := c.Param("namespace")
|
||||
resource := c.Param("resource")
|
||||
name := c.Param("name")
|
||||
apiPath, err := resourceAPIPath(ns, resource, name)
|
||||
if err != nil { c.JSON(400, models.Response{Code: 400, Message: err.Error()}); return }
|
||||
if err := client.DeleteResource(apiPath); err != nil {
|
||||
c.JSON(500, models.Response{Code: 500, Message: "删除失败: " + err.Error()}); return
|
||||
}
|
||||
c.JSON(200, models.Response{Code: 200, Message: "资源已删除"})
|
||||
}
|
||||
|
||||
// getK8sClient builds a K8s API client from cluster's stored kubeconfig
|
||||
func (h *KubernetesHandler) getK8sClient(clusterID int) (*k8sclient.Client, *models.KubernetesCluster, error) {
|
||||
var cluster models.KubernetesCluster
|
||||
|
||||
Reference in new issue
Block a user