Skip to content

Commit

Permalink
Use Table output for kpt alpha repo get (#3054)
Browse files Browse the repository at this point in the history
Show more information about repository in the list Table results.
Fix a bug where the Table generated by the server lacks TypeMeta.
  • Loading branch information
martinmaly committed Apr 25, 2022
1 parent 8de8d13 commit 171c39b
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 81 deletions.
11 changes: 8 additions & 3 deletions e2e/testdata/porch/repo-register/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ commands:
stdout: |
NAME CONTENT DESC
test-blueprints Package Test Blueprints
- args:
- alpha
- repo
- get
- --namespace=repo-register
stdout: |
NAME TYPE CONTENT DEPLOYMENT READY
test-blueprints git Package True
- args:
- alpha
- repo
Expand All @@ -36,6 +44,3 @@ commands:
- repo
- get
- --namespace=repo-register
- --output=custom-columns=NAME:.metadata.name
stdout: |
NAME
12 changes: 5 additions & 7 deletions e2e/testdata/porch/rpkg-get/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ commands:
- get
- --namespace=rpkg-get
- test-blueprints-526fa27229adcc3b6a9a544c455c344a3b4d7597
- --output=custom-columns=NAME:.metadata.name,PKG:.spec.packageName,REPO:.spec.repository,REV:.spec.revision
stdout: |
NAME PKG REPO REV
test-blueprints-526fa27229adcc3b6a9a544c455c344a3b4d7597 basens test-blueprints v1
NAME PACKAGE REVISION LATEST LIFECYCLE REPOSITORY
test-blueprints-526fa27229adcc3b6a9a544c455c344a3b4d7597 basens v1 true Published test-blueprints
- args:
- alpha
- rpkg
- get
- --namespace=rpkg-get
- --name=basens
- --output=custom-columns=NAME:.metadata.name,PKG:.spec.packageName,REPO:.spec.repository,REV:.spec.revision
stdout: |
NAME PKG REPO REV
test-blueprints-9626794e984ff13c9a4c64df5af0f15ec3a146bf basens test-blueprints main
test-blueprints-526fa27229adcc3b6a9a544c455c344a3b4d7597 basens test-blueprints v1
NAME PACKAGE REVISION LATEST LIFECYCLE REPOSITORY
test-blueprints-9626794e984ff13c9a4c64df5af0f15ec3a146bf basens main false Published test-blueprints
test-blueprints-526fa27229adcc3b6a9a544c455c344a3b4d7597 basens v1 true Published test-blueprints
8 changes: 8 additions & 0 deletions e2e/testdata/porch/rpkg-init-deploy/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ commands:
name: kptfile.kpt.dev
kind: ResourceList
yaml: true
- args:
- alpha
- repo
- get
- --namespace=rpkg-init-deploy
stdout: |
NAME TYPE CONTENT DEPLOYMENT READY
git git Package true True
15 changes: 6 additions & 9 deletions e2e/testdata/porch/rpkg-lifecycle/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ commands:
- get
- git-635026ba389a2dd5417b6e7577430c7ccff30f36
- --namespace=rpkg-lifecycle
- --output=custom-columns=NAME:.metadata.name,PKG:.spec.packageName,REV:.spec.revision,LIFECYCLE:.spec.lifecycle
stdout: |
NAME PKG REV LIFECYCLE
git-635026ba389a2dd5417b6e7577430c7ccff30f36 lifecycle-package v1 Draft
NAME PACKAGE REVISION LATEST LIFECYCLE REPOSITORY
git-635026ba389a2dd5417b6e7577430c7ccff30f36 lifecycle-package v1 false Draft git
- args:
- alpha
- rpkg
Expand All @@ -65,10 +64,9 @@ commands:
- get
- git-635026ba389a2dd5417b6e7577430c7ccff30f36
- --namespace=rpkg-lifecycle
- --output=custom-columns=NAME:.metadata.name,PKG:.spec.packageName,REV:.spec.revision,LIFECYCLE:.spec.lifecycle
stdout: |
NAME PKG REV LIFECYCLE
git-635026ba389a2dd5417b6e7577430c7ccff30f36 lifecycle-package v1 Proposed
NAME PACKAGE REVISION LATEST LIFECYCLE REPOSITORY
git-635026ba389a2dd5417b6e7577430c7ccff30f36 lifecycle-package v1 false Proposed git
- args:
- alpha
- rpkg
Expand All @@ -83,10 +81,9 @@ commands:
- get
- git-635026ba389a2dd5417b6e7577430c7ccff30f36
- --namespace=rpkg-lifecycle
- --output=custom-columns=NAME:.metadata.name,PKG:.spec.packageName,REV:.spec.revision,LIFECYCLE:.spec.lifecycle
stdout: |
NAME PKG REV LIFECYCLE
git-635026ba389a2dd5417b6e7577430c7ccff30f36 lifecycle-package v1 Published
NAME PACKAGE REVISION LATEST LIFECYCLE REPOSITORY
git-635026ba389a2dd5417b6e7577430c7ccff30f36 lifecycle-package v1 true Published git
- args:
- alpha
- rpkg
Expand Down
83 changes: 50 additions & 33 deletions internal/cmdrepoget/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ package cmdrepoget

import (
"context"
"strings"

"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/util/porch"
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/cmd/get"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -67,62 +67,79 @@ func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner
type runner struct {
ctx context.Context
cfg *genericclioptions.ConfigFlags
client client.Client
Command *cobra.Command

// Flags
printFlags *get.PrintFlags

requestTable bool
}

func (r *runner) preRunE(cmd *cobra.Command, args []string) error {
const op errors.Op = command + ".preRunE"
client, err := porch.CreateClient(r.cfg)
if err != nil {
return errors.E(op, err)
outputOption := cmd.Flags().Lookup("output").Value.String()
if strings.Contains(outputOption, "custom-columns") || outputOption == "yaml" || strings.Contains(outputOption, "json") {
r.requestTable = false
} else {
r.requestTable = true
}
r.client = client
return nil
}

func (r *runner) runE(cmd *cobra.Command, args []string) error {
const op errors.Op = command + ".runE"

var objs []runtime.Object
// For some reason our use of k8s libraries result in error when decoding
// RepositoryList when we use strongly typed data. Therefore for now we
// use unstructured communication.
// The error is: `no kind "RepositoryList" is registered for the internal
// version of group "config.porch.kpt.dev" in scheme`. Of course there _is_
// no such kind since CRDs seem to have only versioned resources.
b := resource.NewBuilder(r.cfg).
Unstructured().
NamespaceParam(*r.cfg.Namespace).DefaultNamespace()

if len(args) > 0 {
for _, repo := range args {
var repository configapi.Repository
if err := r.client.Get(r.ctx, client.ObjectKey{
Namespace: *r.cfg.Namespace,
Name: repo,
}, &repository); err != nil {
return errors.E(op, err)
}

repository.Kind = "Repository"
repository.APIVersion = configapi.GroupVersion.Identifier()

objs = append(objs, &repository)
}
b.ResourceNames("repository", args...)
} else {
var repositories configapi.RepositoryList
if err := r.client.List(r.ctx, &repositories, client.InNamespace(*r.cfg.Namespace)); err != nil {
return errors.E(op, err)
}
repositories.Kind = "RepositoryList"
repositories.APIVersion = configapi.GroupVersion.Identifier()
objs = append(objs, &repositories)
b = b.SelectAllParam(true).
ResourceTypes("repository")
}

b = b.ContinueOnError().Latest().Flatten()

if r.requestTable {
b = b.TransformRequests(func(req *rest.Request) {
req.SetHeader("Accept", strings.Join([]string{
"application/json;as=Table;g=meta.k8s.io;v=v1",
"application/json",
}, ","))
})
}
res := b.Do()
if err := res.Err(); err != nil {
return errors.E(op, err)
}

infos, err := res.Infos()
if err != nil {
return errors.E(op, err)
}

printer, err := r.printFlags.ToPrinter()
if err != nil {
return errors.E(op, err)
}

if r.requestTable {
printer = &get.TablePrinter{
Delegate: printer,
}
}

w := printers.GetNewTabWriter(cmd.OutOrStdout())

for _, obj := range objs {
if err := printer.PrintObj(obj, w); err != nil {
for _, i := range infos {
if err := printer.PrintObj(i.Object, w); err != nil {
return errors.E(op, err)
}
}
Expand Down
Loading

0 comments on commit 171c39b

Please sign in to comment.