Skip to content

Commit

Permalink
addressed review comments
Browse files Browse the repository at this point in the history
Signed-off-by: shubham <[email protected]>
  • Loading branch information
shubham14bajpai committed May 25, 2020
1 parent 9f18d67 commit 797290b
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 69 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ UPGRADE=upgrade

# Specify the name of the docker repo for amd64
UPGRADE_REPO_NAME_AMD64="upgrade-amd64"
M_MIGRATE_REPO_NAME?=migrate
MIGRATE_REPO_NAME_AMD64="migrate-amd64"

# Specify the name of the docker repo for arm64
UPGRADE_REPO_NAME_ARM64="upgrade-arm64"
MIGRATE_REPO_NAME_ARM64="migrate-arm64"

# build upgrade binary
.PHONY: upgrade
Expand Down
14 changes: 8 additions & 6 deletions build/migrate/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
# its latest binary
#

FROM alpine:3.9
FROM alpine:3.11.5

# copy the latest binary
COPY migrate /usr/local/bin/migrate

ARG BUILD_DATE

ARG ARCH
ARG DBUILD_DATE
ARG DBUILD_REPO_URL
ARG DBUILD_SITE_URL
LABEL org.label-schema.name="migrate"
LABEL org.label-schema.description="migrates openebs components"
LABEL org.label-schema.url="https://openebs.io/"
LABEL org.label-schema.vcs-url="https://github.com/openebs/upgrade"
LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.build-date=$BUILD_DATE
LABEL org.label-schema.build-date=$DBUILD_DATE
LABEL org.label-schema.vcs-url=$DBUILD_REPO_URL
LABEL org.label-schema.url=$DBUILD_SITE_URL

ENTRYPOINT ["migrate"]
20 changes: 16 additions & 4 deletions build/migrate/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@ migrate:
@PNAME=${MIGRATE} CTLNAME=${MIGRATE} CGO_ENABLED=0 sh -c "'$(PWD)/build/build.sh'"

# build migrate image
.PHONY: migrate-image
migrate-image: migrate
.PHONY: migrate-image.amd64
migrate-image.amd64: migrate
@echo "-----------------------------------------------"
@echo "--> ${MIGRATE} image "
@echo "${HUB_USER}/${M_MIGRATE_REPO_NAME}:${IMAGE_TAG}"
@echo "${HUB_USER}/${MIGRATE_REPO_NAME_AMD64}:${IMAGE_TAG}"
@echo "-----------------------------------------------"
@cp bin/${MIGRATE}/${MIGRATE} build/${MIGRATE}/
@cd build/${MIGRATE} && \
sudo docker build -t "${HUB_USER}/${M_MIGRATE_REPO_NAME}:${IMAGE_TAG}" --build-arg BUILD_DATE=${BUILD_DATE} .
sudo docker build -t "${HUB_USER}/${MIGRATE_REPO_NAME_AMD64}:${IMAGE_TAG}" ${DBUILD_ARGS} .
@rm build/${MIGRATE}/${MIGRATE}

# build migrate image
.PHONY: migrate-image.arm64
migrate-image.arm64: migrate
@echo "-----------------------------------------------"
@echo "--> ${MIGRATE} image "
@echo "${HUB_USER}/${MIGRATE_REPO_NAME_ARM64}:${IMAGE_TAG}"
@echo "-----------------------------------------------"
@cp bin/${MIGRATE}/${MIGRATE} build/${MIGRATE}/
@cd build/${MIGRATE} && \
sudo docker build -t "${HUB_USER}/${MIGRATE_REPO_NAME_ARM64}:${IMAGE_TAG}" ${DBUILD_ARGS} .
@rm build/${MIGRATE}/${MIGRATE}

# cleanup migrate build
Expand Down
13 changes: 6 additions & 7 deletions cmd/migrate/executor/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The OpenEBS Authors.
Copyright 2020 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.
Expand All @@ -20,11 +20,10 @@ import (
"strings"

errors "github.com/pkg/errors"

"github.com/spf13/cobra"
)

// MigrateOptions stores information required for migrate
// MigrateOptions stores information required for migration of
// OpenEBS resources
type MigrateOptions struct {
openebsNamespace string
spcName string
Expand All @@ -37,9 +36,9 @@ var (
)

// RunPreFlightChecks will ensure the sanity of the common migrate options
func (u *MigrateOptions) RunPreFlightChecks(cmd *cobra.Command) error {
if len(strings.TrimSpace(u.openebsNamespace)) == 0 {
return errors.Errorf("Cannot execute migrate job: namespace is missing")
func (m *MigrateOptions) RunPreFlightChecks() error {
if len(strings.TrimSpace(m.openebsNamespace)) == 0 {
return errors.Errorf("Cannot execute migrate job: openebs namespace is missing")
}
return nil
}
22 changes: 11 additions & 11 deletions cmd/migrate/executor/pool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The OpenEBS Authors.
Copyright 2020 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.
Expand Down Expand Up @@ -44,9 +44,9 @@ func NewMigratePoolJob() *cobra.Command {
Long: cstorSPCMigrateCmdHelpText,
Example: `migrate cstor-spc --spc-name <spc-name>`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(options.RunPreFlightChecks(cmd), util.Fatal)
util.CheckErr(options.RunCStorSPCMigrateChecks(cmd), util.Fatal)
util.CheckErr(options.RunCStorSPCMigrate(cmd), util.Fatal)
util.CheckErr(options.RunPreFlightChecks(), util.Fatal)
util.CheckErr(options.RunCStorSPCMigrateChecks(), util.Fatal)
util.CheckErr(options.RunCStorSPCMigrate(), util.Fatal)
},
}

Expand All @@ -59,25 +59,25 @@ func NewMigratePoolJob() *cobra.Command {
}

// RunCStorSPCMigrateChecks will ensure the sanity of the cstor SPC migrate options
func (u *MigrateOptions) RunCStorSPCMigrateChecks(cmd *cobra.Command) error {
if len(strings.TrimSpace(u.spcName)) == 0 {
func (m *MigrateOptions) RunCStorSPCMigrateChecks() error {
if len(strings.TrimSpace(m.spcName)) == 0 {
return errors.Errorf("Cannot execute migrate job: cstor spc name is missing")
}

return nil
}

// RunCStorSPCMigrate migrates the given spc.
func (u *MigrateOptions) RunCStorSPCMigrate(cmd *cobra.Command) error {
func (m *MigrateOptions) RunCStorSPCMigrate() error {

klog.Infof("Migrating spc %s to cspc", u.spcName)
klog.Infof("Migrating spc %s to cspc", m.spcName)
migrator := cstor.CSPCMigrator{}
err := migrator.Migrate(u.spcName, u.openebsNamespace)
err := migrator.Migrate(m.spcName, m.openebsNamespace)
if err != nil {
klog.Error(err)
return errors.Errorf("Failed to migrate cStor SPC : %s", u.spcName)
return errors.Errorf("Failed to migrate cStor SPC : %s", m.spcName)
}
klog.Infof("Successfully migrated spc %s to cspc", u.spcName)
klog.Infof("Successfully migrated spc %s to cspc", m.spcName)

return nil
}
2 changes: 1 addition & 1 deletion cmd/migrate/executor/setup_job.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The OpenEBS Authors.
Copyright 2020 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.
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 The OpenEBS Authors.
Copyright 2020 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.
Expand Down
13 changes: 7 additions & 6 deletions cmd/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
package util

import (
menv "github.com/openebs/maya/pkg/env/v1alpha1"
"os"
)

//This file defines the environement variable names that are specific
// to this provisioner. In addition to the variables defined in this file,
// provisioner also uses the following:
// OPENEBS_NAMESPACE
const (
openebsNamespaceEnv = "OPENEBS_NAMESPACE"
)

// GetOpenEBSNamespace gets the openebs namespace set to
// the OPENEBS_NAMESPACE env
func GetOpenEBSNamespace() string {
return menv.Get(menv.OpenEBSNamespace)
return os.Getenv(openebsNamespaceEnv)
}
22 changes: 15 additions & 7 deletions pkg/migrate/cstor/cspc_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,21 @@ var (
}
)

func getBDList(cspObj apis.CStorPool) []cstor.CStorPoolInstanceBlockDevice {
func getDataRaidGroups(cspObj apis.CStorPool) []cstor.RaidGroup {
dataRaidGroups := []cstor.RaidGroup{}
for _, rg := range cspObj.Spec.Group {
dataRaidGroups = append(dataRaidGroups,
cstor.RaidGroup{
CStorPoolInstanceBlockDevices: getBDList(rg),
},
)
}
return dataRaidGroups
}

func getBDList(rg apis.BlockDeviceGroup) []cstor.CStorPoolInstanceBlockDevice {
list := []cstor.CStorPoolInstanceBlockDevice{}
for _, bdcObj := range cspObj.Spec.Group[0].Item {
for _, bdcObj := range rg.Item {
list = append(list,
cstor.CStorPoolInstanceBlockDevice{
BlockDeviceName: bdcObj.Name,
Expand Down Expand Up @@ -83,11 +95,7 @@ func (c *CSPCMigrator) getCSPCSpecForSPC() (*cstor.CStorPoolCluster, error) {
NodeSelector: map[string]string{
types.HostNameLabelKey: cspObj.Labels[string(apis.HostNameCPK)],
},
DataRaidGroups: []cstor.RaidGroup{
{
CStorPoolInstanceBlockDevices: getBDList(cspObj),
},
},
DataRaidGroups: getDataRaidGroups(cspObj),
PoolConfig: cstor.PoolConfig{
DataRaidGroupType: typeMap[cspObj.Spec.PoolSpec.PoolType],
ThickProvision: cspObj.Spec.PoolSpec.ThickProvisioning,
Expand Down
81 changes: 56 additions & 25 deletions pkg/migrate/cstor/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package migrate

import (
"fmt"
"strings"
"time"

"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -80,10 +81,36 @@ func (c *CSPCMigrator) Migrate(name, namespace string) error {
if err != nil {
return errors.Wrap(err, "error building openebs clientset")
}
err = c.validateCSPCOperator()
if err != nil {
return err
}
err = c.migrate(name)
return err
}

func (c *CSPCMigrator) validateCSPCOperator() error {
operatorPods, err := c.KubeClientset.CoreV1().
Pods(c.OpenebsNamespace).
List(metav1.ListOptions{
LabelSelector: "openebs.io/component-name=cspc-operator",
})
if err != nil {
return err
}
if len(operatorPods.Items) == 0 {
return fmt.Errorf("cspc operator pod missing")
}
for _, pod := range operatorPods.Items {
operatorVersion := strings.Split(pod.Labels["openebs.io/version"], "-")[0]
if operatorVersion != "1.11.0" {
return fmt.Errorf("cspc operator is in %s version, please upgrade it to 1.11.0 or above version",
pod.Labels["openebs.io/version"])
}
}
return nil
}

// Pool migrates the pool from SPC schema to CSPC schema
func (c *CSPCMigrator) migrate(spcName string) error {
var err error
Expand All @@ -98,11 +125,11 @@ func (c *CSPCMigrator) migrate(spcName string) error {
}
err = c.validateSPC()
if err != nil {
return err
return errors.Wrapf(err, "failed to validate spc %s", spcName)
}
err = c.updateBDCLabels(spcName)
if err != nil {
return err
return errors.Wrapf(err, "failed to update bdc labels for spc %s", spcName)
}
klog.Infof("Creating equivalent cspc for spc %s", spcName)
c.CSPCObj, err = c.generateCSPC()
Expand Down Expand Up @@ -130,11 +157,9 @@ func (c *CSPCMigrator) migrate(spcName string) error {
// which implies the migration is done and makes it idempotent
cspiItem := cspiItem // pin it
cspiObj := &cspiItem
if cspiObj.Status.Phase != "ONLINE" {
err = c.csptocspi(cspiObj)
if err != nil {
return err
}
err = c.cspTocspi(cspiObj)
if err != nil {
return err
}
}
// Clean up old SPC resources after the migration is complete
Expand Down Expand Up @@ -172,8 +197,10 @@ func (c *CSPCMigrator) validateSPC() error {
bdMap[bdName]++
}
for _, cspObj := range cspList.Items {
for _, bdObj := range cspObj.Spec.Group[0].Item {
bdMap[bdObj.Name]++
for _, rg := range cspObj.Spec.Group {
for _, bdObj := range rg.Item {
bdMap[bdObj.Name]++
}
}
}
for bdName, count := range bdMap {
Expand Down Expand Up @@ -210,34 +237,37 @@ func (c *CSPCMigrator) getSPCWithMigrationStatus(spcName string) (*apis.StorageP
}

// csptocspi migrates a CSP to CSPI based on hostname
func (c *CSPCMigrator) csptocspi(cspiObj *cstor.CStorPoolInstance) error {
func (c *CSPCMigrator) cspTocspi(cspiObj *cstor.CStorPoolInstance) error {
var err1 error
hostnameLabel := types.HostNameLabelKey + "=" + cspiObj.Labels[types.HostNameLabelKey]
spcLabel := string(apis.StoragePoolClaimCPK) + "=" + c.CSPCObj.Name
cspLabel := hostnameLabel + "," + spcLabel
var err1 error
cspObj, err := getCSP(cspLabel)
if err != nil {
return err
}
klog.Infof("Migrating csp %s to cspi %s", cspiObj.Name, cspObj.Name)
err = c.scaleDownDeployment(cspObj, c.OpenebsNamespace)
if err != nil {
return err
}
// once the old pool pod is scaled down and bdcs are patched
// bring up the cspi pod so that the old pool can be renamed and imported.
cspiObj.Annotations[types.OldPoolName] = "cstor-" + string(cspObj.UID)
delete(cspiObj.Annotations, types.OpenEBSDisableReconcileLabelKey)
cspiObj, err = c.OpenebsClientset.CstorV1().
CStorPoolInstances(c.OpenebsNamespace).
Update(cspiObj)
if err != nil {
return err
if cspiObj.Annotations[types.OpenEBSDisableReconcileLabelKey] != "" {
klog.Infof("Migrating csp %s to cspi %s", cspObj.Name, cspiObj.Name)
err = c.scaleDownDeployment(cspObj, c.OpenebsNamespace)
if err != nil {
return err
}
// once the old pool pod is scaled down and bdcs are patched
// bring up the cspi pod so that the old pool can be renamed and imported.
cspiObj.Annotations[types.ExistingPoolName] = "cstor-" + string(cspObj.UID)
delete(cspiObj.Annotations, types.OpenEBSDisableReconcileLabelKey)
cspiObj, err = c.OpenebsClientset.CstorV1().
CStorPoolInstances(c.OpenebsNamespace).
Update(cspiObj)
if err != nil {
return err
}
}
err = retry.
Times(60).
Wait(5 * time.Second).
Try(func(attempt uint) error {
klog.Infof("waiting for cspi %s to come to ONLINE state", cspiObj.Name)
cspiObj, err1 = c.OpenebsClientset.CstorV1().
CStorPoolInstances(c.OpenebsNamespace).
Get(cspiObj.Name, metav1.GetOptions{})
Expand Down Expand Up @@ -323,6 +353,7 @@ func (c *CSPCMigrator) scaleDownDeployment(cspObj *apis.CStorPool, openebsNamesp
Times(60).
Wait(5 * time.Second).
Try(func(attempt uint) error {
klog.Infof("waiting for csp %s deployment to scale down", cspObj.Name)
cspPods, err1 := c.KubeClientset.CoreV1().
Pods(openebsNamespace).
List(metav1.ListOptions{
Expand Down

0 comments on commit 797290b

Please sign in to comment.