Skip to content

Commit

Permalink
feat(upgrade): add pre-upgrade tasks for 0.4.1 to 0.4.2 (#303)
Browse files Browse the repository at this point in the history
* fix node hostname label
* refactor the upgrade package. 
* add pre-upgrade steps for 0.4.1 to 0.4.2

Signed-off-by: Akhil Mohan <[email protected]>
  • Loading branch information
akhilerm authored and kmova committed Aug 29, 2019
1 parent 623e692 commit b677b25
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 19 deletions.
8 changes: 5 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"github.com/openebs/node-disk-manager/pkg/setup"
"github.com/openebs/node-disk-manager/pkg/upgrade"
"github.com/openebs/node-disk-manager/pkg/upgrade/v040_041"
"github.com/openebs/node-disk-manager/pkg/upgrade/v041_042"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -148,7 +150,7 @@ func main() {

// performUpgrade performs the upgrade operations
func performUpgrade(client client.Client) error {
//TODO: this task should be named for release, not for upgrade steps
preUpgradeTask := upgrade.NewPreUpgradeTask("1.0", "1.1", client)
return upgrade.RunUpgrade(preUpgradeTask)
v040_v041UpgradeTask := v040_041.NewUpgradeTask("0.4.0", "0.4.1", client)
v041_v042UpgradeTask := v041_042.NewUpgradeTask("0.4.1", "0.4.2", client)
return upgrade.RunUpgrade(v040_v041UpgradeTask, v041_v042UpgradeTask)
}
7 changes: 6 additions & 1 deletion pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package upgrade

import "fmt"

// Task interfaces gives a set of methods to be implemented
// for performing an upgrade
type Task interface {
Expand All @@ -32,7 +34,10 @@ func RunUpgrade(tasks ...Task) error {
for _, task := range tasks {
_ = task.PreUpgrade() && task.Upgrade() && task.PostUpgrade()
if err := task.IsSuccess(); err != nil {
return err
return fmt.Errorf("upgrade from %s to %s failed. Error : %v",
task.FromVersion(),
task.ToVersion(),
err)
}
}
return nil
Expand Down
29 changes: 14 additions & 15 deletions pkg/upgrade/preupgrade.go → pkg/upgrade/v040_041/preupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package upgrade
package v040_041

import (
"context"
Expand All @@ -30,34 +30,33 @@ const (
newBDCFinalizer = "openebs.io/bdc-protection"
)

// PreUpgrade is the struct which implements the UpgradeTask interface
// UpgradeTask is the struct which implements the Task interface
// which can be used to perform the upgrade
type PreUpgrade struct {
type UpgradeTask struct {
from string
to string
client client.Client
err error
}

// NewPreUpgradeTask creates a new preupgrade with given client
// NewUpgradeTask creates a new preupgrade with given client
// and specified `from` and `to` version
func NewPreUpgradeTask(from, to string, c client.Client) *PreUpgrade {
return &PreUpgrade{from: from, to: to, client: c}
func NewUpgradeTask(from, to string, c client.Client) *UpgradeTask {
return &UpgradeTask{from: from, to: to, client: c}
}

// FromVersion returns the version from which the components need to be updated
func (p *PreUpgrade) FromVersion() string {
func (p *UpgradeTask) FromVersion() string {
return p.from
}

// ToVersion returns the version to which components will be updated. This should be
// the current version
func (p *PreUpgrade) ToVersion() string {
// ToVersion returns the version to which components will be updated.
func (p *UpgradeTask) ToVersion() string {
return p.to
}

// PreUpgrade runs the preupgrade tasks and returns whether it succeeded or not
func (p *PreUpgrade) PreUpgrade() bool {
func (p *UpgradeTask) PreUpgrade() bool {
var err error
bdcList := &apis.BlockDeviceClaimList{}
opts := &client.ListOptions{}
Expand All @@ -78,7 +77,7 @@ func (p *PreUpgrade) PreUpgrade() bool {
}

// Upgrade runs the main upgrade tasks and returns whether it succeeded or not
func (p *PreUpgrade) Upgrade() bool {
func (p *UpgradeTask) Upgrade() bool {
if p.err != nil {
return false
}
Expand All @@ -87,7 +86,7 @@ func (p *PreUpgrade) Upgrade() bool {

// PostUpgrade runs the tasks that need to be performed after upgrade and returns
// whether the tasks where success or not
func (p *PreUpgrade) PostUpgrade() bool {
func (p *UpgradeTask) PostUpgrade() bool {
if p.err != nil {
return false
}
Expand All @@ -96,12 +95,12 @@ func (p *PreUpgrade) PostUpgrade() bool {

// IsSuccess returns error if the upgrade failed, at any step. Else nil will
// be returned
func (p *PreUpgrade) IsSuccess() error {
func (p *UpgradeTask) IsSuccess() error {
return p.err
}

// renameFinalizer renames the finalizer from old to new in BDC
func (p *PreUpgrade) renameFinalizer(claim *apis.BlockDeviceClaim) error {
func (p *UpgradeTask) renameFinalizer(claim *apis.BlockDeviceClaim) error {
if util.Contains(claim.Finalizers, oldBDCFinalizer) {
claim.Finalizers = util.RemoveString(claim.Finalizers, oldBDCFinalizer)
claim.Finalizers = append(claim.Finalizers, newBDCFinalizer)
Expand Down
107 changes: 107 additions & 0 deletions pkg/upgrade/v041_042/preupgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v041_042

import (
"context"
apis "github.com/openebs/node-disk-manager/pkg/apis/openebs/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// UpgradeTask is the struct which implements the Task interface
// which can be used to perform the upgrade
type UpgradeTask struct {
from string
to string
client client.Client
err error
}

// NewUpgradeTask creates a new upgrade task with given client
// and specified `from` and `to` version
func NewUpgradeTask(from, to string, c client.Client) *UpgradeTask {
return &UpgradeTask{from: from, to: to, client: c}
}

// FromVersion returns the version from which the components need to be updated
func (p *UpgradeTask) FromVersion() string {
return p.from
}

// ToVersion returns the version to which components will be updated. This should be
// the current version
func (p *UpgradeTask) ToVersion() string {
return p.to
}

// PreUpgrade runs the preupgrade tasks and returns whether it succeeded or not
func (p *UpgradeTask) PreUpgrade() bool {
var err error
bdcList := &apis.BlockDeviceClaimList{}
opts := &client.ListOptions{}
err = p.client.List(context.TODO(), opts, bdcList)
if err != nil {
p.err = err
return false
}

for _, bdc := range bdcList.Items {
err = p.copyHostName(&bdc)
if err != nil {
p.err = err
return false
}
}
return true
}

// Upgrade runs the main upgrade tasks and returns whether it succeeded or not
func (p *UpgradeTask) Upgrade() bool {
if p.err != nil {
return false
}
return true
}

// PostUpgrade runs the tasks that need to be performed after upgrade and returns
// whether the tasks where success or not
func (p *UpgradeTask) PostUpgrade() bool {
if p.err != nil {
return false
}
return true
}

// IsSuccess returns error if the upgrade failed, at any step. Else nil will
// be returned
func (p *UpgradeTask) IsSuccess() error {
return p.err
}

// copyHostName will copy the hostname string from .spec.hostName to
// .spec.nodeAttributes.hostName
func (p *UpgradeTask) copyHostName(claim *apis.BlockDeviceClaim) error {
// copy the value only if .spec.hostName is non-empty and nodeAttributes.hostName
// is empty. If hostname field is empty, it is not required to copy the value, as
// it may be a new BDC.
if len(claim.Spec.HostName) != 0 &&
len(claim.Spec.BlockDeviceNodeAttributes.HostName) == 0 {
claim.Spec.BlockDeviceNodeAttributes.HostName = claim.Spec.HostName
return p.client.Update(context.TODO(), claim)
}
return nil
}

0 comments on commit b677b25

Please sign in to comment.