github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/alpha/rollouts/status/status.go (about)

     1  // Copyright 2023 The kpt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package status
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/GoogleContainerTools/kpt/commands/alpha/rollouts/rolloutsclient"
    22  	rolloutsapi "github.com/GoogleContainerTools/kpt/rollouts/api/v1alpha1"
    23  	"github.com/spf13/cobra"
    24  	k8scmdutil "k8s.io/kubectl/pkg/cmd/util"
    25  
    26  	"github.com/jedib0t/go-pretty/v6/table"
    27  )
    28  
    29  func newRunner(ctx context.Context, f k8scmdutil.Factory) *runner {
    30  	r := &runner{
    31  		ctx:     ctx,
    32  		factory: f,
    33  	}
    34  	c := &cobra.Command{
    35  		Use:     "status",
    36  		Short:   "displays status of a rollout",
    37  		Long:    "displays status of a rollout",
    38  		Example: "displays status of a rollout",
    39  		RunE:    r.runE,
    40  	}
    41  	r.Command = c
    42  	return r
    43  }
    44  
    45  func NewCommand(ctx context.Context, f k8scmdutil.Factory) *cobra.Command {
    46  	return newRunner(ctx, f).Command
    47  }
    48  
    49  type runner struct {
    50  	ctx     context.Context
    51  	Command *cobra.Command
    52  	factory k8scmdutil.Factory
    53  }
    54  
    55  func (r *runner) runE(cmd *cobra.Command, args []string) error {
    56  	rlc, err := rolloutsclient.New()
    57  	if err != nil {
    58  		fmt.Printf("%s\n", err)
    59  		return err
    60  	}
    61  
    62  	if len(args) == 0 {
    63  		fmt.Printf("must provide rollout name")
    64  		return nil
    65  	}
    66  
    67  	namespace, _, err := r.factory.ToRawKubeConfigLoader().Namespace()
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	rollout, err := rlc.Get(r.ctx, namespace, args[0])
    73  	if err != nil {
    74  		fmt.Printf("%s\n", err)
    75  		return err
    76  	}
    77  
    78  	if len(rollout.Status.WaveStatuses) > 0 {
    79  		renderWaveStatusAsTable(cmd, rollout)
    80  		return nil
    81  	}
    82  	renderStatusAsTable(cmd, rollout)
    83  	return nil
    84  }
    85  
    86  func renderStatusAsTable(cmd *cobra.Command, rollout *rolloutsapi.Rollout) {
    87  	t := table.NewWriter()
    88  	t.SetOutputMirror(cmd.OutOrStdout())
    89  	t.AppendHeader(table.Row{"CLUSTER", "PACKAGE ID", "PACKAGE STATUS", "SYNC STATUS"})
    90  	for _, cluster := range rollout.Status.ClusterStatuses {
    91  		pkgStatus := cluster.PackageStatus
    92  		t.AppendRow([]interface{}{cluster.Name, pkgStatus.PackageID, pkgStatus.Status, pkgStatus.SyncStatus})
    93  	}
    94  	t.AppendSeparator()
    95  	// t.AppendRow([]interface{}{300, "Tyrion", "Lannister", 5000})
    96  	// t.AppendFooter(table.Row{"", "", "Total", 10000})
    97  	t.Render()
    98  }
    99  
   100  func renderWaveStatusAsTable(cmd *cobra.Command, rollout *rolloutsapi.Rollout) {
   101  	t := table.NewWriter()
   102  	t.SetOutputMirror(cmd.OutOrStdout())
   103  	t.AppendHeader(table.Row{"WAVE", "CLUSTER", "PACKAGE ID", "PACKAGE STATUS", "SYNC STATUS"})
   104  	for _, wave := range rollout.Status.WaveStatuses {
   105  		for i, cluster := range wave.ClusterStatuses {
   106  			pkgStatus := cluster.PackageStatus
   107  			waveName := ""
   108  			if i == 0 {
   109  				waveName = wave.Name
   110  			}
   111  			t.AppendRow([]interface{}{waveName, cluster.Name, pkgStatus.PackageID, pkgStatus.Status, pkgStatus.SyncStatus})
   112  		}
   113  		t.AppendSeparator()
   114  	}
   115  	// t.AppendRow([]interface{}{300, "Tyrion", "Lannister", 5000})
   116  	// t.AppendFooter(table.Row{"", "", "Total", 10000})
   117  	t.Render()
   118  }