Skip to content

Commit

Permalink
chore: Update testing and moved cronjob source access to pkg/eventing
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Dec 11, 2019
1 parent 2bd5cf6 commit a0397bf
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 200 deletions.
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_list-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ kn source list-types [flags]

### SEE ALSO

* [kn source](kn_source.md) - Event Source command group
* [kn source](kn_source.md) - Event source command group

47 changes: 0 additions & 47 deletions go.sum

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions pkg/eventing/sources/v1alpha1/apiserver_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/sources/v1alpha1"
client_v1alpha1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/sources/v1alpha1"

kn_errors "knative.dev/client/pkg/errors"
)

// Interface for working with ApiServer sources
type KnApiServerSourcesClient interface {

// Get an ApiServerSource by object
CreateApiServerSource(apisvrsrc *v1alpha1.ApiServerSource) (*v1alpha1.ApiServerSource, error)

// Delete an ApiServerSource by name
DeleteApiServerSource(name string) error

// Get namespace for this client
Namespace() string
}

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

// NewKnSourcesClient is to invoke Eventing Sources Client API to create object
func newKnApiServerSourcesClient(client client_v1alpha1.ApiServerSourceInterface, namespace string) KnApiServerSourcesClient {
return &apiServerSourcesClient{
client: client,
namespace: namespace,
}
}

//CreateApiServerSource is used to create an instance of ApiServerSource
func (c *apiServerSourcesClient) CreateApiServerSource(apisvrsrc *v1alpha1.ApiServerSource) (*v1alpha1.ApiServerSource, error) {
ins, err := c.client.Create(apisvrsrc)
if err != nil {
return nil, kn_errors.GetError(err)
}
return ins, nil
}

//DeleteApiServerSource is used to create an instance of ApiServerSource
func (c *apiServerSourcesClient) DeleteApiServerSource(name string) error {
err := c.client.Delete(name, &v1.DeleteOptions{})
return err
}

// Return the client's namespace
func (c *apiServerSourcesClient) Namespace() string {
return c.namespace
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (

var testNamespace = "test-ns"

func setup() (sources fake.FakeSourcesV1alpha1, client KnSourcesClient) {
func setup() (sources fake.FakeSourcesV1alpha1, client KnApiServerSourcesClient) {
sources = fake.FakeSourcesV1alpha1{Fake: &client_testing.Fake{}}
client = NewKnSourcesClient(&sources, testNamespace)
client = NewKnSourcesClient(&sources, testNamespace).ApiServerSourcesClient()
return
}

Expand Down
45 changes: 14 additions & 31 deletions pkg/eventing/sources/v1alpha1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,41 @@
package v1alpha1

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

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

// KnSourcesClient to Eventing Sources. All methods are relative to the
// namespace specified during construction
type KnSourcesClient interface {
// Namespace in which this client is operating for
Namespace() string

// Get an ApiServerSource by object
CreateApiServerSource(apisvrsrc *v1alpha1.ApiServerSource) (*v1alpha1.ApiServerSource, error)
// Get client for ApiServer sources
ApiServerSourcesClient() KnApiServerSourcesClient

// Delete an ApiServerSource by name
DeleteApiServerSource(name string) error
// Get client for CronJob sources
CronJobSourcesClient() KnCronJobSourcesClient
}

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

// NewKnSourcesClient is to invoke Eventing Sources Client API to create object
// Create a new client for managing all eventing built-in sources
func NewKnSourcesClient(client client_v1alpha1.SourcesV1alpha1Interface, namespace string) KnSourcesClient {
return &knSourcesClient{
return &sourcesClient{
client: client,
namespace: namespace,
}
}

//CreateApiServerSource is used to create an instance of ApiServerSource
func (c *knSourcesClient) CreateApiServerSource(apisvrsrc *v1alpha1.ApiServerSource) (*v1alpha1.ApiServerSource, error) {
ins, err := c.client.ApiServerSources(c.namespace).Create(apisvrsrc)
if err != nil {
return nil, kn_errors.GetError(err)
}
return ins, nil
}

//DeleteApiServerSource is used to create an instance of ApiServerSource
func (c *knSourcesClient) DeleteApiServerSource(name string) error {
err := c.client.ApiServerSources(c.namespace).Delete(name, &apis_v1.DeleteOptions{})
return err
// Get the client for dealing with apiserver sources
func (c *sourcesClient) ApiServerSourcesClient() KnApiServerSourcesClient {
return newKnApiServerSourcesClient(c.client.ApiServerSources(c.namespace), c.namespace)
}

// Return the client's namespace
func (c *knSourcesClient) Namespace() string {
return c.namespace
// Get the client for dealing with cronjob sources
func (c *sourcesClient) CronJobSourcesClient() KnCronJobSourcesClient {
return newKnCronJobSourcesClient(c.client.CronJobSources(c.namespace), c.namespace)
}
110 changes: 110 additions & 0 deletions pkg/eventing/sources/v1alpha1/cronjob_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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 (
"fmt"

meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/sources/v1alpha1"
client_v1alpha1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/sources/v1alpha1"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
)

// Interface for interacting with a cronjob source
type KnCronJobSourcesClient interface {

// Get a single cronjob source by name
GetCronJobSource(name string) (*v1alpha1.CronJobSource, error)

// Create a cronjob source by providing the schedule, data and sink
CreateCronJobSource(name, schedule, data string, sink *duckv1beta1.Destination) error

// Update a cronjob source by providing the schedule, data and sink
UpdateCronJobSource(name, schedule, data string, sink *duckv1beta1.Destination) error

// Delete a cronjob source by name
DeleteCronJobSource(name string) error

// Get namespace for this source
Namespace() string
}

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

// NewKnSourcesClient is to invoke Eventing Sources Client API to create object
func newKnCronJobSourcesClient(client client_v1alpha1.CronJobSourceInterface, namespace string) KnCronJobSourcesClient {
return &cronJobSourcesClient{
client: client,
namespace: namespace,
}
}

// Get the namespace for which this client has been created
func (c *cronJobSourcesClient) Namespace() string {
return c.namespace
}

func (c *cronJobSourcesClient) CreateCronJobSource(name, schedule, data string, sink *duckv1beta1.Destination) error {
if sink == nil {
return fmt.Errorf("a sink is required for creating a source")
}
source := v1alpha1.CronJobSource{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.CronJobSourceSpec{
Schedule: schedule,
Data: data,
Sink: sink,
},
}

_, err := c.client.Create(&source)
return err
}

func (c *cronJobSourcesClient) UpdateCronJobSource(name, schedule, data string, sink *duckv1beta1.Destination) error {
source, err := c.GetCronJobSource(name)
if err != nil {
return err
}

if schedule != "" {
source.Spec.Schedule = schedule
}
if data != "" {
source.Spec.Data = data
}
if sink != nil {
source.Spec.Sink = sink
}
_, err = c.client.Update(source)
return err
}

func (c *cronJobSourcesClient) DeleteCronJobSource(name string) error {
return c.client.Delete(name, &meta_v1.DeleteOptions{})
}

func (c *cronJobSourcesClient) GetCronJobSource(name string) (*v1alpha1.CronJobSource, error) {
return c.client.Get(name, meta_v1.GetOptions{})
}
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/apiserver/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewApiServerCreateCommand(p *commands.KnParams) *cobra.Command {
apisrvsrc.Spec.Sink = objectRef

// create
_, err = sourcesClient.CreateApiServerSource(apisrvsrc)
_, err = sourcesClient.ApiServerSourcesClient().CreateApiServerSource(apisrvsrc)
if err != nil {
return fmt.Errorf(
"cannot create ApiServerSource '%s' in namespace '%s' "+
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/apiserver/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewApiServerDeleteCommand(p *commands.KnParams) *cobra.Command {
return err
}

err = sourcesClient.DeleteApiServerSource(name)
err = sourcesClient.ApiServerSourcesClient().DeleteApiServerSource(name)
if err != nil {
return err
}
Expand Down
105 changes: 0 additions & 105 deletions pkg/kn/commands/source/cronjob/client.go

This file was deleted.

Loading

0 comments on commit a0397bf

Please sign in to comment.