134 lines
3.6 KiB
Go
134 lines
3.6 KiB
Go
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))
|
|
}
|