@@ -18,8 +18,8 @@ package machine
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
- "log"
23
23
"strings"
24
24
"time"
25
25
@@ -29,6 +29,7 @@ import (
29
29
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
30
30
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/util"
31
31
"github.com/packethost/packngo"
32
+ "k8s.io/klog"
32
33
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
33
34
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
34
35
capiutil "sigs.k8s.io/cluster-api/pkg/util"
@@ -82,13 +83,19 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
82
83
if machine == nil {
83
84
return fmt .Errorf ("cannot create nil machine" )
84
85
}
86
+ var msg string
87
+ klog .Infof ("Create machine %s" , machine .Name )
85
88
machineConfig , err := util .MachineProviderFromProviderConfig (machine .Spec .ProviderSpec )
86
89
if err != nil {
87
- return fmt .Errorf ("Unable to read providerSpec from machine config: %v" , err )
90
+ msg = fmt .Sprintf ("Unable to read providerSpec from machine config: %v" , err )
91
+ klog .Info (msg )
92
+ return errors .New (msg )
88
93
}
89
94
clusterConfig , err := util .ClusterProviderFromProviderConfig (cluster .Spec .ProviderSpec )
90
95
if err != nil {
91
- return fmt .Errorf ("unable to unpack cluster provider: %v" , err )
96
+ msg = fmt .Sprintf ("Error reading cluster provider config: %v" , err )
97
+ klog .Info (msg )
98
+ return errors .New (msg )
92
99
}
93
100
94
101
// generate a unique UID that will survive pivot, i.e. is not tied to the cluster itself
@@ -102,7 +109,9 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
102
109
// first we need to find the correct userdata
103
110
userdataTmpl , containerRuntime , err := a .machineConfigGetter .GetUserdata (machineConfig .OS , machine .Spec .Versions )
104
111
if err != nil {
105
- return fmt .Errorf ("Unable to read userdata: %v" , err )
112
+ msg = fmt .Sprintf ("Unable to read userdata: %v" , err )
113
+ klog .Info (msg )
114
+ return errors .New (msg )
106
115
}
107
116
var (
108
117
token = ""
@@ -111,27 +120,37 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
111
120
caKey []byte
112
121
)
113
122
if machine .Spec .Versions .ControlPlane != "" {
123
+ klog .Infof ("building master controlplane node: %s" , machine .Name )
114
124
role = "master"
115
125
caCert = clusterConfig .CAKeyPair .Cert
116
126
caKey = clusterConfig .CAKeyPair .Key
117
127
if len (caCert ) == 0 {
118
- return fmt .Errorf ("CA Certificate not yet created" )
128
+ msg = fmt .Sprintf ("CA Certificate not yet created for cluster %s when building machine: %s" , cluster .Name , machine .Name )
129
+ klog .Info (msg )
130
+ return errors .New (msg )
119
131
}
120
132
if len (caKey ) == 0 {
121
- return fmt .Errorf ("CA Key not yet created" )
133
+ msg = fmt .Sprintf ("CA Key not yet created for cluster %s when building machine: %s" , cluster .Name , machine .Name )
134
+ klog .Info (msg )
135
+ return errors .New (msg )
122
136
}
123
137
tags = append (tags , util .MasterTag )
124
138
} else {
139
+ klog .Infof ("building worker controlplane node: %s" , machine .Name )
125
140
token , err = a .deployer .NewBootstrapToken (cluster )
126
141
if err != nil {
127
- return fmt .Errorf ("failed to create and save token for cluster %q: %v" , cluster .Name , err )
142
+ msg = fmt .Sprintf ("failed to create and save token for cluster %q: %v" , cluster .Name , err )
143
+ klog .Info (msg )
144
+ return errors .New (msg )
128
145
}
129
146
tags = append (tags , util .WorkerTag )
130
147
}
131
148
132
149
userdata , err := parseUserdata (userdataTmpl , role , cluster , machine , machineConfig .OS , token , caCert , caKey , a .controlPort , containerRuntime )
133
150
if err != nil {
134
- return fmt .Errorf ("Unable to generate userdata: %v" , err )
151
+ msg = fmt .Sprintf ("Unable to generate userdata for machine %s: %v" , machine .Name , err )
152
+ klog .Info (msg )
153
+ return errors .New (msg )
135
154
}
136
155
137
156
log .Printf ("Creating machine %v for cluster %v." , machine .Name , cluster .Name )
@@ -148,11 +167,13 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
148
167
149
168
device , _ , err := a .packetClient .Devices .Create (serverCreateOpts )
150
169
if err != nil {
151
- return fmt .Errorf ("failed to create server: %v" , err )
170
+ msg = fmt .Sprintf ("failed to create machine %s: %v" , machine .Name , err )
171
+ klog .Info (msg )
172
+ return errors .New (msg )
152
173
}
153
174
154
175
// we need to loop here until the device exists and has an IP address
155
- log .Printf ("Created device, waiting for it to be ready" )
176
+ log .Printf ("Created device %s , waiting for it to be ready" , machine . Name )
156
177
a .waitForMachineReady (device )
157
178
158
179
// add the annotations so that cluster-api knows it is there (also, because it is useful to have)
@@ -167,6 +188,8 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
167
188
if _ , err = a .updateMachine (cluster , machine ); err != nil {
168
189
return fmt .Errorf ("error updating Machine object with annotations: %v" , err )
169
190
}
191
+ msg = fmt .Sprintf ("machine successfully created and annotated %s" , machine .Name )
192
+ klog .Info (msg )
170
193
171
194
return nil
172
195
}
@@ -248,15 +271,22 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi
248
271
if machine == nil {
249
272
return false , fmt .Errorf ("cannot check if nil machine exists" )
250
273
}
274
+ var msg string
251
275
log .Printf ("Checking if machine %v for cluster %v exists." , machine .Name , cluster .Name )
252
276
device , err := a .packetClient .GetDevice (machine )
253
277
if err != nil {
254
- return false , fmt .Errorf ("error retrieving machine status %s: %v" , machine .Name , err )
278
+ msg = fmt .Sprintf ("error retrieving machine status %s: %v" , machine .Name , err )
279
+ klog .Info (msg )
280
+ return false , errors .New (msg )
255
281
}
256
282
if device == nil {
283
+ msg = fmt .Sprintf ("machine not found %s" , machine .Name )
284
+ klog .Info (msg )
257
285
return false , nil
258
286
}
259
287
288
+ msg = fmt .Sprintf ("machine found %s: %s" , machine .Name , device .ID )
289
+ klog .Info (msg )
260
290
return true , nil
261
291
}
262
292
@@ -289,7 +319,7 @@ func (a *Actuator) updateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
289
319
290
320
func (a * Actuator ) waitForMachineReady (device * packngo.Device ) error {
291
321
err := capiutil .PollImmediate (retryIntervalMachineReady , timeoutMachineReady , func () (bool , error ) {
292
- fmt . Printf ("Waiting for device %v to become ready..." , device .ID )
322
+ klog . Infof ("Waiting for device %v to become ready..." , device .ID )
293
323
dev , _ , err := a .packetClient .Devices .Get (device .ID , nil )
294
324
if err != nil {
295
325
return false , nil
0 commit comments