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(cstor-csi): add support to migrate from non-csi to csi volume #1562

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -79,6 +79,7 @@ M_EXPORTER_REPO_NAME?=m-exporter
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 @@ -119,6 +120,7 @@ include ./buildscripts/apiserver/Makefile.mk
include ./buildscripts/provisioner-localpv/Makefile.mk
include ./buildscripts/upgrade/Makefile.mk
include ./buildscripts/upgrade-082090/Makefile.mk
include ./buildscripts/migrate/Makefile.mk

.PHONY: all
all: compile-tests apiserver-image exporter-image pool-mgmt-image volume-mgmt-image admission-server-image cspc-operator-image cspc-operator-debug-image cspi-mgmt-image upgrade-image provisioner-localpv-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

# 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}
81 changes: 81 additions & 0 deletions cmd/migrate/executor/cstor_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
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 (
"fmt"
"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 (
cstorVolumeMigrateCmdHelpText = `
This command migrates the cStor volume

Usage: migrate cstor-volume --pv-name <pv-name>
`
)

// NewMigrateCStorVolumeJob migrates all the cStor Volumes associated with
// a given Storage Pool Claim
func NewMigrateCStorVolumeJob() *cobra.Command {
cmd := &cobra.Command{
Use: "cstor-volume",
Short: "Migrate cStor Volume",
Long: cstorVolumeMigrateCmdHelpText,
Example: `migrate cstor-volume --pv-name <pv-name>`,
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(options.RunPreFlightChecks(cmd), util.Fatal)
util.CheckErr(options.RunCStorVolumeMigrateChecks(cmd), util.Fatal)
util.CheckErr(options.RunCStorVolumeMigrate(cmd), util.Fatal)
},
}

cmd.Flags().StringVarP(&options.pvName,
"pv-name", "",
options.pvName,
"cstor volume name to be migrated. Run \"kubectl get pv\", to get pv-name")

return cmd
}

// RunCStorVolumeMigrateChecks will ensure the sanity of the cstor SPC migrate options
func (u *MigrateOptions) RunCStorVolumeMigrateChecks(cmd *cobra.Command) error {
if len(strings.TrimSpace(u.pvName)) == 0 {
fmt.Println("name", u.pvName)
return errors.Errorf("Cannot execute migrate job: cstor pv name is missing")
}
return nil
}

// RunCStorVolumeMigrate migrates the given spc.
func (u *MigrateOptions) RunCStorVolumeMigrate(cmd *cobra.Command) error {
klog.Infof("Migrating spc %s to cspc", u.pvName)
err := migrate.Volume(u.pvName, u.openebsNamespace)
if err != nil {
klog.Error(err)
return errors.Errorf("Failed to migrate cStor volume : %s", u.pvName)
}
klog.Infof("Successfully migrated volume %s to csi-volume. Please scale up the application to trigger CSI driver.", u.pvName)
return nil
}
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 @@
/*
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
pvName 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
}
60 changes: 60 additions & 0 deletions cmd/migrate/executor/setup_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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/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(
NewMigrateCStorVolumeJob(),
)

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 := getOpenEBSNamespace()
if len(strings.TrimSpace(namespace)) != 0 {
options.openebsNamespace = namespace
}
}
31 changes: 31 additions & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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 (
"github.com/openebs/maya/cmd/migrate/executor"
mlogger "github.com/openebs/maya/pkg/logs"
)

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

err := executor.NewJob().Execute()
executor.CheckError(err)
}
Loading