@@ -22,40 +22,42 @@ import (
22
22
type Driver struct {
23
23
* drivers.BaseDriver
24
24
25
- AccessToken string
26
- Image string
27
- ImageID int
28
- cachedImage * hcloud.Image
29
- Type string
30
- cachedType * hcloud.ServerType
31
- Location string
32
- cachedLocation * hcloud.Location
33
- KeyID int
34
- cachedKey * hcloud.SSHKey
35
- IsExistingKey bool
36
- originalKey string
37
- danglingKey bool
38
- ServerID int
39
- userData string
40
- volumes []string
41
- networks []string
42
- cachedServer * hcloud.Server
25
+ AccessToken string
26
+ Image string
27
+ ImageID int
28
+ cachedImage * hcloud.Image
29
+ Type string
30
+ cachedType * hcloud.ServerType
31
+ Location string
32
+ cachedLocation * hcloud.Location
33
+ KeyID int
34
+ cachedKey * hcloud.SSHKey
35
+ IsExistingKey bool
36
+ originalKey string
37
+ danglingKey bool
38
+ ServerID int
39
+ userData string
40
+ volumes []string
41
+ networks []string
42
+ UsePrivateNetwork bool
43
+ cachedServer * hcloud.Server
43
44
}
44
45
45
46
const (
46
47
defaultImage = "ubuntu-18.04"
47
48
defaultType = "cx11"
48
49
49
- flagAPIToken = "hetzner-api-token"
50
- flagImage = "hetzner-image"
51
- flagImageID = "hetzner-image-id"
52
- flagType = "hetzner-server-type"
53
- flagLocation = "hetzner-server-location"
54
- flagExKeyID = "hetzner-existing-key-id"
55
- flagExKeyPath = "hetzner-existing-key-path"
56
- flagUserData = "hetzner-user-data"
57
- flagVolumes = "hetzner-volumes"
58
- flagNetworks = "hetzner-networks"
50
+ flagAPIToken = "hetzner-api-token"
51
+ flagImage = "hetzner-image"
52
+ flagImageID = "hetzner-image-id"
53
+ flagType = "hetzner-server-type"
54
+ flagLocation = "hetzner-server-location"
55
+ flagExKeyID = "hetzner-existing-key-id"
56
+ flagExKeyPath = "hetzner-existing-key-path"
57
+ flagUserData = "hetzner-user-data"
58
+ flagVolumes = "hetzner-volumes"
59
+ flagNetworks = "hetzner-networks"
60
+ flagUsePrivateNetwork = "hetzner-use-private-network"
59
61
)
60
62
61
63
func NewDriver () * Driver {
@@ -136,6 +138,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
136
138
Usage : "Network IDs or names which should be attached to the server private network interface" ,
137
139
Value : []string {},
138
140
},
141
+ mcnflag.BoolFlag {
142
+ EnvVar : "HETZNER_USE_PRIVATE_NETWORK" ,
143
+ Name : flagUsePrivateNetwork ,
144
+ Usage : "Use private network" ,
145
+ },
139
146
}
140
147
}
141
148
@@ -151,6 +158,7 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
151
158
d .userData = opts .String (flagUserData )
152
159
d .volumes = opts .StringSlice (flagVolumes )
153
160
d .networks = opts .StringSlice (flagNetworks )
161
+ d .UsePrivateNetwork = opts .Bool (flagUsePrivateNetwork )
154
162
155
163
d .SetSwarmConfigFromFlags (opts )
156
164
@@ -205,6 +213,10 @@ func (d *Driver) PreCreateCheck() error {
205
213
return errors .Wrap (err , "could not get location" )
206
214
}
207
215
216
+ if d .UsePrivateNetwork && len (d .networks ) == 0 {
217
+ return errors .Errorf ("No private network attached." )
218
+ }
219
+
208
220
return nil
209
221
}
210
222
@@ -319,9 +331,26 @@ func (d *Driver) Create() error {
319
331
time .Sleep (1 * time .Second )
320
332
}
321
333
322
- log .Debugf (" -> Server %s[%d] ready" , srv .Server .Name , srv .Server .ID )
323
- d .IPAddress = srv .Server .PublicNet .IPv4 .IP .String ()
334
+ if d .UsePrivateNetwork {
335
+ for {
336
+ // we need to wait until network is attached
337
+ log .Infof ("Wait until private network attached ..." )
338
+ server , _ , err := d .getClient ().Server .GetByID (context .Background (), srv .Server .ID )
339
+ if err != nil {
340
+ return errors .Wrapf (err , "could not get newly created server [%d]" , srv .Server .ID )
341
+ }
342
+ if server .PrivateNet != nil {
343
+ d .IPAddress = server .PrivateNet [0 ].IP .String ()
344
+ break
345
+ }
346
+ time .Sleep (1 * time .Second )
347
+ }
348
+ } else {
349
+ log .Infof ("Using public network ..." )
350
+ d .IPAddress = srv .Server .PublicNet .IPv4 .IP .String ()
351
+ }
324
352
353
+ log .Infof (" -> Server %s[%d] ready. Ip %s" , srv .Server .Name , srv .Server .ID , d .IPAddress )
325
354
d .danglingKey = false
326
355
327
356
return nil
0 commit comments