AmitKumarDas / DC

0 stars 0 forks source link

Check for existence of finalizer(s) for any k8s resource custom or native #1

Open AmitKumarDas opened 5 years ago

AmitKumarDas commented 5 years ago

Motivation

Avoid repetitive codebase such as follows:

// IsFinalizerExistsOnBDC returns true if the object with provided name contains the finalizer.
func (ops *Operations) IsFinalizerExistsOnBDC(bdcName, finalizer string) bool {
    for i := 0; i < maxRetry; i++ {
        bdcObj, err := ops.BDCClient.Get(bdcName, metav1.GetOptions{})
        Expect(err).To(BeNil())
        for _, f := range bdcObj.Finalizers {
            if f == finalizer {
                return true
            }
        }
        time.Sleep(5 * time.Second)
    }
    return false
}
// IsSPCFinalizerExistsOnSPC returns true if the spc with provided name contains the spc finalizer.
func (ops *Operations) IsSPCFinalizerExistsOnSPC(spcName, spcFinalizer string) bool {
    for i := 0; i < maxRetry; i++ {
        gotSPC, err := ops.SPCClient.Get(spcName, metav1.GetOptions{})
        Expect(err).To(BeNil())
        for _, finalizer := range gotSPC.Finalizers {
            if finalizer == spcFinalizer {
                return true
            }
        }
        time.Sleep(5 * time.Second)
    }
    return false
}
// IsSPCFinalizerExistsOnBDCs returns true if the all the BDCs( selected by provided list options)
// has spc finalizer on it.
func (ops *Operations) IsSPCFinalizerExistsOnBDCs(listOptions metav1.ListOptions, spcFinalizer string) bool {
    for i := 0; i < maxRetry; i++ {
        spcFinalizerPresent := true
        gotBDCList, err := ops.BDCClient.List(listOptions)
        Expect(err).To(BeNil())
        for _, BDCObj := range gotBDCList.Items {
            BDCObj := BDCObj
            if !bdc.BuilderForAPIObject(&BDCObj).BDC.HasFinalizer(spcFinalizer) {
                spcFinalizerPresent = false
            }
        }
        if !spcFinalizerPresent {
            time.Sleep(5 * time.Second)
        } else {
            return true
        }
    }
    return false
}