44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
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
|
|
|
|
}
|