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 0f8e630 commit 989ddb9
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

Check warning on line 329 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L325-L329

Added lines #L325 - L329 were not covered by tests
}
}
_, 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

Check warning on line 360 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L356-L360

Added lines #L356 - L360 were not covered by tests
}
}
_, 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

Check warning on line 510 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L506-L510

Added lines #L506 - L510 were not covered by tests
}
}
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

Check warning on line 594 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L590-L594

Added lines #L590 - L594 were not covered by tests
}
}
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

Check warning on line 923 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L920-L923

Added lines #L920 - L923 were not covered by tests
}
coreOps, err := core.NewForConfig(conf)
if err != nil {
return err

Check warning on line 927 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L925-L927

Added lines #L925 - L927 were not covered by tests
}
_, err = coreOps.CreateNamespace(&v1.Namespace{
ObjectMeta: meta.ObjectMeta{
Name: namespace,
},
})
if err != nil && !k8serrors.IsAlreadyExists(err) {
return err

Check warning on line 935 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L929-L935

Added lines #L929 - L935 were not covered by tests
}
return nil

Check warning on line 937 in pkg/storkctl/clusterpair.go

View check run for this annotation

Codecov / codecov/patch

pkg/storkctl/clusterpair.go#L937

Added line #L937 was not covered by tests
}

0 comments on commit 989ddb9

Please sign in to comment.