推理任务测试完成
This commit is contained in:
parent
d73bdee020
commit
ceae117b21
|
@ -1,14 +1,15 @@
|
|||
package container
|
||||
|
||||
type CreateParam struct {
|
||||
ContainerGroupName string `json:"containerGroupName"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Cpu string `json:"cpu,omitempty"`
|
||||
Memory string `json:"memory,omitempty"`
|
||||
Port int32 `json:"port,omitempty"`
|
||||
HostPort int32 `json:"hostPort,omitempty"`
|
||||
MountPath string `json:"mountPath,omitempty"`
|
||||
ContainerGroupName string `json:"containerGroupName"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Cpu string `json:"cpu,omitempty"`
|
||||
Memory string `json:"memory,omitempty"`
|
||||
Port int32 `json:"port,omitempty"`
|
||||
NodePort int32 `json:"nodePort,omitempty"`
|
||||
MountPath string `json:"mountPath,omitempty"`
|
||||
Args []string `json:"args,omitempty"`
|
||||
Envs []struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Value string `json:"value,omitempty"`
|
||||
|
|
|
@ -32,13 +32,18 @@ func TestCreateService(t *testing.T) {
|
|||
|
||||
convey.Convey("all train algorithms", func() {
|
||||
param := container.CreateParam{
|
||||
ContainerGroupName: "hello-nginx",
|
||||
Image: "nginx:latest",
|
||||
Name: "hello-nginx",
|
||||
Cpu: "1.0",
|
||||
Memory: "1.0",
|
||||
ContainerGroupName: "hello-llama",
|
||||
Image: "ghcr.io/ggml-org/llama.cpp:server",
|
||||
//Image: "nginx:latest",
|
||||
Name: "hello-llama",
|
||||
Args: []string{"-m", "/models/ERNIE-4.5-0.3B-PT-GGUF/ernie-4.5-0.3b-pt-q4_k_m.gguf", "--port", "8000", "--host", "0.0.0.0", "-n", "512"},
|
||||
MountPath: "/models",
|
||||
Port: 8000,
|
||||
NodePort: 30000,
|
||||
Cpu: "100m",
|
||||
Memory: "256Mi",
|
||||
}
|
||||
ctn, err := svc.CreateContainer(ctx, 456, ¶m)
|
||||
ctn, err := svc.CreateContainer(ctx, 123, ¶m)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
@ -72,12 +77,13 @@ func TestDeleteService(t *testing.T) {
|
|||
|
||||
convey.Convey("all train algorithms", func() {
|
||||
param := container.DeleteParam{
|
||||
DeleteParameter: &container.EciDeleteParam{
|
||||
RegionId: "cn-hangzhou",
|
||||
ContainerGroupId: "eci-bp1f6qix5wkkeqhzoc77",
|
||||
},
|
||||
//DeleteParameter: &container.EciDeleteParam{
|
||||
//RegionId: "cn-hangzhou",
|
||||
//ContainerGroupId: "eci-bp1f6qix5wkkeqhzoc77",
|
||||
//},
|
||||
Name: "hello-llama",
|
||||
}
|
||||
err := svc.DeleteContainer(ctx, 456, ¶m)
|
||||
err := svc.DeleteContainer(ctx, 123, ¶m)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
|
|
@ -93,6 +93,7 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i
|
|||
Container: model.Container{
|
||||
Name: param.Name,
|
||||
Image: param.Image,
|
||||
Args: param.Args,
|
||||
Limits: struct {
|
||||
Cpu string `json:"cpu,omitempty"`
|
||||
Memory string `json:"memory,omitempty"`
|
||||
|
@ -102,12 +103,16 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i
|
|||
},
|
||||
ContainerPort: struct {
|
||||
Port int32 `json:"port,omitempty"`
|
||||
HostPort int32 `json:"hostPort,omitempty"`
|
||||
}{},
|
||||
NodePort int32 `json:"nodePort,omitempty"`
|
||||
}{
|
||||
Port: param.Port,
|
||||
NodePort: param.NodePort,
|
||||
},
|
||||
Envs: nil,
|
||||
Command: nil,
|
||||
},
|
||||
ContainerGroupName: param.ContainerGroupName,
|
||||
MountPath: param.MountPath,
|
||||
}
|
||||
err := service.CreateContainer(c.opt.ClientSet, &cParam)
|
||||
if err != nil {
|
||||
|
|
|
@ -4,18 +4,18 @@ type CreateContainerParam struct {
|
|||
ContainerGroupName string `json:"containerGroupName"`
|
||||
Container Container `json:"Container"`
|
||||
MountPath string `json:"mountPath,omitempty"`
|
||||
PvcName string `json:"pvcName,omitempty"`
|
||||
}
|
||||
type Container struct {
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Args []string `json:"args,omitempty"`
|
||||
Limits struct {
|
||||
Cpu string `json:"cpu,omitempty"`
|
||||
Memory string `json:"memory,omitempty"`
|
||||
} `json:"limits,omitempty"`
|
||||
ContainerPort struct {
|
||||
Port int32 `json:"port,omitempty"`
|
||||
HostPort int32 `json:"hostPort,omitempty"`
|
||||
NodePort int32 `json:"nodePort,omitempty"`
|
||||
} `json:"containerPorts,omitempty"`
|
||||
Envs []struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
|
|
|
@ -27,25 +27,30 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
|
|||
if !errors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
// 创建pvc
|
||||
if param.MountPath != "" {
|
||||
pvcName, err := CreatePvc(client)
|
||||
// 创建svc
|
||||
if param.Container.ContainerPort.Port != 0 {
|
||||
_, err := CreateSvc(client, param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
param.PvcName = pvcName
|
||||
}
|
||||
// 创建 Pod 对象
|
||||
pod := &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: param.ContainerGroupName, // Pod 名称(必选)
|
||||
Labels: map[string]string{
|
||||
"app": param.ContainerGroupName,
|
||||
},
|
||||
},
|
||||
|
||||
Spec: v1.PodSpec{
|
||||
HostNetwork: true,
|
||||
Containers: []v1.Container{ // 至少一个容器(必选)
|
||||
{
|
||||
ImagePullPolicy: "Never",
|
||||
Name: param.Container.Name,
|
||||
Image: param.Container.Image,
|
||||
Args: param.Container.Args,
|
||||
Resources: v1.ResourceRequirements{
|
||||
Limits: v1.ResourceList{
|
||||
v1.ResourceCPU: resource.MustParse(param.Container.Limits.Cpu),
|
||||
|
@ -61,28 +66,20 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
|
|||
// 挂载pvc
|
||||
if param.MountPath != "" {
|
||||
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
|
||||
Name: param.PvcName,
|
||||
Name: "11mvgt1jh-pvc",
|
||||
MountPath: param.MountPath,
|
||||
})
|
||||
pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{
|
||||
Name: param.PvcName,
|
||||
Name: "11mvgt1jh-pvc",
|
||||
VolumeSource: v1.VolumeSource{
|
||||
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
|
||||
ClaimName: param.PvcName,
|
||||
ClaimName: "11mvgt1jh-pvc",
|
||||
ReadOnly: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// 设置端口
|
||||
for i := range param.Container.ContainerPort.Port {
|
||||
pod.Spec.Containers[0].Ports[i] = v1.ContainerPort{
|
||||
ContainerPort: param.Container.ContainerPort.Port,
|
||||
HostPort: param.Container.ContainerPort.HostPort,
|
||||
}
|
||||
}
|
||||
|
||||
// 设置环境变量
|
||||
for i := range param.Container.Envs {
|
||||
pod.Spec.Containers[0].Env[i] = v1.EnvVar{
|
||||
|
@ -99,7 +96,7 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
|
|||
if err != nil {
|
||||
fmt.Printf(err.Error())
|
||||
// 删除pvc
|
||||
client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{})
|
||||
//client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{})
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -119,12 +116,12 @@ func DeleteContainer(client *kubernetes.Clientset, param *model.DeleteContainerP
|
|||
return err
|
||||
}
|
||||
// 删除pvc
|
||||
if pod.Spec.Volumes[0].PersistentVolumeClaim != nil {
|
||||
err = client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//if pod.Spec.Volumes[0].PersistentVolumeClaim != nil {
|
||||
// err = client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{})
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ func CreatePvc(client *kubernetes.Clientset) (string, error) {
|
|||
},
|
||||
Resources: v1.VolumeResourceRequirements{
|
||||
Requests: v1.ResourceList{
|
||||
v1.ResourceStorage: resource.MustParse("10Gi"),
|
||||
v1.ResourceStorage: resource.MustParse("1Gi"),
|
||||
},
|
||||
},
|
||||
StorageClassName: &sc,
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitlink.org.cn/JointCloud/pcm-participant-k8s/model"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
func CreateSvc(client *kubernetes.Clientset, param *model.CreateContainerParam) (string, error) {
|
||||
|
||||
svc := v1.Service{
|
||||
TypeMeta: metav1.TypeMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: param.ContainerGroupName + "-service",
|
||||
},
|
||||
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: "NodePort",
|
||||
Selector: map[string]string{
|
||||
"app": param.ContainerGroupName,
|
||||
},
|
||||
Ports: []v1.ServicePort{
|
||||
{
|
||||
Port: param.Container.ContainerPort.Port,
|
||||
TargetPort: intstr.IntOrString{
|
||||
Type: intstr.Int,
|
||||
IntVal: param.Container.ContainerPort.Port,
|
||||
},
|
||||
NodePort: param.Container.ContainerPort.NodePort,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err := client.CoreV1().Services("default").Create(context.Background(), &svc, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return svc.Name, nil
|
||||
|
||||
}
|
Loading…
Reference in New Issue