Skip to content

Commit

Permalink
improve 'source apiserver update --resource' to follow the common upd…
Browse files Browse the repository at this point in the history
…ate semantics
  • Loading branch information
Daisy Guo committed Jan 7, 2020
1 parent e0877da commit 8564205
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 133 deletions.
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_apiserver_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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 strings Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. Deployment:apps/v1:true.
--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.
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_apiserver_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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 strings Specification for which events to listen, in the format Kind:APIVersion:isController, e.g. Deployment:apps/v1:true.
--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.
--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: 40 additions & 0 deletions pkg/eventing/sources/v1alpha1/apiserver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
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 @@ -156,6 +159,43 @@ 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
19 changes: 10 additions & 9 deletions pkg/kn/commands/source/apiserver/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,20 @@ func NewAPIServerCreateCommand(p *commands.KnParams) *cobra.Command {
"because: %s", name, namespace, err)
}

// create
resources, err := updateFlags.GetAPIServerResourceArray()
b := v1alpha1.NewAPIServerSourceBuilder(name).
ServiceAccount(updateFlags.ServiceAccountName).
Mode(updateFlags.Mode).
Sink(objectRef)

resources, err := updateFlags.getAPIServerResourceArray()
if err != nil {
return err
}
for _, k := range resources {
b.AddResource(k.ApiVersion, k.Kind, k.IsController)
}

err = apiSourceClient.CreateAPIServerSource(
v1alpha1.NewAPIServerSourceBuilder(name).
ServiceAccount(updateFlags.ServiceAccountName).
Mode(updateFlags.Mode).
Resources(*resources).
Sink(objectRef).
Build())
err = apiSourceClient.CreateAPIServerSource(b.Build())

if err != nil {
return fmt.Errorf(
Expand Down
105 changes: 0 additions & 105 deletions pkg/kn/commands/source/apiserver/create_flag_test.go

This file was deleted.

49 changes: 34 additions & 15 deletions pkg/kn/commands/source/apiserver/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
hprinters "knative.dev/client/pkg/printers"
"knative.dev/client/pkg/util"
)

const (
Expand All @@ -41,27 +42,45 @@ type APIServerSourceUpdateFlags struct {
}

type resourceSpec struct {
kind string
apiVersion string
isController bool
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() (*[]v1alpha1.ApiServerResource, error) {
var resourceList []v1alpha1.ApiServerResource
// 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
for _, r := range f.Resources {
resourceSpec, err := getValidResource(r)
if err != nil {
return nil, err
}
resourceRef := v1alpha1.ApiServerResource{
APIVersion: resourceSpec.apiVersion,
Kind: resourceSpec.kind,
Controller: resourceSpec.isController,
resourceList = append(resourceList, *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

addedArray, removedArray := util.AddListAndRemovalListFromArray(f.Resources)
for _, r := range addedArray {
resourceSpec, err := getValidResource(r)
if err != nil {
return nil, nil, err
}
added = append(added, *resourceSpec)
}
for _, r := range removedArray {
resourceSpec, err := getValidResource(r)
if err != nil {
return nil, nil, err
}
resourceList = append(resourceList, resourceRef)
removed = append(removed, *resourceSpec)
}
return &resourceList, nil
return added, removed, nil
}

func getValidResource(resource string) (*resourceSpec, error) {
Expand All @@ -80,7 +99,7 @@ func getValidResource(resource string) (*resourceSpec, error) {
return nil, fmt.Errorf("cannot parse controller flage in resource specification %s", resource)
}
}
return &resourceSpec{apiVersion: version, kind: kind, isController: isController}, nil
return &resourceSpec{Kind: kind, ApiVersion: version, IsController: isController}, nil
}

//Add is to set parameters
Expand All @@ -95,9 +114,9 @@ func (f *APIServerSourceUpdateFlags) Add(cmd *cobra.Command) {
`The mode the receive adapter controller runs under:,
"Ref" sends only the reference to the resource,
"Resource" send the full resource.`)
cmd.Flags().StringSliceVar(&f.Resources,
cmd.Flags().StringArrayVar(&f.Resources,
"resource",
nil,
[]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.`)
}
Expand Down
Loading

0 comments on commit 8564205

Please sign in to comment.