pcm-participant/participant/k8s/k8s.go

159 lines
3.6 KiB
Go

package k8s
import (
"context"
"errors"
"fmt"
"gitlink.org.cn/JointCloud/pcm-participant-cloud/container"
"gitlink.org.cn/JointCloud/pcm-participant-cloud/platform"
"gitlink.org.cn/JointCloud/pcm-participant-k8s/model"
"gitlink.org.cn/JointCloud/pcm-participant-k8s/service"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"time"
)
type Container struct {
opt *Option
ft *container.Features
}
type ContainerSpec struct {
opt *Option
fSpec *container.FeatureSpec
container *model.Container
}
type Option struct {
platform *platform.Platform
DynamicClient *dynamic.DynamicClient
ClientSet *kubernetes.Clientset
}
type Kubernetes struct {
Container *Container
opt *Option
}
func New(bearerToken, url string, id platform.Id) (*Kubernetes, error) {
restConfig := &rest.Config{
Timeout: 10 * time.Second,
Host: url,
BearerToken: bearerToken,
TLSClientConfig: rest.TLSClientConfig{
Insecure: true,
},
}
dynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
fmt.Println(err.Error())
}
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
fmt.Println(err.Error())
}
opt := &Option{
DynamicClient: dynamicClient,
ClientSet: clientSet,
platform: &platform.Platform{
Id: id,
Type: platform.K8s,
},
}
container := &Container{
opt: opt,
}
return &Kubernetes{
opt: opt,
Container: container,
}, nil
}
func (ic *ContainerSpec) Detail() (interface{}, error) {
return ic.container, nil
}
func (ic *ContainerSpec) Spec() (*container.Container, error) {
return nil, nil
}
func (ic *ContainerSpec) Features() *container.FeatureSpec {
return ic.fSpec
}
func (c *Container) Create(ctx context.Context, param *container.CreateParam) (interface{}, error) {
if param.Name == "" {
return nil, errors.New("Name is required")
}
if param.Image == "" {
return nil, errors.New("Image is required")
}
if param.ContainerGroupName == "" {
return nil, errors.New("ContainerGroupName is required")
}
cParam := model.CreateContainerParam{
Container: model.Container{
Name: param.Name,
Image: param.Image,
Args: param.Args,
Limits: struct {
Cpu string `json:"cpu,omitempty"`
Memory string `json:"memory,omitempty"`
}{
Cpu: param.Cpu,
Memory: param.Memory,
},
ContainerPort: struct {
Port int32 `json:"port,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 {
return nil, err
}
// 返回创建的容器信息
return cParam.Container, nil
}
func (c *Container) Delete(ctx context.Context, param *container.DeleteParam) error {
if param.Name == "" {
return errors.New("Name is required")
}
err := service.DeleteContainer(c.opt.ClientSet, &model.DeleteContainerParam{
ContainerName: param.Name,
})
if err != nil {
return err
}
return nil
}
func (c *Container) Features() *container.Features {
return nil
}
func (k *Kubernetes) Name() string {
return k.opt.platform.Name
}
func (k *Kubernetes) Type() platform.Type {
return k.opt.platform.Type
}
func (k *Kubernetes) Id() platform.Id {
return k.opt.platform.Id
}
func (c *Container) Get(ctx context.Context, param *container.GetParam) (interface{}, error) {
pod, err := service.GetContainer(c.opt.ClientSet, &model.GetContainerParam{
Name: param.Name,
})
if err != nil {
return nil, err
}
return pod, nil
}