Skip to content

Commit

Permalink
PWX-32230: Supporting unidirectional clusterpair creation with storkc…
Browse files Browse the repository at this point in the history
…tl create command.
  • Loading branch information
diptiranjanpx committed Jul 19, 2023
1 parent 05a9fa9 commit 187ee4a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
12 changes: 10 additions & 2 deletions pkg/storkctl/clusterpair.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
var syncDR bool
var pxAuthTokenSrc, pxAuthSecretNamespaceSrc string
var pxAuthTokenDest, pxAuthSecretNamespaceDest string
var uniDirectional bool

createClusterPairCommand := &cobra.Command{
Use: clusterPairSubcommand,
Expand Down Expand Up @@ -327,6 +328,9 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
}
printMsg(fmt.Sprintf("ClusterPair %s created successfully. Direction Source -> Destination\n", clusterPairName), ioStreams.Out)

if uniDirectional {
return
}
destClusterPair, err := generateClusterPair(clusterPairName, cmdFactory.GetNamespace(), sIP, sPort, srcToken, sFile, projectMappingsStr, pxAuthSecretNamespaceSrc, true, true)
if err != nil {
util.CheckErr(err)
Expand Down Expand Up @@ -430,7 +434,7 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
credentialData["encryptionKey"] = []byte(encryptionKey)

// Bail out if portworx-api service type is not loadbalancer type and the endpoints are not provided.
if len(sEP) == 0 {
if !uniDirectional && len(sEP) == 0 {
srcPXEndpoint, err := getPXEndPointDetails(sFile)
if err != nil {
err = fmt.Errorf("unable to get portworx endpoint in source cluster. Err: %v", err)
Expand Down Expand Up @@ -525,6 +529,10 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
}
printMsg(fmt.Sprintf("ClusterPair %s created successfully. Direction Source -> Destination\n", clusterPairName), ioStreams.Out)

if uniDirectional {
return
}

sIP, sPort := getHostPortFromEndPoint(sEP, ioStreams)

if len(srcToken) == 0 {
Expand Down Expand Up @@ -598,7 +606,7 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
createClusterPairCommand.Flags().StringVarP(&destToken, "dest-token", "", "", "(Optional)Destination cluster token for cluster pairing")
createClusterPairCommand.Flags().StringVarP(&projectMappingsStr, "project-mappings", "", "", projectMappingHelpString)
createClusterPairCommand.Flags().StringVarP(&mode, "mode", "", "async-dr", "Mode of DR. [async-dr, sync-dr]")

createClusterPairCommand.Flags().BoolVarP(&uniDirectional, "unidirectional", "u", false, "(Optional) to create Clusterpair from source -> dest only")
// New parameters for creating backuplocation secret
createClusterPairCommand.Flags().StringVarP(&provider, "provider", "p", "", "External objectstore provider name. [s3, azure, google]")
createClusterPairCommand.Flags().StringVar(&bucket, "bucket", "", "Bucket name")
Expand Down
42 changes: 42 additions & 0 deletions pkg/storkctl/clusterpair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package storkctl

import (
"os"
"testing"

storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
Expand Down Expand Up @@ -126,3 +127,44 @@ func TestGenerateClusterPairInvalidNamespace(t *testing.T) {
expected := "error: the Namespace \"test_namespace\" is not valid: [a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')]"
testCommon(t, cmdArgs, nil, expected, true)
}

func TestCreateUniDirectionalClusterPairMissingParameters(t *testing.T) {
cmdArgs := []string{"create", "clusterpair", "uni-pair1", "-n", "test", "--unidirectional"}
expected := "error: missing parameter \"src-kube-file\" - Kubeconfig file missing for source cluster"
testCommon(t, cmdArgs, nil, expected, true)

srcConfig := createTempFile(t, "src.config", "source configuration")
destConfig := createTempFile(t, "dest.config", "destination configuration")
defer os.Remove(srcConfig.Name())
defer os.Remove(destConfig.Name())
cmdArgs = []string{"create", "clusterpair", "uni-pair1", "-n", "test", "--src-kube-file", srcConfig.Name(), "--unidirectional"}
expected = "error: missing parameter \"dest-kube-file\" - Kubeconfig file missing for destination cluster"
testCommon(t, cmdArgs, nil, expected, true)

cmdArgs = []string{"create", "clusterpair", "uni-pair1", "-n", "test", "--src-kube-file", srcConfig.Name(), "--dest-kube-file", destConfig.Name(), "-u"}
expected = "error: missing parameter \"provider\" - External objectstore provider needs to be either of azure, google, s3"
testCommon(t, cmdArgs, nil, expected, true)

cmdArgs = []string{"create", "clusterpair", "uni-pair1", "-n", "test", "--src-kube-file", srcConfig.Name(), "--dest-kube-file", srcConfig.Name(), "-u"}
expected = "error: source kubeconfig file and destination kubeconfig file should be different"
testCommon(t, cmdArgs, nil, expected, true)

srcConfigDuplicate := createTempFile(t, "srcDup.config", "source configuration")
defer os.Remove(srcConfigDuplicate.Name())
cmdArgs = []string{"create", "clusterpair", "uni-pair1", "-n", "test", "--src-kube-file", srcConfig.Name(), "--dest-kube-file", srcConfigDuplicate.Name(), "-u"}
expected = "error: source kubeconfig and destination kubeconfig file should be different"
testCommon(t, cmdArgs, nil, expected, true)

}

func createTempFile(t *testing.T, fileName string, fileContent string) *os.File {
f, err := os.CreateTemp("", fileName)
require.NoError(t, err, "Error creating file %s", fileName)
if _, err := f.Write([]byte(fileContent)); err != nil {
require.NoError(t, err, "Error writing to file %s", fileName)
}
if err := f.Close(); err != nil {
require.NoError(t, err, "Error closing the file %s", fileName)
}
return f
}
5 changes: 3 additions & 2 deletions pkg/storkctl/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

func newGenerateCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
generateCommands := &cobra.Command{
Use: "generate",
Short: "Generate stork specs",
Use: "generate",
Short: "Generate stork specs",
Deprecated: "use command \"create\" instead.",
}

generateCommands.AddCommand(
Expand Down

0 comments on commit 187ee4a

Please sign in to comment.