mirror of https://github.com/knative/client.git
Add machine readable output (-o flag) to kn source apiserver describe (#1146)
Signed-off-by: Arghya Sadhu <arghya88@gmail.com>
This commit is contained in:
parent
2941816951
commit
b8bce53452
|
|
@ -17,6 +17,10 @@
|
||||||
|===
|
|===
|
||||||
| | Description | PR
|
| | Description | PR
|
||||||
|
|
||||||
|
| 🐛
|
||||||
|
| Add machine readable output (-o flag) to kn source apiserver describe
|
||||||
|
| https://github.com/knative/client/pull/1146[#1146]
|
||||||
|
|
||||||
| 🐛
|
| 🐛
|
||||||
| Fix a race condition when using Kubernetes watches
|
| Fix a race condition when using Kubernetes watches
|
||||||
| https://github.com/knative/client/pull/1113[#1113]
|
| https://github.com/knative/client/pull/1113[#1113]
|
||||||
|
|
|
||||||
|
|
@ -14,16 +14,22 @@ kn source apiserver describe NAME
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Describe an ApiServer source with name 'k8sevents'
|
# Describe an api-server source with name 'k8sevents'
|
||||||
kn source apiserver describe k8sevents
|
kn source apiserver describe k8sevents
|
||||||
|
|
||||||
|
# Describe an api-server source with name 'k8sevents' in YAML format
|
||||||
|
kn source apiserver describe k8sevents -o yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
-h, --help help for describe
|
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
|
||||||
-n, --namespace string Specify the namespace to operate in.
|
-h, --help help for describe
|
||||||
-v, --verbose More output.
|
-n, --namespace string Specify the namespace to operate in.
|
||||||
|
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
|
||||||
|
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
|
||||||
|
-v, --verbose More output.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options inherited from parent commands
|
### Options inherited from parent commands
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,10 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
|
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
|
||||||
|
|
||||||
"knative.dev/client/lib/printing"
|
"knative.dev/client/lib/printing"
|
||||||
|
|
@ -27,14 +29,23 @@ import (
|
||||||
"knative.dev/client/pkg/printers"
|
"knative.dev/client/pkg/printers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var describeExample = `
|
||||||
|
# Describe an api-server source with name 'k8sevents'
|
||||||
|
kn source apiserver describe k8sevents
|
||||||
|
|
||||||
|
# Describe an api-server source with name 'k8sevents' in YAML format
|
||||||
|
kn source apiserver describe k8sevents -o yaml`
|
||||||
|
|
||||||
// NewAPIServerDescribeCommand to describe an ApiServer source object
|
// NewAPIServerDescribeCommand to describe an ApiServer source object
|
||||||
func NewAPIServerDescribeCommand(p *commands.KnParams) *cobra.Command {
|
func NewAPIServerDescribeCommand(p *commands.KnParams) *cobra.Command {
|
||||||
apiServerDescribe := &cobra.Command{
|
|
||||||
Use: "describe NAME",
|
// For machine readable output
|
||||||
Short: "Show details of an api-server source",
|
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")
|
||||||
Example: `
|
|
||||||
# Describe an ApiServer source with name 'k8sevents'
|
command := &cobra.Command{
|
||||||
kn source apiserver describe k8sevents`,
|
Use: "describe NAME",
|
||||||
|
Short: "Show details of an api-server source",
|
||||||
|
Example: describeExample,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return errors.New("'kn source apiserver describe' requires name of the source as single argument")
|
return errors.New("'kn source apiserver describe' requires name of the source as single argument")
|
||||||
|
|
@ -52,6 +63,15 @@ func NewAPIServerDescribeCommand(p *commands.KnParams) *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
out := cmd.OutOrStdout()
|
out := cmd.OutOrStdout()
|
||||||
|
|
||||||
|
// Print out machine readable output if requested
|
||||||
|
if machineReadablePrintFlags.OutputFlagSpecified() {
|
||||||
|
printer, err := machineReadablePrintFlags.ToPrinter()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return printer.PrintObj(apiSource, out)
|
||||||
|
}
|
||||||
dw := printers.NewPrefixWriter(out)
|
dw := printers.NewPrefixWriter(out)
|
||||||
|
|
||||||
printDetails, err := cmd.Flags().GetBool("verbose")
|
printDetails, err := cmd.Flags().GetBool("verbose")
|
||||||
|
|
@ -90,11 +110,12 @@ func NewAPIServerDescribeCommand(p *commands.KnParams) *cobra.Command {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
flags := apiServerDescribe.Flags()
|
flags := command.Flags()
|
||||||
commands.AddNamespaceFlags(flags, false)
|
commands.AddNamespaceFlags(flags, false)
|
||||||
flags.BoolP("verbose", "v", false, "More output.")
|
flags.BoolP("verbose", "v", false, "More output.")
|
||||||
|
machineReadablePrintFlags.AddFlags(command)
|
||||||
return apiServerDescribe
|
command.Flag("output").Usage = fmt.Sprintf("Output format. One of: %s.", strings.Join(machineReadablePrintFlags.AllowedFormats(), "|"))
|
||||||
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeResources(dw printers.PrefixWriter, apiVersionKindSelectors []v1alpha2.APIVersionKindSelector) {
|
func writeResources(dw printers.PrefixWriter, apiVersionKindSelectors []v1alpha2.APIVersionKindSelector) {
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,23 @@ func TestSimpleDescribe(t *testing.T) {
|
||||||
apiServerRecorder.Validate()
|
apiServerRecorder.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDescribeMachineReadable(t *testing.T) {
|
||||||
|
apiServerClient := v1alpha2.NewMockKnAPIServerSourceClient(t, "mynamespace")
|
||||||
|
|
||||||
|
apiServerRecorder := apiServerClient.Recorder()
|
||||||
|
sampleSource := createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", map[string]string{"foo": "bar"}, createSinkv1("testsvc", "default"))
|
||||||
|
sampleSource.APIVersion = "sources.knative.dev/v1"
|
||||||
|
sampleSource.Kind = "ApiServerSource"
|
||||||
|
sampleSource.Namespace = "mynamespace"
|
||||||
|
apiServerRecorder.GetAPIServerSource("testsource", sampleSource, nil)
|
||||||
|
|
||||||
|
out, err := executeAPIServerSourceCommand(apiServerClient, nil, "describe", "testsource", "-o", "yaml")
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, util.ContainsAll(out, "kind: ApiServerSource", "spec:", "status:", "metadata:"))
|
||||||
|
|
||||||
|
apiServerRecorder.Validate()
|
||||||
|
}
|
||||||
|
|
||||||
func TestDescribeError(t *testing.T) {
|
func TestDescribeError(t *testing.T) {
|
||||||
apiServerClient := v1alpha2.NewMockKnAPIServerSourceClient(t, "mynamespace")
|
apiServerClient := v1alpha2.NewMockKnAPIServerSourceClient(t, "mynamespace")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,10 @@ func (c *apiServerSourcesClient) GetAPIServerSource(name string) (*v1alpha2.ApiS
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, knerrors.GetError(err)
|
return nil, knerrors.GetError(err)
|
||||||
}
|
}
|
||||||
|
err = updateSourceGVK(apiSource)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return apiSource, nil
|
return apiSource, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue