Skip to content

Commit

Permalink
Use only subm cluster to recover broker-info.subm
Browse files Browse the repository at this point in the history
instead of specifying both Broker and Submariner clusters.

Depnds on:
submariner-io/submariner-operator#2547

Signed-off-by: Janki Chhatbar <[email protected]>
  • Loading branch information
Jaanki committed Mar 24, 2023
1 parent e6cae6d commit 07e23ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 105 deletions.
139 changes: 38 additions & 101 deletions cmd/subctl/recover_broker_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,34 @@ limitations under the License.
package subctl

import (
"errors"
"context"
"fmt"
"github.com/submariner-io/subctl/internal/constants"
"github.com/submariner-io/subctl/pkg/brokercr"
"github.com/submariner-io/subctl/pkg/client"
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/spf13/cobra"
"github.com/submariner-io/admiral/pkg/reporter"
"github.com/submariner-io/subctl/internal/cli"
"github.com/submariner-io/subctl/internal/constants"
"github.com/submariner-io/subctl/internal/exit"
"github.com/submariner-io/subctl/internal/restconfig"
"github.com/submariner-io/subctl/pkg/broker"
"github.com/submariner-io/subctl/pkg/cluster"
"github.com/submariner-io/submariner-operator/api/v1alpha1"
submarinerv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

var recoverRestConfigProducer = restconfig.NewProducer().WithPrefixedContext("broker").
WithDefaultNamespace(constants.DefaultBrokerNamespace).
WithPrefixedNamespace("broker", constants.DefaultBrokerNamespace)
var recoverRestConfigProducer = restconfig.NewProducer()

// recoverBrokerInfo represents the reconstruct command.
var recoverBrokerInfo = &cobra.Command{
Use: "recover-broker-info",
Short: "Recovers the broker-info.subm file from the installed Broker",
Run: func(cmd *cobra.Command, args []string) {
status := cli.NewReporter()
// if --brokerconfig flag provided, get the broker and proceed to get Submariner
contextFound, err := recoverRestConfigProducer.RunOnSelectedPrefixedContext("broker",
recoverBrokerFromConfigContext, status)
exit.OnError(err)

// if --brokerconfig not provided, search for broker on current context and proceed to get Submariner
if !contextFound {
err = recoverRestConfigProducer.RunOnSelectedContext(recoverBrokerFromCurrentContext, status)
}
exit.OnError(err)

exit.OnError(recoverRestConfigProducer.RunOnSelectedContext(restconfig.IfConnectivityInstalled(recoverBrokerInfoFromSubm), status))
},
}

Expand All @@ -63,71 +55,52 @@ func init() {
rootCmd.AddCommand(recoverBrokerInfo)
}

func recoverBrokerFromConfigContext(brokerCluster *cluster.Info, brokerNamespace string, status reporter.Interface) error {
brokerObj, err := getBroker(brokerCluster, brokerNamespace, "Please try again with another cluster", status)
if err != nil {
return err
}
func recoverBrokerInfoFromSubm(submCluster *cluster.Info, brokerNamespace string, status reporter.Interface) error {
// Override the default namespace with the Broker namespace
brokerNamespace = submCluster.Submariner.Spec.BrokerK8sRemoteNamespace

clusters, err := brokerCluster.GetClusters(brokerNamespace)
// Check if Broker is installed on the same cluster as Submariner
brokerObj, err := getBroker(submCluster, brokerNamespace, status)
if err != nil {
return status.Error(err, "error listing joined clusters")
status.Warning("Error getting Broker %q\n", err)
}

ok, err := tryToRecoverFromBroker(brokerCluster, brokerObj, brokerNamespace, clusters, status)
if ok || err != nil {
return err
}
brokerRestConfig := submCluster.RestConfig

status.Warning("Submariner is not installed on the same cluster as Broker")
status.Start("Checking if Submariner is installed on a different cluster")
//nolint:wrapcheck // No need to wrap errors here.
return recoverRestConfigProducer.RunOnSelectedContext(
func(submCluster *cluster.Info, namespace string, status reporter.Interface) error {
if isSubmJoinedToBroker(clusters, submCluster) {
status.Success("Found a Submariner installation on cluster %q joined to the Broker", submCluster.Name)
//nolint:wrapcheck // No need to wrap errors here.
return broker.RecoverData(brokerCluster, submCluster, brokerObj, namespace, status)
}

return status.Error(
fmt.Errorf("submariner is not installed on cluster %s. "+
"Please specify the cluster where Submariner is installed via `--kubeconfig` or `--context` flag"+
"", submCluster.Name), "")
}, status)
}
if brokerObj == nil {
status.Warning("Broker not found on the same cluster. Try to connect to Broker installed on another cluster")
brokerRestConfig, brokerNamespace, err = restconfig.ForBroker(submCluster.Submariner, submCluster.ServiceDiscovery)
if err != nil || brokerRestConfig == nil {
return status.Error(err, "Error getting the broker's rest config")
}

func recoverBrokerFromCurrentContext(clusterInfo *cluster.Info, namespace string, status reporter.Interface) error {
// if --brokerconfig not provided, search for broker on current context and proceed to get Submariner
brokerObj, err := getBroker(clusterInfo, namespace, "Please specify the cluster where the Broker is installed "+
"via the `--brokerconfig` or `--brokercontext` flag and the namespace via the `--brokernamespace` flag", status)
if err != nil {
return err
}
brokerClientProducer, err := client.NewProducerFromRestConfig(brokerRestConfig)
if err != nil {
return status.Error(err,"Error creating broker client Producer")
}

clusters, err := clusterInfo.GetClusters(namespace)
if err != nil {
return status.Error(err, "error listing joined clusters")
brokerObj := &v1alpha1.Broker{}
err = brokerClientProducer.ForGeneral().Get(
context.TODO(), controllerClient.ObjectKey{
Namespace: constants.OperatorNamespace,
Name: brokercr.Name,
}, brokerObj)
if err != nil {
return status.Error(err, "Error getting Broker")
}
}

ok, err := tryToRecoverFromBroker(clusterInfo, brokerObj, namespace, clusters, status)
if ok || err != nil {
return err
}
exit.OnError(broker.RecoverData(submCluster, brokerObj, brokerNamespace, brokerRestConfig, status))

return status.Error(
fmt.Errorf("submariner is not installed on cluster %q. "+
"Please specify the cluster where Submariner is installed via `--kubeconfig` or `--context` flag"+
"", clusterInfo.Name), "")
return nil
}

func getBroker(clusterInfo *cluster.Info, namespace, notFoundMsg string, status reporter.Interface) (*v1alpha1.Broker, error) {
func getBroker(clusterInfo *cluster.Info, namespace string, status reporter.Interface) (*v1alpha1.Broker, error) {
status.Start("Checking if the Broker is installed on cluster %q in namespace %q", clusterInfo.Name, namespace)

brokerObj, err := clusterInfo.GetBroker(namespace)
if apierrors.IsNotFound(err) {
return nil, status.Error(fmt.Errorf("the Broker is not installed on the specified cluster in namespace %s. %s", notFoundMsg,
namespace), "")
return nil, status.Error(fmt.Errorf("the Broker is not installed on the specified cluster in namespace %s", namespace), "")
}

if err != nil {
Expand All @@ -138,39 +111,3 @@ func getBroker(clusterInfo *cluster.Info, namespace, notFoundMsg string, status

return brokerObj, nil
}

func tryToRecoverFromBroker(brokerCluster *cluster.Info, brokerObj *v1alpha1.Broker,
brokerNamespace string, clusters []submarinerv1.Cluster, status reporter.Interface,
) (bool, error) {
status.Start("Checking if there are any clusters joined to the Broker")

if len(clusters) == 0 {
return false, status.Error(
errors.New(
"no clusters are joined to the Broker. Please re-run the `deploy-broker` command to regenerate the broker-info."+
"subm file"), "")
}

status.Success("Found %d cluster(s) joined to the Broker", len(clusters))
status.End()

if isSubmJoinedToBroker(clusters, brokerCluster) {
status.Success("Found a local Submariner installation joined to the Broker")
//nolint:wrapcheck // No need to wrap errors here.
return true, broker.RecoverData(brokerCluster, brokerCluster, brokerObj, brokerNamespace, status)
}

return false, nil
}

func isSubmJoinedToBroker(clusters []submarinerv1.Cluster, clusterInfo *cluster.Info) bool {
if clusterInfo.Submariner != nil {
for i := range clusters {
if clusters[i].Spec.ClusterID == clusterInfo.Submariner.Spec.ClusterID {
return true
}
}
}

return false
}
8 changes: 4 additions & 4 deletions pkg/broker/recover_broker_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ package broker

import (
"encoding/base64"
"k8s.io/client-go/rest"

"github.com/submariner-io/admiral/pkg/reporter"
"github.com/submariner-io/subctl/pkg/cluster"
"github.com/submariner-io/submariner-operator/api/v1alpha1"
"k8s.io/apimachinery/pkg/util/sets"
)

func RecoverData(
brokerCluster, submCluster *cluster.Info, broker *v1alpha1.Broker, namespace string, status reporter.Interface,
) error {
func RecoverData(submCluster *cluster.Info, broker *v1alpha1.Broker, brokerNamespace string,
brokerRestConfig *rest.Config ,status reporter.Interface) error {
status.Start("Retrieving data to reconstruct broker-info.subm")
defer status.End()

Expand All @@ -42,7 +42,7 @@ func RecoverData(

status.Success("Successfully retrieved the data. Writing it to broker-info.subm")

err = WriteInfoToFile(brokerCluster.RestConfig, namespace, decodedPSKSecret,
err = WriteInfoToFile(brokerRestConfig, brokerNamespace, decodedPSKSecret,
sets.New(broker.Spec.Components...), broker.Spec.DefaultCustomDomains, status)

return status.Error(err, "error reconstructing broker-info.subm")
Expand Down

0 comments on commit 07e23ef

Please sign in to comment.