Skip to content

Commit

Permalink
Merge pull request #665 from kthcloud/dev
Browse files Browse the repository at this point in the history
Maintenance Update
  • Loading branch information
saffronjam authored Jun 10, 2024
2 parents 6031f23 + 4290747 commit f355079
Show file tree
Hide file tree
Showing 28 changed files with 431 additions and 111 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Setup Bun
uses: oven-sh/setup-bun@v1

- name: Check for TS errors
run: |
cd export
bun run tsc
- name: Build types
run: |
cd scripts
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<img src="https://github.com/kthcloud/go-deploy/actions/workflows/build-and-push-image.yml/badge.svg"\>
</div>

go-deploy is an API that is used to create websites and virtual machines built on top of kthcloud using Kubernetes with [KubeVirt](https://kubevirt.io/).
go-deploy is an API that is used to create container applications and virtual machines built on top of kthcloud using Kubernetes with [KubeVirt](https://kubevirt.io/).

It is hosted at [https://api.cloud.cbh.kth.se/deploy/v2](https://api.cloud.cbh.kth.se/deploy/v2) using kthcloud authentication.

Expand Down
19 changes: 14 additions & 5 deletions dto/v2/body/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ type DeploymentRead struct {
Volumes []Volume `json:"volumes"`
InitCommands []string `json:"initCommands"`
Args []string `json:"args"`
Private bool `json:"private"`
InternalPort int `json:"internalPort"`
Image *string `json:"image,omitempty"`
HealthCheckPath *string `json:"healthCheckPath,omitempty"`
CustomDomain *CustomDomainRead `json:"customDomain,omitempty"`
Visibility string `json:"visibility"`

// Deprecated: Use Visibility instead.
Private bool `json:"private"`

Status string `json:"status"`
Error *string `json:"error,omitempty"`
Expand All @@ -45,17 +48,20 @@ type DeploymentRead struct {
}

type DeploymentCreate struct {
Name string `json:"name" bson:"name" binding:"required,rfc1035,min=3,max=30"`
Name string `json:"name" bson:"name" binding:"required,rfc1035,min=3,max=30,deployment_name"`

CpuCores *float64 `json:"cpuCores,omitempty" bson:"cpuCores,omitempty" binding:"omitempty,min=0.1"`
RAM *float64 `json:"ram,omitempty" bson:"ram,omitempty" binding:"omitempty,min=0.1"`
Replicas *int `json:"replicas,omitempty" bson:"replicas,omitempty" binding:"omitempty,min=0,max=100"`

Private bool `json:"private" bson:"private" binding:"omitempty,boolean"`
Envs []Env `json:"envs" bson:"envs" binding:"omitempty,env_list,min=0,max=1000,dive"`
Volumes []Volume `json:"volumes" bson:"volumes" binding:"omitempty,min=0,max=100,dive"`
InitCommands []string `json:"initCommands" bson:"initCommands" binding:"omitempty,min=0,max=100,dive,min=0,max=100"`
Args []string `json:"args" bson:"args" binding:"omitempty,min=0,max=100,dive,min=0,max=100"`
Visibility string `json:"visibility" bson:"visibility" binding:"omitempty,oneof=public private auth"`

// Deprecated: Use Visibility instead.
Private bool `json:"private" bson:"private" binding:"omitempty,boolean"`

Image *string `json:"image,omitempty" bson:"image,omitempty" binding:"omitempty,min=1,max=1000"`
HealthCheckPath *string `json:"healthCheckPath" bson:"healthCheckPath,omitempty" binding:"omitempty,min=0,max=1000,health_check_path"`
Expand All @@ -69,17 +75,20 @@ type DeploymentCreate struct {
}

type DeploymentUpdate struct {
Name *string `json:"name,omitempty" bson:"name,omitempty" binding:"omitempty,required,rfc1035,min=3,max=30"`
Name *string `json:"name,omitempty" bson:"name,omitempty" binding:"omitempty,required,rfc1035,min=3,max=30,deployment_name"`

CpuCores *float64 `json:"cpuCores,omitempty" bson:"cpuCores,omitempty" binding:"omitempty,min=0.1"`
RAM *float64 `json:"ram,omitempty" bson:"ram,omitempty" binding:"omitempty,min=0.1"`
Replicas *int `json:"replicas,omitempty" bson:"replicas,omitempty" binding:"omitempty,min=0,max=100"`

Private *bool `json:"private,omitempty" bson:"private,omitempty" binding:"omitempty,boolean"`
Envs *[]Env `json:"envs,omitempty" bson:"envs,omitempty" binding:"omitempty,env_list,min=0,max=1000,dive"`
Volumes *[]Volume `json:"volumes,omitempty" bson:"volumes,omitempty" binding:"omitempty,min=0,max=100,dive"`
InitCommands *[]string `json:"initCommands,omitempty" bson:"initCommands,omitempty" binding:"omitempty,min=0,max=100,dive,min=0,max=100"`
Args *[]string `json:"args,omitempty" bson:"args,omitempty" binding:"omitempty,min=0,max=100,dive,min=0,max=100"`
Visibility *string `json:"visibility" bson:"visibility" binding:"omitempty,oneof=public private auth"`

// Deprecated: Use Visibility instead.
Private *bool `json:"private,omitempty" bson:"private,omitempty" binding:"omitempty,boolean"`

Image *string `json:"image,omitempty,omitempty" bson:"image,omitempty" binding:"omitempty,min=1,max=1000"`
HealthCheckPath *string `json:"healthCheckPath,omitempty" bson:"healthCheckPath,omitempty" binding:"omitempty,min=0,max=1000,health_check_path"`
Expand Down
12 changes: 2 additions & 10 deletions dto/v2/body/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type VmRead struct {
}

type VmCreate struct {
Name string `json:"name" bson:"name" binding:"required,rfc1035,min=3,max=30"`
Name string `json:"name" bson:"name" binding:"required,rfc1035,min=3,max=30,vm_name"`
SshPublicKey string `json:"sshPublicKey" bson:"sshPublicKey" binding:"required,ssh_public_key"`
Ports []PortCreate `json:"ports" bson:"ports" binding:"omitempty,port_list_names,port_list_numbers,port_list_http_proxies,min=0,max=10,dive"`

Expand All @@ -39,18 +39,10 @@ type VmCreate struct {
}

type VmUpdate struct {
Name *string `json:"name,omitempty" bson:"name,omitempty" binding:"omitempty,rfc1035,min=3,max=30,vm_name"`
Ports *[]PortUpdate `json:"ports,omitempty" bson:"ports,omitempty" binding:"omitempty,port_list_names,port_list_numbers,port_list_http_proxies,min=0,max=10,dive"`
CpuCores *int `json:"cpuCores,omitempty" bson:"cpuCores,omitempty" binding:"omitempty,min=1"`
RAM *int `json:"ram,omitempty" bson:"ram,omitempty" binding:"omitempty,min=1"`

// Name is used to rename a VM.
// If specified, only name will be updated.
Name *string `json:"name,omitempty" bson:"name,omitempty" binding:"omitempty,rfc1035,min=3,max=30"`

// OwnerID is used to initiate transfer a VM to another user.
// If specified, only the transfer will happen.
// If specified but empty, the transfer will be canceled.
OwnerID *string `json:"ownerId,omitempty" bson:"ownerId,omitempty" binding:"omitempty"`
}

type VmUpdateOwner struct {
Expand Down
2 changes: 1 addition & 1 deletion export/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.ts", "types/**/*.ts"]
"include": ["index.ts", "types/**/*.ts"]
}
10 changes: 5 additions & 5 deletions models/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ type ConfigType struct {
} `yaml:"metrics"`

Keycloak struct {
Url string `yaml:"url"`
Realm string `yaml:"realm"`
AdminGroup string `yaml:"adminGroup"`
StorageClient struct {
Url string `yaml:"url"`
Realm string `yaml:"realm"`
AdminGroup string `yaml:"adminGroup"`
UserClient struct {
ClientID string `yaml:"clientId"`
ClientSecret string `yaml:"clientSecret"`
} `yaml:"storageClient"`
} `yaml:"userClient"`
} `yaml:"keycloak"`

MongoDB struct {
Expand Down
11 changes: 8 additions & 3 deletions models/model/deployment_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ func (deployment *Deployment) ToDTO(smURL *string, externalPort *int, teams []st
Volumes: volumes,
InitCommands: app.InitCommands,
Args: app.Args,
Private: app.Private,
InternalPort: app.InternalPort,
Image: image,
HealthCheckPath: healthCheckPath,
CustomDomain: customDomain,
Visibility: app.Visibility,

Status: status,
Error: deploymentError,
Expand Down Expand Up @@ -161,7 +161,6 @@ func (p *DeploymentCreateParams) FromDTO(dto *body.DeploymentCreate, fallbackZon
p.RAM = *dto.RAM
}

p.Private = dto.Private
p.Envs = make([]DeploymentEnv, 0)
for _, env := range dto.Envs {
if env.Name == "PORT" {
Expand Down Expand Up @@ -217,6 +216,12 @@ func (p *DeploymentCreateParams) FromDTO(dto *body.DeploymentCreate, fallbackZon
} else {
p.Zone = fallbackZone
}

if dto.Visibility == "" {
p.Visibility = VisibilityPublic
} else {
p.Visibility = dto.Visibility
}
}

// FromDTO converts body.DeploymentUpdate DTO to DeploymentUpdateParams.
Expand Down Expand Up @@ -267,9 +272,9 @@ func (p *DeploymentUpdateParams) FromDTO(dto *body.DeploymentUpdate, deploymentT
p.Name = dto.Name
p.CpuCores = dto.CpuCores
p.RAM = dto.RAM
p.Private = dto.Private
p.InitCommands = dto.InitCommands
p.Args = dto.Args
p.PingPath = dto.HealthCheckPath
p.Replicas = dto.Replicas
p.Visibility = dto.Visibility
}
4 changes: 2 additions & 2 deletions models/model/deployment_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type DeploymentCreateParams struct {

Image string
InternalPort int
Private bool
Envs []DeploymentEnv
Volumes []DeploymentVolume
InitCommands []string
Args []string
PingPath string
CustomDomain *string
Visibility string

Zone string
}
Expand All @@ -28,7 +28,6 @@ type DeploymentUpdateParams struct {
CpuCores *float64
RAM *float64

Private *bool
Envs *[]DeploymentEnv
InternalPort *int
Volumes *[]DeploymentVolume
Expand All @@ -38,6 +37,7 @@ type DeploymentUpdateParams struct {
Image *string
PingPath *string
Replicas *int
Visibility *string
}

type DeploymentUpdateOwnerParams struct {
Expand Down
12 changes: 11 additions & 1 deletion models/model/deployment_related.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const (
LogSourceDeployment = "deployment"
// LogSourceBuild is a log source for a build in GitLab CI.
LogSourceBuild = "build"

// VisibilityPublic is a public app.
VisibilityPublic = "public"
// VisibilityPrivate is a private app.
VisibilityPrivate = "private"
// VisibilityAuth is an app that requires authentication.
VisibilityAuth = "auth"
)

var (
Expand All @@ -31,9 +38,12 @@ type App struct {

Image string `bson:"image"`
InternalPort int `bson:"internalPort"`
Private bool `bson:"private"`
Envs []DeploymentEnv `bson:"envs"`
Volumes []DeploymentVolume `bson:"volumes"`
Visibility string `bson:"visibility"`

// Deprecated: use Visibility instead.
Private bool `bson:"private"`

Args []string `bson:"args"`
InitCommands []string `bson:"initCommands"`
Expand Down
35 changes: 34 additions & 1 deletion pkg/db/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package migrator

import (
"fmt"
"go-deploy/models/model"
"go-deploy/pkg/db/resources/deployment_repo"
"go-deploy/pkg/log"
"go.mongodb.org/mongo-driver/bson"
)

// Migrate runs every migration specified in the getMigrations function.
Expand Down Expand Up @@ -32,5 +35,35 @@ func Migrate() error {
//
// add a date to the migration name to make it easier to identify.
func getMigrations() map[string]func() error {
return map[string]func() error{}
return map[string]func() error{
"migratePrivateBooleanToVisibilityEnum_2024_06_10": migratePrivateBooleanToVisibilityEnum_2024_06_10,
}
}

func migratePrivateBooleanToVisibilityEnum_2024_06_10() error {
deployments, err := deployment_repo.New().List()
if err != nil {
return err
}

for _, deployment := range deployments {
mainApp := deployment.GetMainApp()

if mainApp.Visibility != "" {
continue
}

if mainApp.Private {
mainApp.Visibility = model.VisibilityPrivate
} else {
mainApp.Visibility = model.VisibilityPublic
}

err = deployment_repo.New().SetWithBsonByID(deployment.ID, bson.D{{"apps.main.visibility", mainApp.Visibility}})
if err != nil {
return err
}
}

return nil
}
13 changes: 8 additions & 5 deletions pkg/db/resources/deployment_repo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ func (client *Client) Create(id, ownerID string, params *model.DeploymentCreateP

Image: params.Image,
InternalPort: params.InternalPort,
Private: params.Private,
Envs: params.Envs,
Volumes: params.Volumes,
InitCommands: params.InitCommands,
Visibility: params.Visibility,

Args: params.Args,
InitCommands: params.InitCommands,
CustomDomain: customDomain,
PingResult: 0,
PingPath: params.PingPath,

ReplicaStatus: nil,
PingPath: params.PingPath,
PingResult: 0,
}

deployment := model.Deployment{
Expand Down Expand Up @@ -124,7 +127,6 @@ func (client *Client) UpdateWithParams(id string, params *model.DeploymentUpdate
db.AddIfNotNil(&setUpdate, "ownerId", params.OwnerID)
db.AddIfNotNil(&setUpdate, "apps.main.internalPort", params.InternalPort)
db.AddIfNotNil(&setUpdate, "apps.main.envs", params.Envs)
db.AddIfNotNil(&setUpdate, "apps.main.private", params.Private)
db.AddIfNotNil(&setUpdate, "apps.main.volumes", params.Volumes)
db.AddIfNotNil(&setUpdate, "apps.main.initCommands", params.InitCommands)
db.AddIfNotNil(&setUpdate, "apps.main.args", params.Args)
Expand All @@ -134,6 +136,7 @@ func (client *Client) UpdateWithParams(id string, params *model.DeploymentUpdate
db.AddIfNotNil(&setUpdate, "apps.main.cpuCores", params.CpuCores)
db.AddIfNotNil(&setUpdate, "apps.main.ram", params.RAM)
db.AddIfNotNil(&setUpdate, "apps.main.replicas", params.Replicas)
db.AddIfNotNil(&setUpdate, "apps.main.visibility", params.Visibility)

err = client.UpdateWithBsonByID(id,
bson.D{
Expand Down
23 changes: 23 additions & 0 deletions pkg/db/resources/team_repo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ func (client *Client) Create(id, ownerID string, params *model.TeamCreateParams)
return fetched, nil
}

func (client *Client) ListMemberIDs(ids ...string) ([]string, error) {
teams, err := client.ListWithFilterAndProjection(bson.D{{"id", bson.D{{"$in", ids}}}}, bson.D{{"memberMap", 1}})
if err != nil {
return nil, err
}

memberIDs := make(map[string]bool, 0)
for _, team := range teams {
for memberID := range team.MemberMap {
if _, ok := memberIDs[memberID]; !ok {
memberIDs[memberID] = true
}
}
}

result := make([]string, 0, len(memberIDs))
for memberID := range memberIDs {
result = append(result, memberID)
}

return result, nil
}

func (client *Client) UpdateWithParams(id string, params *model.TeamUpdateParams) error {
updateData := bson.D{
{"updatedAt", time.Now()},
Expand Down
25 changes: 25 additions & 0 deletions pkg/db/resources/user_repo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ func (client *Client) Synchronize(id string, params *model.UserSynchronizeParams
return client.GetByID(id)
}

// GetEmail returns the email for the given user ID.
func (client *Client) GetEmail(id string) (string, error) {
user, err := client.GetWithFilterAndProjection(bson.D{{"id", id}}, bson.D{{"email", 1}})
if err != nil {
return "", err
}

return user.Email, nil
}

// ListEmails returns a list of emails for the given user IDs.
func (client *Client) ListEmails(ids ...string) (map[string]string, error) {
users, err := client.ListWithFilterAndProjection(bson.D{{"id", bson.D{{"$in", ids}}}}, bson.D{{"id", 1}, {"email", 1}})
if err != nil {
return nil, err
}

emails := make(map[string]string)
for _, user := range users {
emails[user.ID] = user.Email
}

return emails, nil
}

// UpdateWithParams updates the user with the given params.
func (client *Client) UpdateWithParams(id string, params *model.UserUpdateParams) error {
updateData := bson.D{}
Expand Down
Loading

0 comments on commit f355079

Please sign in to comment.