@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"log"
23
23
"strings"
24
+ "time"
24
25
25
26
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet"
26
27
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/actuators/machine/machineconfig"
@@ -29,10 +30,13 @@ import (
29
30
"github.com/packethost/packngo"
30
31
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
31
32
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
33
+ capiutil "sigs.k8s.io/cluster-api/pkg/util"
32
34
)
33
35
34
36
const (
35
- ProviderName = "packet"
37
+ ProviderName = "packet"
38
+ retryIntervalMachineReady = 10 * time .Second
39
+ timeoutMachineReady = 10 * time .Minute
36
40
)
37
41
38
42
// Add RBAC rules to access cluster-api resources
@@ -138,11 +142,15 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
138
142
Tags : tags ,
139
143
}
140
144
141
- _ , _ , err = a .packetClient .Devices .Create (serverCreateOpts )
145
+ device , _ , err : = a .packetClient .Devices .Create (serverCreateOpts )
142
146
if err != nil {
143
147
return fmt .Errorf ("failed to create server: %v" , err )
144
148
}
145
149
150
+ // we need to loop here until the device exists and has an IP address
151
+ log .Printf ("Created device, waiting for it to be ready" )
152
+ a .waitForMachineReady (device )
153
+
146
154
// add the annotations so that cluster-api knows it is there (also, because it is useful to have)
147
155
if machine .Annotations == nil {
148
156
machine .Annotations = map [string ]string {}
@@ -274,6 +282,21 @@ func (a *Actuator) updateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
274
282
return updatedMachine , nil
275
283
}
276
284
285
+ func (a * Actuator ) waitForMachineReady (device * packngo.Device ) error {
286
+ err := capiutil .PollImmediate (retryIntervalMachineReady , timeoutMachineReady , func () (bool , error ) {
287
+ fmt .Printf ("Waiting for device %v to become ready..." , device .ID )
288
+ dev , _ , err := a .packetClient .Devices .Get (device .ID , nil )
289
+ if err != nil {
290
+ return false , nil
291
+ }
292
+
293
+ ready := dev .Network == nil || len (dev .Network ) == 0 || dev .Network [0 ].Address == ""
294
+ return ready , nil
295
+ })
296
+
297
+ return err
298
+ }
299
+
277
300
func changedImmutable (projectID string , device * packngo.Device , machine * clusterv1.Machine ) error {
278
301
errors := []string {}
279
302
machineConfig , err := util .MachineProviderFromProviderConfig (machine .Spec .ProviderSpec )
0 commit comments