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 82
/
scheduler.go
213 lines (178 loc) · 7.02 KB
/
scheduler.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
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"
)
// CloudSchedulerSourceOption enables further configuration of a CloudSchedulerSource.
type CloudSchedulerSourceOption func(*v1beta1.CloudSchedulerSource)
// NewCloudSchedulerSource creates a CloudSchedulerSource with CloudSchedulerSourceOptions
func NewCloudSchedulerSource(name, namespace string, so ...CloudSchedulerSourceOption) *v1beta1.CloudSchedulerSource {
s := &v1beta1.CloudSchedulerSource{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: "test-scheduler-uid",
},
}
for _, opt := range so {
opt(s)
}
return s
}
func WithCloudSchedulerSourceSink(gvk metav1.GroupVersionKind, name string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Spec.Sink = duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: apiVersion(gvk),
Kind: gvk.Kind,
Name: name,
},
}
}
}
func WithCloudSchedulerSourceLocation(location string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Spec.Location = location
}
}
func WithCloudSchedulerSourceProject(project string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Spec.Project = project
}
}
func WithCloudSchedulerSourceSchedule(schedule string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Spec.Schedule = schedule
}
}
func WithCloudSchedulerSourceServiceAccount(kServiceAccount string) CloudSchedulerSourceOption {
return func(ps *v1beta1.CloudSchedulerSource) {
ps.Spec.ServiceAccountName = kServiceAccount
}
}
func WithCloudSchedulerSourceData(data string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Spec.Data = data
}
}
func WithCloudSchedulerSourceDeletionTimestamp(s *v1beta1.CloudSchedulerSource) {
t := metav1.NewTime(time.Unix(1e9, 0))
s.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithInitCloudSchedulerSourceConditions initializes the CloudSchedulerSources's conditions.
func WithInitCloudSchedulerSourceConditions(s *v1beta1.CloudSchedulerSource) {
s.Status.InitializeConditions()
}
// WithCloudSchedulerSourceServiceAccountName will give status.ServiceAccountName a k8s service account name, which is related on Workload Identity's Google service account.
func WithCloudSchedulerSourceServiceAccountName(name string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.ServiceAccountName = name
}
}
func WithCloudSchedulerSourceWorkloadIdentityFailed(reason, message string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkWorkloadIdentityFailed(s.ConditionSet(), reason, message)
}
}
// WithCloudSchedulerSourceTopicFailed marks the condition that the
// status of topic is False.
func WithCloudSchedulerSourceTopicFailed(reason, message string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkTopicFailed(s.ConditionSet(), reason, message)
}
}
// WithCloudSchedulerSourceTopicUnknown marks the condition that the
// status of topic is Unknown.
func WithCloudSchedulerSourceTopicUnknown(reason, message string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkTopicUnknown(s.ConditionSet(), reason, message)
}
}
// WithCloudSchedulerSourceTopicNotReady marks the condition that the
// topic is not ready.
func WithCloudSchedulerSourceTopicReady(topicID, projectID string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkTopicReady(s.ConditionSet())
s.Status.TopicID = topicID
s.Status.ProjectID = projectID
}
}
// WithCloudSchedulerSourcePullSubscriptionFailed marks the condition that the
// topic is False.
func WithCloudSchedulerSourcePullSubscriptionFailed(reason, message string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkPullSubscriptionFailed(s.ConditionSet(), reason, message)
}
}
// WithCloudSchedulerSourcePullSubscriptionUnknown marks the condition that the
// topic is Unknown.
func WithCloudSchedulerSourcePullSubscriptionUnknown(reason, message string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkPullSubscriptionUnknown(s.ConditionSet(), reason, message)
}
}
// WithCloudSchedulerSourcePullSubscriptionReady marks the condition that the
// topic is ready.
func WithCloudSchedulerSourcePullSubscriptionReady(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkPullSubscriptionReady(s.ConditionSet())
}
// WithCloudSchedulerSourceJobNotReady marks the condition that the
// CloudSchedulerSource Job is not ready.
func WithCloudSchedulerSourceJobNotReady(reason, message string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkJobNotReady(reason, message)
}
}
// WithCloudSchedulerSourceJobReady marks the condition that the
// CloudSchedulerSource Job is ready and sets Status.JobName to jobName.
func WithCloudSchedulerSourceJobReady(jobName string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.MarkJobReady(jobName)
}
}
// WithCloudSchedulerSourceSinkURI sets the status for sink URI
func WithCloudSchedulerSourceSinkURI(url *apis.URL) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.SinkURI = url
}
}
func WithCloudSchedulerSourceSubscriptionID(subscriptionID string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.SubscriptionID = subscriptionID
}
}
// WithCloudSchedulerSourceJobName sets the status for job Name
func WithCloudSchedulerSourceJobName(jobName string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Status.JobName = jobName
}
}
func WithCloudSchedulerSourceFinalizers(finalizers ...string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.Finalizers = finalizers
}
}
func WithCloudSchedulerSourceAnnotations(Annotations map[string]string) CloudSchedulerSourceOption {
return func(s *v1beta1.CloudSchedulerSource) {
s.ObjectMeta.Annotations = Annotations
}
}
func WithCloudSchedulerSourceSetDefaults(s *v1beta1.CloudSchedulerSource) {
s.SetDefaults(gcpauthtesthelper.ContextWithDefaults())
}