pcm-kubernetes/internal/logic/getappdetaillogic.go

102 lines
3.3 KiB
Go

package logic
import (
"context"
"fmt"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/pkg"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/kubernetes"
"gitlink.org.cn/jcce-pcm/utils/tool"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/zeromicro/go-zero/core/logx"
)
type GetAppDetailLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetAppDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAppDetailLogic {
return &GetAppDetailLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 查询app详情
func (l *GetAppDetailLogic) GetAppDetail(in *kubernetes.AppDetailReq) (*kubernetes.AppDetailResp, error) {
details := make([]*kubernetes.AppDetail, 0)
//查询指定集群下指定命名空间下和名字的deployment详情
for k := range pkg.KClients {
//转换赋值
var (
kDeployment kubernetes.Deployment
kStatefulSets kubernetes.StatefulSet
kIngresses kubernetes.IngressList
kService kubernetes.Service
kConfigMap kubernetes.ConfigMap
kHpa kubernetes.HorizontalPodAutoscaler
kSecret kubernetes.Secret
)
appDetail := kubernetes.AppDetail{
ClusterName: k,
}
//获取指定命名空间下的deployment
deployment, err := pkg.KClients[k].ClientSet.AppsV1().Deployments(in.Namespace).Get(l.ctx, in.Name, metav1.GetOptions{})
if err == nil {
tool.Convert(deployment, &kDeployment)
appDetail.Deployment = &kDeployment
}
//查询指定集群下指定命名空间下和名字的statefulSets详情
statefulSets, err := pkg.KClients[k].ClientSet.AppsV1().StatefulSets(in.Namespace).Get(l.ctx, in.Name, metav1.GetOptions{})
if err == nil {
tool.Convert(statefulSets, &kStatefulSets)
appDetail.StatefulSet = &kStatefulSets
}
// 查询 Ingress 资源
labelSelector := fmt.Sprintf("cloud.sealos.io/app-deploy-manager=%s", in.Name)
// 执行查询操作,获取匹配标签的 Ingress 资源列表
ingresses, err := pkg.KClients[k].ClientSet.NetworkingV1().Ingresses(in.Namespace).List(l.ctx, metav1.ListOptions{LabelSelector: labelSelector})
if err == nil {
tool.Convert(ingresses, &kIngresses)
appDetail.IngressList = &kIngresses
}
//查询 Service 资源
service, err := pkg.KClients[k].ClientSet.CoreV1().Services(in.Namespace).Get(l.ctx, in.Name, metav1.GetOptions{})
if err == nil {
tool.Convert(service, &kService)
appDetail.Service = &kService
}
//查询configmap
configmap, err := pkg.KClients[k].ClientSet.CoreV1().ConfigMaps(in.Namespace).Get(l.ctx, in.Name, metav1.GetOptions{})
if err == nil {
tool.Convert(configmap, &kConfigMap)
appDetail.ConfigMap = &kConfigMap
}
//查询hpa
hpa, err := pkg.KClients[k].ClientSet.AutoscalingV1().HorizontalPodAutoscalers(in.Namespace).Get(l.ctx, in.Name, metav1.GetOptions{})
if err == nil {
tool.Convert(hpa, &kHpa)
appDetail.HorizontalPodAutoscaler = &kHpa
}
secret, err := pkg.KClients[k].ClientSet.CoreV1().Secrets(in.Namespace).Get(l.ctx, in.Name, metav1.GetOptions{})
if err == nil {
tool.Convert(secret, &kSecret)
appDetail.Secret = &kSecret
}
details = append(details, &appDetail)
}
logx.Infof("查询app详情成功")
return &kubernetes.AppDetailResp{
Code: "200",
Msg: "success",
Data: details,
}, nil
}