Skip to content

Commit

Permalink
fix #4986 remove pingsource adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojizhuang committed Mar 2, 2021
1 parent f220668 commit 4ddbae8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 70 deletions.
15 changes: 10 additions & 5 deletions pkg/adapter/mtping/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ package mtping
import (
"context"

"knative.dev/pkg/reconciler"

"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"k8s.io/client-go/tools/cache"

"knative.dev/eventing/pkg/adapter/v2"
"knative.dev/eventing/pkg/apis/sources/v1beta2"
pingsourceinformer "knative.dev/eventing/pkg/client/injection/informers/sources/v1beta2/pingsource"
pingsourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1beta2/pingsource"
"knative.dev/pkg/controller"
"knative.dev/pkg/logging"
"knative.dev/pkg/reconciler"
)

// TODO: code generation
Expand Down Expand Up @@ -64,6 +64,11 @@ func NewController(ctx context.Context, adapter adapter.Adapter) *controller.Imp
})

logging.FromContext(ctx).Info("Setting up event handlers")
pingsourceinformer.Get(ctx).Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))
pingsourceinformer.Get(ctx).Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: impl.Enqueue,
UpdateFunc: controller.PassNew(impl.Enqueue),
DeleteFunc: r.deleteFunc,
})
return impl
}
25 changes: 15 additions & 10 deletions pkg/adapter/mtping/pingsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"context"
"fmt"

"knative.dev/pkg/reconciler"

"knative.dev/eventing/pkg/apis/sources/v1beta2"
pingsourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1beta2/pingsource"
"knative.dev/pkg/kmeta"
"knative.dev/pkg/reconciler"
)

// TODO: code generation
Expand All @@ -36,9 +36,6 @@ type Reconciler struct {
// Check that our Reconciler implements ReconcileKind.
var _ pingsourcereconciler.Interface = (*Reconciler)(nil)

// Check that our Reconciler implements FinalizeKind.
var _ pingsourcereconciler.Finalizer = (*Reconciler)(nil)

func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1beta2.PingSource) reconciler.Event {
if !source.Status.IsReady() {
return fmt.Errorf("warning: PingSource is not ready")
Expand All @@ -50,9 +47,17 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1beta2.PingSour
return nil
}

func (r *Reconciler) FinalizeKind(ctx context.Context, source *v1beta2.PingSource) reconciler.Event {
// Update the adapter state
r.mtadapter.Remove(ctx, source)

return nil
func (r *Reconciler) deleteFunc(obj interface{}) {
if obj == nil {
return
}
acc, err := kmeta.DeletionHandlingAccessor(obj)
if err != nil {
return
}
pingSource, ok := acc.(*v1beta2.PingSource)
if !ok || pingSource == nil {
return
}
r.mtadapter.Remove(context.Background(), pingSource)
}
62 changes: 7 additions & 55 deletions pkg/adapter/mtping/pingsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import (

cloudevents "github.com/cloudevents/sdk-go/v2"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientgotesting "k8s.io/client-go/testing"
"knative.dev/eventing/pkg/apis/sources/v1beta2"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/client/injection/reconciler/sources/v1beta2/pingsource"
Expand All @@ -41,14 +38,13 @@ import (
)

const (
testNS = "test-namespace"
pingSourceName = "test-pingsource"
testSchedule = "*/2 * * * *"
testContentType = cloudevents.TextPlain
testData = "data"
testDataBase64 = "ZGF0YQ=="
sinkName = "mysink"
defaultFinalizerName = "pingsources.sources.knative.dev"
testNS = "test-namespace"
pingSourceName = "test-pingsource"
testSchedule = "*/2 * * * *"
testContentType = cloudevents.TextPlain
testData = "data"
testDataBase64 = "ZGF0YQ=="
sinkName = "mysink"
)

var (
Expand Down Expand Up @@ -91,12 +87,6 @@ func TestAllCases(t *testing.T) {
rttestingv1beta2.WithPingSourceCloudEventAttributes,
),
},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "%s" finalizers`, pingSourceName),
},
WantPatches: []clientgotesting.PatchActionImpl{
patchFinalizers(testNS, pingSourceName, defaultFinalizerName),
},
WantErr: false,
}, {
Name: "valid schedule without contentType, data and dataBase64",
Expand All @@ -116,12 +106,6 @@ func TestAllCases(t *testing.T) {
rttestingv1beta2.WithPingSourceCloudEventAttributes,
),
},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "%s" finalizers`, pingSourceName),
},
WantPatches: []clientgotesting.PatchActionImpl{
patchFinalizers(testNS, pingSourceName, defaultFinalizerName),
},
WantErr: false,
}, {
Name: "valid schedule with dataBase64",
Expand All @@ -143,12 +127,6 @@ func TestAllCases(t *testing.T) {
rttestingv1beta2.WithPingSourceCloudEventAttributes,
),
},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "%s" finalizers`, pingSourceName),
},
WantPatches: []clientgotesting.PatchActionImpl{
patchFinalizers(testNS, pingSourceName, defaultFinalizerName),
},
WantErr: false,
}, {
Name: "valid schedule, with finalizer",
Expand All @@ -168,7 +146,6 @@ func TestAllCases(t *testing.T) {
rttestingv1beta2.WithPingSourceDeployed,
rttestingv1beta2.WithPingSourceSink(sinkURI),
rttestingv1beta2.WithPingSourceCloudEventAttributes,
rttestingv1beta2.WithPingSourceFinalizers(defaultFinalizerName),
),
},
WantErr: false,
Expand All @@ -190,16 +167,9 @@ func TestAllCases(t *testing.T) {
rttestingv1beta2.WithPingSourceDeployed,
rttestingv1beta2.WithPingSourceSink(sinkURI),
rttestingv1beta2.WithPingSourceCloudEventAttributes,
rttestingv1beta2.WithPingSourceFinalizers(defaultFinalizerName),
rttestingv1beta2.WithPingSourceDeleted,
),
},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "%s" finalizers`, pingSourceName),
},
WantPatches: []clientgotesting.PatchActionImpl{
patchFinalizers(testNS, pingSourceName, ""),
},
WantErr: false,
}, {
Name: "valid schedule, deleted without finalizer",
Expand Down Expand Up @@ -236,21 +206,3 @@ func TestAllCases(t *testing.T) {
}, false, logger))

}

func patchFinalizers(namespace, name string, finalizers string) clientgotesting.PatchActionImpl {
fstr := ""
if finalizers != "" {
fstr = `"` + finalizers + `"`
}
return clientgotesting.PatchActionImpl{
ActionImpl: clientgotesting.ActionImpl{
Namespace: namespace,
Verb: "patch",
Resource: schema.GroupVersionResource{Group: "sources.knative.dev", Version: "v1beta1", Resource: "pingsources"},
Subresource: "",
},
Name: name,
PatchType: "application/merge-patch+json",
Patch: []byte(`{"metadata":{"finalizers":[` + fstr + `],"resourceVersion":""}}`),
}
}

0 comments on commit 4ddbae8

Please sign in to comment.