Skip to content

Commit

Permalink
PWX-32573: Creating namespaces as part of clusterpair creation if the…
Browse files Browse the repository at this point in the history
… input namespace does not exist .
  • Loading branch information
diptiranjanpx committed Jul 28, 2023
1 parent 822ae74 commit dde199c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/storkctl/clusterpair.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
storkops "github.com/portworx/sched-ops/k8s/stork"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/validation"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
Expand Down Expand Up @@ -321,6 +322,13 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions

storkops.Instance().SetConfig(conf)
core.Instance().SetConfig(conf)
if len(cmdFactory.GetNamespace()) > 0 {
err = createNamespace(sFile, cmdFactory.GetNamespace())
if err != nil {
util.CheckErr(err)
return
}
}
_, err = storkops.Instance().CreateClusterPair(srcClusterPair)
if err != nil {
util.CheckErr(err)
Expand All @@ -345,6 +353,13 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
storkops.Instance().SetConfig(conf)
core.Instance().SetConfig(conf)

if len(cmdFactory.GetNamespace()) > 0 {
err = createNamespace(dFile, cmdFactory.GetNamespace())
if err != nil {
util.CheckErr(err)
return
}
}
_, err = storkops.Instance().CreateClusterPair(destClusterPair)
if err != nil {
util.CheckErr(err)
Expand Down Expand Up @@ -488,6 +503,13 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
return
}

if len(cmdFactory.GetNamespace()) > 0 {
err = createNamespace(sFile, cmdFactory.GetNamespace())
if err != nil {
util.CheckErr(err)
return
}
}
printMsg(fmt.Sprintf("\nCreating Secret and ObjectstoreLocation in source cluster in namespace %v...", cmdFactory.GetNamespace()), ioStreams.Out)
storkops.Instance().SetConfig(conf)
core.Instance().SetConfig(conf)
Expand Down Expand Up @@ -565,6 +587,13 @@ func newCreateClusterPairCommand(cmdFactory Factory, ioStreams genericclioptions
storkops.Instance().SetConfig(conf)
core.Instance().SetConfig(conf)

if len(cmdFactory.GetNamespace()) > 0 {
err = createNamespace(dFile, cmdFactory.GetNamespace())
if err != nil {
util.CheckErr(err)
return
}
}
printMsg(fmt.Sprintf("Creating Secret and ObjectstoreLocation in destination cluster in namespace %v...", cmdFactory.GetNamespace()), ioStreams.Out)
_, err = core.Instance().CreateSecret(secret)
if err != nil {
Expand Down Expand Up @@ -887,3 +916,23 @@ func getHostPortFromEndPoint(ep string, ioStreams genericclioptions.IOStreams) (
func getMissingParameterError(param string, desc string) error {
return fmt.Errorf("missing parameter %q - %s", param, desc)
}

func createNamespace(config string, namespace string) error {
conf, err := getConfig(config).ClientConfig()
if err != nil {
return err
}
coreOps, err := core.NewForConfig(conf)
if err != nil {
return err
}
_, err = coreOps.CreateNamespace(&v1.Namespace{
ObjectMeta: meta.ObjectMeta{
Name: namespace,
},
})
if err != nil && !k8serrors.IsAlreadyExists(err) {
return err
}
return nil
}

0 comments on commit dde199c

Please sign in to comment.