forked from knative/serving
/
main.go
295 lines (254 loc) · 8.5 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
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 main
import (
"context"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"github.com/ghodss/yaml"
"github.com/google/mako/go/quickstore"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/kmeta"
"knative.dev/pkg/ptr"
"knative.dev/pkg/signals"
"knative.dev/pkg/test/mako"
asv1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1"
netv1alpha1 "knative.dev/serving/pkg/apis/networking/v1alpha1"
"knative.dev/serving/pkg/apis/serving/v1beta1"
servingclient "knative.dev/serving/pkg/client/injection/client"
)
var (
template = flag.String("template", "", "The service template to load from kodata/")
duration = flag.Duration("duration", 25*time.Minute, "The duration of the benchmark to run.")
frequency = flag.Duration("frequency", 5*time.Second, "The frequency at which to create services.")
)
func readTemplate() (*v1beta1.Service, error) {
path := filepath.Join(os.Getenv("KO_DATA_PATH"), *template+"-template.yaml")
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
svc := &v1beta1.Service{}
if err := yaml.Unmarshal([]byte(b), svc); err != nil {
return nil, err
}
svc.OwnerReferences = []metav1.OwnerReference{{
APIVersion: "v1",
Kind: "Pod",
Name: os.Getenv("POD_NAME"),
UID: types.UID(os.Getenv("POD_UID")),
Controller: ptr.Bool(true),
BlockOwnerDeletion: ptr.Bool(true),
}}
return svc, nil
}
func handle(q *quickstore.Quickstore, svc kmeta.Accessor, status duckv1.Status,
seen *sets.String, metric string) {
if seen.Has(svc.GetName()) {
return
}
cc := status.GetCondition(apis.ConditionReady)
if cc == nil || cc.Status == corev1.ConditionUnknown {
return
}
seen.Insert(svc.GetName())
created := svc.GetCreationTimestamp().Time
ready := cc.LastTransitionTime.Inner.Time
elapsed := ready.Sub(created)
if cc.Status == corev1.ConditionTrue {
q.AddSamplePoint(mako.XTime(created), map[string]float64{
metric: elapsed.Seconds(),
})
log.Printf("Ready: %s", svc.GetName())
} else if cc.Status == corev1.ConditionFalse {
q.AddError(mako.XTime(created), cc.Message)
log.Printf("Not Ready: %s; %s: %s", svc.GetName(), cc.Reason, cc.Message)
}
}
func main() {
flag.Parse()
// We want this for properly handling Kubernetes container lifecycle events.
ctx := signals.NewContext()
tmpl, err := readTemplate()
if err != nil {
log.Fatalf("Unable to read template %s: %v", *template, err)
}
// We cron every 30 minutes, so make sure that we don't severely overrun to
// limit how noisy a neighbor we can be.
ctx, cancel := context.WithTimeout(ctx, *duration)
defer cancel()
// Tag this run with the various flag values.
tags := []string{
"template=" + *template,
"duration=" + duration.String(),
"frequency=" + frequency.String(),
}
mc, err := mako.Setup(ctx, tags...)
if err != nil {
log.Fatalf("Failed to setup mako: %v", err)
}
q, qclose, ctx := mc.Quickstore, mc.ShutDownFunc, mc.Context
// Use a fresh context here so that our RPC to terminate the sidecar
// isn't subject to our timeout (or we won't shut it down when we time out)
defer qclose(context.Background())
sc := servingclient.Get(ctx)
cleanup := func() error {
return sc.ServingV1beta1().Services(tmpl.Namespace).DeleteCollection(
&metav1.DeleteOptions{}, metav1.ListOptions{})
}
defer cleanup()
// Wrap fatalf in a helper or our sidecar will live forever.
fatalf := func(f string, args ...interface{}) {
qclose(context.Background())
cleanup()
log.Fatalf(f, args...)
}
// Set up the threshold analyzers for the selected benchmark. This will
// cause Mako/Quickstore to analyze the results we are storing and flag
// things that are outside of expected bounds.
q.Input.ThresholdInputs = append(q.Input.ThresholdInputs,
newDeploy95PercentileLatency(tags...),
newReadyDeploymentCount(tags...),
)
if err := cleanup(); err != nil {
fatalf("Error cleaning up services: %v", err)
}
lo := metav1.ListOptions{TimeoutSeconds: ptr.Int64(int64(duration.Seconds()))}
// TODO(mattmoor): We could maybe use a duckv1.KResource to eliminate this boilerplate.
serviceWI, err := sc.ServingV1beta1().Services(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch services: %v", err)
}
defer serviceWI.Stop()
serviceSeen := sets.String{}
configurationWI, err := sc.ServingV1beta1().Configurations(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch configurations: %v", err)
}
defer configurationWI.Stop()
configurationSeen := sets.String{}
routeWI, err := sc.ServingV1beta1().Routes(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch routes: %v", err)
}
defer routeWI.Stop()
routeSeen := sets.String{}
revisionWI, err := sc.ServingV1beta1().Revisions(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch revisions: %v", err)
}
defer revisionWI.Stop()
revisionSeen := sets.String{}
ingressWI, err := sc.NetworkingV1alpha1().Ingresses(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch ingresss: %v", err)
}
defer ingressWI.Stop()
ingressSeen := sets.String{}
sksWI, err := sc.NetworkingV1alpha1().ServerlessServices(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch skss: %v", err)
}
defer sksWI.Stop()
sksSeen := sets.String{}
paWI, err := sc.AutoscalingV1alpha1().PodAutoscalers(tmpl.Namespace).Watch(lo)
if err != nil {
fatalf("Unable to watch pas: %v", err)
}
defer paWI.Stop()
paSeen := sets.String{}
tick := time.NewTicker(*frequency)
func() {
for {
select {
case <-ctx.Done():
// If we timeout or the pod gets shutdown via SIGTERM then start to
// clean thing up.
return
case ts := <-tick.C:
svc, err := sc.ServingV1beta1().Services(tmpl.Namespace).Create(tmpl)
if err != nil {
q.AddError(mako.XTime(ts), err.Error())
log.Printf("Error creating service: %v", err)
break
}
log.Printf("Created: %s", svc.Name)
case event := <-serviceWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
svc := event.Object.(*v1beta1.Service)
handle(q, svc, svc.Status.Status, &serviceSeen, "dl")
case event := <-configurationWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
cfg := event.Object.(*v1beta1.Configuration)
handle(q, cfg, cfg.Status.Status, &configurationSeen, "cl")
case event := <-routeWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
rt := event.Object.(*v1beta1.Route)
handle(q, rt, rt.Status.Status, &routeSeen, "rl")
case event := <-revisionWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
rev := event.Object.(*v1beta1.Revision)
handle(q, rev, rev.Status.Status, &revisionSeen, "rvl")
case event := <-ingressWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
ing := event.Object.(*netv1alpha1.Ingress)
handle(q, ing, ing.Status.Status, &ingressSeen, "il")
case event := <-sksWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
ing := event.Object.(*netv1alpha1.ServerlessService)
handle(q, ing, ing.Status.Status, &sksSeen, "sksl")
case event := <-paWI.ResultChan():
if event.Type != watch.Modified {
// Skip events other than modifications
break
}
pa := event.Object.(*asv1alpha1.PodAutoscaler)
handle(q, pa, pa.Status.Status, &paSeen, "pal")
}
}
}()
// Commit this benchmark run to Mako!
out, err := q.Store()
if err != nil {
fatalf("q.Store error: %v: %v", out, err)
}
log.Printf("Done! Run: %s\n", out.GetRunChartLink())
}