Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for egctl x status #2550

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/cmd/egctl/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
`,
}

experimentalCommand.AddCommand(NewTranslateCommand())
experimentalCommand.AddCommand(statsCommand())
experimentalCommand.AddCommand(newTranslateCommand())
experimentalCommand.AddCommand(newStatsCommand())
experimentalCommand.AddCommand(newStatusCommand())

Check warning on line 27 in internal/cmd/egctl/experimental.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/experimental.go#L25-L27

Added lines #L25 - L27 were not covered by tests

return experimentalCommand
}
2 changes: 1 addition & 1 deletion internal/cmd/egctl/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"github.com/spf13/cobra"
)

func statsCommand() *cobra.Command {
func newStatsCommand() *cobra.Command {

Check warning on line 12 in internal/cmd/egctl/stats.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/stats.go#L12

Added line #L12 was not covered by tests
c := &cobra.Command{
Use: "stats",
Long: "Retrieve statistics from envoy proxy.",
Expand Down
324 changes: 324 additions & 0 deletions internal/cmd/egctl/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package egctl

import (
"context"
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"text/tabwriter"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
gwv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
)

func newStatusCommand() *cobra.Command {
var (
quiet, verbose, allNamespaces bool
resourceType, namespace string
)

statusCommand := &cobra.Command{
Use: "status",
Short: "Show the summary of the status of resources in Envoy Gateway",
Example: ` # Show the status of gatewayclass resources under default namespace.
egctl x status gatewayclass

# Show the status of gateway resources with less information under default namespace.
egctl x status gateway -q

# Show the status of gateway resources with details under default namespace.
egctl x status gateway -v

# Show the status of httproute resources with details under a specific namespace.
egctl x status httproute -v -n foobar

# Show the status of httproute resources under all namespaces.
egctl x status httproute -A
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

table := newStatusTableWriter(os.Stdout)
k8sClient, err := newK8sClient()
if err != nil {
return err
}

Check warning on line 58 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L27-L58

Added lines #L27 - L58 were not covered by tests

switch {
case len(args) == 1:
resourceType = args[0]
case len(args) > 1:
return fmt.Errorf("unknown args: %s", strings.Join(args[1:], ","))
default:
return fmt.Errorf("invalid args: must specific a resources type")

Check warning on line 66 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L60-L66

Added lines #L60 - L66 were not covered by tests
}

return runStatus(ctx, k8sClient, table, resourceType, namespace, quiet, verbose, allNamespaces)

Check warning on line 69 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L69

Added line #L69 was not covered by tests
},
}

statusCommand.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "Show the status of resources only")
statusCommand.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Show the status of resources with details")
statusCommand.PersistentFlags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "Get resources from all namespaces")
statusCommand.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "Specific a namespace to get resources")

return statusCommand

Check warning on line 78 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L73-L78

Added lines #L73 - L78 were not covered by tests
}

func newStatusTableWriter(out io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(out, 10, 0, 3, ' ', 0)
}

func runStatus(ctx context.Context, cli client.Client, table *tabwriter.Writer, resourceType, namespace string, quiet, verbose, allNamespaces bool) error {
var resourcesList client.ObjectList

if allNamespaces {
namespace = ""
}

Check warning on line 90 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L85-L90

Added lines #L85 - L90 were not covered by tests

switch strings.ToLower(resourceType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something for the future, dont want to block this PR, but all can be helpful here to fetch statuses of all CRDs at once

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i will add this in a follow-up PR

case "gc", "gatewayclass":
gc := gwv1.GatewayClassList{}
if err := cli.List(ctx, &gc, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &gc

Check warning on line 98 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L92-L98

Added lines #L92 - L98 were not covered by tests

case "gtw", "gateway":
gtw := gwv1.GatewayList{}
if err := cli.List(ctx, &gtw, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &gtw

Check warning on line 105 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L100-L105

Added lines #L100 - L105 were not covered by tests

case "httproute":
httproute := gwv1.HTTPRouteList{}
if err := cli.List(ctx, &httproute, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &httproute

Check warning on line 112 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L107-L112

Added lines #L107 - L112 were not covered by tests

case "grpcroute":
grpcroute := gwv1a2.GRPCRouteList{}
if err := cli.List(ctx, &grpcroute, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &grpcroute

Check warning on line 119 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L114-L119

Added lines #L114 - L119 were not covered by tests

case "tcproute":
tcproute := gwv1a2.TCPRouteList{}
if err := cli.List(ctx, &tcproute, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &tcproute

Check warning on line 126 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L121-L126

Added lines #L121 - L126 were not covered by tests

case "udproute":
udproute := gwv1a2.UDPRouteList{}
if err := cli.List(ctx, &udproute, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &udproute

Check warning on line 133 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L128-L133

Added lines #L128 - L133 were not covered by tests

case "tlsroute":
tlsroute := gwv1a2.TLSRouteList{}
if err := cli.List(ctx, &tlsroute, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &tlsroute

Check warning on line 140 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L135-L140

Added lines #L135 - L140 were not covered by tests

case "btlspolicy", "backendtlspolicy":
btlspolicy := gwv1a2.BackendTLSPolicyList{}
if err := cli.List(ctx, &btlspolicy, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &btlspolicy

Check warning on line 147 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L142-L147

Added lines #L142 - L147 were not covered by tests

case "btp", "backendtrafficpolicy":
btp := egv1a1.BackendTrafficPolicyList{}
if err := cli.List(ctx, &btp, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &btp

Check warning on line 154 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L149-L154

Added lines #L149 - L154 were not covered by tests

case "ctp", "clienttrafficpolicy":
ctp := egv1a1.ClientTrafficPolicyList{}
if err := cli.List(ctx, &ctp, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &ctp

Check warning on line 161 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L156-L161

Added lines #L156 - L161 were not covered by tests

case "epp", "enovypatchpolicy":
epp := egv1a1.EnvoyPatchPolicyList{}
if err := cli.List(ctx, &epp, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &epp

Check warning on line 168 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L163-L168

Added lines #L163 - L168 were not covered by tests

case "sp", "securitypolicy":
sp := egv1a1.SecurityPolicyList{}
if err := cli.List(ctx, &sp, client.InNamespace(namespace)); err != nil {
return err
}
resourcesList = &sp

Check warning on line 175 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L170-L175

Added lines #L170 - L175 were not covered by tests

default:
return fmt.Errorf("unknown resource type: %s", resourceType)

Check warning on line 178 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L177-L178

Added lines #L177 - L178 were not covered by tests
}

namespaced, err := cli.IsObjectNamespaced(resourcesList)
if err != nil {
return err
}

Check warning on line 184 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L181-L184

Added lines #L181 - L184 were not covered by tests

needNamespaceHeader := allNamespaces && namespaced
writeStatusHeaders(table, verbose, needNamespaceHeader)

if err = writeStatusBodies(table, resourcesList, resourceType, quiet, verbose, needNamespaceHeader); err != nil {
return err
}

Check warning on line 191 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L186-L191

Added lines #L186 - L191 were not covered by tests

return table.Flush()

Check warning on line 193 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L193

Added line #L193 was not covered by tests
}

func writeStatusHeaders(table *tabwriter.Writer, verbose, needNamespace bool) {
headers := []string{"NAME", "TYPE", "STATUS", "REASON"}

if needNamespace {
headers = append([]string{"NAMESPACE"}, headers...)
}
if verbose {
headers = append(headers, []string{"MESSAGE", "OBSERVED GENERATION", "LAST TRANSITION TIME"}...)
}

fmt.Fprintln(table, strings.Join(headers, "\t"))
}

func writeStatusBodies(table *tabwriter.Writer, resourcesList client.ObjectList, resourceType string, quiet, verbose, needNamespace bool) error {
v := reflect.ValueOf(resourcesList).Elem()

itemsField := v.FieldByName("Items")
if !itemsField.IsValid() {
return fmt.Errorf("failed to load `.Items` field from %s", resourceType)
}

Check warning on line 215 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L214-L215

Added lines #L214 - L215 were not covered by tests

for i := 0; i < itemsField.Len(); i++ {
item := itemsField.Index(i)

var name, namespace string
nameField := item.FieldByName("Name")
if !nameField.IsValid() {
return fmt.Errorf("failed to find `.Items[i].Name` field from %s", resourceType)
}

Check warning on line 224 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L223-L224

Added lines #L223 - L224 were not covered by tests
name = nameField.String()

if needNamespace {
namespaceField := item.FieldByName("Namespace")
if !namespaceField.IsValid() {
return fmt.Errorf("failed to find `.Items[i].Namespace` field from %s", resourceType)
}

Check warning on line 231 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L230-L231

Added lines #L230 - L231 were not covered by tests
namespace = namespaceField.String()
}

statusField := item.FieldByName("Status")
if !statusField.IsValid() {
return fmt.Errorf("failed to find `.Items[i].Status` field from %s", resourceType)
}

Check warning on line 238 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L237-L238

Added lines #L237 - L238 were not covered by tests

// Different resources store the conditions at different position.
switch {
case strings.Contains(resourceType, "route"):
// Scrape conditions from `Resource.Status.Parents[i].Conditions` field
parentsField := statusField.FieldByName("Parents")
if !parentsField.IsValid() {
return fmt.Errorf("failed to find `.Items[i].Status.Parents` field from %s", resourceType)
}

Check warning on line 247 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L246-L247

Added lines #L246 - L247 were not covered by tests

for j := 0; j < parentsField.Len(); j++ {
parentItem := parentsField.Index(j)
if err := findAndWriteConditions(table, parentItem, resourceType, name, namespace, quiet, verbose, needNamespace); err != nil {
return err
}

Check warning on line 253 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L252-L253

Added lines #L252 - L253 were not covered by tests
}

case resourceType == "btlspolicy" || resourceType == "backendtlspolicy":
// Scrape conditions from `Resource.Status.Ancestors[i].Conditions` field
ancestorsField := statusField.FieldByName("Ancestors")
if !ancestorsField.IsValid() {
return fmt.Errorf("failed to find `.Items[i].Status.Ancestors` field from %s", resourceType)
}

Check warning on line 261 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L260-L261

Added lines #L260 - L261 were not covered by tests

for j := 0; j < ancestorsField.Len(); j++ {
ancestorItem := ancestorsField.Index(j)
if err := findAndWriteConditions(table, ancestorItem, resourceType, name, namespace, quiet, verbose, needNamespace); err != nil {
return err
}

Check warning on line 267 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L266-L267

Added lines #L266 - L267 were not covered by tests
}

default:
// Scrape conditions from `Resource.Status.Conditions` field
if err := findAndWriteConditions(table, statusField, resourceType, name, namespace, quiet, verbose, needNamespace); err != nil {
return err
}

Check warning on line 274 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L273-L274

Added lines #L273 - L274 were not covered by tests
}
}

return nil
}

func findAndWriteConditions(table *tabwriter.Writer, parent reflect.Value, resourceType, name, namespace string, quiet, verbose, needNamespace bool) error {
conditionsField := parent.FieldByName("Conditions")
if !conditionsField.IsValid() {
return fmt.Errorf("failed to find `Conditions` field for %s", resourceType)
}

Check warning on line 285 in internal/cmd/egctl/status.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/egctl/status.go#L284-L285

Added lines #L284 - L285 were not covered by tests

conditions := conditionsField.Interface().([]metav1.Condition)
writeConditions(table, conditions, name, namespace, quiet, verbose, needNamespace)

return nil
}

func writeConditions(table *tabwriter.Writer, conditions []metav1.Condition, name, namespace string, quiet, verbose, needNamespace bool) {
// Sort in descending order by time of each condition.
for i := len(conditions) - 1; i >= 0; i-- {
if i < len(conditions)-1 {
name, namespace = "", ""
}

writeCondition(table, conditions[i], name, namespace, verbose, needNamespace)

if quiet {
break
}
}
}

func writeCondition(table *tabwriter.Writer, condition metav1.Condition, name, namespace string, verbose, needNamespace bool) {
row := []string{name, condition.Type, string(condition.Status), condition.Reason}

// Write conditions corresponding to its headers.
if needNamespace {
row = append([]string{namespace}, row...)
}
if verbose {
row = append(row, []string{
condition.Message,
strconv.FormatInt(condition.ObservedGeneration, 10),
condition.LastTransitionTime.String(),
}...)
}

fmt.Fprintln(table, strings.Join(row, "\t"))
}
Loading
Loading