Skip to content

Commit

Permalink
feat(migrate): add support for spc to cspc migration
Browse files Browse the repository at this point in the history
Signed-off-by: shubham <[email protected]>
  • Loading branch information
shubham14bajpai committed May 24, 2020
1 parent 5c766b7 commit 29e5e7f
Show file tree
Hide file tree
Showing 12 changed files with 960 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ UPGRADE=upgrade

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

# Specify the name of the docker repo for arm64
UPGRADE_REPO_NAME_ARM64="upgrade-arm64"
Expand Down Expand Up @@ -125,7 +126,10 @@ cleanup-upgrade:
rm -rf ${GOPATH}/bin/${UPGRADE}


include ./build/migrate/Makefile.mk

# Push images
.PHONY: deploy-images
deploy-images:
@./build/deploy.sh

20 changes: 20 additions & 0 deletions build/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/upgrade"
LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.build-date=$BUILD_DATE

ENTRYPOINT ["migrate"]
32 changes: 32 additions & 0 deletions build/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

# 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)/build/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} build/${MIGRATE}/
@cd build/${MIGRATE} && \
sudo docker build -t "${HUB_USER}/${M_MIGRATE_REPO_NAME}:${IMAGE_TAG}" --build-arg BUILD_DATE=${BUILD_DATE} .
@rm build/${MIGRATE}/${MIGRATE}

# cleanup migrate build
.PHONY: cleanup-migrate
cleanup-migrate:
rm -rf ${GOPATH}/bin/${MIGRATE}
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
}
83 changes: 83 additions & 0 deletions cmd/migrate/executor/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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"

"github.com/openebs/maya/pkg/util"
cstor "github.com/openebs/upgrade/pkg/migrate/cstor"
"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)
migrator := cstor.CSPCMigrator{}
err := migrator.Migrate(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
}
61 changes: 61 additions & 0 deletions cmd/migrate/executor/setup_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
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 (
"flag"
"strings"

"github.com/openebs/upgrade/cmd/util"
"github.com/spf13/cobra"
)

// NewJob will setup a new migrate job
func NewJob() *cobra.Command {
// Create a new command.
cmd := &cobra.Command{
Use: "migrate",
Short: "OpenEBS Migrate Utility",
Long: `An utility to migrate OpenEBS SPC Pools to CSPC and Non-CSI Volumes to CSI Volumes,
run as a Kubernetes Job`,
PersistentPreRun: PreRun,
}

cmd.AddCommand(
NewMigratePoolJob(),
)

cmd.PersistentFlags().StringVarP(&options.openebsNamespace,
"openebs-namespace", "",
options.openebsNamespace,
"namespace where openebs components are installed.")

cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)

// Hack: Without the following line, the logs will be prefixed with Error
_ = flag.CommandLine.Parse([]string{})

return cmd
}

// PreRun will check for environement variables to be read and intialized.
func PreRun(cmd *cobra.Command, args []string) {
namespace := util.GetOpenEBSNamespace()
if len(strings.TrimSpace(namespace)) != 0 {
options.openebsNamespace = namespace
}
}
32 changes: 32 additions & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2018 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 main

import (
mlogger "github.com/openebs/maya/pkg/logs"
"github.com/openebs/upgrade/cmd/migrate/executor"
"github.com/openebs/upgrade/cmd/util"
)

func main() {
// Init logging
mlogger.InitLogs()
defer mlogger.FlushLogs()

err := executor.NewJob().Execute()
util.CheckError(err)
}
30 changes: 30 additions & 0 deletions cmd/util/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 util

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

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)
}
}
Loading

0 comments on commit 29e5e7f

Please sign in to comment.