Skip to content

Commit

Permalink
Add command for creation of a plain trigger (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ying Chun Guo authored and knative-prow-robot committed Dec 16, 2019
1 parent 0ca6f14 commit 7def9f4
Show file tree
Hide file tree
Showing 34 changed files with 2,517 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/cmd/kn.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ Manage your Knative building blocks:
* [kn route](kn_route.md) - Route command group
* [kn service](kn_service.md) - Service command group
* [kn source](kn_source.md) - Event source command group
* [kn trigger](kn_trigger.md) - Trigger command group
* [kn version](kn_version.md) - Prints the client version

33 changes: 33 additions & 0 deletions docs/cmd/kn_trigger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## kn trigger

Trigger command group

### Synopsis

Trigger command group

```
kn trigger [flags]
```

### Options

```
-h, --help help for trigger
```

### Options inherited from parent commands

```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn](kn.md) - Knative client
* [kn trigger create](kn_trigger_create.md) - Create a trigger
* [kn trigger delete](kn_trigger_delete.md) - Delete a trigger.
* [kn trigger describe](kn_trigger_describe.md) - Describe a trigger.

42 changes: 42 additions & 0 deletions docs/cmd/kn_trigger_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## kn trigger create

Create a trigger

### Synopsis

Create a trigger

```
kn trigger create NAME --broker BROKER --filter KEY=VALUE --sink SINK [flags]
```

### Examples

```
# Create a trigger 'mytrigger' to declare a subscription to events with attribute 'type=dev.knative.foo' from default broker. The subscriber is service 'mysvc'
kn trigger create mytrigger --broker default --filter type=dev.knative.foo --sink svc:mysvc
```

### Options

```
--broker string Name of the Broker which the trigger associates with. (default "default")
--filter []string Key-value pair for exact CloudEvent attribute matching against incoming events, e.g type=dev.knative.foo
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
-s, --sink string Addressable sink for events
```

### Options inherited from parent commands

```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn trigger](kn_trigger.md) - Trigger command group

39 changes: 39 additions & 0 deletions docs/cmd/kn_trigger_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## kn trigger delete

Delete a trigger.

### Synopsis

Delete a trigger.

```
kn trigger delete NAME [flags]
```

### Examples

```
# Delete a trigger 'mytrigger' in default namespace
kn trigger delete mytrigger
```

### Options

```
-h, --help help for delete
-n, --namespace string Specify the namespace to operate in.
```

### Options inherited from parent commands

```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn trigger](kn_trigger.md) - Trigger command group

40 changes: 40 additions & 0 deletions docs/cmd/kn_trigger_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## kn trigger describe

Describe a trigger.

### Synopsis

Describe a trigger.

```
kn trigger describe NAME [flags]
```

### Examples

```
# Describe a trigger with name 'my-trigger'
kn trigger describe my-trigger
```

### Options

```
-h, --help help for describe
-n, --namespace string Specify the namespace to operate in.
-v, --verbose More output.
```

### Options inherited from parent commands

```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn trigger](kn_trigger.md) - Trigger command group

84 changes: 84 additions & 0 deletions pkg/eventing/v1alpha1/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright © 2019 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 v1alpha1

import (
apis_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

kn_errors "knative.dev/client/pkg/errors"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
client_v1alpha1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1alpha1"
)

// KnEventingClient to Eventing Sources. All methods are relative to the
// namespace specified during construction
type KnEventingClient interface {
// Namespace in which this client is operating for
Namespace() string
// CreateTrigger is used to create an instance of trigger
CreateTrigger(trigger *v1alpha1.Trigger) (*v1alpha1.Trigger, error)
// DeleteTrigger is used to delete an instance of trigger
DeleteTrigger(name string) error
// GetTrigger is used to get an instance of trigger
GetTrigger(name string) (*v1alpha1.Trigger, error)
}

// KnEventingClient is a combination of Sources client interface and namespace
// Temporarily help to add sources dependencies
// May be changed when adding real sources features
type knEventingClient struct {
client client_v1alpha1.EventingV1alpha1Interface
namespace string
}

// NewKnEventingClient is to invoke Eventing Sources Client API to create object
func NewKnEventingClient(client client_v1alpha1.EventingV1alpha1Interface, namespace string) KnEventingClient {
return &knEventingClient{
client: client,
namespace: namespace,
}
}

//CreateTrigger is used to create an instance of trigger
func (c *knEventingClient) CreateTrigger(trigger *v1alpha1.Trigger) (*v1alpha1.Trigger, error) {
trigger, err := c.client.Triggers(c.namespace).Create(trigger)
if err != nil {
return nil, kn_errors.GetError(err)
}
return trigger, nil
}

//DeleteTrigger is used to delete an instance of trigger
func (c *knEventingClient) DeleteTrigger(name string) error {
err := c.client.Triggers(c.namespace).Delete(name, &apis_v1.DeleteOptions{})
if err != nil {
return kn_errors.GetError(err)
}
return nil
}

//GetTrigger is used to get an instance of trigger
func (c *knEventingClient) GetTrigger(name string) (*v1alpha1.Trigger, error) {
trigger, err := c.client.Triggers(c.namespace).Get(name, apis_v1.GetOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
return trigger, nil
}

// Return the client's namespace
func (c *knEventingClient) Namespace() string {
return c.namespace
}
97 changes: 97 additions & 0 deletions pkg/eventing/v1alpha1/client_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright © 2019 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 v1alpha1

import (
"testing"

"knative.dev/client/pkg/util/mock"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
)

// MockKnEventingClient is a combine of test object and recorder
type MockKnEventingClient struct {
t *testing.T
recorder *EventingRecorder
namespace string
}

// NewMockKnEventingClient returns a new mock instance which you need to record for
func NewMockKnEventingClient(t *testing.T, ns ...string) *MockKnEventingClient {
namespace := "default"
if len(ns) > 0 {
namespace = ns[0]
}
return &MockKnEventingClient{
t: t,
recorder: &EventingRecorder{mock.NewRecorder(t, namespace)},
}
}

// Ensure that the interface is implemented
var _ KnEventingClient = &MockKnEventingClient{}

// EventingRecorder is recorder for eventing objects
type EventingRecorder struct {
r *mock.Recorder
}

// Recorder returns the recorder for registering API calls
func (c *MockKnEventingClient) Recorder() *EventingRecorder {
return c.recorder
}

// Namespace of this client
func (c *MockKnEventingClient) Namespace() string {
return c.recorder.r.Namespace()
}

// CreateTrigger records a call for CreateCronJobSource with the expected error
func (sr *EventingRecorder) CreateTrigger(trigger interface{}, err error) {
sr.r.Add("CreateTrigger", []interface{}{trigger}, []interface{}{trigger, err})
}

// CreateTrigger performs a previously recorded action, failing if non has been registered
func (c *MockKnEventingClient) CreateTrigger(trigger *v1alpha1.Trigger) (*v1alpha1.Trigger, error) {
call := c.recorder.r.VerifyCall("CreateTrigger", trigger)
return call.Result[0].(*v1alpha1.Trigger), mock.ErrorOrNil(call.Result[1])
}

// GetTrigger records a call for GetTrigger with the expected object or error. Either trigger or err should be nil
func (sr *EventingRecorder) GetTrigger(name interface{}, trigger *v1alpha1.Trigger, err error) {
sr.r.Add("GetTrigger", []interface{}{name}, []interface{}{trigger, err})
}

// GetTrigger performs a previously recorded action, failing if non has been registered
func (c *MockKnEventingClient) GetTrigger(name string) (*v1alpha1.Trigger, error) {
call := c.recorder.r.VerifyCall("GetTrigger", name)
return call.Result[0].(*v1alpha1.Trigger), mock.ErrorOrNil(call.Result[1])
}

// DeleteTrigger records a call for DeleteTrigger with the expected error (nil if none)
func (sr *EventingRecorder) DeleteTrigger(name interface{}, err error) {
sr.r.Add("DeleteTrigger", []interface{}{name}, []interface{}{err})
}

// DeleteTrigger performs a previously recorded action, failing if non has been registered
func (c *MockKnEventingClient) DeleteTrigger(name string) error {
call := c.recorder.r.VerifyCall("DeleteTrigger", name)
return mock.ErrorOrNil(call.Result[0])
}

// Validate validates whether every recorded action has been called
func (sr *EventingRecorder) Validate() {
sr.r.CheckThatAllRecordedMethodsHaveBeenCalled()
}
41 changes: 41 additions & 0 deletions pkg/eventing/v1alpha1/client_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2019 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 v1alpha1

import (
"testing"

"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
)

func TestMockKnClient(t *testing.T) {

client := NewMockKnEventingClient(t)

recorder := client.Recorder()

// Record all services
recorder.GetTrigger("hello", nil, nil)
recorder.CreateTrigger(&v1alpha1.Trigger{}, nil)
recorder.DeleteTrigger("hello", nil)

// Call all service
client.GetTrigger("hello")
client.CreateTrigger(&v1alpha1.Trigger{})
client.DeleteTrigger("hello")

// Validate
recorder.Validate()
}
Loading

0 comments on commit 7def9f4

Please sign in to comment.