Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kubernetes ingress support in backend #62

Merged
merged 5 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions src/backend/controllers/ingress/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package ingress

import (
"encoding/json"

"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)

type IngressController struct {
base.APIController
}

func (c *IngressController) URLMapping() {
c.Mapping("GetNames", c.GetNames)
c.Mapping("List", c.List)
c.Mapping("Create", c.Create)
c.Mapping("Get", c.Get)
c.Mapping("Update", c.Update)
c.Mapping("Delete", c.Delete)
c.Mapping("UpdateOrders", c.UpdateOrders)
}

func (c *IngressController) Prepare() {
// Check administration
c.APIController.Prepare()
// Check permission
perAction := ""
_, method := c.GetControllerAndAction()
switch method {
case "Get", "List":
perAction = models.PermissionRead
case "Create":
perAction = models.PermissionCreate
case "Update":
perAction = models.PermissionUpdate
case "Delete":
perAction = models.PermissionDelete
}
if perAction != "" {
c.CheckPermission(models.PermissionTypeStatefulset, perAction)
}
}

func (c *IngressController) GetNames() {
filters := make(map[string]interface{})
deleted := c.GetDeleteFromQuery()

filters["Deleted"] = deleted
if c.AppId != 0 {
filters["App__Id"] = c.AppId
}

services, err := models.IngressModel.GetNames(filters)
if err != nil {
logs.Error("get names error. %v, delete-status %v", err, deleted)
c.HandleError(err)
return
}

c.Success(services)
}

func (c *IngressController) List() {
param := c.BuildQueryParam()
name := c.Input().Get("name")
if name != "" {
param.Query["name__contains"] = name
}

ingrs := []models.Ingress{}
if c.AppId != 0 {
param.Query["App__Id"] = c.AppId
} else if !c.User.Admin {
param.Query["App__AppUsers__User__Id__exact"] = c.User.Id
perName := models.PermissionModel.MergeName(models.PerMissionTypeIngress, models.PermissionRead)
param.Query["App__AppUsers__Group__Permissions__Permission__Name__contains"] = perName
param.Groupby = []string{"Id"}
}

total, err := models.GetTotal(new(models.Ingress), param)
if err != nil {
logs.Error("get total count by param (%s) error. %v", param, err)
c.HandleError(err)
return
}

err = models.GetAll(new(models.Ingress), &ingrs, param)
if err != nil {
logs.Error("list by param (%s) error. %v", param, err)
c.HandleError(err)
return
}
for key, one := range ingrs {
ingrs[key].AppId = one.App.Id
}

c.Success(param.NewPage(total, ingrs))
}

func (c *IngressController) Create() {
var ingr models.Ingress
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ingr)
if err != nil {
logs.Error("get body error. %v", err)
c.AbortBadRequestFormat("Service")
}

ingr.User = c.User.Name
_, err = models.IngressModel.Add(&ingr)

if err != nil {
logs.Error("create error.%v", err.Error())
c.HandleError(err)
return
}
c.Success(ingr)
}

func (c *IngressController) Get() {
id := c.GetIDFromURL()

ingr, err := models.IngressModel.GetById(int64(id))
if err != nil {
logs.Error("get by id (%d) error.%v", id, err)
c.HandleError(err)
return
}

c.Success(ingr)
return
}

func (c *IngressController) Update() {
id := c.GetIDFromURL()
var ingr models.Ingress
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ingr)
if err != nil {
logs.Error("Invalid param body.%v", err)
c.AbortBadRequestFormat("Service")
}

ingr.Id = int64(id)
err = models.IngressModel.UpdateById(&ingr)
if err != nil {
logs.Error("update error.%v", err)
c.HandleError(err)
return
}
c.Success(ingr)
}

func (c *IngressController) UpdateOrders() {
var ingr []*models.Ingress
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ingr)
if err != nil {
logs.Error("Invalid param body.%v", err)
c.AbortBadRequestFormat("ingr")
}

err = models.IngressModel.UpdateOrders(ingr)
if err != nil {
logs.Error("update orders (%v) error.%v", ingr, err)
c.HandleError(err)
return
}
c.Success("ok!")
}

func (c *IngressController) Delete() {
id := c.GetIDFromURL()

logical := c.GetLogicalFromQuery()

err := models.IngressModel.DeleteById(int64(id), logical)
if err != nil {
logs.Error("delete %d error.%v", id, err)
c.HandleError(err)
return
}
c.Success(nil)
}
185 changes: 185 additions & 0 deletions src/backend/controllers/ingress/ingress_tpl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package ingress

import (
"encoding/json"
"fmt"

"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/util/hack"
"github.com/Qihoo360/wayne/src/backend/util/logs"
kapiv1beta1 "k8s.io/api/extensions/v1beta1"
)

type IngressTplController struct {
base.APIController
}

func (c *IngressTplController) URLMapping() {
c.Mapping("List", c.List)
c.Mapping("Create", c.Create)
c.Mapping("Get", c.Get)
c.Mapping("Update", c.Update)
c.Mapping("Delete", c.Delete)
}

func (c *IngressTplController) Prepare() {
// Check administration
c.APIController.Prepare()
// Check permission
perAction := ""
_, method := c.GetControllerAndAction()
switch method {
case "Get", "List":
perAction = models.PermissionRead
case "Create":
perAction = models.PermissionCreate
case "Update":
perAction = models.PermissionUpdate
case "Delete":
perAction = models.PermissionDelete
}
if perAction != "" {
c.CheckPermission(models.PermissionTypeStatefulset, perAction)
}
}

// @Title GetAll
// @Description get all ingressTpl
// @Param pageNo query int false "the page current no"
// @Param pageSize query int false "the page size"
// @Param name query string false "name filter"
// @Param deleted query bool false "is deleted, default list all"
// @Success 200 {object} []models.ingressTemplate success
// @router / [get]
func (c *IngressTplController) List() {
param := c.BuildQueryParam()
name := c.Input().Get("name")
if name != "" {
param.Query["name__contains"] = name
}

isOnline := c.GetIsOnlineFromQuery()

ingressId := c.Input().Get("ingressId")
if ingressId != "" {
param.Query["ingress_id"] = ingressId
}

var ingrsTpls []models.IngressTemplate
total, err := models.ListTemplate(&ingrsTpls, param, models.TableNameIngressTemplate, models.PublishTypeIngress, isOnline)
if err != nil {
logs.Error("list by param (%v) error. %v", param, err)
c.HandleError(err)
return
}
for index, tpl := range ingrsTpls {
ingrsTpls[index].IngressId = tpl.Ingress.Id
}

c.Success(param.NewPage(total, ingrsTpls))
}

// @Title Create
// @Description create ingressTpl
// @Param body body models.ingressTemplate true "The ingressTpl content"
// @Success 200 return models.ingressTemplate success
// @router / [post]
func (c *IngressTplController) Create() {
var ingrTpl models.IngressTemplate
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ingrTpl)
if err != nil {
logs.Error("get body error. %v", err)
c.AbortBadRequestFormat("ingressTemplate")
}
err = validIngressTemplate(ingrTpl.Template)
if err != nil {
logs.Error("valid template err %v", err)
c.AbortBadRequestFormat("Kubeingress")
}

ingrTpl.User = c.User.Name

_, err = models.IngressTemplateModel.Add(&ingrTpl)
if err != nil {
logs.Error("create error.%v", err.Error())
c.HandleError(err)
return
}
c.Success(ingrTpl)
}

func validIngressTemplate(ingrTplStr string) error {
ingr := kapiv1beta1.Ingress{}
err := json.Unmarshal(hack.Slice(ingrTplStr), &ingr)
if err != nil {
return fmt.Errorf("ingress template format error.%v", err.Error())
}
return nil
}

// @Title Get
// @Description find Object by id
// @Param id path int true "the id you want to get"
// @Success 200 {object} models.ingressTemplate success
// @router /:id([0-9]+) [get]
func (c *IngressTplController) Get() {
id := c.GetIDFromURL()

ingrTpl, err := models.IngressTemplateModel.GetById(id)
if err != nil {
logs.Error("get template error %v", err)
c.HandleError(err)
return
}

c.Success(ingrTpl)
}

// @Title Update
// @Description update the ingrTpl
// @Param id path int true "The id you want to update"
// @Param body body models.ingrTemplate true "The body"
// @Success 200 models.ingrTemplate success
// @router /:id([0-9]+) [put]
func (c *IngressTplController) Update() {
id := c.GetIDFromURL()
var ingrTpl models.IngressTemplate
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ingrTpl)
if err != nil {
logs.Error("Invalid param body.%v", err)
c.AbortBadRequestFormat("IngressTemplate")
}
if err = validIngressTemplate(ingrTpl.Template); err != nil {
logs.Error("valid template err %v", err)
c.AbortBadRequestFormat("Kubeingress")
}

ingrTpl.Id = int64(id)
err = models.IngressTemplateModel.UpdateById(&ingrTpl)
if err != nil {
logs.Error("update error.%v", err)
c.HandleError(err)
return
}
c.Success(ingrTpl)
}

// @Title Delete
// @Description delete the ingressTpl
// @Param id path int true "The id you want to delete"
// @Param logical query bool false "is logical deletion,default true"
// @Success 200 {string} delete success!
// @router /:id([0-9]+) [delete]
func (c *IngressTplController) Delete() {
id := c.GetIDFromURL()
logical := c.GetLogicalFromQuery()

err := models.IngressTemplateModel.DeleteById(int64(id), logical)
if err != nil {
logs.Error("delete %d error.%v", id, err)
c.HandleError(err)
return
}
c.Success(nil)
}
Loading