Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: public ports don't have to be same length as private ports #2416

Merged
merged 3 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func StartRegisteredUserServices(
publicPorts := serviceConfig.GetPublicPorts()
if len(publicPorts) > 0 {
privatePorts := serviceConfig.GetPrivatePorts()
err := checkPrivateAndPublicPortsAreOneToOne(privatePorts, publicPorts)
err := checkPrivateAndPublicPortIdsMatch(privatePorts, publicPorts)
if err != nil {
failedServicesPool[serviceUuid] = stacktrace.Propagate(err, "Private and public ports for service with UUID '%v' are not one to one.", serviceUuid)
delete(serviceConfigsToStart, serviceUuid)
Expand Down Expand Up @@ -657,7 +657,7 @@ func createStartServiceOperation(
return nil, stacktrace.Propagate(err, "An error occurred converting private port spec '%v' to a Docker port", portId)
}
//TODO this is a huge hack to temporarily enable static ports for NEAR until we have a more productized solution
if len(publicPorts) > 0 {
if portShouldBeManuallyPublished(portId, publicPorts) {
publicPortSpec, found := publicPorts[portId]
if !found {
return nil, stacktrace.NewError("Expected to receive public port with ID '%v' bound to private port number '%v', but it was not found", portId, privatePortSpec.GetNumber())
Expand Down Expand Up @@ -811,18 +811,11 @@ func getUpdatedEntrypointAndCmdFromFilesToBeMoved(ctx context.Context, dockerMan
return cmdArgs, entrypointArgs, nil
}

// Ensure that provided [privatePorts] and [publicPorts] are one to one by checking:
// - There is a matching publicPort for every portID in privatePorts
// - There are the same amount of private and public ports
// If error is nil, the public and private ports are one to one.
func checkPrivateAndPublicPortsAreOneToOne(privatePorts map[string]*port_spec.PortSpec, publicPorts map[string]*port_spec.PortSpec) error {
if len(privatePorts) != len(publicPorts) {
return stacktrace.NewError("The received private ports length and the public ports length are not equal. Received '%v' private ports and '%v' public ports", len(privatePorts), len(publicPorts))
}

for portID, privatePortSpec := range privatePorts {
if _, found := publicPorts[portID]; !found {
return stacktrace.NewError("Expected to receive public port with ID '%v' bound to private port number '%v', but it was not found", portID, privatePortSpec.GetNumber())
// Ensure that every public port has a matching private port id
func checkPrivateAndPublicPortIdsMatch(privatePorts map[string]*port_spec.PortSpec, publicPorts map[string]*port_spec.PortSpec) error {
for portID, publicPortSpec := range publicPorts {
if _, found := privatePorts[portID]; !found {
return stacktrace.NewError("Expected to receive private port with ID '%v' bound for public port number '%v', but it was not found", portID, publicPortSpec.GetNumber())
}
}
return nil
Expand Down Expand Up @@ -911,3 +904,11 @@ func quoteAndJoinArgs(args []string) string {
}
return strings.Join(quotedArgs, " ")
}

func portShouldBeManuallyPublished(key string, publicPorts map[string]*port_spec.PortSpec) bool {
if len(publicPorts) == 0 {
return false
}
_, found := publicPorts[key]
return found
}
Loading