Skip to content

Commit

Permalink
Added eventtype list and eventingv1beta1 client to Knparams
Browse files Browse the repository at this point in the history
  • Loading branch information
vyasgun committed Feb 2, 2022
1 parent 677276a commit 26a6174
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/cmd/kn.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kn is the command line interface for managing Knative Serving and Eventing resou
* [kn completion](kn_completion.md) - Output shell completion code
* [kn container](kn_container.md) - Manage service's containers (experimental)
* [kn domain](kn_domain.md) - Manage domain mappings
* [kn eventtype](kn_eventtype.md) - Manage eventtypes
* [kn options](kn_options.md) - Print the list of flags inherited by all commands
* [kn plugin](kn_plugin.md) - Manage kn plugins
* [kn revision](kn_revision.md) - Manage service revisions
Expand Down
29 changes: 29 additions & 0 deletions docs/cmd/kn_eventtype.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## kn eventtype

Manage eventtypes

```
kn eventtype
```

### Options

```
-h, --help help for eventtype
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn](kn.md) - kn manages Knative Serving and Eventing resources
* [kn eventtype list](kn_eventtype_list.md) - List eventtypes

46 changes: 46 additions & 0 deletions docs/cmd/kn_eventtype_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## kn eventtype list

List eventtypes

```
kn eventtype list
```

### Examples

```
# List all eventtypes
kn eventtype list
# List all eventtypes in JSON output format
kn eventtype list -o json
```

### Options

```
-A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
--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)
-h, --help help for list
-n, --namespace string Specify the namespace to operate in.
--no-headers When using the default output format, don't print headers (default: print headers).
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
--show-managed-fields If true, keep the managedFields when printing objects in JSON or YAML format.
--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].
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn eventtype](kn_eventtype.md) - Manage eventtypes

81 changes: 81 additions & 0 deletions pkg/eventing/v1beta1/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright © 2022 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1beta1

import (
"context"

apis_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kn_errors "knative.dev/client/pkg/errors"
"knative.dev/client/pkg/util"
eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1"
"knative.dev/eventing/pkg/client/clientset/versioned/scheme"
beta1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta1"
)

// KnEventingV1Beta1Client to Eventing Sources. All methods are relative to the
// namespace specified during construction
type KnEventingV1Beta1Client interface {
// Namespace in which this client is operating for
Namespace() string
// ListEventtypes is used to list eventtypes
ListEventtypes(ctx context.Context) (*eventingv1beta1.EventTypeList, error)
}

// KnEventingV1Beta1Client is a client for eventing v1beta1 resources
type knEventingV1Beta1Client struct {
client beta1.EventingV1beta1Interface
namespace string
}

// NewKnEventingV1Beta1Client is to invoke Eventing Types Client API to create object
func NewKnEventingV1Beta1Client(client *beta1.EventingV1beta1Client, namespace string) KnEventingV1Beta1Client {
return &knEventingV1Beta1Client{
client: client,
namespace: namespace,
}
}

func updateEventingBeta1GVK(obj runtime.Object) error {
return util.UpdateGroupVersionKindWithScheme(obj, eventingv1beta1.SchemeGroupVersion, scheme.Scheme)
}

func (c *knEventingV1Beta1Client) Namespace() string {
return c.namespace
}

func (c *knEventingV1Beta1Client) ListEventtypes(ctx context.Context) (*eventingv1beta1.EventTypeList, error) {
eventTypeList, err := c.client.EventTypes(c.namespace).List(ctx, apis_v1.ListOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
listNew := eventTypeList.DeepCopy()
err = updateEventingBeta1GVK(listNew)
if err != nil {
return nil, err
}

listNew.Items = make([]eventingv1beta1.EventType, len(eventTypeList.Items))
for idx, eventType := range eventTypeList.Items {
clone := eventType.DeepCopy()
err := updateEventingBeta1GVK(clone)
if err != nil {
return nil, err
}
listNew.Items[idx] = *clone
}
return listNew, nil
}
34 changes: 34 additions & 0 deletions pkg/kn/commands/eventtype/eventtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2022 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eventtype

import (
"github.com/spf13/cobra"

"knative.dev/client/pkg/kn/commands"
)

// NewEventTypeCommand represents event type management commands
func NewEventTypeCommand(p *commands.KnParams) *cobra.Command {
eventCmd := &cobra.Command{
Use: "eventtype",
Short: "Manage eventtypes",
Aliases: []string{"eventtypes"},
}
eventCmd.AddCommand(NewEventtypeListCommand(p))
return eventCmd
}
150 changes: 150 additions & 0 deletions pkg/kn/commands/eventtype/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright 2022 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eventtype

import (
"fmt"

"github.com/spf13/cobra"
eventingv1beta1 "knative.dev/eventing/pkg/apis/eventing/v1beta1"

metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
hprinters "knative.dev/client/pkg/printers"
)

var listExample = `
# List all eventtypes
kn eventtype list
# List all eventtypes in JSON output format
kn eventtype list -o json`

// NewEventtypeListCommand represents command to list all eventtypes
func NewEventtypeListCommand(p *commands.KnParams) *cobra.Command {
listFlags := flags.NewListPrintFlags(ListHandlers)

cmd := &cobra.Command{
Use: "list",
Short: "List eventtypes",
Aliases: []string{"ls"},
Example: listExample,
RunE: func(cmd *cobra.Command, args []string) (err error) {
namespace, err := p.GetNamespace(cmd)
if err != nil {
return err
}

eventingV1Beta1Client, err := p.NewEventingV1beta1Client(namespace)
if err != nil {
return err
}

eventTypeList, err := eventingV1Beta1Client.ListEventtypes(cmd.Context())
if err != nil {
return err
}
if !listFlags.GenericPrintFlags.OutputFlagSpecified() && len(eventTypeList.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No eventtypes found.\n")
return nil
}

// empty namespace indicates all-namespaces flag is specified
if namespace == "" {
listFlags.EnsureWithNamespace()
}

err = listFlags.Print(eventTypeList, cmd.OutOrStdout())
if err != nil {
return err
}
return nil
},
}
commands.AddNamespaceFlags(cmd.Flags(), true)
listFlags.AddFlags(cmd)
return cmd
}

// ListHandlers handles printing human readable table for `kn eventtype list` command's output
func ListHandlers(h hprinters.PrintHandler) {
eventTypeColumnDefinitions := []metav1beta1.TableColumnDefinition{
{Name: "Namespace", Type: "string", Description: "Namespace of the EventType instance", Priority: 0},
{Name: "Name", Type: "string", Description: "Name of the EventType instance", Priority: 1},
{Name: "Type", Type: "string", Description: "Type of the EventType instance", Priority: 1},
{Name: "Source", Type: "string", Description: "Source of the EventType instance", Priority: 1},
{Name: "Broker", Type: "string", Description: "Broker of the EventType instance", Priority: 1},
{Name: "Schema", Type: "string", Description: "Schema of the EventType instance", Priority: 1},
{Name: "Age", Type: "string", Description: "Age of the EventType instance", Priority: 1},
{Name: "Conditions", Type: "string", Description: "Ready state conditions", Priority: 1},
{Name: "Ready", Type: "string", Description: "Ready state of the EventType instance", Priority: 1},
{Name: "Reason", Type: "string", Description: "Reason if state is not Ready", Priority: 1},
}
h.TableHandler(eventTypeColumnDefinitions, printEventType)
h.TableHandler(eventTypeColumnDefinitions, printEventTypeList)
}

// printEventTypeList populates the eventtype list table rows
func printEventTypeList(eventTypeList *eventingv1beta1.EventTypeList, options hprinters.PrintOptions) ([]metav1beta1.TableRow, error) {
rows := make([]metav1beta1.TableRow, 0, len(eventTypeList.Items))

for i := range eventTypeList.Items {
eventType := &eventTypeList.Items[i]
r, err := printEventType(eventType, options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}

// printEventType populates the eventtype table rows
func printEventType(eventType *eventingv1beta1.EventType, options hprinters.PrintOptions) ([]metav1beta1.TableRow, error) {
name := eventType.Name
age := commands.TranslateTimestampSince(eventType.CreationTimestamp)
typeEvent := eventType.Spec.Type
source := eventType.Spec.Source
broker := eventType.Spec.Broker
schema := eventType.Spec.SchemaData
conditions := commands.ConditionsValue(eventType.Status.Conditions)
ready := commands.ReadyCondition(eventType.Status.Conditions)
reason := commands.NonReadyConditionReason(eventType.Status.Conditions)

row := metav1beta1.TableRow{
Object: runtime.RawExtension{Object: eventType},
}

if options.AllNamespaces {
row.Cells = append(row.Cells, eventType.Namespace)
}

row.Cells = append(row.Cells,
name,
typeEvent,
source,
broker,
schema,
age,
conditions,
ready,
reason)
return []metav1beta1.TableRow{row}, nil
}
Loading

0 comments on commit 26a6174

Please sign in to comment.