Skip to content

Commit 3235175

Browse files
authored
Servers with no allocation (#86)
* Update packages * Docker: server with no allocation * None bracking package updates * Make SEVRER_IP 127.0.0.1 * Compare to port and not IP * This is not needed anymore as we use 127.0.0.1:0 * Fix ForceOutgoingIp * Fix packages conflict
1 parent 1d34d3e commit 3235175

File tree

6 files changed

+51
-36
lines changed

6 files changed

+51
-36
lines changed

environment/allocations.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import (
99
"github.com/pelican-dev/wings/config"
1010
)
1111

12+
13+
// DefaultAllocationMapping defines the IP and port mapping for a server
14+
type DefaultAllocationMapping struct {
15+
Ip string `json:"ip"`
16+
Port int `json:"port"`
17+
}
1218
// Defines the allocations available for a given server. When using the Docker environment
1319
// driver these correspond to mappings for the container that allow external connections.
1420
type Allocations struct {
@@ -17,13 +23,11 @@ type Allocations struct {
1723
// the DefaultMapping's IP. This is important to servers which rely on external
1824
// services that check the IP of the server (Source Engine servers, for example).
1925
ForceOutgoingIP bool `json:"force_outgoing_ip"`
26+
2027
// Defines the default allocation that should be used for this server. This is
2128
// what will be used for {SERVER_IP} and {SERVER_PORT} when modifying configuration
2229
// files or the startup arguments for a server.
23-
DefaultMapping struct {
24-
Ip string `json:"ip"`
25-
Port int `json:"port"`
26-
} `json:"default"`
30+
DefaultMapping *DefaultAllocationMapping `json:"default"`
2731

2832
// Mappings contains all the ports that should be assigned to a given server
2933
// attached to the IP they correspond to.

environment/docker/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"emperror.dev/errors"
1212
"github.com/docker/docker/api/types"
13+
"github.com/docker/docker/api/types/container"
1314
"github.com/docker/docker/api/types/versions"
1415
"github.com/docker/docker/client"
1516
"github.com/docker/docker/errdefs"
@@ -49,7 +50,7 @@ func configure(c *client.Client) {
4950
// a large number of requests to this endpoint are spawned by Wings, and the
5051
// standard "encoding/json" shows its performance woes badly even with single
5152
// containers running.
52-
func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error) {
53+
func (e *Environment) ContainerInspect(ctx context.Context) (container.InspectResponse, error) {
5354
configure(e.client)
5455

5556
// Support feature flagging of this functionality so that if something goes
@@ -59,7 +60,7 @@ func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON
5960
return e.client.ContainerInspect(ctx, e.Id)
6061
}
6162

62-
var st types.ContainerJSON
63+
var st container.InspectResponse
6364
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/containers/"+e.Id+"/json", nil)
6465
if err != nil {
6566
return st, errors.WithStack(err)

environment/docker/container.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,15 @@ func (e *Environment) Create() error {
155155
cfg := config.Get()
156156
a := e.Configuration.Allocations()
157157
evs := e.Configuration.EnvironmentVariables()
158-
for i, v := range evs {
159-
// Convert 127.0.0.1 to the pelican0 network interface if the environment is Docker
160-
// so that the server operates as expected.
161-
if v == "SERVER_IP=127.0.0.1" {
162-
evs[i] = "SERVER_IP=" + cfg.Docker.Network.Interface
158+
159+
// If port is 0 then we have a server with no allocation and this should stay 127.0.0.1 and not the docker network interface ip.
160+
if a.DefaultMapping.Port != 0 {
161+
for i, v := range evs {
162+
// Convert 127.0.0.1 to the pelican0 network interface if the environment is Docker
163+
// so that the server operates as expected.
164+
if v == "SERVER_IP=127.0.0.1" {
165+
evs[i] = "SERVER_IP=" + cfg.Docker.Network.Interface
166+
}
163167
}
164168
}
165169

@@ -196,31 +200,36 @@ func (e *Environment) Create() error {
196200

197201
networkMode := container.NetworkMode(cfg.Docker.Network.Mode)
198202
if a.ForceOutgoingIP {
199-
enableIPv6 := false
200-
e.log().Debug("environment/docker: forcing outgoing IP address")
201-
networkName := "ip-" + strings.ReplaceAll(strings.ReplaceAll(a.DefaultMapping.Ip, ".", "-"), ":", "-")
202-
networkMode = container.NetworkMode(networkName)
203-
204-
if _, err := e.client.NetworkInspect(ctx, networkName, network.InspectOptions{}); err != nil {
205-
if !client.IsErrNotFound(err) {
206-
return err
207-
}
203+
// We can't use ForceOutgoingIP if we made a server with no allocation
204+
if a.DefaultMapping.Port != 0 {
205+
enableIPv6 := false
206+
e.log().Debug("environment/docker: forcing outgoing IP address")
207+
networkName := "ip-" + strings.ReplaceAll(strings.ReplaceAll(a.DefaultMapping.Ip, ".", "-"), ":", "-")
208+
networkMode = container.NetworkMode(networkName)
209+
210+
if _, err := e.client.NetworkInspect(ctx, networkName, network.InspectOptions{}); err != nil {
211+
if !client.IsErrNotFound(err) {
212+
return err
213+
}
208214

209-
if _, err := e.client.NetworkCreate(ctx, networkName, network.CreateOptions{
210-
Driver: "bridge",
211-
EnableIPv6: &enableIPv6,
212-
Internal: false,
213-
Attachable: false,
214-
Ingress: false,
215-
ConfigOnly: false,
216-
Options: map[string]string{
217-
"encryption": "false",
218-
"com.docker.network.bridge.default_bridge": "false",
219-
"com.docker.network.host_ipv4": a.DefaultMapping.Ip,
220-
},
221-
}); err != nil {
222-
return err
215+
if _, err := e.client.NetworkCreate(ctx, networkName, network.CreateOptions{
216+
Driver: "bridge",
217+
EnableIPv6: &enableIPv6,
218+
Internal: false,
219+
Attachable: false,
220+
Ingress: false,
221+
ConfigOnly: false,
222+
Options: map[string]string{
223+
"encryption": "false",
224+
"com.docker.network.bridge.default_bridge": "false",
225+
"com.docker.network.host_ipv4": a.DefaultMapping.Ip,
226+
},
227+
}); err != nil {
228+
return err
229+
}
223230
}
231+
} else {
232+
e.log().Warn("environment/docker: Cannot force outgoing IP - server has no allocation")
224233
}
225234
}
226235

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,4 @@ require (
151151
modernc.org/mathutil v1.6.0 // indirect
152152
modernc.org/memory v1.8.0 // indirect
153153
modernc.org/sqlite v1.29.6 // indirect
154-
)
154+
)

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,4 +714,4 @@ nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYm
714714
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
715715
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
716716
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
717-
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
717+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

server/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func DetermineServerTimezone(envvars map[string]interface{}, defaultTimezone str
166166
return defaultTimezone
167167
}
168168

169+
169170
// parseInvocation parses the start command in the same way we already do in the entrypoint
170171
// We can use this to set the container command with all variables replaced.
171172
func parseInvocation(invocation string, envvars map[string]interface{}, memory int64, port int, ip string) (parsed string) {

0 commit comments

Comments
 (0)