Skip to content

Commit

Permalink
Merge pull request #212 from kthcloud/dev
Browse files Browse the repository at this point in the history
add missing features for vm http proxy, and fix bug with vm stuck in being created
  • Loading branch information
saffronjam authored Oct 17, 2023
2 parents b555e80 + d88fd87 commit 97e0ab4
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 60 deletions.
39 changes: 21 additions & 18 deletions models/dto/body/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import "time"

type VmHttpProxy struct {
Name string `json:"name" binding:"required,rfc1035,min=3,max=30"`
CustomDomain *string `json:"customDomain,omitempty" binding:"omitempty,domain_name,custom_domain,min=1,max=253"`
CustomDomain *string `json:"customDomain,omitempty" binding:"omitempty,domain_name,min=1,max=253"`

URL *string `json:"url,omitempty" `
CustomDomainURL *string `json:"customDomainUrl,omitempty" `
}

type Port struct {
type PortRead struct {
Name string `json:"name,omitempty" binding:"required,min=1,max=100"`
Port int `json:"port,omitempty" binding:"required,min=1,max=65535"`
ExternalPort int `json:"externalPort,omitempty"`
Expand All @@ -34,31 +37,31 @@ type VmRead struct {
OwnerID string `json:"ownerId"`
Zone string `json:"zone"`

Specs Specs `json:"specs,omitempty"`
Ports []Port `json:"ports"`
GPU *VmGpu `json:"gpu,omitempty"`
SshPublicKey string `json:"sshPublicKey"`
Specs Specs `json:"specs,omitempty"`
Ports []PortRead `json:"ports"`
GPU *VmGpu `json:"gpu,omitempty"`
SshPublicKey string `json:"sshPublicKey"`

Status string `json:"status"`
ConnectionString *string `json:"connectionString,omitempty"`
}

type VmCreate struct {
Name string `json:"name" binding:"required,rfc1035,min=3,max=30"`
SshPublicKey string `json:"sshPublicKey" binding:"required,ssh_public_key"`
Ports []Port `json:"ports" binding:"omitempty,port_list_names,port_list_numbers,port_list_http_proxies,min=0,max=100,dive"`
CpuCores int `json:"cpuCores" binding:"required,min=2"`
RAM int `json:"ram" binding:"required,min=1"`
DiskSize int `json:"diskSize" binding:"required,min=20"`
Zone *string `json:"zone" binding:"omitempty"`
Name string `json:"name" binding:"required,rfc1035,min=3,max=30"`
SshPublicKey string `json:"sshPublicKey" binding:"required,ssh_public_key"`
Ports []PortRead `json:"ports" binding:"omitempty,port_list_names,port_list_numbers,port_list_http_proxies,min=0,max=100,dive"`
CpuCores int `json:"cpuCores" binding:"required,min=2"`
RAM int `json:"ram" binding:"required,min=1"`
DiskSize int `json:"diskSize" binding:"required,min=20"`
Zone *string `json:"zone" binding:"omitempty"`
}

type VmUpdate struct {
SnapshotID *string `json:"snapshotId" binding:"omitempty,uuid4"`
GpuID *string `json:"gpuId" binding:"omitempty,min=0,max=100"`
Ports *[]Port `json:"ports" binding:"omitempty,port_list_names,port_list_numbers,port_list_http_proxies,min=0,max=1000,dive"`
CpuCores *int `json:"cpuCores" binding:"omitempty,min=1"`
RAM *int `json:"ram" binding:"omitempty,min=1"`
SnapshotID *string `json:"snapshotId" binding:"omitempty,uuid4"`
GpuID *string `json:"gpuId" binding:"omitempty,min=0,max=100"`
Ports *[]PortRead `json:"ports" binding:"omitempty,port_list_names,port_list_numbers,port_list_http_proxies,min=0,max=1000,dive"`
CpuCores *int `json:"cpuCores" binding:"omitempty,min=1"`
RAM *int `json:"ram" binding:"omitempty,min=1"`
}

type VmCreated struct {
Expand Down
50 changes: 46 additions & 4 deletions models/sys/vm/converters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package vm

import "go-deploy/models/dto/body"
import (
"go-deploy/models/dto/body"
"go-deploy/service"
)

func (vm *VM) ToDTO(status string, connectionString *string, gpu *body.GpuRead, externalPortMapper map[string]int) body.VmRead {

Expand All @@ -14,7 +17,7 @@ func (vm *VM) ToDTO(status string, connectionString *string, gpu *body.GpuRead,
}
}

ports := make([]body.Port, 0)
ports := make([]body.PortRead, 0)
if vm.Ports != nil && externalPortMapper != nil {
for _, port := range vm.Ports {
if port.Name == "__ssh" {
Expand All @@ -26,11 +29,38 @@ func (vm *VM) ToDTO(status string, connectionString *string, gpu *body.GpuRead,
continue
}

ports = append(ports, body.Port{
var url *string
if ingress := vm.Subsystems.K8s.GetIngress(vm.Name + "-" + port.Name); service.Created(ingress) {
if len(ingress.Hosts) > 0 {
urlStr := "https://" + ingress.Hosts[0]
url = &urlStr
}
}

var customDomainUrl *string
if ingress := vm.Subsystems.K8s.GetIngress(vm.Name + "-" + port.Name + "-custom-domain"); service.Created(ingress) {
if len(ingress.Hosts) > 0 {
urlStr := "https://" + ingress.Hosts[0]
customDomainUrl = &urlStr
}
}

var httpProxy *body.VmHttpProxy
if port.HttpProxy != nil {
httpProxy = &body.VmHttpProxy{
Name: port.HttpProxy.Name,
CustomDomain: port.HttpProxy.CustomDomain,
URL: url,
CustomDomainURL: customDomainUrl,
}
}

ports = append(ports, body.PortRead{
Name: port.Name,
Port: port.Port,
ExternalPort: externalPort,
Protocol: port.Protocol,
HttpProxy: httpProxy,
})
}
}
Expand Down Expand Up @@ -67,6 +97,12 @@ func (p *CreateParams) FromDTO(dto *body.VmCreate, fallbackZone *string, deploym

p.Ports = append(p.Ports, fromDtoPort(&port))
}
p.Ports = append(p.Ports, Port{
Name: "__ssh",
Port: 22,
Protocol: "tcp",
})

p.CpuCores = dto.CpuCores
p.RAM = dto.RAM
p.DiskSize = dto.DiskSize
Expand Down Expand Up @@ -95,6 +131,12 @@ func (p *UpdateParams) FromDTO(dto *body.VmUpdate) {

ports = append(ports, fromDtoPort(&port))
}
ports = append(ports, Port{
Name: "__ssh",
Port: 22,
Protocol: "tcp",
})

p.Ports = &ports
} else {
p.Ports = nil
Expand All @@ -103,7 +145,7 @@ func (p *UpdateParams) FromDTO(dto *body.VmUpdate) {
p.RAM = dto.RAM
}

func fromDtoPort(port *body.Port) Port {
func fromDtoPort(port *body.PortRead) Port {
var httpProxy *PortHttpProxy
if port.HttpProxy != nil {
httpProxy = &PortHttpProxy{
Expand Down
74 changes: 63 additions & 11 deletions pkg/workers/confirm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go-deploy/models/sys/deployment"
"go-deploy/models/sys/vm"
"go-deploy/service"
)

func appCreatedK8s(deployment *deployment.Deployment, app *deployment.App) bool {
Expand All @@ -27,8 +28,8 @@ func appCreatedK8s(deployment *deployment.Deployment, app *deployment.App) bool
}

serviceCreated := false
for mapName, service := range deployment.Subsystems.K8s.ServiceMap {
if service.Created() && mapName == deployment.Name {
for mapName, k8sService := range deployment.Subsystems.K8s.ServiceMap {
if k8sService.Created() && mapName == deployment.Name {
serviceCreated = true
}
}
Expand Down Expand Up @@ -64,8 +65,8 @@ func appDeletedK8s(deployment *deployment.Deployment, app *deployment.App) bool
}

serviceDeleted := true
for mapName, service := range deployment.Subsystems.K8s.ServiceMap {
if service.Created() && mapName == deployment.Name {
for mapName, k8sService := range deployment.Subsystems.K8s.ServiceMap {
if k8sService.Created() && mapName == deployment.Name {
serviceDeleted = false
}
}
Expand Down Expand Up @@ -120,10 +121,10 @@ func harborCreated(deployment *deployment.Deployment) (bool, error) {
return true, nil
}

return harbor.Project.ID != 0 &&
harbor.Robot.ID != 0 &&
harbor.Repository.ID != 0 &&
harbor.Webhook.ID != 0, nil
return harbor.Project.Created() &&
harbor.Robot.Created() &&
harbor.Repository.Created() &&
harbor.Webhook.Created(), nil
}

func harborDeleted(deployment *deployment.Deployment) (bool, error) {
Expand All @@ -148,7 +149,7 @@ func gitHubCreated(deployment *deployment.Deployment) (bool, error) {
return true, nil
}

return github.Webhook.ID != 0, nil
return github.Webhook.Created(), nil
}

func gitHubDeleted(deployment *deployment.Deployment) (bool, error) {
Expand All @@ -165,9 +166,12 @@ func gitHubDeleted(deployment *deployment.Deployment) (bool, error) {
func csCreated(vm *vm.VM) (bool, error) {
cs := &vm.Subsystems.CS

_, hasSshRule := cs.PortForwardingRuleMap["__ssh"]
sshRule, ok := cs.PortForwardingRuleMap["__ssh"]
if !ok {
return false, nil
}

return cs.VM.ID != "" && hasSshRule, nil
return cs.VM.Created() && sshRule.Created(), nil
}

func csDeleted(vm *vm.VM) (bool, error) {
Expand All @@ -182,6 +186,54 @@ func csDeleted(vm *vm.VM) (bool, error) {
return cs.VM.ID == "", nil
}

func k8sCreatedVM(vm *vm.VM) (bool, error) {
k8s := &vm.Subsystems.K8s

for _, port := range vm.Ports {
if port.Name == "__ssh" {
continue
}

resourceName := vm.Name + "-" + port.Name

if service.NotCreated(k8s.GetDeployment(resourceName)) {
return false, nil
}

if service.NotCreated(k8s.GetService(resourceName)) {
return false, nil
}

if service.NotCreated(k8s.GetIngress(resourceName)) {
return false, nil
}
}

if len(vm.Ports) > 1 && !k8s.Namespace.Created() {
return false, nil
}

return true, nil
}

func k8sDeletedVM(vm *vm.VM) (bool, error) {
k8s := &vm.Subsystems.K8s

if len(k8s.DeploymentMap) > 0 {
return false, nil
}

if len(k8s.ServiceMap) > 0 {
return false, nil
}

if len(k8s.IngressMap) > 0 {
return false, nil
}

return k8s.Namespace.ID == "", nil
}

func gpuCleared(vm *vm.VM) (bool, error) {
return vm.GpuID == "", nil
}
2 changes: 2 additions & 0 deletions pkg/workers/confirm/subsystems.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ func getDeploymentDeletedConfirmers() []func(*deployment.Deployment) (bool, erro
func getVmCreatedConfirmers() []func(*vm.VM) (bool, error) {
return []func(*vm.VM) (bool, error){
csCreated,
k8sCreatedVM,
}
}

func getVmDeletedConfirmers() []func(*vm.VM) (bool, error) {
return []func(*vm.VM) (bool, error){
csDeleted,
gpuCleared,
k8sDeletedVM,
}
}

Expand Down
6 changes: 3 additions & 3 deletions routers/api/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func EnvList(fl validator.FieldLevel) bool {
}

func PortListNames(fl validator.FieldLevel) bool {
portList, ok := fl.Field().Interface().([]body.Port)
portList, ok := fl.Field().Interface().([]body.PortRead)
if !ok {
return false
}
Expand All @@ -100,7 +100,7 @@ func PortListNames(fl validator.FieldLevel) bool {
}

func PortListNumbers(fl validator.FieldLevel) bool {
portList, ok := fl.Field().Interface().([]body.Port)
portList, ok := fl.Field().Interface().([]body.PortRead)
if !ok {
return false
}
Expand All @@ -117,7 +117,7 @@ func PortListNumbers(fl validator.FieldLevel) bool {
}

func PortListHttpProxies(fl validator.FieldLevel) bool {
portList, ok := fl.Field().Interface().([]body.Port)
portList, ok := fl.Field().Interface().([]body.PortRead)
if !ok {
return false
}
Expand Down
27 changes: 8 additions & 19 deletions service/vm_service/vm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func Create(id, ownerID string, vmCreate *body.VmCreate) error {
params := &vmModel.CreateParams{}
params.FromDTO(vmCreate, &fallback, &deploymentZone)

if params.Ports != nil {
addDeploySshToPortMap(&params.Ports)
}

created, err := vmModel.New().Create(id, ownerID, conf.Env.Manager, params)
if err != nil {
return makeError(err)
Expand All @@ -46,9 +42,14 @@ func Create(id, ownerID string, vmCreate *body.VmCreate) error {
return makeError(err)
}

err = k8s_service.Create(id, params)
if err != nil {
return makeError(err)
// there is always at least one port: __ssh
if len(params.Ports) > 1 {
err = k8s_service.Create(id, params)
if err != nil {
return makeError(err)
}
} else {
log.Println("skipping k8s setup for vm", id, "since it has no ports")
}

return nil
Expand All @@ -68,10 +69,6 @@ func Update(vmID string, dtoVmUpdate *body.VmUpdate) error {
return makeError(err)
}
} else {
if vmUpdate.Ports != nil {
addDeploySshToPortMap(vmUpdate.Ports)
}

err := vmModel.New().UpdateWithParamsByID(vmID, vmUpdate)
if err != nil {
return makeError(err)
Expand Down Expand Up @@ -505,14 +502,6 @@ func GetExternalPortMapper(vmID string) (map[string]int, error) {
return mapper, nil
}

func addDeploySshToPortMap(portMap *[]vmModel.Port) {
*portMap = append(*portMap, vmModel.Port{
Port: 22,
Name: "__ssh",
Protocol: "tcp",
})
}

func removeDeploySshFromPortMap(portMap *[]vmModel.Port) {
for i, port := range *portMap {
if (port.Port == 22 || port.Name == "__ssh") && port.Protocol == "tcp" {
Expand Down
Loading

0 comments on commit 97e0ab4

Please sign in to comment.