Add command to create/delete a plain trigger#541
Conversation
887f4da to
2ea32d6
Compare
|
/test pull-knative-client-integration-tests |
18880c5 to
5bd6789
Compare
|
/approve |
|
/retest |
maximilien
left a comment
There was a problem hiding this comment.
OK I stopped at file 19. There are too many issues. Please address and I’ll take another look.
Also, use the /lint option to see many of these issues and address them before submitting PR
| ### SEE ALSO | ||
|
|
||
| * [kn](kn.md) - Knative client | ||
| * [kn trigger create](kn_trigger_create.md) - Create a Trigger |
There was a problem hiding this comment.
Why is “trigger” spelled with capital T here? Be consistent with Delete and the rest.
There was a problem hiding this comment.
Change to lower case t in the whole PR (unless starting a sentence)
| @@ -0,0 +1,42 @@ | |||
| ## kn trigger create | |||
|
|
|||
| Create a Trigger | |||
There was a problem hiding this comment.
Consistent spelling of Trigger or trigger (unless starting a sentence)... across all text.
There was a problem hiding this comment.
Change to lower case t in the whole PR (unless starting a sentence)
There was a problem hiding this comment.
Change to lower case t in the whole PR (unless starting a sentence)
| @@ -0,0 +1,39 @@ | |||
| ## kn trigger delete | |||
|
|
|||
| Delete a trigger. | |||
There was a problem hiding this comment.
Change to lower case t in the whole PR (unless starting a sentence)
There was a problem hiding this comment.
Change to lower case t in the whole PR (unless starting a sentence)
| // Temporarily help to add sources dependencies | ||
| // May be changed when adding real sources features | ||
| type knEventingClient struct { | ||
| client client_v1alpha1.EventingV1alpha1Interface |
There was a problem hiding this comment.
Be consistent with creation in the order of the fields? It’s reverse here to the NewKnEventingClient constructor...
There was a problem hiding this comment.
I changed the order of fields in NewKnEventingClient constructor to be consistent.
|
|
||
| //CreateTrigger is used to create an instance of Trigger | ||
| func (c *knEventingClient) CreateTrigger(trigger *v1alpha1.Trigger) (*v1alpha1.Trigger, error) { | ||
| ins, err := c.client.Triggers(c.namespace).Create(trigger) |
There was a problem hiding this comment.
Minor: What does ins mean? Why not call it trigger or something descriptive.
There was a problem hiding this comment.
ins means instance. :). I updated to use trigger
|
|
||
| // BrokerInterface has methods to work with Broker resources. | ||
| type BrokerInterface interface { | ||
| Create(*v1alpha1.Broker) (*v1alpha1.Broker, error) |
There was a problem hiding this comment.
Inconsistent use of comments. In previous interface you added comments for each method. Not here. IMO less comments is better since if name is well selected it’s clear. However, I also would agree to have comments for public interfaces and methods.
Either way be consistent and follow the rest of code. This is not being done here.
There was a problem hiding this comment.
It's Eventing client dependency file, added by go mod while building and testing.
| restClient rest.Interface | ||
| } | ||
|
|
||
| func (c *EventingV1alpha1Client) Brokers(namespace string) BrokerInterface { |
There was a problem hiding this comment.
Public methods need comments or won’t pass the linter
There was a problem hiding this comment.
It's Eventing client dependency file, added by go mod while building and testing.
| // NewForConfig creates a new EventingV1alpha1Client for the given config. | ||
| func NewForConfig(c *rest.Config) (*EventingV1alpha1Client, error) { | ||
| config := *c | ||
| if err := setConfigDefaults(&config); err != nil { |
There was a problem hiding this comment.
Don’t use this form. Extract the setCopnfigDefaults(&config) outside of the if since that’s consistent to the other line and most of this repo’s code
There was a problem hiding this comment.
It's Eventing client dependency file, added by go mod while building and testing.
| // RESTClient returns a RESTClient that is used to communicate | ||
| // with API server by this client implementation. | ||
| func (c *EventingV1alpha1Client) RESTClient() rest.Interface { | ||
| if c == nil { |
There was a problem hiding this comment.
How would c == nil be ever true?
If c is nill then an exception will be thrown before RESTClient() be executed. So these three lines are dead code...
There was a problem hiding this comment.
It's Eventing client dependency file, added by go mod while building and testing.
There was a problem hiding this comment.
@maximilien Of course can c be nil. Try
EventingV1alphaClient(nil).RESTClient()There was a problem hiding this comment.
and as mentioned, its a dependeny added. It's also autogenerated.
There was a problem hiding this comment.
The reason for this check is to allow chained called where an intermediate call returns nil like in
myServicHub.EventingClient().RESTClient()and EventingClient() return nil (but is of type *EventingV1alphaClient)
|
|
||
| // Get takes name of the eventType, and returns the corresponding eventType object, and an error if there is any. | ||
| func (c *eventTypes) Get(name string, options v1.GetOptions) (result *v1alpha1.EventType, err error) { | ||
| result = &v1alpha1.EventType{} |
There was a problem hiding this comment.
Again do not use named return args since it’s not consistent With rest of your code and this repo.
Future versions of Golang might remove this “feature” which is also another reason.
There was a problem hiding this comment.
It's Eventing client dependency file, added by go mod while building and testing.
|
@maximilien Thank you for review. This pr has dependencies to All files under "vendor" folder are automatically added by the build and test scripts. |
1c113a1 to
2173fa1
Compare
|
/lint |
knative-prow-robot
left a comment
There was a problem hiding this comment.
@daisy-ycguo: 6 warnings.
Details
In response to this:
/lint
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
| ) | ||
|
|
||
| // TriggerUpdateFlags are flags for create and update a trigger | ||
| type TriggerUpdateFlags struct { |
There was a problem hiding this comment.
| type TriggerUpdateFlags struct { | |
| type UpdateFlags struct { |
Golint naming: type name will be used as trigger.TriggerUpdateFlags by other packages, and that stutters; consider calling this UpdateFlags. More info.
| duckv1 "knative.dev/pkg/apis/duck/v1" | ||
| ) | ||
|
|
||
| func NewTriggerCreateCommand(p *commands.KnParams) *cobra.Command { |
There was a problem hiding this comment.
Golint comments: exported function NewTriggerCreateCommand should have comment or be unexported. More info.
| "knative.dev/client/pkg/kn/commands" | ||
| ) | ||
|
|
||
| func NewTriggerCommand(p *commands.KnParams) *cobra.Command { |
There was a problem hiding this comment.
Golint comments: exported function NewTriggerCommand should have comment or be unexported. More info.
| "knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||
| ) | ||
|
|
||
| type MockKnEventingClient struct { |
There was a problem hiding this comment.
Golint comments: exported type MockKnEventingClient should have comment or be unexported. More info.
| // Ensure that the interface is implemented | ||
| var _ KnEventingClient = &MockKnEventingClient{} | ||
|
|
||
| // recorder for service |
There was a problem hiding this comment.
Golint comments: comment on exported type EventingRecorder should be of the form "EventingRecorder ..." (with optional leading article). More info.
| return mock.ErrorOrNil(call.Result[0]) | ||
| } | ||
|
|
||
| // Validates validates whether every recorded action has been called |
There was a problem hiding this comment.
Golint comments: comment on exported method EventingRecorder.Validate should be of the form "Validate ...". More info.
rhuss
left a comment
There was a problem hiding this comment.
Thanks a lot, some comments see below.
In general we should use only Mock tests for the command tests. Also it woudl be awesome if you could reduce duplicated code in the tests to minimize the references to v1alpha1 structs, so that a later upgrade becomes easier.
| ### Options | ||
|
|
||
| ``` | ||
| --broker string Name of the Broker which the trigger associates with. Defaults to 'default' if not specified. (default "default") |
There was a problem hiding this comment.
You could skip the explanation for 'default' as it is automatically added by cobra
|
|
||
| ``` | ||
| --broker string Name of the Broker which the trigger associates with. Defaults to 'default' if not specified. (default "default") | ||
| --filter strings Comma seperate key-value pair for exact CloudEvent attribute matching against incoming events, e.g type=dev.knative.foo |
There was a problem hiding this comment.
Again, I would prefer to allow multiple flter arguments instead of puttng everything tog gether into a single filter. E.g. what's about escaping for value that contain comma ?
There was a problem hiding this comment.
I changed to use multiple --filter
| --filter strings Comma seperate 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 |
There was a problem hiding this comment.
Should contain examples, like for sources.
There was a problem hiding this comment.
I mean, describing the possible format (e.g. prefix : name) and a reference to the list of possible prefixes. Lets keep it like now, going to make an overall check of all sources later.
| ) | ||
|
|
||
| // MockKnEventingClient is a combine of test object and recorder | ||
| type MockKnEventingClient struct { |
|
|
||
| //GetTrigger is used to get an instance of trigger | ||
| func (c *knEventingClient) GetTrigger(name string) (*v1alpha1.Trigger, error) { | ||
| return c.client.Triggers(c.namespace).Get(name, apis_v1.GetOptions{}) |
There was a problem hiding this comment.
Why no error conversion here ?
| } | ||
|
|
||
| //construct a wanted instance | ||
| wanted := &v1alpha1.Trigger{ |
There was a problem hiding this comment.
You should put the duplicated code imo a method, parameterized with the stuff that changes. The more often you use v1alpha objects directly the harder it will be to upgrade to a v1 version. Even in tests, we should avoid duplicated code as much as possible.
| cmd, fakeEventing, buf := commands.CreateEventingTestKnCommand(NewTriggerCommand(knParams), knParams) | ||
| fakeEventing.AddReactor("delete", "triggers", | ||
| func(a client_testing.Action) (bool, runtime.Object, error) { | ||
| deleteAction, _ := a.(client_testing.DeleteAction) |
There was a problem hiding this comment.
Because the test will panic anyway if its wrong.
| //Add is to set parameters | ||
| func (f *TriggerUpdateFlags) Add(cmd *cobra.Command) { | ||
| cmd.Flags().StringVar(&f.Broker, "broker", "default", "Name of the Broker which the trigger associates with. Defaults to 'default' if not specified.") | ||
| cmd.Flags().StringSliceVar(&f.Filter, "filter", nil, "Comma seperate key-value pair for exact CloudEvent attribute matching against incoming events, e.g type=dev.knative.foo") |
There was a problem hiding this comment.
we allow multiple --filter options, with every option being a single filter. So singular is correct. However its inocrrect that you can use comma separated filters in one option, if I understand the code above correctly.
|
|
||
| if params.NewEventingClient == nil { | ||
| params.NewEventingClient = params.newEventingClient | ||
| } |
There was a problem hiding this comment.
This technique allows test code to hook in by setting the NewEventingClient in advance before reaching this method with a fake implementation.
I'm also not happy with this construct (for other reasons, mostly because of coupling layers unnecessarily), but I'm happy to take this over as is (as its follows the same scheme like for the other clients).
In a subsequent PR we should refactor the client handling to be more delocalized maybe.
| // RESTClient returns a RESTClient that is used to communicate | ||
| // with API server by this client implementation. | ||
| func (c *EventingV1alpha1Client) RESTClient() rest.Interface { | ||
| if c == nil { |
There was a problem hiding this comment.
The reason for this check is to allow chained called where an intermediate call returns nil like in
myServicHub.EventingClient().RESTClient()and EventingClient() return nil (but is of type *EventingV1alphaClient)
|
The following is the coverage report on the affected files.
|
|
@maximilien @rhuss @sixolet This PR is ready for a second review.
|
| } | ||
|
|
||
| //CreateTrigger is used to create an instance of trigger | ||
| func (c *knEventingClient) CreateTrigger(trigger *v1alpha1.Trigger) (*v1alpha1.Trigger, error) { |
There was a problem hiding this comment.
To be in accordance with our other APIs, we should not return the created trigger here.
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: daisy-ycguo, rhuss, sixolet The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes #479
Proposed Changes