容器接口更新

This commit is contained in:
zhangwei 2025-07-30 14:46:34 +08:00
parent fe28feeee9
commit 7402375a2e
8 changed files with 21 additions and 13 deletions

View File

@ -48,7 +48,7 @@ func (c *cloudApi) CreateContainerHandler(ctx *gin.Context) {
fmt.Println(c.service)
createContainer, err := c.service.CreateContainer(ctx.Request.Context(), pfId, &param)
if err != nil {
model.Response(ctx, http.StatusInternalServerError, err, nil)
model.Response(ctx, http.StatusInternalServerError, err.Error(), nil)
return
}
model.Response(ctx, http.StatusOK, "success", createContainer)

View File

@ -41,12 +41,12 @@ func main() {
// zap.L().Fatal("集群初始化失败", zap.Error(err))
//}
// 初始化智算集群连接
aiSvc, err := initialize.InitAICluster(cfg)
if err != nil {
initialize.Panic("Server started failed: %s", err)
return
}
api.AiApi.RegisterSvc(aiSvc)
//aiSvc, err := initialize.InitAICluster(cfg)
//if err != nil {
// initialize.Panic("Server started failed: %s", err)
// return
//}
//api.AiApi.RegisterSvc(aiSvc)
// 初始化通算集群连接
cloudSvc, err := initialize.InitCloudCluster(cfg)
if err != nil {

View File

@ -2,6 +2,7 @@ package container
type CreateParam struct {
ContainerGroupName string `json:"containerGroupName"`
Description string `json:"description,omitempty"`
Name string `json:"name"`
Image string `json:"image"`
Cpu string `json:"cpu,omitempty"`

View File

@ -6,6 +6,7 @@ import (
)
type CreateContainerParam struct {
Description string `json:"description,omitempty"`
RegionId string `json:"regionId"`
ContainerGroupName string `json:"containerGroupName"`
Containers *[]eci.CreateContainerGroupContainer `json:"containers"`

View File

@ -17,7 +17,7 @@ func CreateContainer(ctx *gin.Context) {
client := common.GetK8sClient()
err := service.CreateContainer(client, &param)
if err != nil {
model.Response(ctx, 500, common.INVOKEERROR, err)
model.Response(ctx, 500, common.INVOKEERROR, err.Error())
return
}

View File

@ -90,10 +90,12 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i
return nil, errors.New("ContainerGroupName is required")
}
cParam := model.CreateContainerParam{
Description: param.Description,
Container: model.Container{
Name: param.Name,
Image: param.Image,
Args: param.Args,
Limits: struct {
Cpu string `json:"cpu,omitempty"`
Memory string `json:"memory,omitempty"`

View File

@ -4,6 +4,7 @@ type CreateContainerParam struct {
ContainerGroupName string `json:"containerGroupName"`
Container Container `json:"Container"`
MountPath string `json:"mountPath,omitempty"`
Description string `json:"description,omitempty"`
}
type Container struct {
Name string `json:"name"`

View File

@ -2,10 +2,11 @@ package service
import (
"context"
"errors"
"fmt"
"gitlink.org.cn/JointCloud/pcm-participant-k8s/model"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
k8sErr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
@ -13,12 +14,13 @@ import (
func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerParam) error {
// 查询pod是否存在
_, err := client.CoreV1().Pods("default").Get(context.TODO(), param.ContainerGroupName, metav1.GetOptions{})
if err == nil {
queryPod, err := client.CoreV1().Pods("default").Get(context.TODO(), param.ContainerGroupName, metav1.GetOptions{})
if err != nil && !k8sErr.IsNotFound(err) {
return err
}
if !errors.IsNotFound(err) {
return err
if queryPod.Name != "" {
return errors.New("pod already exists.")
}
// 创建svc
if param.Container.ContainerPort.Port != 0 {
@ -30,6 +32,7 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
// 创建 Pod 对象
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: param.ContainerGroupName, // Pod 名称(必选)
Labels: map[string]string{
"app": param.ContainerGroupName,