Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cspc): migrate pools from spc to cspc #1531

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
050b97d
feat(migrate): generate CSPC form given SPC
shubham14bajpai Nov 19, 2019
82e8d56
feat(cspc): migrate pools from spc to cspc
shubham14bajpai Nov 28, 2019
5b63cad
fix linting issues
shubham14bajpai Nov 28, 2019
3d62a27
renamed one file
shubham14bajpai Nov 28, 2019
f1e0f7e
added idempotent support
shubham14bajpai Nov 28, 2019
18a5e6e
moved hardcoded strings in function calls to global constants
shubham14bajpai Nov 29, 2019
a6e1356
added comments for the function and removed unnecessary checks
shubham14bajpai Nov 29, 2019
206c214
This commit chnages the design from schema changes to use of
shubham14bajpai Dec 10, 2019
ea82cd8
fixed golang-ci issues
shubham14bajpai Dec 10, 2019
b590ef3
addressed review comments for pkg
shubham14bajpai Dec 26, 2019
976f301
fix scopelint issue and add priorityclass
shubham14bajpai Dec 27, 2019
99b37f0
feat(migrate): generate CSPC form given SPC
shubham14bajpai Nov 19, 2019
2559bc3
feat(cspc): migrate pools from spc to cspc
shubham14bajpai Nov 28, 2019
627c12b
fix linting issues
shubham14bajpai Nov 28, 2019
33f4d9c
renamed one file
shubham14bajpai Nov 28, 2019
edc729d
moved hardcoded strings in function calls to global constants
shubham14bajpai Nov 29, 2019
4e771b1
added comments for the function and removed unnecessary checks
shubham14bajpai Nov 29, 2019
2a24183
added aux resources to migrated pool
shubham14bajpai Dec 31, 2019
d81cf5c
resolved conflicts
shubham14bajpai Dec 31, 2019
524897a
addressed review comments
shubham14bajpai Jan 7, 2020
aab7caa
added object name to logs and fixed annotation deletion
shubham14bajpai Jan 17, 2020
e5f6235
fixed golang issue
shubham14bajpai Jan 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ HUB_USER?=openebs
ADMISSION_SERVER_REPO_NAME?=admission-server
M_UPGRADE_REPO_NAME?=m-upgrade
CSPC_OPERATOR_REPO_NAME?=cspc-operator
M_MIGRATE_REPO_NAME?=migrate

ifeq (${IMAGE_TAG}, )
IMAGE_TAG = ci
Expand Down Expand Up @@ -122,6 +123,7 @@ include ./buildscripts/cstor-pool-mgmt/Makefile.mk
include ./buildscripts/cstor-volume-mgmt/Makefile.mk
include ./buildscripts/cspi-mgmt/Makefile.mk
include ./buildscripts/cvc-operator/Makefile.mk
include ./buildscripts/migrate/Makefile.mk

.PHONY: all
all: compile-tests apiserver-image exporter-image pool-mgmt-image volume-mgmt-image \
Expand Down
20 changes: 20 additions & 0 deletions buildscripts/migrate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# This builds openebs migratte image using
# its latest binary
#

FROM alpine:3.9

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

ARG BUILD_DATE

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/maya"
LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.build-date=$BUILD_DATE

ENTRYPOINT ["migrate"]
32 changes: 32 additions & 0 deletions buildscripts/migrate/Makefile.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# Specify the name for the binaries
MIGRATE=migrate
shubham14bajpai marked this conversation as resolved.
Show resolved Hide resolved

# build migrate binary
.PHONY: migrate
migrate:
@echo "----------------------------"
@echo "--> ${MIGRATE} "
@echo "----------------------------"
@# PNAME is the sub-folder in ./bin where binary will be placed.
@# CTLNAME indicates the folder/pkg under cmd that needs to be built.
@# The output binary will be: ./bin/${PNAME}/<os-arch>/${CTLNAME}
@# A copy of the binary will also be placed under: ./bin/${PNAME}/${CTLNAME}
@PNAME=${MIGRATE} CTLNAME=${MIGRATE} CGO_ENABLED=0 sh -c "'$(PWD)/buildscripts/build.sh'"

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

# cleanup migrate build
.PHONY: cleanup-migrate
cleanup-migrate:
rm -rf ${GOPATH}/bin/${MIGRATE}
2 changes: 1 addition & 1 deletion cmd/cstor-pool-mgmt/pool/v1alpha2/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Create(csp *apis.CStorPoolInstance) error {

// Let's check if there is any disk having the pool config
// If so then we will not create the pool
ret, notImported, err := checkIfPoolNotImported(csp)
ret, notImported, err := checkIfPoolIsImportable(csp)
if err != nil {
return errors.Errorf("Failed to check not imported pool %s", err.Error())
}
Expand Down
39 changes: 31 additions & 8 deletions cmd/cstor-pool-mgmt/pool/v1alpha2/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,49 @@ import (
// It will return -
// - If pool is imported or not
// - If any error occurred during import operation
// The oldName argument is used to rename the old CSP
// uid name to the current PoolName(cspi) format
func Import(cspi *apis.CStorPoolInstance) (bool, error) {
if poolExist := checkIfPoolPresent(PoolName(cspi)); poolExist {
// If the pool is renamed and imported but the pool-mgmt restarts
// for some reason then the annotation should be removed.
delete(cspi.Annotations, string(apis.OldPoolName))
return true, nil
}

// Pool is not imported.. Let's update the syncResource
var cmdOut []byte
var err error
common.SyncResources.IsImported = false
var poolImported bool
var poolImported, poolNotImported bool

_, poolNotImported, _ = checkIfPoolIsImportable(cspi)
if poolNotImported {
// If the pool is renamed but not imported, remove the
// annotation to avoid not found errors.
delete(cspi.Annotations, string(apis.OldPoolName))
}

bdPath, err := getPathForBDev(cspi.Spec.RaidGroups[0].BlockDevices[0].BlockDeviceName)
if err != nil {
return false, err
}

// oldName denotes the pool name that may be present
// from previous version and needs to be imported with new name
oldName := cspi.Annotations[string(apis.OldPoolName)]

klog.Infof("Importing pool %s %s", string(cspi.GetUID()), PoolName(cspi))
devID := pool.GetDevPathIfNotSlashDev(bdPath[0])
cmd := zfs.NewPoolImport().
WithCachefile(cspi.Spec.PoolConfig.CacheFile).
WithProperty("cachefile", cspi.Spec.PoolConfig.CacheFile).
WithDirectory(devID).
WithPool(oldName).
WithNewPool(PoolName(cspi))

if len(devID) != 0 {
cmdOut, err = zfs.NewPoolImport().
WithCachefile(cspi.Spec.PoolConfig.CacheFile).
WithProperty("cachefile", cspi.Spec.PoolConfig.CacheFile).
WithDirectory(devID).
WithPool(PoolName(cspi)).
Execute()
cmdOut, err = cmd.Execute()
if err == nil {
poolImported = true
} else {
Expand All @@ -69,7 +87,8 @@ func Import(cspi *apis.CStorPoolInstance) (bool, error) {
cmdOut, err = zfs.NewPoolImport().
WithCachefile(cspi.Spec.PoolConfig.CacheFile).
WithProperty("cachefile", cspi.Spec.PoolConfig.CacheFile).
WithPool(PoolName(cspi)).
WithNewPool(PoolName(cspi)).
WithPool(oldName).
Execute()
}

Expand All @@ -80,6 +99,10 @@ func Import(cspi *apis.CStorPoolInstance) (bool, error) {
}

klog.Infof("Pool Import successful: %v", string(PoolName(cspi)))
// after successful import of pool the annotation needs to be deleted
// to avoid renaming of pool that is already renamed which will cause
// pool not found errors
delete(cspi.Annotations, string(apis.OldPoolName))
return true, nil
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/cstor-pool-mgmt/pool/v1alpha2/pool_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func checkIfDeviceUsed(path []string, t zpool.Topology) (string, bool) {
return usedPath, isUsed
}

func checkIfPoolNotImported(cspi *apis.CStorPoolInstance) (string, bool, error) {
// checkIfPoolIsImportable checks if the pool is imported or not. If the pool
// is present on the disk but not imported it returns true as the pool can be
// imported. It also returns false if pool is not found on the disk.
func checkIfPoolIsImportable(cspi *apis.CStorPoolInstance) (string, bool, error) {
var cmdOut []byte
var err error

Expand Down
30 changes: 30 additions & 0 deletions cmd/migrate/executor/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this package?

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 executor

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

//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

func getOpenEBSNamespace() string {
return menv.Get(menv.OpenEBSNamespace)
}
34 changes: 34 additions & 0 deletions cmd/migrate/executor/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 executor

import (
"context"
"fmt"
"os"
)

// CheckError prints err to stderr and exits with code 1 if err is not nil. Otherwise, it is a
// no-op.
func CheckError(err error) {
if err != nil {
if err != context.Canceled {
fmt.Fprintf(os.Stderr, fmt.Sprintf("An error occurred: %v\n", err))
}
os.Exit(1)
}
}
45 changes: 45 additions & 0 deletions cmd/migrate/executor/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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 executor

import (
"strings"

errors "github.com/pkg/errors"

"github.com/spf13/cobra"
)

// MigrateOptions stores information required for migrate
type MigrateOptions struct {
openebsNamespace string
spcName string
}

var (
options = &MigrateOptions{
openebsNamespace: "openebs",
}
)

// 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")
}
return nil
}
82 changes: 82 additions & 0 deletions cmd/migrate/executor/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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 executor

import (
"strings"

migrate "github.com/openebs/maya/pkg/cstor/migrate"
"github.com/openebs/maya/pkg/util"
"github.com/spf13/cobra"
"k8s.io/klog"

errors "github.com/pkg/errors"
)

var (
cstorSPCMigrateCmdHelpText = `
This command migrates the cStor SPC

Usage: migrate pool --spc-name <spc-name>
`
)

// NewMigratePoolJob migrates all the cStor Pools associated with
// a given Storage Pool Claim
func NewMigratePoolJob() *cobra.Command {
cmd := &cobra.Command{
Use: "pool",
Short: "Migrate cStor SPC",
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)
},
}

cmd.Flags().StringVarP(&options.spcName,
"spc-name", "",
options.spcName,
"cstor SPC name to be migrated. Run \"kubectl get spc\", to get spc-name")

return cmd
}

// 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 {
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 {

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

return nil
}
Loading