diff --git a/cmd/manager/main.go b/cmd/manager/main.go index a1f0d7ad4..6bb1e508e 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -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" @@ -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) } diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index aff8c9b11..006ff70b6 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -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 { @@ -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 diff --git a/pkg/upgrade/preupgrade.go b/pkg/upgrade/v040_041/preupgrade.go similarity index 78% rename from pkg/upgrade/preupgrade.go rename to pkg/upgrade/v040_041/preupgrade.go index fbe30aa2a..8f95bb0fb 100644 --- a/pkg/upgrade/preupgrade.go +++ b/pkg/upgrade/v040_041/preupgrade.go @@ -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" @@ -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{} @@ -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 } @@ -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 } @@ -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) diff --git a/pkg/upgrade/v041_042/preupgrade.go b/pkg/upgrade/v041_042/preupgrade.go new file mode 100644 index 000000000..e23791553 --- /dev/null +++ b/pkg/upgrade/v041_042/preupgrade.go @@ -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 +}