This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Add PullSubscription and Topic to internal.events.cloud.google.com #951
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
072041b
Add PullSubscription and Topic to internal.events.cloud.google.com.
Harwayne 534f508
Symlink from intevents to pubsub to ensure everything stays in sync.
Harwayne 8eed039
Fix intevents unit tests.
Harwayne 653e8e2
The type tests can't be symlinked because the group is embedded in th…
Harwayne a2071bf
Change v1beta1/doc.go to match v1alpha1/doc.go.
Harwayne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2019 Google LLC | ||
|
||
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 ( | ||
"testing" | ||
|
||
"knative.dev/pkg/apis/duck" | ||
duckv1 "knative.dev/pkg/apis/duck/v1" | ||
) | ||
|
||
func TestTypesImplements(t *testing.T) { | ||
testCases := []struct { | ||
instance interface{} | ||
iface duck.Implementable | ||
}{ | ||
{instance: &PullSubscription{}, iface: &duckv1.Source{}}, | ||
{instance: &PullSubscription{}, iface: &duckv1.Conditions{}}, | ||
{instance: &Topic{}, iface: &duckv1.Conditions{}}, | ||
{instance: &Topic{}, iface: &duckv1.Addressable{}}, | ||
} | ||
for _, tc := range testCases { | ||
if err := duck.VerifyType(tc.instance, tc.iface); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
pkg/apis/intevents/v1alpha1/pullsubscription_conversion.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright 2020 Google LLC | ||
|
||
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 ( | ||
"context" | ||
"fmt" | ||
|
||
convert "github.com/google/knative-gcp/pkg/apis/convert" | ||
"github.com/google/knative-gcp/pkg/apis/pubsub/v1beta1" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
// ConvertTo implements apis.Convertible. | ||
// Converts source (from v1beta1.PullSubscription) into v1alpha1.PullSubscription. | ||
func (source *PullSubscription) ConvertTo(_ context.Context, to apis.Convertible) error { | ||
switch sink := to.(type) { | ||
case *v1beta1.PullSubscription: | ||
sink.ObjectMeta = source.ObjectMeta | ||
sink.Spec.PubSubSpec = convert.ToV1beta1PubSubSpec(source.Spec.PubSubSpec) | ||
sink.Spec.Topic = source.Spec.Topic | ||
sink.Spec.AckDeadline = source.Spec.AckDeadline | ||
sink.Spec.RetainAckedMessages = source.Spec.RetainAckedMessages | ||
sink.Spec.RetentionDuration = source.Spec.RetentionDuration | ||
sink.Spec.Transformer = source.Spec.Transformer | ||
if mode, err := convertToV1beta1ModeType(source.Spec.Mode); err != nil { | ||
return err | ||
} else { | ||
sink.Spec.Mode = mode | ||
} | ||
sink.Spec.AdapterType = source.Spec.AdapterType | ||
sink.Status.PubSubStatus = convert.ToV1beta1PubSubStatus(source.Status.PubSubStatus) | ||
sink.Status.TransformerURI = source.Status.TransformerURI | ||
sink.Status.SubscriptionID = source.Status.SubscriptionID | ||
return nil | ||
default: | ||
return fmt.Errorf("unknown conversion, got: %T", sink) | ||
|
||
} | ||
} | ||
|
||
// ConvertFrom implements apis.Convertible. | ||
// Converts obj from v1alpha1.PullSubscription into v1beta1.PullSubscription. | ||
func (sink *PullSubscription) ConvertFrom(_ context.Context, from apis.Convertible) error { | ||
switch source := from.(type) { | ||
case *v1beta1.PullSubscription: | ||
sink.ObjectMeta = source.ObjectMeta | ||
sink.Spec.PubSubSpec = convert.FromV1beta1PubSubSpec(source.Spec.PubSubSpec) | ||
sink.Spec.Topic = source.Spec.Topic | ||
sink.Spec.AckDeadline = source.Spec.AckDeadline | ||
sink.Spec.RetainAckedMessages = source.Spec.RetainAckedMessages | ||
sink.Spec.RetentionDuration = source.Spec.RetentionDuration | ||
sink.Spec.Transformer = source.Spec.Transformer | ||
if mode, err := convertFromV1beta1ModeType(source.Spec.Mode); err != nil { | ||
return err | ||
} else { | ||
sink.Spec.Mode = mode | ||
} | ||
sink.Spec.AdapterType = source.Spec.AdapterType | ||
sink.Status.PubSubStatus = convert.FromV1beta1PubSubStatus(source.Status.PubSubStatus) | ||
sink.Status.TransformerURI = source.Status.TransformerURI | ||
sink.Status.SubscriptionID = source.Status.SubscriptionID | ||
return nil | ||
default: | ||
return fmt.Errorf("unknown conversion, got: %T", source) | ||
} | ||
} | ||
|
||
func convertToV1beta1ModeType(from ModeType) (v1beta1.ModeType, error) { | ||
switch from { | ||
case ModeCloudEventsBinary: | ||
return v1beta1.ModeCloudEventsBinary, nil | ||
case ModeCloudEventsStructured: | ||
return v1beta1.ModeCloudEventsStructured, nil | ||
case ModePushCompatible: | ||
return v1beta1.ModePushCompatible, nil | ||
case "": | ||
return "", nil | ||
default: | ||
return "unknown", fmt.Errorf("unknown ModeType %v", from) | ||
} | ||
} | ||
|
||
func convertFromV1beta1ModeType(from v1beta1.ModeType) (ModeType, error) { | ||
switch from { | ||
case v1beta1.ModeCloudEventsBinary: | ||
return ModeCloudEventsBinary, nil | ||
case v1beta1.ModeCloudEventsStructured: | ||
return ModeCloudEventsStructured, nil | ||
case v1beta1.ModePushCompatible: | ||
return ModePushCompatible, nil | ||
case "": | ||
return "", nil | ||
default: | ||
return "unknown", fmt.Errorf("unknown ModeType %v", from) | ||
} | ||
} |
222 changes: 222 additions & 0 deletions
222
pkg/apis/intevents/v1alpha1/pullsubscription_conversion_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
/* | ||
Copyright 2020 Google LLC | ||
|
||
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 ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
duckv1alpha1 "github.com/google/knative-gcp/pkg/apis/duck/v1alpha1" | ||
"github.com/google/knative-gcp/pkg/apis/pubsub/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"knative.dev/pkg/apis" | ||
duckv1 "knative.dev/pkg/apis/duck/v1" | ||
) | ||
|
||
// These variables are used to create a 'complete' version of PullSubscription where every field is | ||
// filled in. | ||
var ( | ||
trueVal = true | ||
seconds = int64(314) | ||
duration = "30s" | ||
|
||
completeObjectMeta = metav1.ObjectMeta{ | ||
Name: "name", | ||
GenerateName: "generateName", | ||
Namespace: "namespace", | ||
SelfLink: "selfLink", | ||
UID: "uid", | ||
ResourceVersion: "resourceVersion", | ||
Generation: 2012, | ||
CreationTimestamp: metav1.Time{ | ||
Time: time.Unix(1, 1), | ||
}, | ||
DeletionTimestamp: &metav1.Time{ | ||
Time: time.Unix(2, 3), | ||
}, | ||
DeletionGracePeriodSeconds: &seconds, | ||
Labels: map[string]string{"steel": "heart"}, | ||
Annotations: map[string]string{"New": "Cago"}, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "apiVersion", | ||
Kind: "kind", | ||
Name: "n", | ||
UID: "uid", | ||
Controller: &trueVal, | ||
BlockOwnerDeletion: &trueVal, | ||
}, | ||
}, | ||
Finalizers: []string{"finalizer-1", "finalizer-2"}, | ||
ClusterName: "clusterName", | ||
} | ||
|
||
completeURL = apis.URL{ | ||
Scheme: "scheme", | ||
Opaque: "opaque", | ||
User: url.User("user"), | ||
Host: "host", | ||
Path: "path", | ||
RawPath: "rawPath", | ||
ForceQuery: false, | ||
RawQuery: "rawQuery", | ||
Fragment: "fragment", | ||
} | ||
|
||
completeDestination = duckv1.Destination{ | ||
Ref: &duckv1.KReference{ | ||
APIVersion: "apiVersion", | ||
Kind: "kind", | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, | ||
URI: &completeURL, | ||
} | ||
|
||
completeSourceSpec = duckv1.SourceSpec{ | ||
Sink: completeDestination, | ||
CloudEventOverrides: &duckv1.CloudEventOverrides{ | ||
Extensions: map[string]string{"supers": "reckoners"}, | ||
}, | ||
} | ||
|
||
completeIdentitySpec = duckv1alpha1.IdentitySpec{ | ||
GoogleServiceAccount: "googleServiceAccount", | ||
} | ||
|
||
completeSecret = &v1.SecretKeySelector{ | ||
LocalObjectReference: v1.LocalObjectReference{ | ||
Name: "name", | ||
}, | ||
Key: "key", | ||
Optional: &trueVal, | ||
} | ||
|
||
completePubSubSpec = duckv1alpha1.PubSubSpec{ | ||
SourceSpec: completeSourceSpec, | ||
IdentitySpec: completeIdentitySpec, | ||
Secret: completeSecret, | ||
Project: "project", | ||
} | ||
|
||
completeIdentityStatus = duckv1alpha1.IdentityStatus{ | ||
Status: duckv1.Status{ | ||
ObservedGeneration: 7, | ||
Conditions: duckv1.Conditions{ | ||
{ | ||
Type: "Ready", | ||
Status: "True", | ||
}, | ||
}, | ||
}, | ||
ServiceAccountName: "serviceAccountName", | ||
} | ||
|
||
completePubSubStatus = duckv1alpha1.PubSubStatus{ | ||
IdentityStatus: completeIdentityStatus, | ||
SinkURI: &completeURL, | ||
CloudEventAttributes: []duckv1.CloudEventAttributes{ | ||
{ | ||
Type: "type", | ||
Source: "source", | ||
}, | ||
}, | ||
ProjectID: "projectID", | ||
TopicID: "topicID", | ||
SubscriptionID: "subscriptionID", | ||
} | ||
|
||
// completePullSubscription is a PullSubscription with every field filled in, except TypeMeta. | ||
// TypeMeta is excluded because conversions do not convert it and this variable was created to | ||
// test conversions. | ||
completePullSubscription = &PullSubscription{ | ||
ObjectMeta: completeObjectMeta, | ||
Spec: PullSubscriptionSpec{ | ||
PubSubSpec: completePubSubSpec, | ||
Topic: "topic", | ||
AckDeadline: &duration, | ||
RetainAckedMessages: false, | ||
RetentionDuration: &duration, | ||
Transformer: &completeDestination, | ||
Mode: ModeCloudEventsBinary, | ||
AdapterType: "adapterType", | ||
}, | ||
Status: PullSubscriptionStatus{ | ||
PubSubStatus: completePubSubStatus, | ||
TransformerURI: &completeURL, | ||
SubscriptionID: "subscriptionID", | ||
}, | ||
} | ||
) | ||
|
||
func TestPullSubscriptionConversionBadType(t *testing.T) { | ||
good, bad := &PullSubscription{}, &Topic{} | ||
|
||
if err := good.ConvertTo(context.Background(), bad); err == nil { | ||
t.Errorf("ConvertTo() = %#v, wanted error", bad) | ||
} | ||
|
||
if err := good.ConvertFrom(context.Background(), bad); err == nil { | ||
t.Errorf("ConvertFrom() = %#v, wanted error", good) | ||
} | ||
} | ||
|
||
func TestPullSubscriptionConversion(t *testing.T) { | ||
// Just one for now, just adding the for loop for ease of future changes. | ||
versions := []apis.Convertible{&v1beta1.PullSubscription{}} | ||
|
||
tests := []struct { | ||
name string | ||
in *PullSubscription | ||
}{{ | ||
name: "min configuration", | ||
in: &PullSubscription{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "ps-name", | ||
Namespace: "ps-ns", | ||
Generation: 17, | ||
}, | ||
Spec: PullSubscriptionSpec{}, | ||
}, | ||
}, { | ||
name: "full configuration", | ||
in: completePullSubscription, | ||
}} | ||
for _, test := range tests { | ||
for _, version := range versions { | ||
t.Run(test.name, func(t *testing.T) { | ||
ver := version | ||
if err := test.in.ConvertTo(context.Background(), ver); err != nil { | ||
t.Errorf("ConvertTo() = %v", err) | ||
} | ||
got := &PullSubscription{} | ||
if err := got.ConvertFrom(context.Background(), ver); err != nil { | ||
t.Errorf("ConvertFrom() = %v", err) | ||
} | ||
ignoreUsername := cmp.AllowUnexported(url.Userinfo{}) | ||
if diff := cmp.Diff(test.in, got, ignoreUsername); diff != "" { | ||
t.Errorf("roundtrip (-want, +got) = %v", diff) | ||
} | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What method did you use to choose the files to symlink? This file seems identical to https://github.com/google/knative-gcp/blob/aa7d86b360964c9891af1065c3ba32359c446ec9/pkg/apis/pubsub/v1alpha1/implements_test.go but it's not a symlink.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is identical, but the 'real' files are in
intevents
, the symlinks are inpubsub
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Thanks for the
ls
output, it's much clearer there./hold cancel