Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Add PullSubscription and Topic to internal.events.cloud.google.com #951

Merged
merged 5 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions pkg/apis/intevents/v1alpha1/implements_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copy link
Contributor

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.

Copy link
Contributor Author

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 in pubsub.

ls -l pkg/apis/pubsub/v1beta1
total 64
-rw-r--r--  1 harwayne  eng   861 Apr 30 15:05 doc.go
lrwxr-xr-x  1 harwayne  eng    42 Apr 30 15:04 implements_test.go -> ../../intevents/v1beta1/implements_test.go
lrwxr-xr-x  1 harwayne  eng    54 Apr 30 15:04 pullsubscription_conversion.go -> ../../intevents/v1beta1/pullsubscription_conversion.go
lrwxr-xr-x  1 harwayne  eng    59 Apr 30 15:04 pullsubscription_conversion_test.go -> ../../intevents/v1beta1/pullsubscription_conversion_test.go
lrwxr-xr-x  1 harwayne  eng    52 Apr 30 15:04 pullsubscription_defaults.go -> ../../intevents/v1beta1/pullsubscription_defaults.go
lrwxr-xr-x  1 harwayne  eng    57 Apr 30 15:04 pullsubscription_defaults_test.go -> ../../intevents/v1beta1/pullsubscription_defaults_test.go
lrwxr-xr-x  1 harwayne  eng    53 Apr 30 15:04 pullsubscription_lifecycle.go -> ../../intevents/v1beta1/pullsubscription_lifecycle.go
lrwxr-xr-x  1 harwayne  eng    58 Apr 30 15:04 pullsubscription_lifecycle_test.go -> ../../intevents/v1beta1/pullsubscription_lifecycle_test.go
lrwxr-xr-x  1 harwayne  eng    49 Apr 30 15:04 pullsubscription_types.go -> ../../intevents/v1beta1/pullsubscription_types.go
-rw-r--r--  1 harwayne  eng  4114 Apr 27 14:29 pullsubscription_types_test.go
lrwxr-xr-x  1 harwayne  eng    54 Apr 30 15:04 pullsubscription_validation.go -> ../../intevents/v1beta1/pullsubscription_validation.go
lrwxr-xr-x  1 harwayne  eng    59 Apr 30 15:04 pullsubscription_validation_test.go -> ../../intevents/v1beta1/pullsubscription_validation_test.go
-rw-r--r--  1 harwayne  eng  1704 Apr 27 14:23 register.go
-rw-r--r--  1 harwayne  eng  1711 Apr 27 14:23 register_test.go
lrwxr-xr-x  1 harwayne  eng    43 Apr 30 15:04 topic_conversion.go -> ../../intevents/v1beta1/topic_conversion.go
lrwxr-xr-x  1 harwayne  eng    48 Apr 30 15:04 topic_conversion_test.go -> ../../intevents/v1beta1/topic_conversion_test.go
lrwxr-xr-x  1 harwayne  eng    41 Apr 30 15:04 topic_defaults.go -> ../../intevents/v1beta1/topic_defaults.go
lrwxr-xr-x  1 harwayne  eng    46 Apr 30 15:04 topic_defaults_test.go -> ../../intevents/v1beta1/topic_defaults_test.go
lrwxr-xr-x  1 harwayne  eng    42 Apr 30 15:04 topic_lifecycle.go -> ../../intevents/v1beta1/topic_lifecycle.go
lrwxr-xr-x  1 harwayne  eng    47 Apr 30 15:04 topic_lifecycle_test.go -> ../../intevents/v1beta1/topic_lifecycle_test.go
lrwxr-xr-x  1 harwayne  eng    38 Apr 30 15:04 topic_types.go -> ../../intevents/v1beta1/topic_types.go
-rw-r--r--  1 harwayne  eng  2534 Apr 27 14:29 topic_types_test.go
lrwxr-xr-x  1 harwayne  eng    43 Apr 30 15:04 topic_validation.go -> ../../intevents/v1beta1/topic_validation.go
lrwxr-xr-x  1 harwayne  eng    48 Apr 30 15:04 topic_validation_test.go -> ../../intevents/v1beta1/topic_validation_test.go
-rw-r--r--  1 harwayne  eng  7011 Apr 30 15:11 zz_generated.deepcopy.go

$ ls -l pkg/apis/intevents/v1beta1
total 296
-rw-r--r--  1 harwayne  eng    953 Apr 30 15:11 doc.go
-rw-r--r--  1 harwayne  eng   1146 Apr 30 15:04 implements_test.go
-rw-r--r--  1 harwayne  eng   1037 Apr 30 15:04 pullsubscription_conversion.go
-rw-r--r--  1 harwayne  eng    974 Apr 30 15:04 pullsubscription_conversion_test.go
-rw-r--r--  1 harwayne  eng   1723 Apr 30 15:04 pullsubscription_defaults.go
-rw-r--r--  1 harwayne  eng   4958 Apr 30 15:04 pullsubscription_defaults_test.go
-rw-r--r--  1 harwayne  eng   3972 Apr 30 15:04 pullsubscription_lifecycle.go
-rw-r--r--  1 harwayne  eng  11715 Apr 30 15:04 pullsubscription_lifecycle_test.go
-rw-r--r--  1 harwayne  eng   7770 Apr 30 15:04 pullsubscription_types.go
-rw-r--r--  1 harwayne  eng   4123 Apr 30 15:04 pullsubscription_types_test.go
-rw-r--r--  1 harwayne  eng   4389 Apr 30 15:04 pullsubscription_validation.go
-rw-r--r--  1 harwayne  eng  16330 Apr 30 15:04 pullsubscription_validation_test.go
-rw-r--r--  1 harwayne  eng   1709 Apr 30 15:04 register.go
-rw-r--r--  1 harwayne  eng   2148 Apr 30 15:04 register_test.go
-rw-r--r--  1 harwayne  eng   1015 Apr 30 15:04 topic_conversion.go
-rw-r--r--  1 harwayne  eng    941 Apr 30 15:04 topic_conversion_test.go
-rw-r--r--  1 harwayne  eng   1126 Apr 30 15:04 topic_defaults.go
-rw-r--r--  1 harwayne  eng   1144 Apr 30 15:04 topic_defaults_test.go
-rw-r--r--  1 harwayne  eng   4034 Apr 30 15:04 topic_lifecycle.go
-rw-r--r--  1 harwayne  eng   4692 Apr 30 15:04 topic_lifecycle_test.go
-rw-r--r--  1 harwayne  eng   6289 Apr 30 15:04 topic_types.go
-rw-r--r--  1 harwayne  eng   2543 Apr 30 15:04 topic_types_test.go
-rw-r--r--  1 harwayne  eng   1661 Apr 30 15:04 topic_validation.go
-rw-r--r--  1 harwayne  eng   2797 Apr 30 15:04 topic_validation_test.go
-rw-r--r--  1 harwayne  eng   7011 Apr 30 15:04 zz_generated.deepcopy.go

Copy link
Contributor

@grantr grantr Apr 30, 2020

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

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 pkg/apis/intevents/v1alpha1/pullsubscription_conversion.go
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 pkg/apis/intevents/v1alpha1/pullsubscription_conversion_test.go
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)
}
})
}
}
}
Loading