Skip to content

Commit

Permalink
Apiseversource reconciler and e2e for v1beta1 (#3627)
Browse files Browse the repository at this point in the history
* added v1beta1 apiserversource type

* fixed UT

* fixed v1alpha1 apiserver_lifecycle

* added apiversource source conversion for v1beta1

* fixed format

* added ut for v1alpha2 conversion

* reconciler and e2e for v1beta1 apiserversource

* Update test/e2e/source_api_server_v1beta1_test.go

Co-authored-by: Matt Moore <mattmoor@vmware.com>

Co-authored-by: Matt Moore <mattmoor@vmware.com>
  • Loading branch information
capri-xiyue and mattmoor authored Jul 17, 2020
1 parent 9f95260 commit 5632b09
Show file tree
Hide file tree
Showing 16 changed files with 537 additions and 237 deletions.
4 changes: 2 additions & 2 deletions pkg/adapter/apiserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package apiserver

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/eventing/pkg/apis/sources/v1alpha2"
"knative.dev/eventing/pkg/apis/sources/v1beta1"
)

type ResourceWatch struct {
Expand All @@ -44,7 +44,7 @@ type Config struct {
// owned by a specific resource type. If ResourceOwner matches Resources[n]
// then Resources[n] is allowed to pass the ResourceOwner filter.
// +optional
ResourceOwner *v1alpha2.APIVersionKind `json:"owner,omitempty"`
ResourceOwner *v1beta1.APIVersionKind `json:"owner,omitempty"`

// EventMode controls the format of the event.
// `Reference` sends a dataref event type for the resource under watch.
Expand Down
14 changes: 7 additions & 7 deletions pkg/adapter/apiserver/delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@ package apiserver
import (
"testing"

sourcesv1alpha1 "knative.dev/eventing/pkg/apis/sources/v1alpha1"
sourcesv1beta1 "knative.dev/eventing/pkg/apis/sources/v1beta1"
)

func TestResourceAddEvent(t *testing.T) {
d, ce := makeResourceAndTestingClient()
d.Add(simplePod("unit", "test"))
validateSent(t, ce, sourcesv1alpha1.ApiServerSourceAddEventType)
validateSent(t, ce, sourcesv1beta1.ApiServerSourceAddEventType)
}

func TestResourceUpdateEvent(t *testing.T) {
d, ce := makeResourceAndTestingClient()
d.Update(simplePod("unit", "test"))
validateSent(t, ce, sourcesv1alpha1.ApiServerSourceUpdateEventType)
validateSent(t, ce, sourcesv1beta1.ApiServerSourceUpdateEventType)
}

func TestResourceDeleteEvent(t *testing.T) {
d, ce := makeResourceAndTestingClient()
d.Delete(simplePod("unit", "test"))
validateSent(t, ce, sourcesv1alpha1.ApiServerSourceDeleteEventType)
validateSent(t, ce, sourcesv1beta1.ApiServerSourceDeleteEventType)
}

func TestResourceAddEventNil(t *testing.T) {
d, ce := makeResourceAndTestingClient()
d.Add(nil)
validateNotSent(t, ce, sourcesv1alpha1.ApiServerSourceAddEventType)
validateNotSent(t, ce, sourcesv1beta1.ApiServerSourceAddEventType)
}

func TestResourceUpdateEventNil(t *testing.T) {
d, ce := makeResourceAndTestingClient()
d.Update(nil)
validateNotSent(t, ce, sourcesv1alpha1.ApiServerSourceUpdateEventType)
validateNotSent(t, ce, sourcesv1beta1.ApiServerSourceUpdateEventType)
}

func TestResourceDeleteEventNil(t *testing.T) {
d, ce := makeResourceAndTestingClient()
d.Delete(nil)
validateNotSent(t, ce, sourcesv1alpha1.ApiServerSourceDeleteEventType)
validateNotSent(t, ce, sourcesv1beta1.ApiServerSourceDeleteEventType)
}

// HACKHACKHACK For test coverage.
Expand Down
14 changes: 7 additions & 7 deletions pkg/adapter/apiserver/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
sourcesv1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
sourcesv1beta1 "knative.dev/eventing/pkg/apis/sources/v1beta1"
)

func MakeAddEvent(source string, obj interface{}, ref bool) (cloudevents.Event, error) {
Expand All @@ -37,10 +37,10 @@ func MakeAddEvent(source string, obj interface{}, ref bool) (cloudevents.Event,
var eventType string
if ref {
data = getRef(object)
eventType = sourcesv1alpha2.ApiServerSourceAddRefEventType
eventType = sourcesv1beta1.ApiServerSourceAddRefEventType
} else {
data = object
eventType = sourcesv1alpha2.ApiServerSourceAddEventType
eventType = sourcesv1beta1.ApiServerSourceAddEventType
}

return makeEvent(source, eventType, object, data)
Expand All @@ -56,10 +56,10 @@ func MakeUpdateEvent(source string, obj interface{}, ref bool) (cloudevents.Even
var eventType string
if ref {
data = getRef(object)
eventType = sourcesv1alpha2.ApiServerSourceUpdateRefEventType
eventType = sourcesv1beta1.ApiServerSourceUpdateRefEventType
} else {
data = object
eventType = sourcesv1alpha2.ApiServerSourceUpdateEventType
eventType = sourcesv1beta1.ApiServerSourceUpdateEventType
}

return makeEvent(source, eventType, object, data)
Expand All @@ -74,10 +74,10 @@ func MakeDeleteEvent(source string, obj interface{}, ref bool) (cloudevents.Even
var eventType string
if ref {
data = getRef(object)
eventType = sourcesv1alpha2.ApiServerSourceDeleteRefEventType
eventType = sourcesv1beta1.ApiServerSourceDeleteRefEventType
} else {
data = object
eventType = sourcesv1alpha2.ApiServerSourceDeleteEventType
eventType = sourcesv1beta1.ApiServerSourceDeleteEventType
}

return makeEvent(source, eventType, object, data)
Expand Down
26 changes: 13 additions & 13 deletions pkg/adapter/apiserver/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,79 @@ import (
"testing"

adaptertest "knative.dev/eventing/pkg/adapter/v2/test"
sourcesv1alpha1 "knative.dev/eventing/pkg/apis/sources/v1alpha1"
sourcesv1beta1 "knative.dev/eventing/pkg/apis/sources/v1beta1"
)

func TestControllerAddEventWithNoController(t *testing.T) {
c, tc := makeController("v1", "Pod")
c.Add(simplePod("unit", "test"))
validateNotSent(t, tc, sourcesv1alpha1.ApiServerSourceAddRefEventType)
validateNotSent(t, tc, sourcesv1beta1.ApiServerSourceAddRefEventType)
}

func TestControllerAddEventWithWrongController(t *testing.T) {
c, tc := makeController("v1", "Pod")
c.Add(simpleOwnedPod("unit", "test"))
validateNotSent(t, tc, sourcesv1alpha1.ApiServerSourceAddRefEventType)
validateNotSent(t, tc, sourcesv1beta1.ApiServerSourceAddRefEventType)
}

func TestControllerAddEventWithGoodController(t *testing.T) {
c, tc := makeController("apps/v1", "ReplicaSet")
c.Add(simpleOwnedPod("unit", "test"))
validateSent(t, tc, sourcesv1alpha1.ApiServerSourceAddRefEventType)
validateSent(t, tc, sourcesv1beta1.ApiServerSourceAddRefEventType)
}

func TestControllerAddEventWithGoodControllerNoAPIVersion(t *testing.T) {
c, tc := makeController("", "ReplicaSet")
c.Add(simpleOwnedPod("unit", "test"))
validateSent(t, tc, sourcesv1alpha1.ApiServerSourceAddRefEventType)
validateSent(t, tc, sourcesv1beta1.ApiServerSourceAddRefEventType)
}

func TestControllerUpdateEventWithNoController(t *testing.T) {
c, tc := makeController("v1", "Pod")
c.Update(simplePod("unit", "test"))
validateNotSent(t, tc, sourcesv1alpha1.ApiServerSourceUpdateRefEventType)
validateNotSent(t, tc, sourcesv1beta1.ApiServerSourceUpdateRefEventType)
}

func TestControllerUpdateEventWithWrongController(t *testing.T) {
c, tc := makeController("v1", "Pod")
c.Update(simpleOwnedPod("unit", "test"))
validateNotSent(t, tc, sourcesv1alpha1.ApiServerSourceUpdateRefEventType)
validateNotSent(t, tc, sourcesv1beta1.ApiServerSourceUpdateRefEventType)
}

func TestControllerUpdateEventWithGoodController(t *testing.T) {
c, tc := makeController("apps/v1", "ReplicaSet")
c.Update(simpleOwnedPod("unit", "test"))
validateSent(t, tc, sourcesv1alpha1.ApiServerSourceUpdateRefEventType)
validateSent(t, tc, sourcesv1beta1.ApiServerSourceUpdateRefEventType)
}

func TestControllerUpdateEventWithGoodControllerNoAPIVersion(t *testing.T) {
c, tc := makeController("", "ReplicaSet")
c.Update(simpleOwnedPod("unit", "test"))
validateSent(t, tc, sourcesv1alpha1.ApiServerSourceUpdateRefEventType)
validateSent(t, tc, sourcesv1beta1.ApiServerSourceUpdateRefEventType)
}

func TestControllerDeleteEventWithNoController(t *testing.T) {
c, tc := makeController("v1", "Pod")
c.Delete(simplePod("unit", "test"))
validateNotSent(t, tc, sourcesv1alpha1.ApiServerSourceDeleteRefEventType)
validateNotSent(t, tc, sourcesv1beta1.ApiServerSourceDeleteRefEventType)
}

func TestControllerDeleteEventWithWrongController(t *testing.T) {
c, tc := makeController("v1", "Pod")
c.Delete(simpleOwnedPod("unit", "test"))
validateNotSent(t, tc, sourcesv1alpha1.ApiServerSourceDeleteRefEventType)
validateNotSent(t, tc, sourcesv1beta1.ApiServerSourceDeleteRefEventType)
}

func TestControllerDeleteEventWithGoodController(t *testing.T) {
c, tc := makeController("apps/v1", "ReplicaSet")
c.Delete(simpleOwnedPod("unit", "test"))
validateSent(t, tc, sourcesv1alpha1.ApiServerSourceDeleteRefEventType)
validateSent(t, tc, sourcesv1beta1.ApiServerSourceDeleteRefEventType)
}

func TestControllerDeleteEventWithGoodControllerNoAPIVersion(t *testing.T) {
c, tc := makeController("", "ReplicaSet")
c.Delete(simpleOwnedPod("unit", "test"))
validateSent(t, tc, sourcesv1alpha1.ApiServerSourceDeleteRefEventType)
validateSent(t, tc, sourcesv1beta1.ApiServerSourceDeleteRefEventType)
}

func makeController(apiVersion, kind string) (*controllerFilter, *adaptertest.TestCloudEventsClient) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/adapter/apiserver/ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,44 @@ package apiserver
import (
"testing"

sourcesv1alpha1 "knative.dev/eventing/pkg/apis/sources/v1alpha1"
sourcesv1beta1 "knative.dev/eventing/pkg/apis/sources/v1beta1"
)

func TestRefAddEvent(t *testing.T) {
d, ce := makeRefAndTestingClient()
d.Add(simplePod("unit", "test"))
validateSent(t, ce, sourcesv1alpha1.ApiServerSourceAddRefEventType)
validateSent(t, ce, sourcesv1beta1.ApiServerSourceAddRefEventType)
}

func TestRefUpdateEvent(t *testing.T) {
d, ce := makeRefAndTestingClient()
d.Update(simplePod("unit", "test"))
validateSent(t, ce, sourcesv1alpha1.ApiServerSourceUpdateRefEventType)
validateSent(t, ce, sourcesv1beta1.ApiServerSourceUpdateRefEventType)
}

func TestRefDeleteEvent(t *testing.T) {
d, ce := makeRefAndTestingClient()
d.Delete(simplePod("unit", "test"))
validateSent(t, ce, sourcesv1alpha1.ApiServerSourceDeleteRefEventType)
validateSent(t, ce, sourcesv1beta1.ApiServerSourceDeleteRefEventType)

}

func TestRefAddEventNil(t *testing.T) {
d, ce := makeRefAndTestingClient()
d.Add(nil)
validateNotSent(t, ce, sourcesv1alpha1.ApiServerSourceAddRefEventType)
validateNotSent(t, ce, sourcesv1beta1.ApiServerSourceAddRefEventType)
}

func TestRefUpdateEventNil(t *testing.T) {
d, ce := makeRefAndTestingClient()
d.Update(nil)
validateNotSent(t, ce, sourcesv1alpha1.ApiServerSourceUpdateRefEventType)
validateNotSent(t, ce, sourcesv1beta1.ApiServerSourceUpdateRefEventType)
}

func TestRefDeleteEventNil(t *testing.T) {
d, ce := makeRefAndTestingClient()
d.Delete(nil)
validateNotSent(t, ce, sourcesv1alpha1.ApiServerSourceDeleteRefEventType)
validateNotSent(t, ce, sourcesv1beta1.ApiServerSourceDeleteRefEventType)
}

// HACKHACKHACK For test coverage.
Expand Down
16 changes: 8 additions & 8 deletions pkg/reconciler/apiserversource/apiserversource.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import (
pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/pkg/resolver"

"knative.dev/eventing/pkg/apis/sources/v1alpha2"
apiserversourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha2/apiserversource"
listers "knative.dev/eventing/pkg/client/listers/sources/v1alpha2"
"knative.dev/eventing/pkg/apis/sources/v1beta1"
apiserversourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1beta1/apiserversource"
listers "knative.dev/eventing/pkg/client/listers/sources/v1beta1"
"knative.dev/eventing/pkg/logging"
"knative.dev/eventing/pkg/reconciler/apiserversource/resources"
reconcilersource "knative.dev/eventing/pkg/reconciler/source"
Expand Down Expand Up @@ -84,7 +84,7 @@ type Reconciler struct {

var _ apiserversourcereconciler.Interface = (*Reconciler)(nil)

func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1alpha2.ApiServerSource) pkgreconciler.Event {
func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1beta1.ApiServerSource) pkgreconciler.Event {
// This Source attempts to reconcile three things.
// 1. Determine the sink's URI.
// - Nothing to delete.
Expand Down Expand Up @@ -128,7 +128,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1alpha2.ApiServ
return newReconciledNormal(source.Namespace, source.Name)
}

func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1alpha2.ApiServerSource, sinkURI string) (*appsv1.Deployment, error) {
func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1beta1.ApiServerSource, sinkURI string) (*appsv1.Deployment, error) {
// TODO: missing.
// if err := checkResourcesStatus(src); err != nil {
// return nil, err
Expand Down Expand Up @@ -196,7 +196,7 @@ func (r *Reconciler) podSpecChanged(oldPodSpec corev1.PodSpec, newPodSpec corev1
return false
}

func (r *Reconciler) runAccessCheck(src *v1alpha2.ApiServerSource) error {
func (r *Reconciler) runAccessCheck(src *v1beta1.ApiServerSource) error {
if src.Spec.Resources == nil || len(src.Spec.Resources) == 0 {
src.Status.MarkSufficientPermissions()
return nil
Expand Down Expand Up @@ -263,8 +263,8 @@ func (r *Reconciler) runAccessCheck(src *v1alpha2.ApiServerSource) error {
}

func (r *Reconciler) createCloudEventAttributes() []duckv1.CloudEventAttributes {
ceAttributes := make([]duckv1.CloudEventAttributes, 0, len(v1alpha2.ApiServerSourceEventTypes))
for _, apiServerSourceType := range v1alpha2.ApiServerSourceEventTypes {
ceAttributes := make([]duckv1.CloudEventAttributes, 0, len(v1beta1.ApiServerSourceEventTypes))
for _, apiServerSourceType := range v1beta1.ApiServerSourceEventTypes {
ceAttributes = append(ceAttributes, duckv1.CloudEventAttributes{
Type: apiServerSourceType,
Source: r.ceSource,
Expand Down
Loading

0 comments on commit 5632b09

Please sign in to comment.