Skip to content

Commit

Permalink
docs: crab 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ipv4sec committed Dec 18, 2021
1 parent b851292 commit 75cd4a3
Show file tree
Hide file tree
Showing 19 changed files with 2,746 additions and 536 deletions.
84 changes: 62 additions & 22 deletions app/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"bytes"
"context"
"crab/aam/v1alpha1"
"crab/cluster"
Expand All @@ -13,7 +14,9 @@ import (
"github.com/blang/semver/v4"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"strconv"
Expand Down Expand Up @@ -116,7 +119,7 @@ func GetAppHandlerFunc(c *gin.Context) {

v := map[string] interface{}{}

cronJob, err := cluster.Client.Clientset.BatchV1().CronJobs(id).List(context.Background(), metav1.ListOptions{})
cronJob, err := cluster.Client.Clientset.BatchV1beta1().CronJobs(id).List(context.Background(), metav1.ListOptions{})
if err == nil {
v["cronJob"] = cronJob.Items
}
Expand Down Expand Up @@ -236,8 +239,16 @@ func PostAppHandlerFunc(c *gin.Context) {
if err != nil {
klog.Errorln("序列化依赖字段错误:", err.Error())
}
id := fmt.Sprintf("ins%v", time.Now().Unix())
island, err := cluster.Client.Clientset.CoreV1().ConfigMaps("island-system").
Get(context.Background(), "island-info", metav1.GetOptions{})
if err != nil {
klog.Errorln("获取根域失败", err.Error())
c.JSON(200, utils.ErrorResponse(utils.ErrClusterInternalServer, "获取根域失败"))
return
}
v, _ := island.Data["root-domain"]

id := fmt.Sprintf("ins%v", time.Now().Unix())
app := App{
ID: id ,

Expand All @@ -247,7 +258,7 @@ func PostAppHandlerFunc(c *gin.Context) {
Dependencies: string(dependenciesBytes),

Manifest: string(bytes),
Entry: "", // TODO
Entry: fmt.Sprintf("%s.%s", id, v),

Additional: "",
Parameters: "",
Expand Down Expand Up @@ -336,15 +347,6 @@ func PutAppHandlerFunc(c *gin.Context) {
return
}
if param.Status == 1 {
island, err := cluster.Client.Clientset.CoreV1().ConfigMaps("island-system").
Get(context.Background(), "island-info", metav1.GetOptions{})
if err != nil {
klog.Errorln("获取根域失败", err.Error())
c.JSON(200, utils.ErrorResponse(utils.ErrClusterInternalServer, "获取根域失败"))
return
}
v, _ := island.Data["root-domain"]

for i := 0; i < len(param.Dependencies); i++ {
if param.Dependencies[i].ID != "" {
var a App
Expand All @@ -360,21 +362,15 @@ func PutAppHandlerFunc(c *gin.Context) {
continue
}
for j := 0; j < len(manifest.Spec.Workloads); j++ {
if utils.ContainsTrait(manifest.Spec.Workloads[j].Traits, "globalsphare.com/v1alpha1/trait/ingress") {
if utils.ContainsTrait(manifest.Spec.Workloads[j].Traits, "ingress") {
param.Dependencies[i].EntryService = manifest.Spec.Workloads[j].Name
}
}
}
}

mirror, _ := island.Data["mirror"]
savedMirrorPath := "/usr/local/workloads/"
err = utils.InitRepo(savedMirrorPath, mirror)
if err != nil {
klog.Errorln("更新工作负载错误:", err.Error())
}
val, err := provider.Yaml(app.Manifest, app.ID, v, param.Configurations,
provider.ConvertToDependency(param.Dependencies), savedMirrorPath)
val, err := provider.Yaml(app.Manifest, app.ID, app.Entry, param.Configurations,
provider.ConvertToDependency(param.Dependencies))
if err != nil {
klog.Errorln("连接到翻译器错误:", err.Error())
c.JSON(200, utils.ErrorResponse(utils.ErrInternalServer, "连接到翻译器错误"))
Expand All @@ -392,7 +388,7 @@ func PutAppHandlerFunc(c *gin.Context) {
}

err = db.Client.Model(App{}).Where("pk = ?", app.PK).Updates(map[string]interface{}{
"status": 1, "deployment": val, "parameters": string(parameters), "additional": additional}).Error
"deployment": val, "parameters": string(parameters), "additional": additional}).Error
if err != nil {
klog.Errorln("数据库更新错误:", err.Error())
c.JSON(200, utils.ErrorResponse(utils.ErrDatabaseInternalServer, "更新状态错误"))
Expand Down Expand Up @@ -449,3 +445,47 @@ func DeleteAppHandlerFunc(c *gin.Context) {
klog.Infoln("执行命令结果:", output)
c.JSON(200, utils.SuccessResponse("删除完成"))
}

func GetPodLogsHandlerFunc(c *gin.Context) {
id := c.Param("id")
pods, err := cluster.Client.Clientset.CoreV1().Pods(id).List(context.Background(), metav1.ListOptions{})
if err != nil {

}
type Logs struct {
Name string `json:"name"`
Value string `json:"value"`
}
var result []Logs
for i := 0; i < len(pods.Items); i++ {
logs, err := GetPodLogs(id, pods.Items[i].Name)
if err != nil {
klog.Errorln("")
continue
}
result = append(result, Logs{
Name: pods.Items[i].Name,
Value: logs,
})
}
c.JSON(200, utils.SuccessResponse(result))
}

func GetPodLogs(ns, name string) (string, error) {
req := cluster.Client.Clientset.CoreV1().Pods(ns).GetLogs(name, &v1.PodLogOptions{})
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 5)
defer cancel()
podLogs, err := req.Stream(ctx)
if err != nil {
return "", err
}
defer podLogs.Close()

buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return "", err
}
return buf.String(), nil
}

46 changes: 0 additions & 46 deletions assets/island/1.island-cache.yaml

This file was deleted.

3 changes: 2 additions & 1 deletion assets/island/2.island-db.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ data:
`dependencies` longtext COLLATE utf8mb4_bin NOT NULL COMMENT '应用依赖',
`manifest` longtext COLLATE utf8mb4_bin NOT NULL COMMENT '应用描述',
`entry` varchar(128) COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '域名',
`parameters` blob NOT NULL COMMENT '配置参数',
`parameters` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '配置参数',
`additional` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '依赖配置',
`deployment` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '应用部署',
`created_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
`updated_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间',
Expand Down
46 changes: 0 additions & 46 deletions assets/island/5.island-scheduler.yaml

This file was deleted.

3 changes: 2 additions & 1 deletion cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GetResourceHandlerFunc(c *gin.Context) {
var err error
switch resourceType {
case "cronJob":
v, err = Client.Clientset.BatchV1().CronJobs(namespace).Get(context.Background(), resourceName, metav1.GetOptions{})
v, err = Client.Clientset.BatchV1beta1().CronJobs(namespace).Get(context.Background(), resourceName, metav1.GetOptions{})
case "daemonSet":
v, err = Client.Clientset.AppsV1().DaemonSets(namespace).Get(context.Background(), resourceName, metav1.GetOptions{})
case "deployment":
Expand Down Expand Up @@ -53,3 +53,4 @@ func GetResourceHandlerFunc(c *gin.Context) {
}
c.JSON(200, utils.SuccessResponse(v))
}

24 changes: 8 additions & 16 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"crab/db"
"crab/deployment"
"crab/domain"
"crab/mirror"
"crab/tool"
"crab/trait"
"crab/user"
Expand Down Expand Up @@ -45,35 +44,30 @@ func main() {
panic(err)
}

//klog.Infoln("开始集群认证")
//err = cluster.Init()
//if err != nil {
// panic(fmt.Errorf("获取集群认证失败: %w", err))
//}
//klog.Infoln("集群认证成功")
klog.Infoln("开始集群认证")
err = cluster.Init()
if err != nil {
panic(fmt.Errorf("获取集群认证失败: %w", err))
}

klog.Infoln("开始提供服务")
gin.SetMode(gin.ReleaseMode)
routers := gin.Default()
routers.GET("/", func(c *gin.Context) {
c.String(200, "crab")
})

routers.GET("/user/:username", user.GetUserHandlerFunc)
routers.PUT("/user/:username", user.PutUserHandlerFunc)

routers.POST("/app", app.PostAppHandlerFunc)
routers.GET("/app", app.GetAppsHandlerFunc)
routers.GET("/app/:id", app.GetAppHandlerFunc) // TODO
routers.GET("/app/:id", app.GetAppHandlerFunc)
routers.PUT("/app/:id", app.PutAppHandlerFunc)
routers.DELETE("/app/:id", app.DeleteAppHandlerFunc)

routers.GET("/app/:id/logs", app.GetPodLogsHandlerFunc)

routers.GET("/cluster/domain", domain.GetDomainHandlerFunc)
routers.PUT("/cluster/domain", domain.PutDomainHandlerFunc)

routers.GET("/cluster/mirror", mirror.GetMirrorHandlerFunc)
routers.PUT("/cluster/mirror", mirror.PutMirrorHandlerFunc)

routers.PUT("/deployment/:id", deployment.PutDeploymentHandlerFunc)

routers.POST("/trait", trait.PostTraitHandlerFunc)
Expand All @@ -100,8 +94,6 @@ func main() {

routers.GET("/resource/:namespace/:resourceType/:resourceName", cluster.GetResourceHandlerFunc)



err = routers.Run("0.0.0.0:3000")
if err != nil {
panic(fmt.Errorf("监听端口失败: %w", err))
Expand Down
7 changes: 1 addition & 6 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@ mysql:
port: 3306
username: root
password: 123456
database: crab

redis:
host: island-cache
port: 6379
password:
database: crab
Loading

0 comments on commit 75cd4a3

Please sign in to comment.