推理任务测试完成
This commit is contained in:
parent
d73bdee020
commit
ceae117b21
|
@ -1,14 +1,15 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
type CreateParam struct {
|
type CreateParam struct {
|
||||||
ContainerGroupName string `json:"containerGroupName"`
|
ContainerGroupName string `json:"containerGroupName"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
Cpu string `json:"cpu,omitempty"`
|
Cpu string `json:"cpu,omitempty"`
|
||||||
Memory string `json:"memory,omitempty"`
|
Memory string `json:"memory,omitempty"`
|
||||||
Port int32 `json:"port,omitempty"`
|
Port int32 `json:"port,omitempty"`
|
||||||
HostPort int32 `json:"hostPort,omitempty"`
|
NodePort int32 `json:"nodePort,omitempty"`
|
||||||
MountPath string `json:"mountPath,omitempty"`
|
MountPath string `json:"mountPath,omitempty"`
|
||||||
|
Args []string `json:"args,omitempty"`
|
||||||
Envs []struct {
|
Envs []struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Value string `json:"value,omitempty"`
|
Value string `json:"value,omitempty"`
|
||||||
|
|
|
@ -32,13 +32,18 @@ func TestCreateService(t *testing.T) {
|
||||||
|
|
||||||
convey.Convey("all train algorithms", func() {
|
convey.Convey("all train algorithms", func() {
|
||||||
param := container.CreateParam{
|
param := container.CreateParam{
|
||||||
ContainerGroupName: "hello-nginx",
|
ContainerGroupName: "hello-llama",
|
||||||
Image: "nginx:latest",
|
Image: "ghcr.io/ggml-org/llama.cpp:server",
|
||||||
Name: "hello-nginx",
|
//Image: "nginx:latest",
|
||||||
Cpu: "1.0",
|
Name: "hello-llama",
|
||||||
Memory: "1.0",
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -72,12 +77,13 @@ func TestDeleteService(t *testing.T) {
|
||||||
|
|
||||||
convey.Convey("all train algorithms", func() {
|
convey.Convey("all train algorithms", func() {
|
||||||
param := container.DeleteParam{
|
param := container.DeleteParam{
|
||||||
DeleteParameter: &container.EciDeleteParam{
|
//DeleteParameter: &container.EciDeleteParam{
|
||||||
RegionId: "cn-hangzhou",
|
//RegionId: "cn-hangzhou",
|
||||||
ContainerGroupId: "eci-bp1f6qix5wkkeqhzoc77",
|
//ContainerGroupId: "eci-bp1f6qix5wkkeqhzoc77",
|
||||||
},
|
//},
|
||||||
|
Name: "hello-llama",
|
||||||
}
|
}
|
||||||
err := svc.DeleteContainer(ctx, 456, ¶m)
|
err := svc.DeleteContainer(ctx, 123, ¶m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,7 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i
|
||||||
Container: model.Container{
|
Container: model.Container{
|
||||||
Name: param.Name,
|
Name: param.Name,
|
||||||
Image: param.Image,
|
Image: param.Image,
|
||||||
|
Args: param.Args,
|
||||||
Limits: struct {
|
Limits: struct {
|
||||||
Cpu string `json:"cpu,omitempty"`
|
Cpu string `json:"cpu,omitempty"`
|
||||||
Memory string `json:"memory,omitempty"`
|
Memory string `json:"memory,omitempty"`
|
||||||
|
@ -102,12 +103,16 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i
|
||||||
},
|
},
|
||||||
ContainerPort: struct {
|
ContainerPort: struct {
|
||||||
Port int32 `json:"port,omitempty"`
|
Port int32 `json:"port,omitempty"`
|
||||||
HostPort int32 `json:"hostPort,omitempty"`
|
NodePort int32 `json:"nodePort,omitempty"`
|
||||||
}{},
|
}{
|
||||||
|
Port: param.Port,
|
||||||
|
NodePort: param.NodePort,
|
||||||
|
},
|
||||||
Envs: nil,
|
Envs: nil,
|
||||||
Command: nil,
|
Command: nil,
|
||||||
},
|
},
|
||||||
ContainerGroupName: param.ContainerGroupName,
|
ContainerGroupName: param.ContainerGroupName,
|
||||||
|
MountPath: param.MountPath,
|
||||||
}
|
}
|
||||||
err := service.CreateContainer(c.opt.ClientSet, &cParam)
|
err := service.CreateContainer(c.opt.ClientSet, &cParam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -4,18 +4,18 @@ type CreateContainerParam struct {
|
||||||
ContainerGroupName string `json:"containerGroupName"`
|
ContainerGroupName string `json:"containerGroupName"`
|
||||||
Container Container `json:"Container"`
|
Container Container `json:"Container"`
|
||||||
MountPath string `json:"mountPath,omitempty"`
|
MountPath string `json:"mountPath,omitempty"`
|
||||||
PvcName string `json:"pvcName,omitempty"`
|
|
||||||
}
|
}
|
||||||
type Container struct {
|
type Container struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
|
Args []string `json:"args,omitempty"`
|
||||||
Limits struct {
|
Limits struct {
|
||||||
Cpu string `json:"cpu,omitempty"`
|
Cpu string `json:"cpu,omitempty"`
|
||||||
Memory string `json:"memory,omitempty"`
|
Memory string `json:"memory,omitempty"`
|
||||||
} `json:"limits,omitempty"`
|
} `json:"limits,omitempty"`
|
||||||
ContainerPort struct {
|
ContainerPort struct {
|
||||||
Port int32 `json:"port,omitempty"`
|
Port int32 `json:"port,omitempty"`
|
||||||
HostPort int32 `json:"hostPort,omitempty"`
|
NodePort int32 `json:"nodePort,omitempty"`
|
||||||
} `json:"containerPorts,omitempty"`
|
} `json:"containerPorts,omitempty"`
|
||||||
Envs []struct {
|
Envs []struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
|
|
@ -27,25 +27,30 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
|
||||||
if !errors.IsNotFound(err) {
|
if !errors.IsNotFound(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 创建pvc
|
// 创建svc
|
||||||
if param.MountPath != "" {
|
if param.Container.ContainerPort.Port != 0 {
|
||||||
pvcName, err := CreatePvc(client)
|
_, err := CreateSvc(client, param)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
param.PvcName = pvcName
|
|
||||||
}
|
}
|
||||||
// 创建 Pod 对象
|
// 创建 Pod 对象
|
||||||
pod := &v1.Pod{
|
pod := &v1.Pod{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: param.ContainerGroupName, // Pod 名称(必选)
|
Name: param.ContainerGroupName, // Pod 名称(必选)
|
||||||
|
Labels: map[string]string{
|
||||||
|
"app": param.ContainerGroupName,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
Spec: v1.PodSpec{
|
Spec: v1.PodSpec{
|
||||||
|
HostNetwork: true,
|
||||||
Containers: []v1.Container{ // 至少一个容器(必选)
|
Containers: []v1.Container{ // 至少一个容器(必选)
|
||||||
{
|
{
|
||||||
ImagePullPolicy: "Never",
|
ImagePullPolicy: "Never",
|
||||||
Name: param.Container.Name,
|
Name: param.Container.Name,
|
||||||
Image: param.Container.Image,
|
Image: param.Container.Image,
|
||||||
|
Args: param.Container.Args,
|
||||||
Resources: v1.ResourceRequirements{
|
Resources: v1.ResourceRequirements{
|
||||||
Limits: v1.ResourceList{
|
Limits: v1.ResourceList{
|
||||||
v1.ResourceCPU: resource.MustParse(param.Container.Limits.Cpu),
|
v1.ResourceCPU: resource.MustParse(param.Container.Limits.Cpu),
|
||||||
|
@ -61,28 +66,20 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
|
||||||
// 挂载pvc
|
// 挂载pvc
|
||||||
if param.MountPath != "" {
|
if param.MountPath != "" {
|
||||||
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
|
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
|
||||||
Name: param.PvcName,
|
Name: "11mvgt1jh-pvc",
|
||||||
MountPath: param.MountPath,
|
MountPath: param.MountPath,
|
||||||
})
|
})
|
||||||
pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{
|
pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{
|
||||||
Name: param.PvcName,
|
Name: "11mvgt1jh-pvc",
|
||||||
VolumeSource: v1.VolumeSource{
|
VolumeSource: v1.VolumeSource{
|
||||||
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
|
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
|
||||||
ClaimName: param.PvcName,
|
ClaimName: "11mvgt1jh-pvc",
|
||||||
ReadOnly: false,
|
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 {
|
for i := range param.Container.Envs {
|
||||||
pod.Spec.Containers[0].Env[i] = v1.EnvVar{
|
pod.Spec.Containers[0].Env[i] = v1.EnvVar{
|
||||||
|
@ -99,7 +96,7 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf(err.Error())
|
fmt.Printf(err.Error())
|
||||||
// 删除pvc
|
// 删除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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,12 +116,12 @@ func DeleteContainer(client *kubernetes.Clientset, param *model.DeleteContainerP
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 删除pvc
|
// 删除pvc
|
||||||
if pod.Spec.Volumes[0].PersistentVolumeClaim != nil {
|
//if pod.Spec.Volumes[0].PersistentVolumeClaim != nil {
|
||||||
err = client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{})
|
// err = client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{})
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func CreatePvc(client *kubernetes.Clientset) (string, error) {
|
||||||
},
|
},
|
||||||
Resources: v1.VolumeResourceRequirements{
|
Resources: v1.VolumeResourceRequirements{
|
||||||
Requests: v1.ResourceList{
|
Requests: v1.ResourceList{
|
||||||
v1.ResourceStorage: resource.MustParse("10Gi"),
|
v1.ResourceStorage: resource.MustParse("1Gi"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
StorageClassName: &sc,
|
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