This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
pubsub_types.go
173 lines (142 loc) · 4.97 KB
/
pubsub_types.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
165
166
167
168
169
170
171
172
173
/*
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 v1beta1
import (
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
duckv1 "knative.dev/pkg/apis/duck/v1"
)
// PubSub is an Implementable "duck type".
var _ duck.Implementable = (*PubSub)(nil)
// +genduck
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// PubSub is a shared type that GCP sources which create a
// Topic / PullSubscription will use.
// This duck type is intended to allow implementors of GCP sources
// which use PubSub for their transport.
type PubSub struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec PubSubSpec `json:"spec"`
Status PubSubStatus `json:"status"`
}
type PubSubSpec struct {
// This brings in CloudEventOverrides and Sink.
duckv1.SourceSpec `json:",inline"`
IdentitySpec `json:",inline"`
// Secret is the credential to use to poll from a Cloud Pub/Sub subscription.
// If not specified, defaults to:
// Name: google-cloud-key
// Key: key.json
// +optional
Secret *corev1.SecretKeySelector `json:"secret,omitempty"`
// Project is the ID of the Google Cloud Project that the PubSub Topic exists in.
// If omitted, defaults to same as the cluster.
// +optional
Project string `json:"project,omitempty"`
}
// PubSubStatus shows how we expect folks to embed Addressable in
// their Status field.
type PubSubStatus struct {
IdentityStatus `json:",inline"`
// SinkURI is the current active sink URI that has been configured for the Source.
// +optional
SinkURI *apis.URL `json:"sinkUri,omitempty"`
// CloudEventAttributes are the specific attributes that the Source uses
// as part of its CloudEvents.
// +optional
CloudEventAttributes []duckv1.CloudEventAttributes `json:"ceAttributes,omitempty"`
// ProjectID is the project ID of the Topic, might have been resolved.
// +optional
ProjectID string `json:"projectId,omitempty"`
// TopicID where the notifications are sent to.
// +optional
TopicID string `json:"topicId,omitempty"`
// SubscriptionID is the created subscription ID.
// +optional
SubscriptionID string `json:"subscriptionId,omitempty"`
}
const (
// TopicReady has status True when the PubSub Topic is ready.
TopicReady apis.ConditionType = "TopicReady"
// PullSubscriptionReay has status True when the PullSubscription is ready.
PullSubscriptionReady apis.ConditionType = "PullSubscriptionReady"
)
// IsReady returns true if the resource is ready overall.
func (ss *PubSubStatus) IsReady() bool {
for _, c := range ss.Conditions {
switch c.Type {
// Look for the "happy" condition, which is the only condition that
// we can reliably understand to be the overall state of the resource.
case apis.ConditionReady, apis.ConditionSucceeded:
return c.IsTrue()
}
}
return false
}
var (
// Verify PubSub resources meet duck contracts.
_ duck.Populatable = (*PubSub)(nil)
_ apis.Listable = (*PubSub)(nil)
)
// GetFullType implements duck.Implementable
func (*PubSub) GetFullType() duck.Populatable {
return &PubSub{}
}
// Populate implements duck.Populatable
func (s *PubSub) Populate() {
s.Spec.Sink = duckv1.Destination{
URI: &apis.URL{
Scheme: "https",
Host: "tableflip.dev",
RawQuery: "flip=mattmoor",
},
}
s.Spec.CloudEventOverrides = &duckv1.CloudEventOverrides{
Extensions: map[string]string{"boosh": "kakow"},
}
s.Spec.Secret = &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "secret"},
Key: "secretkey",
}
s.Status.ObservedGeneration = 42
s.Status.Conditions = duckv1.Conditions{{
// Populate ALL fields
Type: duckv1.SourceConditionSinkProvided,
Status: corev1.ConditionTrue,
LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 02, 28, 18, 52, 00, 00, time.UTC))},
}}
s.Status.SinkURI = &apis.URL{
Scheme: "https",
Host: "tableflip.dev",
RawQuery: "flip=mattmoor",
}
s.Status.ProjectID = "projectid"
s.Status.TopicID = "topicid"
s.Status.SubscriptionID = "subscriptionid"
}
// GetListType implements apis.Listable
func (*PubSub) GetListType() runtime.Object {
return &PubSubList{}
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// PubSubList is a list of PubSub resources
type PubSubList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []PubSub `json:"items"`
}