create app
This commit is contained in:
parent
ab30afbce0
commit
ef982d7a05
|
|
@ -1,5 +1,11 @@
|
|||
package k8s_manager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AppModel struct {
|
||||
Id int
|
||||
AppName string
|
||||
|
|
@ -39,3 +45,51 @@ func (a *AppModel) Delete() error {
|
|||
db.Delete(a, a.Id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *AppModel) NamePrefix() string {
|
||||
return fmt.Sprintf("%s-%s", a.UserName, a.AppName)
|
||||
}
|
||||
|
||||
func (a *AppModel) PodName() string {
|
||||
return fmt.Sprintf("%s-pod", a.NamePrefix())
|
||||
}
|
||||
|
||||
func (a *AppModel) ContainerName() string {
|
||||
return fmt.Sprintf("%s-container", a.NamePrefix())
|
||||
}
|
||||
|
||||
func (a *AppModel) ServiceName() string {
|
||||
return fmt.Sprintf("%s-service", a.NamePrefix())
|
||||
}
|
||||
|
||||
func (a *AppModel) DeploymentName() string {
|
||||
return fmt.Sprintf("%s-deployment", a.NamePrefix())
|
||||
}
|
||||
|
||||
func (a *AppModel) GetCommandOrBlack() []string {
|
||||
if strings.TrimSpace(a.Command) != "" {
|
||||
return []string{"bash", "-c", a.Command}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AppModel) GetLabel() (string, string) {
|
||||
return "cg-label", fmt.Sprintf("%s-label", a.NamePrefix())
|
||||
}
|
||||
|
||||
func (a *AppModel) GetEnv() []v1.EnvVar {
|
||||
if strings.TrimSpace(a.Env) == "" {
|
||||
return nil
|
||||
}
|
||||
sep1 := strings.Split(a.Env, " ")
|
||||
ans := make([]v1.EnvVar, len(sep1))
|
||||
for i, v := range sep1 {
|
||||
kv := strings.SplitN(v, "=", 2)
|
||||
ans[i].Name = kv[0]
|
||||
if len(kv) == 2 {
|
||||
ans[i].Value = kv[1]
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,116 +4,90 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
appv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/api/apps/v1beta1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"time"
|
||||
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ListPods() {
|
||||
type K8sClient struct {
|
||||
*kubernetes.Clientset
|
||||
Namespace string
|
||||
}
|
||||
|
||||
func InitClient() *K8sClient {
|
||||
var err error
|
||||
config, err := rest.InClusterConfig()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("InClusterConfig")
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
client, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("NewForConfig")
|
||||
}
|
||||
for {
|
||||
pods, err := client.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
return &K8sClient{client, "apps"}
|
||||
}
|
||||
|
||||
func (c *K8sClient) ListPods() {
|
||||
pods, err := c.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("ListPods")
|
||||
} else {
|
||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
||||
}
|
||||
}
|
||||
|
||||
func fake_main() {
|
||||
wg := sync.WaitGroup{}
|
||||
config, err := rest.InClusterConfig()
|
||||
func (c *K8sClient) CreateApp(app *AppModel) {
|
||||
|
||||
clientSet, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
//开启一个集群监听
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
defer wg.Done()
|
||||
watcher, err := clientSet.CoreV1().Events("default").Watch(context.Background(), metav1.ListOptions{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
eventChan := watcher.ResultChan()
|
||||
for {
|
||||
event := <-eventChan
|
||||
log.Println(event.Type)
|
||||
}
|
||||
}()
|
||||
|
||||
//一个简单的例子:创建一个 deployment,启动3个 busybox 副本
|
||||
container := corev1.Container{
|
||||
Name: "busybox",
|
||||
Image: "busybox:latest",
|
||||
Command: []string{"top"}, //这里直接执行top命令来避免容器退出
|
||||
}
|
||||
podSpec := corev1.PodSpec{
|
||||
Containers: []corev1.Container{container},
|
||||
}
|
||||
podTemplateSpec := corev1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"dev": "busybox"}},
|
||||
Spec: podSpec,
|
||||
}
|
||||
spec := appv1.DeploymentSpec{
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchLabels: map[string]string{
|
||||
"dev": "busybox",
|
||||
var rep int32 = 1
|
||||
labelKey, labelValue := app.GetLabel()
|
||||
labels := map[string]string{labelKey: labelValue}
|
||||
deployment := &v1beta1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app.DeploymentName(),
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: v1beta1.DeploymentSpec{
|
||||
Replicas: &rep,
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchLabels: labels,
|
||||
},
|
||||
Template: v1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{{
|
||||
Name: app.ContainerName(),
|
||||
Image: app.Image,
|
||||
Command: app.GetCommandOrBlack(),
|
||||
Env: app.GetEnv(),
|
||||
VolumeMounts: nil,
|
||||
}},
|
||||
},
|
||||
},
|
||||
},
|
||||
Replicas: Int32Value(3),
|
||||
Template: podTemplateSpec,
|
||||
}
|
||||
deployment := appv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "busybox-test",
|
||||
},
|
||||
Spec: spec,
|
||||
}
|
||||
resultDeployment, err := clientSet.AppsV1().Deployments("default").Get(context.Background(), "busybox-test", metav1.GetOptions{})
|
||||
deployment, err := c.AppsV1beta1().Deployments(c.Namespace).Create(context.Background(), deployment, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "not found") {
|
||||
resultDeployment, err = clientSet.AppsV1().Deployments("default").Create(context.Background(), &deployment, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
} else {
|
||||
resultDeployment, err = clientSet.AppsV1().Deployments("default").Update(context.Background(), &deployment, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.WithError(err).Error("Create Deployment error.")
|
||||
}
|
||||
|
||||
log.Println(resultDeployment.Name)
|
||||
wg.Wait()
|
||||
}
|
||||
service := &v1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app.ServiceName(),
|
||||
},
|
||||
Spec: v1.ServiceSpec{
|
||||
Type: v1.ServiceTypeClusterIP,
|
||||
Selector: labels,
|
||||
},
|
||||
}
|
||||
|
||||
func Int32Value(a int32) *int32 {
|
||||
return &a
|
||||
service, err = c.CoreV1().Services(c.Namespace).Create(context.Background(), service, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Create Service ")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue