@@ -45,6 +45,7 @@ type HostReconciler struct {
45
45
K8sInstaller IK8sInstaller
46
46
SkipK8sInstallation bool
47
47
UseInstallerController bool
48
+ DownloadPath string
48
49
}
49
50
50
51
const (
@@ -94,6 +95,8 @@ func (r *HostReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctr
94
95
95
96
func (r * HostReconciler ) reconcileNormal (ctx context.Context , byoHost * infrastructurev1beta1.ByoHost ) (ctrl.Result , error ) {
96
97
logger := ctrl .LoggerFrom (ctx )
98
+ logger = logger .WithValues ("ByoHost" , byoHost .Name )
99
+ logger .Info ("reconcile normal" )
97
100
if byoHost .Status .MachineRef == nil {
98
101
logger .Info ("Machine ref not yet set" )
99
102
conditions .MarkFalse (byoHost , infrastructurev1beta1 .K8sNodeBootstrapSucceeded , infrastructurev1beta1 .WaitingForMachineRefReason , clusterv1 .ConditionSeverityInfo , "" )
@@ -117,10 +120,20 @@ func (r *HostReconciler) reconcileNormal(ctx context.Context, byoHost *infrastru
117
120
if r .SkipK8sInstallation {
118
121
logger .Info ("Skipping installation of k8s components" )
119
122
} else if r .UseInstallerController {
120
- if byoHost .Spec .InstallationSecret == nil {
121
- logger .Info ("K8sInstallationSecret not ready" )
122
- conditions .MarkFalse (byoHost , infrastructurev1beta1 .K8sNodeBootstrapSucceeded , infrastructurev1beta1 .K8sInstallationSecretUnavailableReason , clusterv1 .ConditionSeverityInfo , "" )
123
- return ctrl.Result {}, nil
123
+ if ! conditions .IsTrue (byoHost , infrastructurev1beta1 .K8sComponentsInstallationSucceeded ) {
124
+ if byoHost .Spec .InstallationSecret == nil {
125
+ logger .Info ("InstallationSecret not ready" )
126
+ conditions .MarkFalse (byoHost , infrastructurev1beta1 .K8sComponentsInstallationSucceeded , infrastructurev1beta1 .K8sInstallationSecretUnavailableReason , clusterv1 .ConditionSeverityInfo , "" )
127
+ return ctrl.Result {}, nil
128
+ }
129
+ err = r .executeInstallerController (ctx , byoHost )
130
+ if err != nil {
131
+ return ctrl.Result {}, err
132
+ }
133
+ r .Recorder .Event (byoHost , corev1 .EventTypeNormal , "InstallScriptExecutionSucceeded" , "install script executed" )
134
+ conditions .MarkTrue (byoHost , infrastructurev1beta1 .K8sComponentsInstallationSucceeded )
135
+ } else {
136
+ logger .Info ("install script already executed" )
124
137
}
125
138
} else {
126
139
err = r .installK8sComponents (ctx , byoHost )
@@ -156,6 +169,34 @@ func (r *HostReconciler) reconcileNormal(ctx context.Context, byoHost *infrastru
156
169
return ctrl.Result {}, nil
157
170
}
158
171
172
+ func (r * HostReconciler ) executeInstallerController (ctx context.Context , byoHost * infrastructurev1beta1.ByoHost ) error {
173
+ logger := ctrl .LoggerFrom (ctx )
174
+ secret := & corev1.Secret {}
175
+ err := r .Client .Get (ctx , types.NamespacedName {Name : byoHost .Spec .InstallationSecret .Name , Namespace : byoHost .Spec .InstallationSecret .Namespace }, secret )
176
+ if err != nil {
177
+ logger .Error (err , "error getting install and uninstall script" )
178
+ r .Recorder .Eventf (byoHost , corev1 .EventTypeWarning , "ReadInstallationSecretFailed" , "install and uninstall script %s not found" , byoHost .Spec .InstallationSecret .Name )
179
+ return err
180
+ }
181
+ installScript := string (secret .Data ["install" ])
182
+ uninstallScript := string (secret .Data ["uninstall" ])
183
+
184
+ byoHost .Spec .UninstallationScript = & uninstallScript
185
+ installScript , err = r .parseScript (ctx , installScript )
186
+ if err != nil {
187
+ return err
188
+ }
189
+ logger .Info ("executing install script" )
190
+ err = r .CmdRunner .RunCmd (ctx , installScript )
191
+ if err != nil {
192
+ logger .Error (err , "error executing installation script" )
193
+ r .Recorder .Event (byoHost , corev1 .EventTypeWarning , "InstallScriptExecutionFailed" , "install script execution failed" )
194
+ conditions .MarkFalse (byoHost , infrastructurev1beta1 .K8sComponentsInstallationSucceeded , infrastructurev1beta1 .K8sComponentsInstallationFailedReason , clusterv1 .ConditionSeverityInfo , "" )
195
+ return err
196
+ }
197
+ return nil
198
+ }
199
+
159
200
func (r * HostReconciler ) reconcileDelete (ctx context.Context , byoHost * infrastructurev1beta1.ByoHost ) (ctrl.Result , error ) {
160
201
return ctrl.Result {}, nil
161
202
}
@@ -171,6 +212,18 @@ func (r *HostReconciler) getBootstrapScript(ctx context.Context, dataSecretName,
171
212
return bootstrapSecret , nil
172
213
}
173
214
215
+ func (r * HostReconciler ) parseScript (ctx context.Context , script string ) (string , error ) {
216
+ data , err := cloudinit.TemplateParser {
217
+ Template : map [string ]string {
218
+ "BundleDownloadPath" : r .DownloadPath ,
219
+ },
220
+ }.ParseTemplate (script )
221
+ if err != nil {
222
+ return "" , fmt .Errorf ("unable to apply install parsed template to the data object" )
223
+ }
224
+ return data , nil
225
+ }
226
+
174
227
// SetupWithManager sets up the controller with the manager
175
228
func (r * HostReconciler ) SetupWithManager (ctx context.Context , mgr manager.Manager ) error {
176
229
return ctrl .NewControllerManagedBy (mgr ).
@@ -215,6 +268,23 @@ func (r *HostReconciler) hostCleanUp(ctx context.Context, byoHost *infrastructur
215
268
}
216
269
if r .SkipK8sInstallation {
217
270
logger .Info ("Skipping uninstallation of k8s components" )
271
+ } else if r .UseInstallerController {
272
+ if byoHost .Spec .UninstallationScript == nil {
273
+ return fmt .Errorf ("UninstallationScript not found in Byohost %s" , byoHost .Name )
274
+ }
275
+ logger .Info ("Executing Uninstall script" )
276
+ uninstallScript := * byoHost .Spec .UninstallationScript
277
+ uninstallScript , err = r .parseScript (ctx , uninstallScript )
278
+ if err != nil {
279
+ logger .Error (err , "error parsing Uninstallation script" )
280
+ return err
281
+ }
282
+ err = r .CmdRunner .RunCmd (ctx , uninstallScript )
283
+ if err != nil {
284
+ logger .Error (err , "error execting Uninstallation script" )
285
+ r .Recorder .Event (byoHost , corev1 .EventTypeWarning , "UninstallScriptExecutionFailed" , "uninstall script execution failed" )
286
+ return err
287
+ }
218
288
} else {
219
289
err = r .uninstallk8sComponents (ctx , byoHost )
220
290
if err != nil {
@@ -236,6 +306,8 @@ func (r *HostReconciler) hostCleanUp(ctx context.Context, byoHost *infrastructur
236
306
return err
237
307
}
238
308
309
+ byoHost .Spec .InstallationSecret = nil
310
+ byoHost .Spec .UninstallationScript = nil
239
311
r .removeAnnotations (ctx , byoHost )
240
312
conditions .MarkFalse (byoHost , infrastructurev1beta1 .K8sNodeBootstrapSucceeded , infrastructurev1beta1 .K8sNodeAbsentReason , clusterv1 .ConditionSeverityInfo , "" )
241
313
return nil
@@ -245,7 +317,7 @@ func (r *HostReconciler) resetNode(ctx context.Context, byoHost *infrastructurev
245
317
logger := ctrl .LoggerFrom (ctx )
246
318
logger .Info ("Running kubeadm reset" )
247
319
248
- err := r .CmdRunner .RunCmd (KubeadmResetCommand )
320
+ err := r .CmdRunner .RunCmd (ctx , KubeadmResetCommand )
249
321
if err != nil {
250
322
r .Recorder .Event (byoHost , corev1 .EventTypeWarning , "ResetK8sNodeFailed" , "k8s Node Reset failed" )
251
323
return errors .Wrapf (err , "failed to exec kubeadm reset" )
0 commit comments