github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/alpha/rollouts/get/get.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 get 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/jedib0t/go-pretty/v6/table" 24 "github.com/spf13/cobra" 25 k8scmdutil "k8s.io/kubectl/pkg/cmd/util" 26 ) 27 28 func NewCommand(ctx context.Context, f k8scmdutil.Factory) *cobra.Command { 29 return newRunner(ctx, f).Command 30 } 31 32 func newRunner(ctx context.Context, f k8scmdutil.Factory) *runner { 33 r := &runner{ 34 ctx: ctx, 35 factory: f, 36 } 37 c := &cobra.Command{ 38 Use: "get", 39 Short: "lists rollouts", 40 Long: "lists rollouts", 41 Example: "lists rollouts", 42 RunE: r.runE, 43 } 44 r.Command = c 45 return r 46 } 47 48 type runner struct { 49 ctx context.Context 50 Command *cobra.Command 51 factory k8scmdutil.Factory 52 } 53 54 func (r *runner) runE(cmd *cobra.Command, _ []string) error { 55 rlc, err := rolloutsclient.New() 56 if err != nil { 57 fmt.Printf("%s\n", err) 58 return err 59 } 60 61 namespace, _, err := r.factory.ToRawKubeConfigLoader().Namespace() 62 if err != nil { 63 return err 64 } 65 rollouts, err := rlc.List(r.ctx, namespace) 66 if err != nil { 67 fmt.Printf("%s\n", err) 68 return err 69 } 70 renderRolloutsAsTable(cmd, rollouts) 71 return nil 72 } 73 74 func renderRolloutsAsTable(cmd *cobra.Command, rollouts *rolloutsapi.RolloutList) { 75 t := table.NewWriter() 76 t.SetOutputMirror(cmd.OutOrStdout()) 77 t.AppendHeader(table.Row{"ROLLOUT", "STATUS", "CLUSTERS (READY/TOTAL)"}) 78 for _, rollout := range rollouts.Items { 79 readyCount := 0 80 for _, cluster := range rollout.Status.ClusterStatuses { 81 if cluster.PackageStatus.Status == "Synced" { 82 readyCount++ 83 } 84 } 85 t.AppendRow([]interface{}{ 86 rollout.Name, 87 rollout.Status.Overall, 88 fmt.Sprintf("%d/%d", readyCount, len(rollout.Status.ClusterStatuses))}) 89 } 90 t.AppendSeparator() 91 t.Render() 92 }