mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
feat: print service endpoints on service creation
This commit is contained in:
@@ -4,8 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/distribution/reference"
|
||||
"maps"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"slices"
|
||||
"uncloud/internal/machine/api/pb"
|
||||
)
|
||||
|
||||
@@ -79,6 +81,50 @@ type MachineContainer struct {
|
||||
Container Container
|
||||
}
|
||||
|
||||
// Endpoints returns the exposed HTTP and HTTPS endpoints of the service.
|
||||
func (s *Service) Endpoints() []string {
|
||||
endpoints := make(map[string]struct{})
|
||||
|
||||
// Container specs may differ between containers in the same service, e.g. during a rolling update,
|
||||
// so we need to collect all unique endpoints.
|
||||
for _, ctr := range s.Containers {
|
||||
ports, err := ctr.Container.ServicePorts()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, port := range ports {
|
||||
protocol := ""
|
||||
switch port.Protocol {
|
||||
case ProtocolHTTP:
|
||||
protocol = "http"
|
||||
case ProtocolHTTPS:
|
||||
protocol = "https"
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if port.Hostname == "" {
|
||||
// There shouldn't be http(s) ports without a hostname but just in case ignore them.
|
||||
continue
|
||||
}
|
||||
|
||||
endpoint := fmt.Sprintf("%s://%s", protocol, port.Hostname)
|
||||
if port.PublishedPort != 0 {
|
||||
// For non-standard ports (80/443), include the port in the URL.
|
||||
if !(port.Protocol == ProtocolHTTP && port.PublishedPort == 80) &&
|
||||
!(port.Protocol == ProtocolHTTPS && port.PublishedPort == 443) {
|
||||
endpoint += fmt.Sprintf(":%d", port.PublishedPort)
|
||||
}
|
||||
}
|
||||
|
||||
endpoints[endpoint] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
return slices.Sorted(maps.Keys(endpoints))
|
||||
}
|
||||
|
||||
func ServiceFromProto(s *pb.Service) (Service, error) {
|
||||
var err error
|
||||
containers := make([]MachineContainer, len(s.Containers))
|
||||
|
||||
Reference in New Issue
Block a user