Skip to content

Commit

Permalink
refactor: move populateServiceUrls to the get_deployment package
Browse files Browse the repository at this point in the history
Also introduce the interface KeyedResult in the storage pkg and make the builder.KeyedResult implements it.
  • Loading branch information
YuukanOO committed Jun 24, 2024
1 parent 4f6b599 commit fe3e8ed
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 94 deletions.
8 changes: 4 additions & 4 deletions cmd/serve/front/src/assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abbr {
cursor: help;
}

/** Apply global styles to make things simplier */
/** Apply global styles to make things simpler */
*:disabled,
*[aria-disabled='true'] {
pointer-events: none;
Expand Down Expand Up @@ -136,7 +136,7 @@ table tbody tr {
display: block;
}

table tbody tr+tr {
table tbody tr + tr {
border-block-start: 1px solid var(--co-divider-4);
margin-block-start: var(--sp-2);
padding-block-start: var(--sp-2);
Expand All @@ -151,7 +151,7 @@ table tbody tr+tr {
display: table-row;
}

table tbody tr+tr {
table tbody tr + tr {
margin: 0;
padding: 0;
}
Expand All @@ -164,4 +164,4 @@ table tbody tr+tr {
table tbody td::before {
display: none;
}
}
}
70 changes: 69 additions & 1 deletion internal/deployment/app/get_deployment/get_deployment.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package get_deployment

import (
"strconv"
"strings"
"time"

"github.com/YuukanOO/seelf/internal/deployment/app"
"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/pkg/bus"
"github.com/YuukanOO/seelf/pkg/monad"
"github.com/YuukanOO/seelf/pkg/storage"
Expand Down Expand Up @@ -31,9 +34,11 @@ type (
RequestedBy app.UserSummary `json:"requested_by"`
}

// This summary is specific in the sense that it represents a target which may
// have been deleted since hence the optional fields.
TargetSummary struct {
ID string `json:"id"`
Name monad.Maybe[string] `json:"name"` // Since the target could have been deleted, the name is nullable here.
Name monad.Maybe[string] `json:"name"`
Url monad.Maybe[string] `json:"url"`
Status monad.Maybe[uint8] `json:"status"`
Entrypoints monad.Maybe[Entrypoints] `json:"-"`
Expand Down Expand Up @@ -88,3 +93,66 @@ func (s *Services) Scan(value any) error {
func (e *Entrypoints) Scan(value any) error {
return storage.ScanJSON(value, e)
}

// Since the target domain is dynamic, compute exposed service urls based on the resolved
// target url and entrypoints if available.
//
// This method should be called after the deployment has been loaded.
func (d *Deployment) ResolveServicesUrls() {
services, hasServices := d.State.Services.TryGet()
url, hasUrl := d.Target.Url.TryGet()
entrypoints, hasEntrypoints := d.Target.Entrypoints.TryGet()

// Target not found, could not populate services urls
if !hasUrl || !hasServices || !hasEntrypoints {
return
}

idx := strings.Index(url, "://")
targetScheme, targetHost := url[:idx+3], url[idx+3:]

for i, service := range services {
// Compatibility with old deployments
if service.Url.HasValue() || service.Subdomain.HasValue() {
compatEntrypoint := Entrypoint{
Name: "default",
Router: string(domain.RouterHttp),
Port: 80,
Subdomain: service.Subdomain, // (> 2.0.0 - < 2.2.0)
Url: service.Url, // (< 2.0.0)
}

if subdomain, isSet := compatEntrypoint.Subdomain.TryGet(); !service.Url.HasValue() && isSet {
compatEntrypoint.Url.Set(targetScheme + subdomain + "." + targetHost)
}

services[i].Entrypoints = append(service.Entrypoints, compatEntrypoint)
continue
}

for j, entrypoint := range service.Entrypoints {
host := targetHost

if subdomain, isSet := entrypoint.Subdomain.TryGet(); isSet {
host = subdomain + "." + targetHost
}

if !entrypoint.IsCustom {
entrypoint.Url.Set(targetScheme + host)
services[i].Entrypoints[j] = entrypoint
continue
}

publishedPort, isAssigned := entrypoints[d.AppID][d.Environment][entrypoint.Name].TryGet()

if !isAssigned {
continue
}

entrypoint.PublishedPort.Set(publishedPort)
entrypoint.Url.Set(entrypoint.Router + "://" + host + ":" + strconv.FormatUint(uint64(publishedPort), 10))

services[i].Entrypoints[j] = entrypoint
}
}
}
Loading

0 comments on commit fe3e8ed

Please sign in to comment.