Skip to content

Commit

Permalink
Merge pull request #211 from kthcloud/dev
Browse files Browse the repository at this point in the history
implement custom domains for vm http proxy, fix bad naming, and remove migrations
  • Loading branch information
saffronjam authored Oct 17, 2023
2 parents a0274d2 + 0017a56 commit b555e80
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 120 deletions.
2 changes: 1 addition & 1 deletion models/dto/body/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type VmCreate struct {
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,min=0,max=1000,dive"`
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"`
}
Expand Down
94 changes: 47 additions & 47 deletions models/sys/deployment/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,53 +94,6 @@ func (g *GitHubRepository) ToDTO() body.GitHubRepository {
}
}

func (p *UpdateParams) FromDTO(dto *body.DeploymentUpdate, deploymentType string) {
if dto.Envs != nil {
envs := make([]Env, 0)
for _, env := range *dto.Envs {
if env.Name == "PORT" {
port, _ := strconv.Atoi(env.Value)
p.InternalPort = &port
continue
}

envs = append(envs, Env{
Name: env.Name,
Value: env.Value,
})
}
p.Envs = &envs
}

if dto.Volumes != nil {
volumes := make([]Volume, len(*dto.Volumes))
for i, volume := range *dto.Volumes {
volumes[i] = Volume{
Name: volume.Name,
AppPath: volume.AppPath,
ServerPath: volume.ServerPath,
}
}
p.Volumes = &volumes
}

p.Private = dto.Private

if dto.CustomDomain != nil {
if punyEncoded, err := idna.New().ToASCII(*dto.CustomDomain); err == nil {
p.CustomDomain = &punyEncoded
} else {
utils.PrettyPrintError(fmt.Errorf("failed to puny encode domain %s when creating update params details: %w", *dto.CustomDomain, err))
}
}

if deploymentType == TypePrebuilt {
p.Image = dto.Image
}

p.PingPath = dto.HealthCheckPath
}

func (p *CreateParams) FromDTO(dto *body.DeploymentCreate, fallbackZone, fallbackImage string, fallbackPort int) {
p.Name = dto.Name

Expand Down Expand Up @@ -212,6 +165,53 @@ func (p *CreateParams) FromDTO(dto *body.DeploymentCreate, fallbackZone, fallbac
}
}

func (p *UpdateParams) FromDTO(dto *body.DeploymentUpdate, deploymentType string) {
if dto.Envs != nil {
envs := make([]Env, 0)
for _, env := range *dto.Envs {
if env.Name == "PORT" {
port, _ := strconv.Atoi(env.Value)
p.InternalPort = &port
continue
}

envs = append(envs, Env{
Name: env.Name,
Value: env.Value,
})
}
p.Envs = &envs
}

if dto.Volumes != nil {
volumes := make([]Volume, len(*dto.Volumes))
for i, volume := range *dto.Volumes {
volumes[i] = Volume{
Name: volume.Name,
AppPath: volume.AppPath,
ServerPath: volume.ServerPath,
}
}
p.Volumes = &volumes
}

p.Private = dto.Private

if dto.CustomDomain != nil {
if punyEncoded, err := idna.New().ToASCII(*dto.CustomDomain); err == nil {
p.CustomDomain = &punyEncoded
} else {
utils.PrettyPrintError(fmt.Errorf("failed to puny encode domain %s when creating update params details: %w", *dto.CustomDomain, err))
}
}

if deploymentType == TypePrebuilt {
p.Image = dto.Image
}

p.PingPath = dto.HealthCheckPath
}

func (p *BuildParams) FromDTO(dto *body.DeploymentBuild) {
p.Tag = dto.Tag
p.Branch = dto.Branch
Expand Down
4 changes: 2 additions & 2 deletions models/sys/enviroment/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ type Environment struct {
RepairInterval int `yaml:"repairInterval"`
} `yaml:"gpu"`

DockerRegistry struct {
Registry struct {
URL string `yaml:"url"`
PlaceholderImage string `yaml:"placeholderImage"`
VmHttpProxyImage string `yaml:"vmHttpProxyImage"`
} `yaml:"dockerRegistry"`
} `yaml:"registry"`

Deployment Deployment `yaml:"deployment"`
VM VM `yaml:"vm"`
Expand Down
46 changes: 23 additions & 23 deletions models/sys/vm/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,6 @@ func (vm *VM) ToDTO(status string, connectionString *string, gpu *body.GpuRead,
}
}

func (p *UpdateParams) FromDTO(dto *body.VmUpdate) {
p.SnapshotID = dto.SnapshotID
if dto.Ports != nil {
var ports []Port
for _, port := range *dto.Ports {
if port.Name == "__ssh" {
continue
}

if port.Port == 22 {
continue
}

ports = append(ports, fromDtoPort(&port))
}
p.Ports = &ports
} else {
p.Ports = nil
}
p.CpuCores = dto.CpuCores
p.RAM = dto.RAM
}

func (p *CreateParams) FromDTO(dto *body.VmCreate, fallbackZone *string, deploymentZone *string) {
p.Name = dto.Name
p.SshPublicKey = dto.SshPublicKey
Expand Down Expand Up @@ -103,6 +80,29 @@ func (p *CreateParams) FromDTO(dto *body.VmCreate, fallbackZone *string, deploym
p.DeploymentZone = deploymentZone
}

func (p *UpdateParams) FromDTO(dto *body.VmUpdate) {
p.SnapshotID = dto.SnapshotID
if dto.Ports != nil {
var ports []Port
for _, port := range *dto.Ports {
if port.Name == "__ssh" {
continue
}

if port.Port == 22 {
continue
}

ports = append(ports, fromDtoPort(&port))
}
p.Ports = &ports
} else {
p.Ports = nil
}
p.CpuCores = dto.CpuCores
p.RAM = dto.RAM
}

func fromDtoPort(port *body.Port) Port {
var httpProxy *PortHttpProxy
if port.HttpProxy != nil {
Expand Down
4 changes: 2 additions & 2 deletions models/sys/vm/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ const (

type PortHttpProxy struct {
Name string `bson:"name"`
CustomDomain *string `bson:"customDomain,omitempty"`
CustomDomain *string `bson:"customDomain"`
}

type Port struct {
Name string `bson:"name"`
Port int `bson:"port"`
Protocol string `bson:"protocol"`
HttpProxy *PortHttpProxy `bson:"httpProxy,omitempty"`
HttpProxy *PortHttpProxy `bson:"httpProxy"`
}

type Subsystems struct {
Expand Down
2 changes: 1 addition & 1 deletion models/sys/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type VM struct {

Zone string `bson:"zone"`
// used for port http proxy, set in most cases, but kept as optional if no k8s is available
DeploymentZone *string `bson:"deploymentZone,omitempty"`
DeploymentZone *string `bson:"deploymentZone"`

CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
Expand Down
33 changes: 1 addition & 32 deletions pkg/workers/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package migrator

import (
deploymentModels "go-deploy/models/sys/deployment"
"go-deploy/service"
"go-deploy/utils/subsystemutils"
"log"
)

Expand Down Expand Up @@ -36,33 +33,5 @@ func Migrate() {
//
// add a date to the migration name to make it easier to identify.
func getMigrations() map[string]func() error {
return map[string]func() error{
"2023-10-16-set-correct-namespace-name": setCorrectNamespaceName_2023_10_16,
}
}

func setCorrectNamespaceName_2023_10_16() error {
deployments, err := deploymentModels.New().GetAll()
if err != nil {
return err
}

for _, deployment := range deployments {
if ns := deployment.Subsystems.K8s.GetNamespace(); service.Created(ns) {
correctName := subsystemutils.GetPrefixedName(deployment.OwnerID)

if ns.Name == correctName {
continue
}

ns.Name = correctName

err = deploymentModels.New().UpdateSubsystemByID(deployment.ID, "k8s.namespace", ns)
if err != nil {
return err
}
}
}

return nil
return map[string]func() error{}
}
4 changes: 2 additions & 2 deletions service/deployment_service/ci_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetCIConfig(deploymentID string, auth *service.AuthInfo) (*body.CiConfig, e
}

tag := fmt.Sprintf("%s/%s/%s",
conf.Env.DockerRegistry.URL,
conf.Env.Registry.URL,
subsystemutils.GetPrefixedName(auth.UserID),
deployment.Name,
)
Expand All @@ -49,7 +49,7 @@ func GetCIConfig(deploymentID string, auth *service.AuthInfo) (*body.CiConfig, e
Name: "Login to Docker Hub",
Uses: "docker/login-action@v2",
With: deploymentModel.With{
Registry: conf.Env.DockerRegistry.URL,
Registry: conf.Env.Registry.URL,
Username: username,
Password: password,
},
Expand Down
2 changes: 1 addition & 1 deletion service/deployment_service/deployment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Create(deploymentID, ownerID string, deploymentCreate *body.DeploymentCreat

// temporary hard-coded fallback
fallbackZone := "se-flem"
fallbackImage := fmt.Sprintf("%s/%s/%s", conf.Env.DockerRegistry.URL, subsystemutils.GetPrefixedName(ownerID), deploymentCreate.Name)
fallbackImage := fmt.Sprintf("%s/%s/%s", conf.Env.Registry.URL, subsystemutils.GetPrefixedName(ownerID), deploymentCreate.Name)
fallbackPort := conf.Env.Deployment.Port

params := &deploymentModel.CreateParams{}
Expand Down
4 changes: 2 additions & 2 deletions service/deployment_service/gitlab_service/gitlab_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func CreateBuild(id string, params *deploymentModel.BuildParams) error {
"docker:20.10.16-dind",
},
Variables: map[string]string{
"CI_REGISTRY": conf.Env.DockerRegistry.URL,
"CI_REGISTRY_IMAGE": conf.Env.DockerRegistry.URL + "/" + subsystemutils.GetPrefixedName(deployment.OwnerID) + "/" + deployment.Name,
"CI_REGISTRY": conf.Env.Registry.URL,
"CI_REGISTRY_IMAGE": conf.Env.Registry.URL + "/" + subsystemutils.GetPrefixedName(deployment.OwnerID) + "/" + deployment.Name,
"CI_COMMIT_REF_SLUG": params.Tag,
"CI_REGISTRY_USER": escapedHarborName,
"CI_REGISTRY_PASSWORD": deployment.Subsystems.Harbor.Robot.Secret,
Expand Down
2 changes: 1 addition & 1 deletion service/resources/harbor_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (hg *HarborGenerator) Robot() *models.RobotPublic {
}

func (hg *HarborGenerator) Repository() *models.RepositoryPublic {
splits := strings.Split(conf.Env.DockerRegistry.PlaceholderImage, "/")
splits := strings.Split(conf.Env.Registry.PlaceholderImage, "/")
project := splits[len(splits)-2]
repository := splits[len(splits)-1]

Expand Down
42 changes: 37 additions & 5 deletions service/resources/k8s_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (kg *K8sGenerator) Deployments() []models.DeploymentPublic {
res = append(res, models.DeploymentPublic{
Name: getVmProxyDeploymentName(kg.v.vm, port.HttpProxy.Name),
Namespace: kg.namespace,
Image: conf.Env.DockerRegistry.VmHttpProxyImage,
Image: conf.Env.Registry.VmHttpProxyImage,
EnvVars: envVars,
Resources: models.Resources{
Limits: models.Limits{
Expand Down Expand Up @@ -536,13 +536,24 @@ func (kg *K8sGenerator) Ingresses() []models.IngressPublic {
ports := kg.v.vm.Ports

for mapName, ingress := range kg.v.vm.Subsystems.K8s.GetIngressMap() {
if slices.IndexFunc(ports, func(p vm.Port) bool {
idx := slices.IndexFunc(ports, func(p vm.Port) bool {
if p.HttpProxy == nil {
return false
}

return getVmProxyIngressName(kg.v.vm, p.HttpProxy.Name) == mapName
}) != -1 {
return getVmProxyIngressName(kg.v.vm, p.HttpProxy.Name) == mapName ||
(getVmProxyCustomDomainIngressName(kg.v.vm, p.HttpProxy.Name) == mapName && p.HttpProxy.CustomDomain != nil)
})

if idx != -1 {
if getVmProxyCustomDomainIngressName(kg.v.vm, ports[idx].HttpProxy.Name) == mapName {
if ports[idx].HttpProxy.CustomDomain != nil {
ingress.Hosts = []string{*ports[idx].HttpProxy.CustomDomain}
}
} else {
ingress.Hosts = []string{getVmProxyExternalURL(ports[idx].HttpProxy.Name, kg.v.deploymentZone)}
}

res = append(res, ingress)
}
}
Expand All @@ -562,6 +573,23 @@ func (kg *K8sGenerator) Ingresses() []models.IngressPublic {
Hosts: []string{getVmProxyExternalURL(port.HttpProxy.Name, kg.v.deploymentZone)},
})
}

if port.HttpProxy.CustomDomain != nil {
if _, ok := kg.v.vm.Subsystems.K8s.GetIngressMap()[getVmProxyCustomDomainIngressName(kg.v.vm, port.HttpProxy.Name)]; !ok {
res = append(res, models.IngressPublic{
Name: getVmProxyCustomDomainIngressName(kg.v.vm, port.HttpProxy.Name),
Namespace: kg.namespace,
ServiceName: getVmProxyServiceName(kg.v.vm, port.HttpProxy.Name),
ServicePort: 8080,
IngressClass: conf.Env.Deployment.IngressClass,
Hosts: []string{*port.HttpProxy.CustomDomain},
CustomCert: &models.CustomCert{
ClusterIssuer: "letsencrypt-prod-deploy-http",
CommonName: *port.HttpProxy.CustomDomain,
},
})
}
}
}

return res
Expand Down Expand Up @@ -702,7 +730,7 @@ func (kg *K8sGenerator) Secrets() []models.SecretPublic {

if kg.d.deployment != nil {
if kg.d.deployment.Subsystems.Harbor.Robot.Created() && kg.d.deployment.Type == deployment.TypeCustom {
registry := conf.Env.DockerRegistry.URL
registry := conf.Env.Registry.URL
username := kg.d.deployment.Subsystems.Harbor.Robot.HarborName
password := kg.d.deployment.Subsystems.Harbor.Robot.Secret

Expand Down Expand Up @@ -795,6 +823,10 @@ func getVmProxyIngressName(vm *vm.VM, portName string) string {
return fmt.Sprintf("%s-%s", vm.Name, portName)
}

func getVmProxyCustomDomainIngressName(vm *vm.VM, portName string) string {
return fmt.Sprintf("%s-%s-custom-domain", vm.Name, portName)
}

func getVmProxyExternalURL(portName string, zone *enviroment.DeploymentZone) string {
return fmt.Sprintf("%s.%s", portName, zone.ParentDomainVM)
}
Expand Down
2 changes: 1 addition & 1 deletion test/acc/subsystems/k8s/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func withK8sDeployment(t *testing.T, namespace *models.NamespacePublic) *models.
deploymentPublic := &models.DeploymentPublic{
Name: "acc-test-" + uuid.New().String(),
Namespace: namespace.Name,
Image: conf.Env.DockerRegistry.PlaceholderImage,
Image: conf.Env.Registry.PlaceholderImage,
EnvVars: nil,
}

Expand Down

0 comments on commit b555e80

Please sign in to comment.