Skip to content

Commit

Permalink
Merge pull request #16 from KubeLilin/dev
Browse files Browse the repository at this point in the history
* 服务(添加/编辑) apply
* 服务(添加/编辑) apply ; fixed bugs for service.
* 项目部署指标统计
* 修改服务名=部署名+'-svc'
* 获取部署列表添加服务端口属性
  • Loading branch information
yoyofx authored Nov 3, 2022
2 parents 4dd9e6e + 02c1f83 commit 59b5406
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 70 deletions.
20 changes: 20 additions & 0 deletions scripts/apisix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 集群内部署APISIX
## 修改配置
在apisix.yaml中找到service配置成NodePort(默认30000),端口只要与主机端口不冲突即可。

记录下文件中 admin_key的值,用于记录网关认证。
## 安装
* kubectl apply -f ./etcd.yaml
* kubectl apply -f ./apisix.yaml
* kubectl apply -f ./apisix-dashboard.yaml

## Nginx Or Caddy 代理
使用反代服务将主机NodePort映射出来,这里反代IP即为网关的出口IP,可以绑定域名使用。

## 网关登记
PaaS数据库中,application_api_gateway表,新添加一条记录:
* access_token: {admin_key}
* admin_url:绑定的域名
* cluster_id:PaaS集群IP
* vip:内网IP
* cluster_ip:出口IP
13 changes: 6 additions & 7 deletions scripts/apisix/apisix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ data:
# cache_levels: "1:2"
allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 127.0.0.1/24
- 0.0.0.0/0
# - "::/64"
port_admin: 9180
# port_admin: 9180
# Default token when use API to call for Admin API.
# *NOTE*: Highly recommended to modify this value to protect APISIX's Admin API.
Expand Down Expand Up @@ -208,11 +208,10 @@ data:
ssl_protocols: "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3"
ssl_ciphers: "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA"
nginx_config: # config for render the template to
genarate nginx.conf
error_log: "/dev/stderr"
error_log_level: "warn" # warn,error
worker_rlimit_nofile: 20480 # the number of files a worker process can open, should be larger than worker_connections
nginx_config: # config for render the template to genarate nginx.conf
error_log: "/dev/stderr"
error_log_level: "warn" # warn,error
worker_rlimit_nofile: 20480 # the number of files a worker process can open, should be larger than worker_connections
event:
worker_connections: 10620
http:
Expand Down
2 changes: 1 addition & 1 deletion scripts/jenkins/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec:
namespace: kube-lilin
spec:
containers:
- image: jenkins/jenkins:2.328
- image: kubelilin/jenkins:v2.361.3-lts-alpine
#imagePullPolicy: Always
imagePullPolicy: IfNotPresent
name: jenkins
Expand Down
17 changes: 17 additions & 0 deletions src/api/controllers/apigateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,20 @@ func (controller *ApiGatewayController) PostCreateOrEditRouter(request *requests
}
return mvc.Success(true)
}

func (controller *ApiGatewayController) DeleteRoute(ctx *context.HttpContext) mvc.ApiResult {
id, _ := utils.StringToUInt64(ctx.Input.QueryDefault("id", "0"))
gatewayId, _ := utils.StringToUInt64(ctx.Input.QueryDefault("gatewayId", "0"))
err := controller.service.DeleteRouter(id)
if err != nil {
return mvc.FailWithMsg(false, err.Error())
}

gatewayEntity, _ := controller.service.GetById(gatewayId)
apisixProxy := networks.NewAPISIXProxy(gatewayEntity.AdminURI, gatewayEntity.AccessToken)
err = apisixProxy.DeleteRoute(utils.ToString(id))
if err != nil {
return mvc.FailWithMsg(false, err.Error())
}
return mvc.Success(true)
}
4 changes: 2 additions & 2 deletions src/api/controllers/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ func (controller DeploymentController) PostNotify(notifyReq *requests2.DeployNot

}

func (c DeploymentController) PostRollBackByReleaseRecord(ctx *context.HttpContext, execReq *requests2.ExecDeploymentRequest) mvc.ApiResult {
func (controller DeploymentController) PostRollBackByReleaseRecord(ctx *context.HttpContext, execReq *requests2.ExecDeploymentRequest) mvc.ApiResult {
userInfo := requests2.GetUserInfo(ctx)
execReq.TenantId = userInfo.TenantID
execReq.Operator = uint64(userInfo.UserId)
res, err := c.deploymentSupervisor.ExecuteDeployment(execReq)
res, err := controller.deploymentSupervisor.ExecuteDeployment(execReq)
if err == nil {
return mvc.Success(res)
}
Expand Down
28 changes: 25 additions & 3 deletions src/api/controllers/devops_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"github.com/yoyofx/yoyogo/web/mvc"
requests2 "kubelilin/api/dto/requests"
"kubelilin/domain/business/app"
"kubelilin/domain/business/devops"
"kubelilin/utils"
)

type DevopsController struct {
mvc.ApiController
devopsService *app.DevopsService
devopsService *app.DevopsService
projectService *devops.ProjectService
}

func NewDevopsController(devops *app.DevopsService) *DevopsController {
return &DevopsController{devopsService: devops}
func NewDevopsController(devops *app.DevopsService, project *devops.ProjectService) *DevopsController {
return &DevopsController{devopsService: devops, projectService: project}
}

func (controller DevopsController) PostCreateProject(ctx *context.HttpContext, request *requests2.NewProject) mvc.ApiResult {
Expand Down Expand Up @@ -67,3 +69,23 @@ func (controller DevopsController) GetAppList(ctx *context.HttpContext, request
}
return mvc.Success(res)
}

func (controller DevopsController) GetPipelineList(ctx *context.HttpContext) mvc.ApiResult {
userInfo := requests2.GetUserInfo(ctx)
projectId := utils.GetNumberOfParam[uint64](ctx, "projectId")
res, err := controller.devopsService.GetPipelineListByProjectId(projectId, userInfo.TenantID)
if err != nil {
return mvc.FailWithMsg(nil, err.Error())
}
return mvc.Success(res)
}

func (controller DevopsController) GetResourceMetrics(ctx *context.HttpContext) mvc.ApiResult {
userInfo := requests2.GetUserInfo(ctx)
projectId := utils.GetNumberOfParam[uint64](ctx, "projectId")
res, err := controller.projectService.GetResourceMetrics(userInfo.TenantID, projectId)
if err != nil {
return mvc.FailWithMsg(nil, err.Error())
}
return mvc.Success(res)
}
15 changes: 9 additions & 6 deletions src/api/controllers/service_controller.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package controllers

import (
"encoding/json"
"fmt"
"github.com/yoyofx/yoyogo/web/context"
"github.com/yoyofx/yoyogo/web/mvc"
requests2 "kubelilin/api/dto/requests"
"kubelilin/domain/business/kubernetes"
"kubelilin/utils"
)

type ServiceController struct {
Expand All @@ -27,7 +26,6 @@ func (c *ServiceController) GetServiceList(ctx *context.HttpContext) mvc.ApiResu
_ = ctx.BindWithUri(&reqParam)
userInfo := requests2.GetUserInfo(ctx)
reqParam.TenantId = userInfo.TenantID
//userInfo := requests.GetUserInfo(ctx)
list, err := c.svcSupervisor.QueryServiceList(reqParam)
if err != nil {
return mvc.FailWithMsg(nil, err.Error())
Expand All @@ -50,15 +48,20 @@ func (c *ServiceController) GetInfo(ctx *context.HttpContext) mvc.ApiResult {

func (c *ServiceController) GetNamespaceByTenant(ctx *context.HttpContext) mvc.ApiResult {
userInfo := requests2.GetUserInfo(ctx)
list := c.svcSupervisor.QueryNameSpaceByTenant(userInfo.TenantID)
clusterId := utils.GetNumberOfParam[uint64](ctx, "clusterId")
list := c.svcSupervisor.QueryNameSpaceByTenant(userInfo.TenantID, clusterId)
return mvc.Success(list)
}

func (c *ServiceController) GetNamespaceList(ctx *context.HttpContext) mvc.ApiResult {
userInfo := requests2.GetUserInfo(ctx)
clusterId := utils.GetNumberOfParam[uint64](ctx, "clusterId")
list, _ := c.svcSupervisor.QueryNamespaceList(userInfo.TenantID, clusterId)
return mvc.Success(list)
}
func (c *ServiceController) PostChangeService(ctx *context.HttpContext, svcReq *requests2.ServiceInfoReq) mvc.ApiResult {
userInfo := requests2.GetUserInfo(ctx)
svcReq.TenantId = userInfo.TenantID
marshal, _ := json.Marshal(svcReq)
fmt.Println(string(marshal))
err := c.svcSupervisor.ChangeService(svcReq)
if err != nil {
return mvc.FailWithMsg(nil, err.Error())
Expand Down
3 changes: 3 additions & 0 deletions src/api/dto/requests/service_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ type ServiceRequest struct {
ClusterId uint64 `json:"clusterId" uri:"clusterId"`
Namespace string `json:"namespace" uri:"namespace"`
ContinueStr string `json:"continueStr" uri:"continueStr"`
OnlyPAAS bool `json:"onlyPAAS" uri:"onlyPAAS"`
}

type ServiceInfoReq struct {
mvc.RequestBody
ClusterId uint64 `json:"clusterId" uri:"clusterId"`
TenantId uint64 `json:"tenantId" uri:"tenantId"`
Namespace string `json:"namespace" uri:"namespace"`
Name string `json:"name" uri:"name"`
Expand All @@ -30,5 +32,6 @@ type ServicePortInfo struct {
Name string `json:"name"`
Port intstr.IntOrString `json:"port"`
TargetPort intstr.IntOrString `json:"targetPort"`
NodePort intstr.IntOrString `json:"nodePort"`
Protocol string `json:"protocol"`
}
4 changes: 2 additions & 2 deletions src/domain/business/app/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (deployment *DeploymentService) NewOrUpdateDeployment(deployModel *requests

func (deployment *DeploymentService) CreateDeploymentStep1(deployModel *requests.DeploymentStepRequest) (error, *models.SgrTenantDeployments) {
dpModel := &models.SgrTenantDeployments{}
dpModel.ServiceName = deployModel.Name + "-svc-cluster-sgr"
dpModel.ServiceName = deployModel.Name + "-svc"
dpModel.WorkloadType = "Deployment"
dpModel.ServicePortType = "TCP"
err := copier.Copy(dpModel, deployModel)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (deployment *DeploymentService) GetDeployments(profile string, appId uint64
dataSql := strings.Builder{}
dataSql.WriteString(`SELECT d.id, d.nickname ,d.name,lev.name level, c.name as 'clusterName' ,app.name as 'appName',
d.cluster_id as 'clusterId' , n.namespace ,d.last_image as 'lastImage', 0 'running' ,
d.replicas 'expected', '0.0.0.0' as 'serviceIP', d.service_name as 'serviceName'
d.replicas 'expected', '0.0.0.0' as 'serviceIP', d.service_name as 'serviceName',d.service_Port as 'servicePort'
FROM sgr_tenant_deployments d
INNER JOIN sgr_tenant_cluster c on c.id = d.cluster_id
INNER JOIN sgr_tenant_namespace n on n.id = d.namespace_id
Expand Down
14 changes: 14 additions & 0 deletions src/domain/business/app/devops.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,17 @@ WHERE t1.status = 1 AND papp.project_id = ? `)

return page.StartPage(service.db, req.PageIndex, req.PageSize).DoScan(res, sb.String(), sqlParams...)
}

func (service *DevopsService) GetPipelineListByProjectId(projectId uint64, tenantId uint64) ([]dto.ProjectPipelines, error) {
sql := `SELECT ppl.*,app.name appName FROM devops_projects devp
INNER JOIN devops_projects_apps depa on depa.project_id = devp.id
INNER JOIN sgr_tenant_application_pipelines ppl on ppl.appid = depa.application_id
INNER JOIN sgr_tenant_application app on app.id = depa.application_id
WHERE devp.id = ? AND devp.tenant_id = ? AND ppl.status=1 `
var res []dto.ProjectPipelines
err := service.db.Raw(sql, projectId, tenantId).Find(&res).Error
if err != nil {
return nil, err
}
return res, nil
}
70 changes: 70 additions & 0 deletions src/domain/business/devops/devops_project_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package devops

import (
"gorm.io/gorm"
"kubelilin/domain/dto"
)

type ProjectService struct {
db *gorm.DB
}

func NewProjectService(db *gorm.DB) *ProjectService {
return &ProjectService{db: db}
}

func (service *ProjectService) GetResourceMetrics(tenantId uint64, projectId uint64) (dto.DevOpsProjectResourceTotals, error) {
var resourceList []dto.DevOpsProjectResource
sql := `SELECT deploy.level,SUM(deploy.replicas) replicas,SUM(container.limit_cpu * deploy.replicas) sum_cpu,SUM(container.limit_memory * deploy.replicas) sum_memory
FROM devops_projects dps
INNER JOIN devops_projects_apps dpsapp on dpsapp.project_id = dps.id
INNER JOIN sgr_tenant_deployments deploy on deploy.app_id = dpsapp.application_id
INNER JOIN sgr_tenant_deployments_containers container on container.deploy_id = deploy.id
WHERE dps.tenant_id = ? AND dps.id = ?
GROUP BY deploy.level`
err := service.db.Raw(sql, tenantId, projectId).Scan(&resourceList).Error
return getTotalDetailsInfo(resourceList), err
}

func (service *ProjectService) GetResourceMetricsByTenantId(tenantId uint64) (dto.DevOpsProjectResourceTotals, error) {
var resourceList []dto.DevOpsProjectResource
sql := `SELECT deploy.level,SUM(deploy.replicas) replicas,SUM(container.limit_cpu * deploy.replicas) sum_cpu,SUM(container.limit_memory * deploy.replicas) sum_memory
FROM sgr_tenant_deployments deploy
INNER JOIN sgr_tenant_deployments_containers container on container.deploy_id = deploy.id
WHERE deploy.tenant_id = ?
GROUP BY deploy.level`
err := service.db.Raw(sql, tenantId).Scan(&resourceList).Error
return getTotalDetailsInfo(resourceList), err
}

func getTotalDetailsInfo(items []dto.DevOpsProjectResource) dto.DevOpsProjectResourceTotals {
resourceTotal := &dto.DevOpsProjectResourceTotals{
TotalCpu: 0,
TotalMemory: 0,
DevMetrics: dto.DevOpsProjectResource{Level: "dev", Replicas: 0, SumCpu: 0, SumMemory: 0},
TestMetrics: dto.DevOpsProjectResource{Level: "test", Replicas: 0, SumCpu: 0, SumMemory: 0},
ReleaseMetrics: dto.DevOpsProjectResource{Level: "release", Replicas: 0, SumCpu: 0, SumMemory: 0},
ProdMetrics: dto.DevOpsProjectResource{Level: "prod", Replicas: 0, SumCpu: 0, SumMemory: 0},
}
for _, item := range items {
resourceTotal.TotalMemory += item.SumMemory
resourceTotal.TotalCpu += item.SumCpu

switch item.Level {
case "dev":
resourceTotal.DevMetrics = item
break
case "test":
resourceTotal.TestMetrics = item
break
case "release":
resourceTotal.ReleaseMetrics = item
break
case "prod":
resourceTotal.ProdMetrics = item
break
}
}

return *resourceTotal
}
Loading

0 comments on commit 59b5406

Please sign in to comment.