forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revision.go
276 lines (240 loc) · 8.01 KB
/
revision.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2020 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 v1
import (
"context"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/kmeta"
"knative.dev/serving/pkg/apis/serving"
v1 "knative.dev/serving/pkg/apis/serving/v1"
)
// RevisionOption enables further configuration of a Revision.
type RevisionOption func(*v1.Revision)
// WithRevisionDeletionTimestamp will set the DeletionTimestamp on the Revision.
func WithRevisionDeletionTimestamp(r *v1.Revision) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithInitRevConditions calls .Status.InitializeConditions() on a Revision.
func WithInitRevConditions(r *v1.Revision) {
r.Status.InitializeConditions()
}
// WithRevName sets the name of the revision
func WithRevName(name string) RevisionOption {
return func(rev *v1.Revision) {
rev.Name = name
}
}
// MarkResourceNotOwned calls the function of the same name on the Revision's status.
func MarkResourceNotOwned(kind, name string) RevisionOption {
return func(rev *v1.Revision) {
rev.Status.MarkResourcesAvailableFalse(
v1.ReasonNotOwned,
v1.ResourceNotOwnedMessage(kind, name),
)
}
}
// WithRevContainerConcurrency sets the given Revision's concurrency.
func WithRevContainerConcurrency(cc int64) RevisionOption {
return func(rev *v1.Revision) {
rev.Spec.ContainerConcurrency = &cc
}
}
// WithLogURL sets the .Status.LogURL to the expected value.
func WithLogURL(r *v1.Revision) {
r.Status.LogURL = "http://logger.io/test-uid"
}
// WithCreationTimestamp sets the Revision's timestamp to the provided time.
// TODO(mattmoor): Ideally this could be a more generic Option and use meta.Accessor,
// but unfortunately Go's type system cannot support that.
func WithCreationTimestamp(t time.Time) RevisionOption {
return func(rev *v1.Revision) {
rev.ObjectMeta.CreationTimestamp = metav1.Time{Time: t}
}
}
// WithRevisionPreserveAnnotation updates the annotation with preserve key.
func WithRevisionPreserveAnnotation() RevisionOption {
return func(rev *v1.Revision) {
rev.Annotations = kmeta.UnionMaps(rev.Annotations,
map[string]string{
serving.RevisionPreservedAnnotationKey: "true",
})
}
}
// WithRoutingStateModified updates the annotation to the provided timestamp.
func WithRoutingStateModified(t time.Time) RevisionOption {
return func(rev *v1.Revision) {
rev.Annotations = kmeta.UnionMaps(rev.Annotations,
map[string]string{
serving.RoutingStateModifiedAnnotationKey: t.UTC().Format(time.RFC3339),
})
}
}
// WithRoutingState updates the annotation to the provided timestamp.
func WithRoutingState(s v1.RoutingState, c clock.PassiveClock) RevisionOption {
return func(rev *v1.Revision) {
rev.SetRoutingState(s, c.Now())
}
}
// WithRevStatus is a generic escape hatch for creating hard-to-craft
// status orientations.
func WithRevStatus(st v1.RevisionStatus) RevisionOption {
return func(rev *v1.Revision) {
rev.Status = st
}
}
// WithImagePullSecrets updates the revision spec ImagePullSecrets to
// the provided secrets
func WithImagePullSecrets(secretName string) RevisionOption {
return func(rev *v1.Revision) {
rev.Spec.ImagePullSecrets = []corev1.LocalObjectReference{{
Name: secretName,
}}
}
}
// MarkActive calls .Status.MarkActive on the Revision.
func MarkActive(r *v1.Revision) {
r.Status.MarkActiveTrue()
}
// MarkInactive calls .Status.MarkInactive on the Revision.
func MarkInactive(reason, message string) RevisionOption {
return func(r *v1.Revision) {
r.Status.MarkActiveFalse(reason, message)
}
}
// MarkActivating calls .Status.MarkActivating on the Revision.
func MarkActivating(reason, message string) RevisionOption {
return func(r *v1.Revision) {
r.Status.MarkActiveUnknown(reason, message)
}
}
// MarkDeploying calls .Status.MarkDeploying on the Revision.
func MarkDeploying(reason string) RevisionOption {
return func(r *v1.Revision) {
r.Status.MarkResourcesAvailableUnknown(reason, "")
r.Status.MarkContainerHealthyUnknown(reason, "")
}
}
// MarkProgressDeadlineExceeded calls the method of the same name on the Revision
// with the message we expect the Revision Reconciler to pass.
func MarkProgressDeadlineExceeded(message string) RevisionOption {
return func(r *v1.Revision) {
r.Status.MarkResourcesAvailableFalse(
v1.ReasonProgressDeadlineExceeded,
message,
)
}
}
// MarkContainerMissing calls .Status.MarkContainerMissing on the Revision.
func MarkContainerMissing(rev *v1.Revision) {
rev.Status.MarkContainerHealthyFalse(v1.ReasonContainerMissing, "It's the end of the world as we know it")
}
// MarkContainerExiting calls .Status.MarkContainerExiting on the Revision.
func MarkContainerExiting(exitCode int32, message string) RevisionOption {
return func(r *v1.Revision) {
r.Status.MarkContainerHealthyFalse(v1.ExitCodeReason(exitCode), message)
}
}
// MarkResourcesUnavailable calls .Status.MarkResourcesUnavailable on the Revision.
func MarkResourcesUnavailable(reason, message string) RevisionOption {
return func(r *v1.Revision) {
r.Status.MarkResourcesAvailableFalse(reason, message)
}
}
// MarkRevisionReady calls the necessary helpers to make the Revision Ready=True.
func MarkRevisionReady(r *v1.Revision) {
WithInitRevConditions(r)
MarkActive(r)
r.Status.MarkResourcesAvailableTrue()
r.Status.MarkContainerHealthyTrue()
r.Status.ObservedGeneration = r.Generation
}
// WithRevisionLabel attaches a particular label to the revision.
func WithRevisionLabel(key, value string) RevisionOption {
return func(rev *v1.Revision) {
rev.Labels = kmeta.UnionMaps(rev.Labels, map[string]string{key: value})
}
}
// WithRevisionAnn attaches a particular label to the revision.
func WithRevisionAnn(key, value string) RevisionOption {
return func(rev *v1.Revision) {
rev.Annotations = kmeta.UnionMaps(rev.Annotations, map[string]string{key: value})
}
}
// WithContainerStatuses sets the .Status.ContainerStatuses to the Revision.
func WithContainerStatuses(containerStatus []v1.ContainerStatus) RevisionOption {
return func(r *v1.Revision) {
r.Status.ContainerStatuses = containerStatus
}
}
// WithRevisionObservedGeneration sets the observed generation on the
// revision status.
func WithRevisionObservedGeneration(gen int64) RevisionOption {
return func(r *v1.Revision) {
r.Status.ObservedGeneration = gen
}
}
func WithRevisionInitContainers() RevisionOption {
return func(r *v1.Revision) {
r.Spec.InitContainers = []corev1.Container{{
Name: "init1",
Image: "initimage",
}, {
Name: "init2",
Image: "initimage",
}}
}
}
func WithRevisionPVC() RevisionOption {
return func(r *v1.Revision) {
r.Spec.Volumes = []corev1.Volume{{
Name: "claimvolume",
VolumeSource: corev1.VolumeSource{PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "myclaim",
ReadOnly: false,
}}},
}
r.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{{
Name: "claimvolume",
MountPath: "/data",
}}
}
}
// Revision creates a revision object with given ns/name and options.
func Revision(namespace, name string, ro ...RevisionOption) *v1.Revision {
r := &v1.Revision{
ObjectMeta: metav1.ObjectMeta{
SelfLink: "/apis/serving/v1/namespaces/test/revisions/" + name,
Name: name,
Namespace: namespace,
UID: "test-uid",
Generation: 1,
},
Spec: v1.RevisionSpec{
PodSpec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: name,
Image: "busybox",
}},
},
},
}
r.SetDefaults(context.Background())
for _, opt := range ro {
opt(r)
}
return r
}