补充事件相关功能

This commit is contained in:
yumaojun03 2025-03-30 16:44:29 +08:00
parent 001b220ccc
commit 475c75995e
4 changed files with 208 additions and 39 deletions

View File

@ -0,0 +1,34 @@
package impl_test
import (
"context"
k8s_client "github.com/infraboard/mpaas/provider/k8s"
"gitlab.com/go-course-project/go17/devcloud-mini/mpaas/apps/k8s"
"gitlab.com/go-course-project/go17/devcloud-mini/mpaas/test"
)
var (
ctx = context.Background()
client *k8s_client.Client
)
func InitClinet() {
// 创建好叻
ins, err := k8s.GetService().DescribeCluster(ctx, &k8s.DescribeClusterRequest{
Id: "docker-desktop",
})
if err != nil {
panic(err)
}
client, err = ins.GetK8sClient()
if err != nil {
panic(err)
}
}
func init() {
test.SetUp()
InitClinet()
}

View File

@ -0,0 +1,133 @@
package impl_test
import (
"fmt"
"testing"
"github.com/infraboard/mcube/v2/tools/pretty"
k8s_client "github.com/infraboard/mpaas/provider/k8s"
"github.com/infraboard/mpaas/provider/k8s/meta"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// 查询deployment
func TestListDeployment(t *testing.T) {
set, err := client.WorkLoad().ListDeployment(ctx, meta.NewListRequest().WithNamespace("default"))
if err != nil {
t.Fatal(err)
}
t.Log(set)
}
func TestCreateDeployment(t *testing.T) {
var Replicas int32 = 2
var TerminationGracePeriodSeconds int64 = 30
deployment := &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx",
Namespace: "default",
},
Spec: v1.DeploymentSpec{
Replicas: &Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"k8s-app": "nginx"},
},
Strategy: v1.DeploymentStrategy{
Type: v1.RollingUpdateDeploymentStrategyType,
RollingUpdate: &v1.RollingUpdateDeployment{
MaxSurge: k8s_client.NewIntStr(1),
MaxUnavailable: k8s_client.NewIntStr(0),
},
},
// Pod模板参数
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Labels: map[string]string{
"k8s-app": "nginx",
},
},
Spec: corev1.PodSpec{
// Pod参数
DNSPolicy: corev1.DNSClusterFirst,
RestartPolicy: corev1.RestartPolicyAlways,
SchedulerName: "default-scheduler",
TerminationGracePeriodSeconds: &TerminationGracePeriodSeconds,
// Container参数
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx:latest",
ImagePullPolicy: corev1.PullAlways,
Env: []corev1.EnvVar{
{Name: "APP_NAME", Value: "nginx"},
{Name: "APP_VERSION", Value: "v1"},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("50Mi"),
},
},
},
},
},
},
},
}
// 全量修改
// client.WorkLoad().UpdateDeployment(ctx, deployment)
resp, err := client.WorkLoad().CreateDeployment(ctx, deployment)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.MustToYaml(resp))
}
func TestScaleDeployment(t *testing.T) {
req := meta.NewScaleRequest()
req.Scale.Namespace = "default"
req.Scale.Name = "nginx"
req.Scale.Spec.Replicas = 1
resp, err := client.WorkLoad().ScaleDeployment(ctx, req)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.MustToYaml(resp))
}
func TestReDeploy(t *testing.T) {
resp, err := client.WorkLoad().ReDeploy(ctx, meta.NewGetRequest("nginx").WithNamespace("default"))
if err != nil {
t.Fatal(err)
}
t.Log(pretty.MustToYaml(resp))
}
func TestListPod(t *testing.T) {
resp, err := client.WorkLoad().ListPod(ctx, meta.NewListRequest().
WithNamespace("default").
WithLabelSelector(meta.NewLabelSelector().Add("k8s-app", "nginx")))
if err != nil {
t.Fatal(err)
}
t.Log(pretty.MustToYaml(resp))
}
func TestListPodEvent(t *testing.T) {
req := meta.NewListRequest()
req.Opts.FieldSelector = fmt.Sprintf("regarding.name=%s,regarding.kind=%s", "nginx-7dbf64989-cr89v", "Pod")
resp, err := client.Event().ListEvent(ctx, req)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.MustToYaml(resp))
}

View File

@ -4,7 +4,6 @@ import (
"os" "os"
"testing" "testing"
"github.com/infraboard/mpaas/provider/k8s/meta"
"gitlab.com/go-course-project/go17/devcloud-mini/mpaas/apps/k8s" "gitlab.com/go-course-project/go17/devcloud-mini/mpaas/apps/k8s"
) )
@ -37,17 +36,5 @@ func TestDescribeCluster(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
client, err := ins.GetK8sClient() t.Log(ins)
if err != nil {
t.Fatal(err)
}
// 查询deployment
set, err := client.WorkLoad().ListDeployment(ctx, meta.NewListRequest().WithNamespace("default"))
if err != nil {
t.Fatal(err)
}
t.Log(set)
// 创建Deployment
} }

View File

@ -1,81 +1,96 @@
<mxfile host="65bd71144e"> <mxfile host="65bd71144e">
<diagram id="dKE0DoaGGKq9DsSHO4Zo" name="第 1 页"> <diagram id="dKE0DoaGGKq9DsSHO4Zo" name="第 1 页">
<mxGraphModel dx="852" dy="570" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0"> <mxGraphModel dx="832" dy="321" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root> <root>
<mxCell id="0"/> <mxCell id="0"/>
<mxCell id="1" parent="0"/> <mxCell id="1" parent="0"/>
<mxCell id="3" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="3" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="60" y="90" width="480" height="310" as="geometry"/> <mxGeometry x="60" y="90" width="480" height="310" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="4" value="k8s 托管集群: 管理集群的凭证" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="4" value="k8s 托管集群: 管理集群的凭证" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="185" y="40" width="220" height="30" as="geometry"/> <mxGeometry x="185" y="40" width="220" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="5" value="k8s a" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="5" value="k8s a" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="100" y="150" width="390" height="30" as="geometry"/> <mxGeometry x="100" y="150" width="390" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="6" value="&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;k8s b&lt;/span&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="6" value="&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;k8s b&lt;/span&gt;" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="100" y="190" width="390" height="30" as="geometry"/> <mxGeometry x="100" y="190" width="390" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="7" value="k8s ..." style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="7" value="k8s ..." style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="100" y="240" width="390" height="30" as="geometry"/> <mxGeometry x="100" y="240" width="390" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="8" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="8" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="100" y="290" width="390" height="30" as="geometry"/> <mxGeometry x="100" y="290" width="390" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="9" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="9" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="100" y="340" width="390" height="30" as="geometry"/> <mxGeometry x="100" y="340" width="390" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="10" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="10" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="590" y="90" width="190" height="310" as="geometry"/> <mxGeometry x="590" y="90" width="190" height="310" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="12" value="k8s ui" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="12" value="k8s ui" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="590" y="50" width="60" height="30" as="geometry"/> <mxGeometry x="590" y="50" width="60" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="13" value="运维,开发不一定会用" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="13" value="运维,开发不一定会用" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="580" y="20" width="160" height="30" as="geometry"/> <mxGeometry x="580" y="20" width="160" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="14" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="14" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="60" y="485" width="330" height="245" as="geometry"/> <mxGeometry x="60" y="485" width="330" height="245" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="15" value="部署接口:&amp;nbsp; 应用" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="15" value="部署接口:&amp;nbsp; 应用" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="60" y="450" width="100" height="30" as="geometry"/> <mxGeometry x="60" y="450" width="100" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="16" value="部署类型" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="16" value="部署类型" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="90" y="500" width="60" height="30" as="geometry"/> <mxGeometry x="90" y="500" width="60" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="17" value="部署配置" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="17" value="部署配置" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="90" y="540" width="60" height="30" as="geometry"/> <mxGeometry x="90" y="540" width="60" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="18" value="访问方式" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"> <mxCell id="18" value="访问方式" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
<mxGeometry x="90" y="580" width="60" height="30" as="geometry"/> <mxGeometry x="90" y="580" width="60" height="30" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="19" value="Deployment" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="19" value="Deployment" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="230" y="510" width="120" height="60" as="geometry"/> <mxGeometry x="230" y="510" width="120" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="20" value="service" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="20" value="service" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="230" y="580" width="120" height="60" as="geometry"/> <mxGeometry x="230" y="580" width="120" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="21" value="ingress" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="21" value="ingress" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="230" y="650" width="120" height="60" as="geometry"/> <mxGeometry x="230" y="650" width="120" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="22" value="部署A" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="22" value="部署A" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="50" y="840" width="330" height="245" as="geometry"/> <mxGeometry x="50" y="840" width="330" height="245" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="23" value="部署A" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="23" value="部署A" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="430" y="840" width="330" height="245" as="geometry"/> <mxGeometry x="430" y="840" width="330" height="245" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="24" value="部署A" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="24" value="部署A" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="50" y="1120" width="330" height="245" as="geometry"/> <mxGeometry x="50" y="1120" width="330" height="245" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="25" value="登录" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="25" value="登录" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="220" y="850" width="75" height="20" as="geometry"/> <mxGeometry x="220" y="850" width="75" height="20" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="26" value="日志" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="26" value="日志" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="295" y="850" width="75" height="20" as="geometry"/> <mxGeometry x="295" y="850" width="75" height="20" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="27" value="状态" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="27" value="状态" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="60" y="850" width="80" height="20" as="geometry"/> <mxGeometry x="60" y="850" width="80" height="20" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="28" value="Cron" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="60" y="1050" width="80" height="20" as="geometry"/>
</mxCell>
<mxCell id="29" value="Config" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="290" y="1050" width="80" height="20" as="geometry"/>
</mxCell>
<mxCell id="30" value="P1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="60" y="900" width="40" height="20" as="geometry"/>
</mxCell>
<mxCell id="31" value="P1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="100" y="900" width="40" height="20" as="geometry"/>
</mxCell>
<mxCell id="32" value="P1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="140" y="900" width="40" height="20" as="geometry"/>
</mxCell>
</root> </root>
</mxGraphModel> </mxGraphModel>
</diagram> </diagram>