WIP: Delete CR now properly handles a sip finalizer.

This commit is contained in:
Rodolfo Pacheco
2020-11-11 12:23:48 -05:00
parent dba81c5b6c
commit dc220e8645
3 changed files with 44 additions and 11 deletions

Binary file not shown.

View File

@@ -58,6 +58,9 @@ func (r *SIPClusterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
}
// Check for Deletion
// name of our custom finalizer
sipFinalizerName := "sip.airship.airshipit.org/finalizer"
// Tghis only works if I add a finalizer to CRD TODO
if sip.ObjectMeta.DeletionTimestamp.IsZero() {
// machines
err, machines := r.gatherVBMH(sip)
@@ -79,10 +82,19 @@ func (r *SIPClusterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error)
}
} else {
// Deleting the SIP , what do we do now
err := r.finalize(sip)
if err != nil {
log.Error(err, "unable to finalize")
return ctrl.Result{}, err
if containsString(sip.ObjectMeta.Finalizers, sipFinalizerName) {
// our finalizer is present, so lets handle any external dependency
err := r.finalize(sip)
if err != nil {
log.Error(err, "unable to finalize")
return ctrl.Result{}, err
}
// remove our finalizer from the list and update it.
sip.ObjectMeta.Finalizers = removeString(sip.ObjectMeta.Finalizers, sipFinalizerName)
if err := r.Update(context.Background(), &sip); err != nil {
return ctrl.Result{}, err
}
}
}
@@ -95,6 +107,27 @@ func (r *SIPClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}
// Helper functions to check and remove string from a slice of strings.
// There might be a golang funuction to do this . Will check later
func containsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
func removeString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}
/*
### Gather Phase

View File

@@ -613,28 +613,28 @@ func (ml *MachineList) RemoveLabels(sip airshipv1.SIPCluster, c client.Client) e
for _, machine := range ml.bmhs {
bmh := &machine.Bmh
fmt.Printf("ApplyLabels bmh.ObjectMeta.Name:%s\n", bmh.ObjectMeta.Name)
bmh.Labels[SipClusterLabel] = "-" // REMOVE IT
fmt.Printf("RemoveLabels bmh.ObjectMeta.Name:%s\n", bmh.ObjectMeta.Name)
bmh.Labels[SipClusterLabel] = "" // REMOVE IT TODO This only blanks it out doesnt remove the label
bmh.Labels[SipScheduleLabel] = "false"
bmh.Labels[SipNodeTypeLabel] = "-" // REMOVE IT
bmh.Labels[SipNodeTypeLabel] = "" // REMOVE IT
// This is bombing when it find 1 error
// Might be better to acculumalte the errors, and
// Allow it to continue.
err := c.Update(context.Background(), bmh)
if err != nil {
fmt.Printf("ApplyLabel bmh:%s err:%v\n", bmh.ObjectMeta.Name, err)
fmt.Printf("RemoveLabels bmh:%s err:%v\n", bmh.ObjectMeta.Name, err)
return err
}
}
return nil
}
func (ml *MachineList) addLabels(bmh metal3.BareMetalHost, m *Machine) {
}
func (ml *MachineList) GetCluster(sip airshipv1.SIPCluster, c client.Client) error {
// Initialize teh Target list
ml.bmhs = ml.init(sip.Spec.Nodes)
bmhList := &metal3.BareMetalHostList{}
scheduleLabels := map[string]string{
SipScheduleLabel: "true",