cleanup: reduce the code complexity of controller

created a new helper function to getCredentials.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2020-12-04 13:57:42 +05:30 committed by mergify[bot]
parent e243c0006b
commit b2fb43b335

View File

@ -81,7 +81,14 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil return nil
} }
func (r *ReconcilePersistentVolume) getCredentials(name, namespace string) (map[string]string, error) { func (r *ReconcilePersistentVolume) getCredentials(name, namespace string) (*util.Credentials, error) {
var cr *util.Credentials
if name == "" || namespace == "" {
errStr := "secret name or secret namespace is empty"
util.ErrorLogMsg(errStr)
return nil, errors.New(errStr)
}
secret := &corev1.Secret{} secret := &corev1.Secret{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, secret) err := r.client.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, secret)
if err != nil { if err != nil {
@ -92,12 +99,20 @@ func (r *ReconcilePersistentVolume) getCredentials(name, namespace string) (map[
for key, value := range secret.Data { for key, value := range secret.Data {
credentials[key] = string(value) credentials[key] = string(value)
} }
return credentials, nil
cr, err = util.NewUserCredentials(credentials)
if err != nil {
util.ErrorLogMsg("failed to get user credentials %s", err)
return nil, err
}
return cr, nil
} }
func checkStaticVolume(staticVol string) (bool, error) { func checkStaticVolume(pv *corev1.PersistentVolume) (bool, error) {
static := false static := false
var err error var err error
staticVol := pv.Spec.CSI.VolumeAttributes["staticVolume"]
if staticVol != "" { if staticVol != "" {
static, err = strconv.ParseBool(staticVol) static, err = strconv.ParseBool(staticVol)
if err != nil { if err != nil {
@ -123,7 +138,7 @@ func (r ReconcilePersistentVolume) reconcilePV(obj runtime.Object) error {
secretName := "" secretName := ""
secretNamespace := "" secretNamespace := ""
// check static volume // check static volume
static, err := checkStaticVolume(pv.Spec.CSI.VolumeAttributes["staticVolume"]) static, err := checkStaticVolume(pv)
if err != nil { if err != nil {
return err return err
} }
@ -138,23 +153,14 @@ func (r ReconcilePersistentVolume) reconcilePV(obj runtime.Object) error {
secretName = pv.Spec.CSI.NodeStageSecretRef.Name secretName = pv.Spec.CSI.NodeStageSecretRef.Name
secretNamespace = pv.Spec.CSI.NodeStageSecretRef.Namespace secretNamespace = pv.Spec.CSI.NodeStageSecretRef.Namespace
} }
if secretName == "" || secretNamespace == "" {
errStr := "secretname or secret namespace is empty"
util.ErrorLogMsg(errStr)
return errors.New(errStr)
}
secrets, err := r.getCredentials(secretName, secretNamespace) cr, err := r.getCredentials(secretName, secretNamespace)
if err != nil { if err != nil {
util.ErrorLogMsg("failed to get secrets %s", err) util.ErrorLogMsg("failed to get credentials %s", err)
return err
}
cr, err := util.NewUserCredentials(secrets)
if err != nil {
util.ErrorLogMsg("failed to get user credentials %s", err)
return err return err
} }
defer cr.DeleteCredentials() defer cr.DeleteCredentials()
err = rbd.RegenerateJournal(imageName, volumeHandler, pool, journalPool, requestName, cr) err = rbd.RegenerateJournal(imageName, volumeHandler, pool, journalPool, requestName, cr)
if err != nil { if err != nil {
util.ErrorLogMsg("failed to regenerate journal %s", err) util.ErrorLogMsg("failed to regenerate journal %s", err)