From fe90df4bf4bfee6577d2154c911a8336aa358b59 Mon Sep 17 00:00:00 2001 From: Mike Kolesnik Date: Mon, 7 Mar 2022 17:23:49 +0200 Subject: [PATCH] Print out brokers information on `subctl show all` Print out the info, even if clusters only have brokers. Clusters that don't have brokers will just show informatory "Detecting brokers" message and nothing further. Resolves: #1894 Signed-off-by: Mike Kolesnik (cherry picked from commit 56dbe5102c3c0cddabb4ef951ddcd46e9cb9f047) --- pkg/subctl/cmd/show/all.go | 11 ++++- pkg/subctl/cmd/show/brokers.go | 71 +++++++++++++++++++++++++++++++++ pkg/subctl/cmd/show/versions.go | 12 +++++- 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 pkg/subctl/cmd/show/brokers.go diff --git a/pkg/subctl/cmd/show/all.go b/pkg/subctl/cmd/show/all.go index 93bf89ad0..9980b1c5f 100644 --- a/pkg/subctl/cmd/show/all.go +++ b/pkg/subctl/cmd/show/all.go @@ -41,14 +41,21 @@ func init() { func showAll(cluster *cmd.Cluster) bool { status := cli.NewStatus() + success := showBrokers(cluster) + + fmt.Println() + if cluster.Submariner == nil { + success = getVersions(cluster) && success + + fmt.Println() status.Start(cmd.SubmMissingMessage) status.EndWith(cli.Warning) - return true + return success } - success := showConnections(cluster) + success = showConnections(cluster) && success fmt.Println() diff --git a/pkg/subctl/cmd/show/brokers.go b/pkg/subctl/cmd/show/brokers.go new file mode 100644 index 000000000..a06fcb0ad --- /dev/null +++ b/pkg/subctl/cmd/show/brokers.go @@ -0,0 +1,71 @@ +/* +SPDX-License-Identifier: Apache-2.0 + +Copyright Contributors to the Submariner project. + +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 show + +import ( + "context" + "fmt" + "strings" + + "github.com/submariner-io/submariner-operator/internal/cli" + "github.com/submariner-io/submariner-operator/pkg/client" + "github.com/submariner-io/submariner-operator/pkg/subctl/cmd" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func showBrokers(cluster *cmd.Cluster) bool { + template := "%-25.24s%-25.24s%-40.39s\n" + status := cli.NewStatus() + + status.Start("Detecting broker(s)") + + clientProducer, err := client.NewProducerFromRestConfig(cluster.Config) + if err != nil { + status.EndWithFailure("Error creating client producer") + return false + } + + brokerList, err := clientProducer.ForOperator().SubmarinerV1alpha1().Brokers(corev1.NamespaceAll).List( + context.TODO(), metav1.ListOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + status.EndWithFailure(err.Error()) + return false + } + + status.End() + + brokers := brokerList.Items + if len(brokers) == 0 { + return true + } + + fmt.Printf(template, "NAMESPACE", "NAME", "COMPONENTS") + + for i := range brokers { + fmt.Printf( + template, + brokers[i].Namespace, + brokers[i].Name, + strings.Join(brokers[i].Spec.Components, ", "), + ) + } + + return true +} diff --git a/pkg/subctl/cmd/show/versions.go b/pkg/subctl/cmd/show/versions.go index b2dd07293..6f9bbfd60 100644 --- a/pkg/subctl/cmd/show/versions.go +++ b/pkg/subctl/cmd/show/versions.go @@ -70,6 +70,10 @@ func getSubmarinerVersion(submariner *v1alpha1.Submariner, versions []versionIma func getOperatorVersion(clientSet kubernetes.Interface, versions []versionImageInfo) ([]versionImageInfo, error) { operatorConfig, err := clientSet.AppsV1().Deployments(cmd.OperatorNamespace).Get(context.TODO(), names.OperatorComponent, v1.GetOptions{}) if err != nil { + if apierrors.IsNotFound(err) { + return versions, nil + } + return nil, errors.Wrap(err, "error retrieving Deployment") } @@ -105,7 +109,9 @@ func getVersions(cluster *cmd.Cluster) bool { submarinerClient, err := submarinerclientset.NewForConfig(cluster.Config) exit.OnErrorWithMessage(err, "Unable to get the Submariner client") - versions = getSubmarinerVersion(cluster.Submariner, versions) + if cluster.Submariner != nil { + versions = getSubmarinerVersion(cluster.Submariner, versions) + } versions, err = getOperatorVersion(cluster.KubeClient, versions) exit.OnErrorWithMessage(err, "Unable to get the Operator version") @@ -116,6 +122,10 @@ func getVersions(cluster *cmd.Cluster) bool { printVersions(versions) status.EndWith(cli.Success) + if len(versions) > 0 { + printVersions(versions) + } + return true }