create app
This commit is contained in:
parent
ab30afbce0
commit
ef982d7a05
|
|
@ -1,5 +1,11 @@
|
||||||
package k8s_manager
|
package k8s_manager
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type AppModel struct {
|
type AppModel struct {
|
||||||
Id int
|
Id int
|
||||||
AppName string
|
AppName string
|
||||||
|
|
@ -39,3 +45,51 @@ func (a *AppModel) Delete() error {
|
||||||
db.Delete(a, a.Id)
|
db.Delete(a, a.Id)
|
||||||
return nil
|
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"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
appv1 "k8s.io/api/apps/v1"
|
"k8s.io/api/apps/v1beta1"
|
||||||
corev1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"time"
|
|
||||||
|
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListPods() {
|
type K8sClient struct {
|
||||||
|
*kubernetes.Clientset
|
||||||
|
Namespace string
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitClient() *K8sClient {
|
||||||
var err error
|
var err error
|
||||||
config, err := rest.InClusterConfig()
|
config, err := rest.InClusterConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error("InClusterConfig")
|
log.WithError(err).Error("InClusterConfig")
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := kubernetes.NewForConfig(config)
|
client, err := kubernetes.NewForConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error("NewForConfig")
|
log.WithError(err).Error("NewForConfig")
|
||||||
}
|
}
|
||||||
for {
|
return &K8sClient{client, "apps"}
|
||||||
pods, err := client.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
|
}
|
||||||
|
|
||||||
|
func (c *K8sClient) ListPods() {
|
||||||
|
pods, err := c.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.WithError(err).Error("ListPods")
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
||||||
}
|
}
|
||||||
time.Sleep(10 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fake_main() {
|
func (c *K8sClient) CreateApp(app *AppModel) {
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
config, err := rest.InClusterConfig()
|
|
||||||
|
|
||||||
clientSet, err := kubernetes.NewForConfig(config)
|
var rep int32 = 1
|
||||||
if err != nil {
|
labelKey, labelValue := app.GetLabel()
|
||||||
log.Println(err)
|
labels := map[string]string{labelKey: labelValue}
|
||||||
return
|
deployment := &v1beta1.Deployment{
|
||||||
}
|
|
||||||
|
|
||||||
//开启一个集群监听
|
|
||||||
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",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Replicas: Int32Value(3),
|
|
||||||
Template: podTemplateSpec,
|
|
||||||
}
|
|
||||||
deployment := appv1.Deployment{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "busybox-test",
|
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,
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
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 err != nil {
|
||||||
if strings.Contains(err.Error(), "not found") {
|
log.WithError(err).Error("Create Deployment error.")
|
||||||
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.Println(resultDeployment.Name)
|
service := &v1.Service{
|
||||||
wg.Wait()
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
}
|
Name: app.ServiceName(),
|
||||||
|
},
|
||||||
|
Spec: v1.ServiceSpec{
|
||||||
|
Type: v1.ServiceTypeClusterIP,
|
||||||
|
Selector: labels,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func Int32Value(a int32) *int32 {
|
service, err = c.CoreV1().Services(c.Namespace).Create(context.Background(), service, metav1.CreateOptions{})
|
||||||
return &a
|
if err != nil {
|
||||||
|
log.WithError(err).Error("Create Service ")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue