@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"time"
24
24
25
+ "github.com/go-logr/logr"
25
26
apicorev1 "k8s.io/api/core/v1"
26
27
"k8s.io/apimachinery/pkg/types"
27
28
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -45,116 +46,109 @@ const (
45
46
type Machine struct {
46
47
Core corev1.CoreV1Interface
47
48
ClusterAPI v1alpha1.ClusterV1alpha1Interface
48
- }
49
-
50
- // NewMachineActuator returns a new machine actuator object
51
- func NewMachineActuator (clusterapi v1alpha1.ClusterV1alpha1Interface , core corev1.CoreV1Interface ) * Machine {
52
- return & Machine {
53
- Core : core ,
54
- ClusterAPI : clusterapi ,
55
- }
49
+ Log logr.Logger
56
50
}
57
51
58
52
// Create creates a machine for a given cluster
59
53
// Note: have to print all the errors because cluster-api swallows them
60
54
func (m * Machine ) Create (ctx context.Context , c * clusterv1.Cluster , machine * clusterv1.Machine ) error {
61
55
old := machine .DeepCopy ()
62
- fmt . Printf ("Creating a machine for cluster %q \n " , c .Name )
56
+ m . Log . Info ("Creating a machine for cluster" , "cluster-name " , c .Name )
63
57
clusterExists , err := cluster .IsKnown (c .Name )
64
58
if err != nil {
65
- fmt . Printf ( "%+v " , err )
59
+ m . Log . Error ( err , "Error finding cluster-name " , "cluster" , c . Name )
66
60
return err
67
61
}
68
62
// If there's no cluster, requeue the request until there is one
69
63
if ! clusterExists {
70
- fmt . Println ("There is no cluster yet, waiting for a cluster before creating machines" )
64
+ m . Log . Info ("There is no cluster yet, waiting for a cluster before creating machines" )
71
65
return & capierror.RequeueAfterError {RequeueAfter : 30 * time .Second }
72
66
}
73
67
74
68
controlPlanes , err := actions .ListControlPlanes (c .Name )
75
69
if err != nil {
76
- fmt . Printf ( "%+v \n " , err )
70
+ m . Log . Error ( err , "Error listing control planes" )
77
71
return err
78
72
}
79
- fmt . Printf ("Is there a cluster? %v \n " , clusterExists )
73
+ m . Log . Info ("Is there a cluster?" , "cluster-exists " , clusterExists )
80
74
setValue := getRole (machine )
81
- fmt . Printf ("This node has a role of %q \n " , setValue )
75
+ m . Log . Info ("This node has a role" , "role " , setValue )
82
76
if setValue == clusterAPIControlPlaneSetLabel {
83
77
if len (controlPlanes ) > 0 {
84
- fmt . Println ("Adding a control plane" )
78
+ m . Log . Info ("Adding a control plane" )
85
79
controlPlaneNode , err := actions .AddControlPlane (c .Name , machine .GetName (), machine .Spec .Versions .ControlPlane )
86
80
if err != nil {
87
- fmt . Printf ( "%+v" , err )
81
+ m . Log . Error ( err , "Error adding control plane" )
88
82
return err
89
83
}
90
84
nodeUID , err := actions .GetNodeRefUID (c .GetName (), controlPlaneNode .Name ())
91
85
if err != nil {
92
- fmt . Printf ( "%+v" , err )
86
+ m . Log . Error ( err , "Error getting node reference UID" )
93
87
return err
94
88
}
95
89
providerID := providerID (controlPlaneNode .Name ())
96
90
machine .Spec .ProviderID = & providerID
97
91
return m .save (old , machine , getNodeRef (controlPlaneNode .Name (), nodeUID ))
98
92
}
99
93
100
- fmt . Println ("Creating a brand new cluster" )
94
+ m . Log . Info ("Creating a brand new cluster" )
101
95
elb , err := getExternalLoadBalancerNode (c .Name )
102
96
if err != nil {
103
- fmt . Printf ( "%+v \n " , err )
97
+ m . Log . Error ( err , "Error getting external load balancer node" )
104
98
return err
105
99
}
106
100
lbip , err := elb .IP ()
107
101
if err != nil {
108
- fmt . Printf ( "%+v \n " , err )
102
+ m . Log . Error ( err , "Error getting node IP address" )
109
103
return err
110
104
}
111
105
controlPlaneNode , err := actions .CreateControlPlane (c .Name , machine .GetName (), lbip , machine .Spec .Versions .ControlPlane )
112
106
if err != nil {
113
- fmt . Printf ( "%+v \n " , err )
107
+ m . Log . Error ( err , "Error creating control plane" )
114
108
return err
115
109
}
116
110
nodeUID , err := actions .GetNodeRefUID (c .GetName (), controlPlaneNode .Name ())
117
111
if err != nil {
118
- fmt . Printf ( "%+v" , err )
112
+ m . Log . Error ( err , "Error getting node reference UID" )
119
113
return err
120
114
}
121
115
// set the machine's providerID
122
116
providerID := providerID (controlPlaneNode .Name ())
123
117
machine .Spec .ProviderID = & providerID
124
118
if err := m .save (old , machine , getNodeRef (controlPlaneNode .Name (), nodeUID )); err != nil {
125
- fmt . Printf ( "%+v \n " , err )
119
+ m . Log . Error ( err , "Error setting machine's provider ID" )
126
120
return err
127
121
}
128
122
s , err := kubeconfigToSecret (c .Name , c .Namespace )
129
123
if err != nil {
130
- fmt . Printf ( "%+v \n " , err )
124
+ m . Log . Error ( err , "Error converting kubeconfig to a secret" )
131
125
return err
132
126
}
133
127
// Save the secret to the management cluster
134
128
if _ , err := m .Core .Secrets (machine .GetNamespace ()).Create (s ); err != nil {
135
- fmt . Printf ( "%+v \n " , err )
129
+ m . Log . Error ( err , "Error saving secret to management cluster" )
136
130
return err
137
131
}
138
132
return nil
139
133
}
140
134
141
135
// If there are no control plane then we should hold off on joining workers
142
136
if len (controlPlanes ) == 0 {
143
- fmt . Printf ("Sending machine %q back since there is no cluster to join\n " , machine .Name )
137
+ m . Log . Info ("Sending machine back since there is no cluster to join" , "machine " , machine .Name )
144
138
return & capierror.RequeueAfterError {RequeueAfter : 30 * time .Second }
145
139
}
146
140
147
- fmt . Println ("Creating a new worker node" )
141
+ m . Log . Info ("Creating a new worker node" )
148
142
worker , err := actions .AddWorker (c .Name , machine .GetName (), machine .Spec .Versions .Kubelet )
149
143
if err != nil {
150
- fmt . Printf ( "%+v" , err )
144
+ m . Log . Error ( err , "Error creating new worker node" )
151
145
return err
152
146
}
153
147
providerID := providerID (worker .Name ())
154
148
machine .Spec .ProviderID = & providerID
155
149
nodeUID , err := actions .GetNodeRefUID (c .GetName (), worker .Name ())
156
150
if err != nil {
157
- fmt . Printf ( "%+v" , err )
151
+ m . Log . Error ( err , "Error getting node reference ID" )
158
152
return err
159
153
}
160
154
return m .save (old , machine , getNodeRef (worker .Name (), nodeUID ))
@@ -169,18 +163,18 @@ func (m *Machine) Delete(ctx context.Context, cluster *clusterv1.Cluster, machin
169
163
if exists {
170
164
setValue := getRole (machine )
171
165
if setValue == clusterAPIControlPlaneSetLabel {
172
- fmt . Printf ("Deleting a control plane: %q \n " , machine .GetName ())
166
+ m . Log . Info ("Deleting a control plane" , "machine " , machine .GetName ())
173
167
return actions .DeleteControlPlane (cluster .Name , machine .GetName ())
174
168
}
175
- fmt . Printf ("Deleting a worker: %q \n " , machine .GetName ())
169
+ m . Log . Info ("Deleting a worker" , "machine " , machine .GetName ())
176
170
return actions .DeleteWorker (cluster .Name , machine .GetName ())
177
171
}
178
172
return nil
179
173
}
180
174
181
175
// Update updates a machine
182
176
func (m * Machine ) Update (ctx context.Context , cluster * clusterv1.Cluster , machine * clusterv1.Machine ) error {
183
- fmt . Println ("Update machine is not implemented yet. " )
177
+ m . Log . Info ("Update machine is not implemented yet" )
184
178
return nil
185
179
}
186
180
@@ -197,41 +191,41 @@ func (m *Machine) Exists(ctx context.Context, cluster *clusterv1.Cluster, machin
197
191
fmt .Sprintf ("label=%s=%s" , constants .ClusterLabelKey , cluster .Name ),
198
192
fmt .Sprintf ("name=^%s$" , machine .GetName ()),
199
193
}
200
- fmt . Printf ("using labels: %v \n " , labels )
194
+ m . Log . Info ("using labels" , "labels " , labels )
201
195
nodeList , err := nodes .List (labels ... )
202
196
if err != nil {
203
197
return false , err
204
198
}
205
- fmt . Printf ("found nodes: %v \n " , nodeList )
199
+ m . Log . Info ("found nodes" , "nodes " , nodeList )
206
200
return len (nodeList ) >= 1 , nil
207
201
}
208
202
209
203
// patches the object and saves the status.
210
204
func (m * Machine ) save (old , new * clusterv1.Machine , noderef * apicorev1.ObjectReference ) error {
211
- fmt . Println ("updating machine" )
205
+ m . Log . Info ("updating machine" )
212
206
p , err := patch .NewJSONPatch (old , new )
213
207
if err != nil {
214
- fmt . Printf ( "%+v \n " , err )
208
+ m . Log . Error ( err , "Error updating machine" )
215
209
return err
216
210
}
217
- fmt . Println ("Patches for machine" , p )
211
+ m . Log . Info ("Patches for machine" , "patches " , p )
218
212
if len (p ) != 0 {
219
213
pb , err := json .MarshalIndent (p , "" , " " )
220
214
if err != nil {
221
- fmt . Printf ( "%+v \n " , err )
215
+ m . Log . Error ( err , "Error marshalling machine" )
222
216
return err
223
217
}
224
218
new , err = m .ClusterAPI .Machines (old .Namespace ).Patch (new .Name , types .JSONPatchType , pb )
225
219
if err != nil {
226
- fmt . Printf ( "%+v \n " , err )
220
+ m . Log . Error ( err , "Error patching machine" )
227
221
return err
228
222
}
229
- fmt . Println ("updated machine" )
223
+ m . Log . Info ("updated machine" )
230
224
}
231
225
// set the noderef after so we don't try and patch it in during the first update
232
226
new .Status .NodeRef = noderef
233
227
if _ , err := m .ClusterAPI .Machines (old .Namespace ).UpdateStatus (new ); err != nil {
234
- fmt . Printf ( "%+v \n " , err )
228
+ m . Log . Error ( err , "Error setting node reference" )
235
229
return err
236
230
}
237
231
return nil
0 commit comments