Skip to content

Commit

Permalink
[gwctl] Expand the output of gwctl describe gatewayclass (#2930)
Browse files Browse the repository at this point in the history
* feat: expand gwctl describe gatewayclass

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* chore: nits

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* fix: tests and deal with empty outputs gracefully

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* chore: introduce vendor to .gitignore

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* chore: nit cleanup

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* chore: use k8s.io/ptr for generic ptr ops

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* fix: deps

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* chore: address comments

Signed-off-by: Yashvardhan Kukreja <[email protected]>

* chore: nit

Signed-off-by: Yashvardhan Kukreja <[email protected]>

---------

Signed-off-by: Yashvardhan Kukreja <[email protected]>
  • Loading branch information
yashvardhan-kukreja authored Apr 18, 2024
1 parent 7f9f510 commit 90e22fe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gwctl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
47 changes: 44 additions & 3 deletions gwctl/pkg/printer/gatewayclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package printer
import (
"fmt"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"os"
v1 "sigs.k8s.io/gateway-api/apis/v1"
"sort"
"strings"
"text/tabwriter"
Expand All @@ -39,11 +42,19 @@ type GatewayClassesPrinter struct {
}

type gatewayClassDescribeView struct {
APIVersion string `json:",omitempty"`
Kind string `json:",omitempty"`
Metadata *metav1.ObjectMeta `json:",omitempty"`
Labels *map[string]string `json:",omitempty"`
Annotations *map[string]string `json:",omitempty"`

// GatewayClass name
Name string `json:",omitempty"`
ControllerName string `json:",omitempty"`
// GatewayClass description
Description *string `json:",omitempty"`
Description *string `json:",omitempty"`

Status *v1.GatewayClassStatus `json:",omitempty"`
DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"`
}

Expand Down Expand Up @@ -89,11 +100,33 @@ func (gcp *GatewayClassesPrinter) PrintDescribeView(resourceModel *resourcedisco
index := 0
for _, gatewayClassNode := range resourceModel.GatewayClasses {
index++

apiVersion, kind := gatewayClassNode.GatewayClass.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
metadata := gatewayClassNode.GatewayClass.ObjectMeta.DeepCopy()
metadata.Labels = nil
metadata.Annotations = nil
metadata.Name = ""
metadata.Namespace = ""

// views ordered with respect to https://gateway-api.sigs.k8s.io/geps/gep-2722/
views := []gatewayClassDescribeView{
{
Name: gatewayClassNode.GatewayClass.GetName(),
},
{
Labels: ptr.To(gatewayClassNode.GatewayClass.GetLabels()),
},
{
Annotations: ptr.To(gatewayClassNode.GatewayClass.GetAnnotations()),
},
{
APIVersion: apiVersion,
},
{
Kind: kind,
},
{
Metadata: metadata,
},
{
ControllerName: string(gatewayClassNode.GatewayClass.Spec.ControllerName),
},
Expand All @@ -103,6 +136,9 @@ func (gcp *GatewayClassesPrinter) PrintDescribeView(resourceModel *resourcedisco
Description: gatewayClassNode.GatewayClass.Spec.Description,
})
}
views = append(views, gatewayClassDescribeView{
Status: &gatewayClassNode.GatewayClass.Status,
})

if policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(gatewayClassNode.Policies); len(policyRefs) != 0 {
views = append(views, gatewayClassDescribeView{
Expand All @@ -116,7 +152,12 @@ func (gcp *GatewayClassesPrinter) PrintDescribeView(resourceModel *resourcedisco
fmt.Fprintf(os.Stderr, "failed to marshal to yaml: %v\n", err)
os.Exit(1)
}
fmt.Fprint(gcp.Out, string(b))
output := string(b)

emptyOutput := strings.TrimSpace(output) == "{}"
if !emptyOutput {
fmt.Fprint(gcp.Out, output)
}
}

if index+1 <= len(resourceModel.GatewayClasses) {
Expand Down
16 changes: 16 additions & 0 deletions gwctl/pkg/printer/gatewayclasses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,14 @@ func TestGatewayClassesPrinter_PrintDescribeView(t *testing.T) {
},
want: `
Name: foo-gatewayclass
Labels: null
Annotations: null
Metadata:
creationTimestamp: null
resourceVersion: "999"
ControllerName: example.net/gateway-controller
Description: random
Status: {}
DirectlyAttachedPolicies:
- Group: foo.com
Kind: HealthCheckPolicy
Expand All @@ -196,6 +202,9 @@ DirectlyAttachedPolicies:
&gatewayv1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: "foo-gatewayclass",
Labels: map[string]string{
"foo": "bar",
},
},
Spec: gatewayv1.GatewayClassSpec{
ControllerName: "example.net/gateway-controller",
Expand All @@ -204,7 +213,14 @@ DirectlyAttachedPolicies:
},
want: `
Name: foo-gatewayclass
Labels:
foo: bar
Annotations: null
Metadata:
creationTimestamp: null
resourceVersion: "999"
ControllerName: example.net/gateway-controller
Status: {}
`,
},
}
Expand Down
6 changes: 5 additions & 1 deletion gwctl/pkg/resourcediscovery/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"os"

"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/gwctl/pkg/common"
Expand Down Expand Up @@ -286,6 +285,11 @@ func fetchGatewayClasses(ctx context.Context, k8sClients *common.K8sClients, fil
return []gatewayv1.GatewayClass{}, err
}

// Because the `TypeMeta` attribute doesn't get populated here
// Ref: https://github.com/kubernetes/kubernetes/issues/80609
gatewayClass.APIVersion = gatewayv1.GroupVersion.String()
gatewayClass.Kind = "GatewayClass"

return []gatewayv1.GatewayClass{*gatewayClass}, nil
}

Expand Down

0 comments on commit 90e22fe

Please sign in to comment.