Skip to content

Commit

Permalink
add more tests and update based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Jan 15, 2020
1 parent de2a54d commit 6edd8b7
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 138 deletions.
5 changes: 3 additions & 2 deletions docs/cmd/kn_source_apiserver_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ kn source apiserver create NAME --resource RESOURCE --service-account ACCOUNTNAM
"Ref" sends only the reference to the resource,
"Resource" send the full resource. (default "Ref")
-n, --namespace string Specify the namespace to operate in.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. Deployment:apps/v1:true.
"isController" can be omitted and is "false" by default.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. "Deployment:v1:true".
"APIVersion" can be omitted and is "v1" by default, e.g. "Deployment::true";
"isController" can be omitted and is "false" by default, e.g. "Deployment:v1" or just "Deployment".
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
```
Expand Down
5 changes: 3 additions & 2 deletions docs/cmd/kn_source_apiserver_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ kn source apiserver update NAME --resource RESOURCE --service-account ACCOUNTNAM
"Ref" sends only the reference to the resource,
"Resource" send the full resource. (default "Ref")
-n, --namespace string Specify the namespace to operate in.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. Deployment:apps/v1:true.
"isController" can be omitted and is "false" by default.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. "Deployment:v1:true".
"APIVersion" can be omitted and is "v1" by default, e.g. "Deployment::true";
"isController" can be omitted and is "false" by default, e.g. "Deployment:v1" or just "Deployment".
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
```
Expand Down
40 changes: 0 additions & 40 deletions pkg/eventing/sources/v1alpha1/apiserver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package v1alpha1

import (
"fmt"
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kn_errors "knative.dev/client/pkg/errors"
"knative.dev/eventing/pkg/apis/sources/v1alpha1"
Expand Down Expand Up @@ -159,43 +156,6 @@ func (b *APIServerSourceBuilder) Resources(resources []v1alpha1.ApiServerResourc
return b
}

// AddResource which should be streamed
func (b *APIServerSourceBuilder) AddResource(version string, kind string, isController bool) *APIServerSourceBuilder {
resources := b.apiServerSource.Spec.Resources
if resources == nil {
resources = []v1alpha1.ApiServerResource{}
b.apiServerSource.Spec.Resources = resources
}
resourceRef := v1alpha1.ApiServerResource{
APIVersion: version,
Kind: kind,
Controller: isController,
}
b.apiServerSource.Spec.Resources = append(resources, resourceRef)
return b
}

// RemoveResource which should be streamed
func (b *APIServerSourceBuilder) RemoveResource(version string, kind string, isController bool) (*APIServerSourceBuilder, error) {
resources := b.apiServerSource.Spec.Resources
if resources == nil {
resources = []v1alpha1.ApiServerResource{}
b.apiServerSource.Spec.Resources = resources
}
resourceRef := v1alpha1.ApiServerResource{
APIVersion: version,
Kind: kind,
Controller: isController,
}
for i, k := range resources {
if reflect.DeepEqual(k, resourceRef) {
resources = append(resources[:i], resources[i+1:]...)
return b, nil
}
}
return b, fmt.Errorf("cannot find resource %s:%s:%t to remove", version, kind, isController)
}

// ServiceAccount with which this source should operate
func (b *APIServerSourceBuilder) ServiceAccount(sa string) *APIServerSourceBuilder {
b.apiServerSource.Spec.ServiceAccountName = sa
Expand Down
4 changes: 1 addition & 3 deletions pkg/kn/commands/source/apiserver/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ func NewAPIServerCreateCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
for _, k := range resources {
b.AddResource(k.ApiVersion, k.Kind, k.IsController)
}
b.Resources(resources)

err = apiSourceClient.CreateAPIServerSource(b.Build())

Expand Down
84 changes: 60 additions & 24 deletions pkg/kn/commands/source/apiserver/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package apiserver

import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
Expand All @@ -41,15 +42,9 @@ type APIServerSourceUpdateFlags struct {
Resources []string
}

type resourceSpec struct {
Kind string
ApiVersion string
IsController bool
}

// getAPIServerResourceArray is to return an array of ApiServerResource from a string. A sample is Event:v1:true,Pod:v2:false
func (f *APIServerSourceUpdateFlags) getAPIServerResourceArray() ([]resourceSpec, error) {
var resourceList []resourceSpec
// getAPIServerResourceArray is to construct an array of resources.
func (f *APIServerSourceUpdateFlags) getAPIServerResourceArray() ([]v1alpha1.ApiServerResource, error) {
var resourceList []v1alpha1.ApiServerResource
for _, r := range f.Resources {
resourceSpec, err := getValidResource(r)
if err != nil {
Expand All @@ -60,12 +55,43 @@ func (f *APIServerSourceUpdateFlags) getAPIServerResourceArray() ([]resourceSpec
return resourceList, nil
}

// getAPIServerResourceArray is to return an array of ApiServerResource from a string. A sample is Event:v1:true
func (f *APIServerSourceUpdateFlags) getUpdateAPIServerResourceArray() ([]resourceSpec, []resourceSpec, error) {
var added []resourceSpec
var removed []resourceSpec
// updateExistingAPIServerResourceArray is to update an array of resources.
func (f *APIServerSourceUpdateFlags) updateExistingAPIServerResourceArray(existing []v1alpha1.ApiServerResource) ([]v1alpha1.ApiServerResource, error) {
var found bool

added, removed, err := f.getUpdateAPIServerResourceArray()
if err != nil {
return nil, err
}

if existing == nil {
existing = []v1alpha1.ApiServerResource{}
}

existing = append(existing, added...)

for _, item := range removed {
found = false
for i, ref := range existing {
if reflect.DeepEqual(item, ref) {
existing = append(existing[:i], existing[i+1:]...)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("cannot find resource %s:%s:%t to remove", item.Kind, item.APIVersion, item.Controller)
}
}
return existing, nil
}

// getUpdateAPIServerResourceArray is to construct an array of resources for update action.
func (f *APIServerSourceUpdateFlags) getUpdateAPIServerResourceArray() ([]v1alpha1.ApiServerResource, []v1alpha1.ApiServerResource, error) {
var added []v1alpha1.ApiServerResource
var removed []v1alpha1.ApiServerResource

addedArray, removedArray := util.AddListAndRemovalListFromArray(f.Resources)
addedArray, removedArray := util.AddedAndRemovalListsFromArray(f.Resources)
for _, r := range addedArray {
resourceSpec, err := getValidResource(r)
if err != nil {
Expand All @@ -83,23 +109,32 @@ func (f *APIServerSourceUpdateFlags) getUpdateAPIServerResourceArray() ([]resour
return added, removed, nil
}

func getValidResource(resource string) (*resourceSpec, error) {
var isController = false //false as default
//getValidResource is to parse resource spec from a string
func getValidResource(resource string) (*v1alpha1.ApiServerResource, error) {
var isController bool
var version string
var err error

parts := strings.Split(resource, apiVersionSplitChar)
parts := strings.SplitN(resource, apiVersionSplitChar, 3)
if len(parts[0]) == 0 {
return nil, fmt.Errorf("cannot find KIND in resource specification %s", resource)
}
kind := parts[0]
if len(parts) < 2 {
return nil, fmt.Errorf("no APIVersion given for resource %s", resource)

if len(parts) >= 2 && len(parts[1]) > 0 {
version = parts[1]
} else {
version = "v1"
}
version := parts[1]
if len(parts) >= 3 {
if len(parts) >= 3 && len(parts[2]) > 0 {
isController, err = strconv.ParseBool(parts[2])
if err != nil {
return nil, fmt.Errorf("cannot parse controller flage in resource specification %s", resource)
}
} else {
isController = false
}
return &resourceSpec{Kind: kind, ApiVersion: version, IsController: isController}, nil
return &v1alpha1.ApiServerResource{Kind: kind, APIVersion: version, Controller: isController}, nil
}

//Add is to set parameters
Expand All @@ -117,8 +152,9 @@ func (f *APIServerSourceUpdateFlags) Add(cmd *cobra.Command) {
cmd.Flags().StringArrayVar(&f.Resources,
"resource",
[]string{},
`Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. Deployment:apps/v1:true.
"isController" can be omitted and is "false" by default.`)
`Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. "Deployment:v1:true".
"APIVersion" can be omitted and is "v1" by default, e.g. "Deployment::true";
"isController" can be omitted and is "false" by default, e.g. "Deployment:v1" or just "Deployment".`)
}

// APIServerSourceListHandlers handles printing human readable table for `kn source apiserver list` command's output
Expand Down
Loading

0 comments on commit 6edd8b7

Please sign in to comment.