github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/migrator/peer/fabric/migrator.go (about) 1 /* 2 * Copyright contributors to the Hyperledger Fabric Operator project 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package fabric 20 21 import ( 22 config "github.com/IBM-Blockchain/fabric-operator/operatorconfig" 23 "github.com/pkg/errors" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 26 logf "sigs.k8s.io/controller-runtime/pkg/log" 27 ) 28 29 var log = logf.Log.WithName("peer_fabric_migrator") 30 31 type Version string 32 33 const ( 34 V2 Version = "V2" 35 ) 36 37 //go:generate counterfeiter -o mocks/migrator.go -fake-name Migrator . Migrator 38 type Migrator interface { 39 MigrationNeeded(metav1.Object) bool 40 UpgradeDBs(metav1.Object, config.DBMigrationTimeouts) error 41 UpdateConfig(metav1.Object, string) error 42 SetChaincodeLauncherResourceOnCR(metav1.Object) error 43 } 44 45 func V2Migrate(instance metav1.Object, migrator Migrator, version string, timeouts config.DBMigrationTimeouts) error { 46 if !migrator.MigrationNeeded(instance) { 47 log.Info("Migration not needed, skipping migration") 48 return nil 49 } 50 log.Info("Migration is needed, starting migration") 51 52 if err := migrator.SetChaincodeLauncherResourceOnCR(instance); err != nil { 53 return errors.Wrap(err, "failed to update chaincode launcher resources on CR") 54 } 55 56 if err := migrator.UpdateConfig(instance, version); err != nil { 57 return errors.Wrap(err, "failed to update config") 58 } 59 60 if err := migrator.UpgradeDBs(instance, timeouts); err != nil { 61 return errors.Wrap(err, "failed to upgrade peer's dbs") 62 } 63 64 return nil 65 } 66 67 func V24Migrate(instance metav1.Object, migrator Migrator, version string, timeouts config.DBMigrationTimeouts) error { 68 if err := migrator.UpdateConfig(instance, version); err != nil { 69 return errors.Wrap(err, "failed to update v2.4.1 configs") 70 } 71 return nil 72 }