Skip to content

Commit

Permalink
feat: helm install cluster (#78)
Browse files Browse the repository at this point in the history
Signed-off-by: 张启航 <[email protected]>
  • Loading branch information
ZhangSetSail authored Feb 22, 2024
1 parent eced501 commit a3a9b4b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 322 deletions.
5 changes: 5 additions & 0 deletions api/cloud-adaptor/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,8 @@ func (a ByRainbondComponentPodPhase) phaseScore(i int) int {
type RainbondComponentEvent struct {
corev1.Event
}

type AddHelmInstallCluster struct {
Token string `json:"token"`
APIHost string `json:"api_host"`
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.6 // indirect
Expand Down Expand Up @@ -190,7 +190,6 @@ require (
google.golang.org/genproto v0.0.0-20220714211235-042d03aeabc9 // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/gorp.v1 v1.7.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
328 changes: 8 additions & 320 deletions go.sum

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions internal/handler/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package handler

import (
"encoding/json"
"goodrain.com/cloud-adaptor/internal/model"
"goodrain.com/cloud-adaptor/pkg/util/ssh"
"io/ioutil"
"strings"
Expand Down Expand Up @@ -784,3 +786,68 @@ func (e *ClusterHandler) listPodEvents(c *gin.Context) {
components, err := e.cluster.ListPodEvents(c.Request.Context(), eid, clusterID, providerName, c.Param("podName"))
ginutil.JSONv2(c, components, err)
}

func (e *ClusterHandler) GetInstallHelmRegionEvent(ctx *gin.Context) {
eid := ctx.Param("eid")
events, err := e.cluster.TaskEventRepo.ListEvent(eid, "helm_install_region")
if err != nil {
ginutil.JSON(ctx, nil, err)
return
}
type Ret struct {
CreatStatus bool `json:"create_status"`
v1.AddHelmInstallCluster
}
ret := Ret{CreatStatus: false}
if events != nil && len(events) > 0 {
ret.CreatStatus = true
var ren v1.AddHelmInstallCluster
err = json.Unmarshal([]byte(events[0].Message), &ren)
if err != nil {
ginutil.JSON(ctx, nil, err)
return
}
ret.Token = ren.Token
ret.APIHost = ren.APIHost
ginutil.JSON(ctx, ret, nil)
return
}
ginutil.JSON(ctx, ret, err)
}

func (e *ClusterHandler) InitInstallHelmRegionEvent(ctx *gin.Context) {
eid := ctx.Param("eid")
var req v1.AddHelmInstallCluster
if err := ctx.ShouldBindJSON(&req); err != nil {
logrus.Errorf("bind update init status failure %s", err.Error())
ginutil.JSON(ctx, nil, bcode.BadRequest)
return
}
message, err := json.Marshal(req)
if err != nil {
ginutil.JSON(ctx, nil, err)
return
}
ent := &model.TaskEvent{
TaskID: "helm_install_region",
EnterpriseID: eid,
Message: string(message),
}
type Ret struct {
CreateStatus bool `json:"create_status"`
}
ret := Ret{CreateStatus: true}
err = e.cluster.TaskEventRepo.CreateEvent(ent)
if err != nil {
ret.CreateStatus = false
ginutil.JSON(ctx, ret, err)
return
}
ginutil.JSON(ctx, ret, err)
}

func (e *ClusterHandler) DeleteInstallHelmRegionEvent(ctx *gin.Context) {
eid := ctx.Param("eid")
err := e.cluster.TaskEventRepo.DeleteEvent(eid, "helm_install_region")
ginutil.JSON(ctx, nil, err)
}
5 changes: 5 additions & 0 deletions internal/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func (r *Router) NewRouter() *gin.Engine {
entv1.GET("/accesskey", r.cluster.GetAccessKey)
entv1.GET("/last-ck-task", r.cluster.GetLastAddKubernetesClusterTask)
entv1.GET("/ck-task/:taskID", r.cluster.GetAddKubernetesClusterTask)

entv1.GET("/tasks/helm_region_install", r.cluster.GetInstallHelmRegionEvent)
entv1.POST("/tasks/helm_region_install", r.cluster.InitInstallHelmRegionEvent)
entv1.DELETE("/tasks/helm_region_install", r.cluster.DeleteInstallHelmRegionEvent)

entv1.GET("/tasks/:taskID/events", r.cluster.GetTaskEventList)
entv1.GET("/init-task/:clusterID", r.cluster.GetInitRainbondTask)
entv1.GET("/init-tasks", r.cluster.GetRunningInitRainbondTask)
Expand Down
15 changes: 15 additions & 0 deletions internal/repo/cloud_task_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func (t *TaskEventRepo) Transaction(tx *gorm.DB) TaskEventRepository {
return &TaskEventRepo{DB: tx}
}

func (t *TaskEventRepo) CreateEvent(te *model.TaskEvent) error {
if err := t.DB.Debug().Save(te).Error; err != nil {
return err
}
return nil
}

//Create create an event
func (t *TaskEventRepo) Create(te *model.TaskEvent) error {
if len(te.Message) > 512 {
Expand Down Expand Up @@ -78,6 +85,14 @@ func (t *TaskEventRepo) ListEvent(eid, taskID string) ([]*model.TaskEvent, error
return list, nil
}

//DeleteEvent delete task events
func (t *TaskEventRepo) DeleteEvent(eid, taskID string) error {
if err := t.DB.Where("eid = ? and task_id=?", eid, taskID).Delete(&model.TaskEvent{}).Error; err != nil {
return err
}
return nil
}

// UpdateStatusInBatch -
func (t *TaskEventRepo) UpdateStatusInBatch(eventIDs []string, status string) error {
if err := t.DB.Where("event_id in (?)", eventIDs).Updates(&model.TaskEvent{Status: status}).Error; err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/repo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ type UpdateKubernetesTaskRepository interface {
type TaskEventRepository interface {
Transaction(tx *gorm.DB) TaskEventRepository
Create(ent *model.TaskEvent) error
CreateEvent(te *model.TaskEvent) error
ListEvent(eid, taskID string) ([]*model.TaskEvent, error)
UpdateStatusInBatch(eventIDs []string, status string) error
DeleteEvent(eid, taskID string) error
}

//RainbondClusterConfigRepository -
Expand Down

0 comments on commit a3a9b4b

Please sign in to comment.