github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/status.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/create"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    10  
    11  	"github.com/olli-ai/jx/v2/pkg/util"
    12  
    13  	"github.com/jenkins-x/jx-logging/pkg/log"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    15  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    16  	"github.com/olli-ai/jx/v2/pkg/kube"
    17  	"github.com/spf13/cobra"
    18  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  )
    20  
    21  type StatusOptions struct {
    22  	*opts.CommonOptions
    23  	node string
    24  }
    25  
    26  var (
    27  	StatusLong = templates.LongDesc(`
    28  		Gets the current status of the Kubernetes cluster
    29  
    30  `)
    31  
    32  	StatusExample = templates.Examples(`
    33  		# displays the current status of the Kubernetes cluster
    34  		jx status
    35  `)
    36  )
    37  
    38  func NewCmdStatus(commonOpts *opts.CommonOptions) *cobra.Command {
    39  	options := &StatusOptions{
    40  		CommonOptions: commonOpts,
    41  	}
    42  	cmd := &cobra.Command{
    43  		Use:     "status [node]",
    44  		Short:   "status of the Kubernetes cluster or named node",
    45  		Long:    StatusLong,
    46  		Example: StatusExample,
    47  		Aliases: []string{"status"},
    48  		Run: func(cmd *cobra.Command, args []string) {
    49  			options.Cmd = cmd
    50  			options.Args = args
    51  			err := options.Run()
    52  			helper.CheckErr(err)
    53  		},
    54  	}
    55  
    56  	cmd.Flags().StringVarP(&options.node, "node", "n", "", "the named node to get ")
    57  	return cmd
    58  }
    59  
    60  func (o *StatusOptions) Run() error {
    61  
    62  	client, namespace, err := o.KubeClientAndNamespace()
    63  	if err != nil {
    64  
    65  		log.Logger().Warnf("Unable to connect to Kubernetes cluster -  is one running ?")
    66  		log.Logger().Warnf("you could try: jx create cluster - e.g: %s", create.CreateClusterExample)
    67  		log.Logger().Warnf(create.CreateClusterLong)
    68  
    69  		return err
    70  	}
    71  
    72  	/*
    73  	 * get status for all pods in all namespaces
    74  	 */
    75  	clusterStatus, err := kube.GetClusterStatus(client, "", o.Verbose)
    76  	if err != nil {
    77  		log.Logger().Errorf("Failed to get cluster status %s", err.Error())
    78  		return err
    79  	}
    80  
    81  	deployList, err := client.AppsV1().Deployments(namespace).List(metav1.ListOptions{})
    82  	if err != nil {
    83  		log.Logger().Errorf("Failed to get deployed  status %s", err.Error())
    84  		return err
    85  	}
    86  
    87  	if deployList == nil || len(deployList.Items) == 0 {
    88  		log.Logger().Warnf("Unable to find JX components in %s", clusterStatus.Info())
    89  		log.Logger().Infof("you could try: %s", create.InstalExample)
    90  		log.Logger().Info(create.InstalLong)
    91  		return fmt.Errorf("no deployments found in namespace %s", namespace)
    92  	}
    93  
    94  	for _, d := range deployList.Items {
    95  		err = kube.WaitForDeploymentToBeReady(client, d.Name, namespace, 5*time.Second)
    96  		if err != nil {
    97  			log.Logger().Warnf("%s: jx deployment %s not ready in namespace %s", clusterStatus.Info(), d.Name, namespace)
    98  			return err
    99  		}
   100  	}
   101  
   102  	resourceStr := clusterStatus.CheckResource()
   103  
   104  	if resourceStr != "" {
   105  		log.Logger().Warnf("Jenkins X installed for %s.\n%s", clusterStatus.Info(), util.ColorWarning(resourceStr))
   106  	} else {
   107  		log.Logger().Infof("Jenkins X checks passed for %s.", clusterStatus.Info())
   108  	}
   109  
   110  	return nil
   111  }