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
/
pubsub.go
164 lines (135 loc) · 5.18 KB
/
pubsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2019 The Knative Authors
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 testing
import (
"time"
gcpauthtesthelper "github.com/google/knative-gcp/pkg/apis/configs/gcpauth/testhelper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"github.com/google/knative-gcp/pkg/apis/events/v1beta1"
)
// CloudPubSubSourceOption enables further configuration of a CloudPubSubSource.
type CloudPubSubSourceOption func(*v1beta1.CloudPubSubSource)
// NewCloudPubSubSource creates a CloudPubSubSource with CloudPubSubSourceOptions
func NewCloudPubSubSource(name, namespace string, so ...CloudPubSubSourceOption) *v1beta1.CloudPubSubSource {
ps := &v1beta1.CloudPubSubSource{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: "test-pubsub-uid",
},
}
for _, opt := range so {
opt(ps)
}
return ps
}
func WithCloudPubSubSourceSink(gvk metav1.GroupVersionKind, name string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Spec.Sink = duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: apiVersion(gvk),
Kind: gvk.Kind,
Name: name,
},
}
}
}
func WithCloudPubSubSourceServiceAccount(kServiceAccount string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Spec.ServiceAccountName = kServiceAccount
}
}
func WithCloudPubSubSourceDeletionTimestamp(s *v1beta1.CloudPubSubSource) {
t := metav1.NewTime(time.Unix(1e9, 0))
s.ObjectMeta.SetDeletionTimestamp(&t)
}
func WithCloudPubSubSourceProject(project string) CloudPubSubSourceOption {
return func(s *v1beta1.CloudPubSubSource) {
s.Spec.Project = project
}
}
func WithCloudPubSubSourceTopic(topicID string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Spec.Topic = topicID
}
}
// WithInitCloudPubSubSourceConditions initializes the CloudPubSubSource's conditions.
func WithInitCloudPubSubSourceConditions(ps *v1beta1.CloudPubSubSource) {
ps.Status.InitializeConditions()
}
// WithCloudPubSubSourceServiceAccountName will give status.ServiceAccountName a k8s service account name, which is related on Workload Identity's Google service account.
func WithCloudPubSubSourceServiceAccountName(name string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.ServiceAccountName = name
}
}
func WithCloudPubSubSourceWorkloadIdentityFailed(reason, message string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.MarkWorkloadIdentityFailed(ps.ConditionSet(), reason, message)
}
}
// WithCloudPubSubSourcePullSubscriptionFailed marks the condition that the
// status of PullSubscription is False
func WithCloudPubSubSourcePullSubscriptionFailed(reason, message string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.MarkPullSubscriptionFailed(ps.ConditionSet(), reason, message)
}
}
// WithCloudPubSubSourcePullSubscriptionUnknown marks the condition that the
// topic is Unknown
func WithCloudPubSubSourcePullSubscriptionUnknown(reason, message string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.MarkPullSubscriptionUnknown(ps.ConditionSet(), reason, message)
}
}
// WithCloudPubSubSourcePullSubscriptionReady marks the condition that the
// topic is not ready
func WithCloudPubSubSourcePullSubscriptionReady(ps *v1beta1.CloudPubSubSource) {
ps.Status.MarkPullSubscriptionReady(ps.ConditionSet())
}
// WithCloudPubSubSourceSinkURI sets the status for sink URI
func WithCloudPubSubSourceSinkURI(url *apis.URL) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.SinkURI = url
}
}
func WithCloudPubSubSourceSubscriptionID(subscriptionID string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.SubscriptionID = subscriptionID
}
}
func WithCloudPubSubSourceFinalizers(finalizers ...string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Finalizers = finalizers
}
}
func WithCloudPubSubSourceStatusObservedGeneration(generation int64) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.Status.Status.ObservedGeneration = generation
}
}
func WithCloudPubSubSourceObjectMetaGeneration(generation int64) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.ObjectMeta.Generation = generation
}
}
func WithCloudPubSubSourceAnnotations(Annotations map[string]string) CloudPubSubSourceOption {
return func(ps *v1beta1.CloudPubSubSource) {
ps.ObjectMeta.Annotations = Annotations
}
}
func WithCloudPubSubSourceSetDefaults(ps *v1beta1.CloudPubSubSource) {
ps.SetDefaults(gcpauthtesthelper.ContextWithDefaults())
}