github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/upgrade/v040_041/preupgrade.go (about)

     1  /*
     2  Copyright 2019 The OpenEBS Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v040_041
    18  
    19  import (
    20  	"context"
    21  
    22  	apis "github.com/openebs/node-disk-manager/api/v1alpha1"
    23  	"github.com/openebs/node-disk-manager/pkg/util"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  const (
    28  	// oldBDCFinalizer is the old string from which BDC should be updated
    29  	oldBDCFinalizer = "blockdeviceclaim.finalizer"
    30  	// newBDCFinalizer is the new string to which BDC to be updated
    31  	newBDCFinalizer = "openebs.io/bdc-protection"
    32  )
    33  
    34  // UpgradeTask is the struct which implements the Task interface
    35  // which can be used to perform the upgrade
    36  type UpgradeTask struct {
    37  	from   string
    38  	to     string
    39  	client client.Client
    40  	err    error
    41  }
    42  
    43  // NewUpgradeTask creates a new pre-upgrade with given client
    44  // and specified `from` and `to` version
    45  func NewUpgradeTask(from, to string, c client.Client) *UpgradeTask {
    46  	return &UpgradeTask{from: from, to: to, client: c}
    47  }
    48  
    49  // PreUpgrade runs the pre-upgrade tasks and returns whether it succeeded or not
    50  func (p *UpgradeTask) PreUpgrade() bool {
    51  	var err error
    52  	bdcList := &apis.BlockDeviceClaimList{}
    53  	err = p.client.List(context.TODO(), bdcList)
    54  	if err != nil {
    55  		p.err = err
    56  		return false
    57  	}
    58  
    59  	for i := range bdcList.Items {
    60  		err = p.renameFinalizer(&bdcList.Items[i])
    61  		if err != nil {
    62  			p.err = err
    63  			return false
    64  		}
    65  	}
    66  	return true
    67  }
    68  
    69  // IsSuccess returns error if the upgrade failed, at any step. Else nil will
    70  // be returned
    71  func (p *UpgradeTask) IsSuccess() error {
    72  	return p.err
    73  }
    74  
    75  // renameFinalizer renames the finalizer from old to new in BDC
    76  func (p *UpgradeTask) renameFinalizer(claim *apis.BlockDeviceClaim) error {
    77  	if util.Contains(claim.Finalizers, oldBDCFinalizer) {
    78  		claim.Finalizers = util.RemoveString(claim.Finalizers, oldBDCFinalizer)
    79  		claim.Finalizers = append(claim.Finalizers, newBDCFinalizer)
    80  		return p.client.Update(context.TODO(), claim)
    81  	}
    82  	return nil
    83  }