github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/upgrade/v041_042/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 v041_042
    18  
    19  import (
    20  	"context"
    21  
    22  	apis "github.com/openebs/node-disk-manager/api/v1alpha1"
    23  	"sigs.k8s.io/controller-runtime/pkg/client"
    24  )
    25  
    26  // UpgradeTask is the struct which implements the Task interface
    27  // which can be used to perform the upgrade
    28  type UpgradeTask struct {
    29  	from   string
    30  	to     string
    31  	client client.Client
    32  	err    error
    33  }
    34  
    35  // NewUpgradeTask creates a new upgrade task with given client
    36  // and specified `from` and `to` version
    37  func NewUpgradeTask(from, to string, c client.Client) *UpgradeTask {
    38  	return &UpgradeTask{from: from, to: to, client: c}
    39  }
    40  
    41  // PreUpgrade runs the pre-upgrade tasks and returns whether it succeeded or not
    42  func (p *UpgradeTask) PreUpgrade() bool {
    43  	var err error
    44  	bdcList := &apis.BlockDeviceClaimList{}
    45  	err = p.client.List(context.TODO(), bdcList)
    46  	if err != nil {
    47  		p.err = err
    48  		return false
    49  	}
    50  
    51  	for i := range bdcList.Items {
    52  		err = p.copyHostName(&bdcList.Items[i])
    53  		if err != nil {
    54  			p.err = err
    55  			return false
    56  		}
    57  	}
    58  	return true
    59  }
    60  
    61  // IsSuccess returns error if the upgrade failed, at any step. Else nil will
    62  // be returned
    63  func (p *UpgradeTask) IsSuccess() error {
    64  	return p.err
    65  }
    66  
    67  // copyHostName will copy the hostname string from .spec.hostName to
    68  // .spec.nodeAttributes.hostName
    69  func (p *UpgradeTask) copyHostName(claim *apis.BlockDeviceClaim) error {
    70  	// copy the value only if .spec.hostName is non-empty and nodeAttributes.hostName
    71  	// is empty. If hostname field is empty, it is not required to copy the value, as
    72  	// it may be a new BDC.
    73  	if len(claim.Spec.HostName) != 0 &&
    74  		len(claim.Spec.BlockDeviceNodeAttributes.HostName) == 0 {
    75  		claim.Spec.BlockDeviceNodeAttributes.HostName = claim.Spec.HostName
    76  		return p.client.Update(context.TODO(), claim)
    77  	}
    78  	return nil
    79  }