/
runner.go
176 lines (142 loc) · 5.53 KB
/
runner.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
/*
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 mtping
import (
"context"
"encoding/base64"
"fmt"
"math/rand"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
cecontext "github.com/cloudevents/sdk-go/v2/context"
"github.com/google/uuid"
"github.com/robfig/cron/v3"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
kncloudevents "knative.dev/eventing/pkg/adapter/v2"
"knative.dev/eventing/pkg/adapter/v2/util/crstatusevent"
sourcesv1 "knative.dev/eventing/pkg/apis/sources/v1"
"knative.dev/eventing/pkg/observability"
)
type CronJobRunner interface {
Start(stopCh <-chan struct{})
Stop()
AddSchedule(source *sourcesv1.PingSource) cron.EntryID
RemoveSchedule(id cron.EntryID)
}
type cronJobsRunner struct {
// The cron job runner
cron cron.Cron
// client sends cloudevents.
Client cloudevents.Client
// Where to send logs
Logger *zap.SugaredLogger
// kubeClient for sending k8s events
kubeClient kubernetes.Interface
}
const (
resourceGroup = "pingsources.sources.knative.dev"
)
func NewCronJobsRunner(ceClient cloudevents.Client, kubeClient kubernetes.Interface, logger *zap.SugaredLogger, opts ...cron.Option) *cronJobsRunner {
return &cronJobsRunner{
cron: *cron.New(opts...),
Client: ceClient,
Logger: logger,
kubeClient: kubeClient,
}
}
func (a *cronJobsRunner) AddSchedule(source *sourcesv1.PingSource) cron.EntryID {
event, err := makeEvent(source)
if err != nil {
a.Logger.Error("failed to makeEvent: ", zap.Error(err))
}
ctx := context.Background()
ctx = cloudevents.ContextWithTarget(ctx, source.Status.SinkURI.String())
var kubeEventSink record.EventSink = &typedcorev1.EventSinkImpl{Interface: a.kubeClient.CoreV1().Events(source.Namespace)}
ctx = crstatusevent.ContextWithCRStatus(ctx, &kubeEventSink, "ping-source-mt-adapter", source, a.Logger.Infof)
// Simple retry configuration to be less than 1mn.
// We might want to retry more times for less-frequent schedule.
ctx = cloudevents.ContextWithRetriesExponentialBackoff(ctx, 50*time.Millisecond, 5)
metricTag := &kncloudevents.MetricTag{
Namespace: source.Namespace,
Name: source.Name,
ResourceGroup: resourceGroup,
}
// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/messaging.md#span-name
spanName := source.Status.SinkURI.String() + " send"
ctx = observability.WithSpanData(ctx, spanName, int(trace.SpanKindProducer),
observability.K8sAttributes(source.Name, source.Namespace, sourcesv1.Resource("pingsource").String()))
schedule := source.Spec.Schedule
if source.Spec.Timezone != "" {
schedule = "CRON_TZ=" + source.Spec.Timezone + " " + schedule
}
ctx = kncloudevents.ContextWithMetricTag(ctx, metricTag)
id, _ := a.cron.AddFunc(schedule, a.cronTick(ctx, event))
return id
}
func (a *cronJobsRunner) RemoveSchedule(id cron.EntryID) {
a.cron.Remove(id)
}
func (a *cronJobsRunner) Start(stopCh <-chan struct{}) {
a.cron.Start()
<-stopCh
}
func (a *cronJobsRunner) Stop() {
ctx := a.cron.Stop() // no more ticks
if ctx != nil {
// Wait for all jobs to be done.
<-ctx.Done()
}
}
func (a *cronJobsRunner) cronTick(ctx context.Context, event cloudevents.Event) func() {
return func() {
event := event.Clone()
event.SetID(uuid.New().String()) // provide an ID here so we can track it with logging
defer a.Logger.Debug("Finished sending cloudevent id: ", event.ID())
target := cecontext.TargetFrom(ctx).String()
source := event.Context.GetSource()
// Provide a delay so not all ping fired instantaneously distribute load on resources.
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) //nolint:gosec // Cryptographic randomness not necessary here.
a.Logger.Debugf("sending cloudevent id: %s, source: %s, target: %s", event.ID(), source, target)
if result := a.Client.Send(ctx, event); !cloudevents.IsACK(result) {
// Exhausted number of retries. Event is lost.
a.Logger.Error("failed to send cloudevent result: ", zap.Any("result", result),
zap.String("source", source), zap.String("target", target), zap.String("id", event.ID()))
}
}
}
func makeEvent(source *sourcesv1.PingSource) (cloudevents.Event, error) {
event := cloudevents.NewEvent()
event.SetType(sourcesv1.PingSourceEventType)
event.SetSource(sourcesv1.PingSourceSource(source.Namespace, source.Name))
if source.Spec.CloudEventOverrides != nil && source.Spec.CloudEventOverrides.Extensions != nil {
for key, override := range source.Spec.CloudEventOverrides.Extensions {
event.SetExtension(key, override)
}
}
var data interface{}
if source.Spec.DataBase64 != "" {
data, _ = base64.StdEncoding.DecodeString(source.Spec.DataBase64)
} else if source.Spec.Data != "" {
data = []byte(source.Spec.Data)
}
if data != nil {
if err := event.SetData(source.Spec.ContentType, data); err != nil {
return event, fmt.Errorf("error when SetData(%v, %v), err: %v", source.Spec.ContentType, data, err)
}
}
return event, nil
}