-
Notifications
You must be signed in to change notification settings - Fork 35
/
rss_controller.go
369 lines (328 loc) · 10.7 KB
/
rss_controller.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright 2022 The open-podcasts Authors. All rights reserved.
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 controllers
import (
"context"
"fmt"
"github.com/SlyMarbo/rss"
strip "github.com/grokify/html-strip-tags-go"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"strings"
"time"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
v1alpha1 "github.com/opensource-f2f/open-podcasts/api/v1alpha1"
)
// RSSReconciler reconciles a RSS object
type RSSReconciler struct {
client.Client
Scheme *runtime.Scheme
recorder record.EventRecorder
}
//+kubebuilder:rbac:groups=osf2f.my.domain,resources=rsses,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=osf2f.my.domain,resources=rsses/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=osf2f.my.domain,resources=rsses/finalizers,verbs=update
//+kubebuilder:rbac:groups=osf2f.my.domain,resources=episodes,verbs=get;create;update;patch;delete
//+kubebuilder:rbac:groups=osf2f.my.domain,resources=categories,verbs=get;list;watch;create;update;patch
//+kubebuilder:rbac:groups=osf2f.my.domain,resources=authors,verbs=get;list;watch;create;update;patch
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.0/pkg/reconcile
func (r *RSSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
_ = log.FromContext(ctx)
rssObj := &v1alpha1.RSS{}
if err = r.Client.Get(ctx, req.NamespacedName, rssObj); err != nil {
err = client.IgnoreNotFound(err)
return
}
if err = r.linkToCategory(rssObj); err != nil {
return
}
//if err = r.linkToAuthor(rssObj); err != nil {
// return
//}
address := rssObj.Spec.Address
if address == "" {
result = ctrl.Result{RequeueAfter: time.Minute}
err = r.errorAndRecord(rssObj, v1.EventTypeWarning, "Failed to fetch RSS",
fmt.Sprintf("the address of the RSS: %s is empty", req.NamespacedName.String()))
return
}
result, err = r.fetchByRSS(address, rssObj)
return
}
//func (r *RSSReconciler) linkToAuthor(rss *v1alpha1.RSS) (err error) {
// ctx := context.Background()
// authorName := rss.Spec.Author
//
// py := pinyin.New()
// py.Split = "-"
// py.Upper = false
//
// authorNamePy, _ := py.Convert(authorName)
//
// author := &v1alpha1.Author{
// Spec: v1alpha1.AuthorSpec{
// Name: authorName,
// },
// }
// author.SetName(authorNamePy)
// AddOwnerReference(author, rss.TypeMeta, rss.ObjectMeta)
//
// if err = r.Get(ctx, types.NamespacedName{Name: authorNamePy}, author); err != nil {
// if errors.IsNotFound(err) {
// // create it if is not found
// err = r.Create(ctx, author)
// }
// } else {
// AddOwnerReference(author, rss.TypeMeta, rss.ObjectMeta)
// err = r.Update(ctx, author)
// }
// return
//}
func (r *RSSReconciler) linkToCategory(rss *v1alpha1.RSS) (err error) {
ctx := context.Background()
categories := rss.Spec.Categories
if len(categories) == 0 {
return
}
for i := range categories {
categoryName := strings.ToLower(categories[i])
category := &v1alpha1.Category{}
category.SetName(categoryName)
AddOwnerReference(category, rss.TypeMeta, rss.ObjectMeta)
if err = r.Get(ctx, types.NamespacedName{Name: categoryName}, category); err != nil {
if errors.IsNotFound(err) {
// create it if is not found
err = r.Create(ctx, category)
} else {
return
}
} else {
AddOwnerReference(category, rss.TypeMeta, rss.ObjectMeta)
err = r.Update(ctx, category)
}
}
return
}
func AddOwnerReference(object metav1.Object, typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) {
SetOwnerReference(object, metav1.OwnerReference{
Kind: typeMeta.Kind,
APIVersion: typeMeta.APIVersion,
Name: objectMeta.Name,
UID: objectMeta.UID,
})
}
func SetOwnerReference(object metav1.Object, ownerRef metav1.OwnerReference) {
allRefs := object.GetOwnerReferences()
if len(allRefs) == 0 {
object.SetOwnerReferences([]metav1.OwnerReference{ownerRef})
} else {
for i, ref := range allRefs {
if ref.Name == ownerRef.Name && ref.Kind == ownerRef.Kind &&
ref.APIVersion == ownerRef.APIVersion {
allRefs[i] = ownerRef
return
}
}
object.SetOwnerReferences(append(object.GetOwnerReferences(), ownerRef))
}
}
func (r *RSSReconciler) fetchByRSS(address string, rssObject *v1alpha1.RSS) (result ctrl.Result, err error) {
var feed *rss.Feed
if feed, err = rss.Fetch(address); err != nil {
err = r.errorAndRecord(rssObject, v1.EventTypeWarning, "Failed to fetch RSS",
fmt.Sprintf("failed to fetch RSS by address: %s, error is %v", address, err))
return
}
rssObject.Spec.Title = strings.TrimSpace(feed.Title)
rssObject.Spec.Language = feed.Language
rssObject.Spec.Author = strings.TrimSpace(feed.Author)
rssObject.Spec.Description = strings.TrimSpace(strip.StripTags(feed.Description))
rssObject.Spec.Link = getFixedLink(address, feed)
rssObject.Spec.Categories = removeDuplicateStr(feed.Categories)
if feed.Image != nil {
if feed.Image.URL != "" {
rssObject.Spec.Image = feed.Image.URL
} else if feed.Image.Href != "" {
rssObject.Spec.Image = feed.Image.Href
}
}
if err = r.updateRSS(rssObject.DeepCopy()); err != nil {
return
}
if err = r.storeEpisodes(feed.Items, rssObject.ObjectMeta); err == nil {
err = r.setLastUpdateTime(rssObject.Namespace, rssObject.Name)
}
if err == nil {
result = ctrl.Result{
RequeueAfter: feed.Refresh.Sub(time.Now()),
}
}
return
}
func removeDuplicateStr(strSlice []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func getFixedLink(source string, feed *rss.Feed) string {
if strings.HasPrefix(source, feed.Link) && strings.HasPrefix(source, "http://www.ximalaya.com") {
return strings.TrimSuffix(source, ".xml")
}
return feed.Link
}
func (r *RSSReconciler) updateRSS(newRSS *v1alpha1.RSS) (err error) {
existingRSS := &v1alpha1.RSS{}
if err = r.Client.Get(context.Background(), types.NamespacedName{
Namespace: newRSS.Namespace,
Name: newRSS.Name,
}, existingRSS); err != nil {
return
}
existingRSS.Spec = newRSS.Spec
err = r.Client.Update(context.Background(), existingRSS)
return
}
func (r *RSSReconciler) setLastUpdateTime(ns, name string) (err error) {
rssObj := &v1alpha1.RSS{}
if err = r.Client.Get(context.Background(), types.NamespacedName{
Namespace: ns,
Name: name,
}, rssObj); err != nil {
err = client.IgnoreNotFound(err)
return
}
rssObj.Status.LastUpdateTime = metav1.NewTime(time.Now())
err = r.Client.Status().Update(context.Background(), rssObj)
return
}
func (r *RSSReconciler) storeEpisodes(items []*rss.Item, meta metav1.ObjectMeta) (err error) {
for i, _ := range items {
rssMeta := meta.DeepCopy()
episodeName := fmt.Sprintf("%s-%d", meta.Name, i)
var created bool
if created, err = r.storeEpisode(items[i], rssMeta, episodeName); err != nil {
return
}
if created {
go func() {
time.Sleep(time.Second * 3)
_ = r.recordNewEpisodeEvent(meta.Name, episodeName, meta.Namespace)
}()
}
}
return
}
func (r *RSSReconciler) recordNewEpisodeEvent(rssName, episodeName, ns string) (err error) {
rssObj := &v1alpha1.RSS{}
if err = r.Get(context.Background(), types.NamespacedName{
Namespace: ns,
Name: rssName,
}, rssObj); err == nil {
episode := &v1alpha1.Episode{}
if err = r.Get(context.Background(), types.NamespacedName{
Namespace: ns,
Name: episodeName,
}, episode); err == nil {
r.recorder.Eventf(rssObj, v1.EventTypeNormal, episodeName, "《%s》更新节目了: '%s'",
rssObj.Spec.Title, episode.Spec.Title)
}
}
return
}
func (r *RSSReconciler) storeEpisode(item *rss.Item, meta *metav1.ObjectMeta, episodeName string) (created bool, err error) {
episode := &v1alpha1.Episode{}
if err = r.Client.Get(context.Background(), types.NamespacedName{
Namespace: meta.Namespace,
Name: episodeName,
}, episode); err != nil {
episode := &v1alpha1.Episode{
ObjectMeta: metav1.ObjectMeta{
Namespace: meta.Namespace,
Name: episodeName,
Labels: map[string]string{
"rss": meta.Name,
},
OwnerReferences: []metav1.OwnerReference{{
Name: meta.Name,
UID: meta.UID,
Kind: "RSS",
APIVersion: "osf2f.my.domain/v1alpha1",
}},
},
}
created = true
updateEpisode(episode, item)
err = r.Client.Create(context.Background(), episode)
} else {
episode.OwnerReferences = []metav1.OwnerReference{{
Name: meta.Name,
UID: meta.UID,
Kind: "RSS",
APIVersion: "osf2f.my.domain/v1alpha1",
}}
episode.Labels = map[string]string{
"rss": meta.Name,
}
updateEpisode(episode, item)
err = r.Client.Update(context.Background(), episode)
}
return
}
func updateEpisode(episode *v1alpha1.Episode, item *rss.Item) {
episode.Spec.Title = item.Title
episode.Spec.Summary = strings.TrimSpace(strip.StripTags(item.Summary))
episode.Spec.Content = item.Content
episode.Spec.Link = item.Link
episode.Spec.Date = metav1.NewTime(item.Date)
if len(item.Enclosures) > 0 {
episode.Spec.AudioSource = item.Enclosures[0].URL
episode.Spec.AudioType = item.Enclosures[0].Type
episode.Spec.AudioLength = item.Enclosures[0].Length
}
if item.Image != nil {
if item.Image.URL != "" {
episode.Spec.CoverImage = item.Image.URL
} else if item.Image.Href != "" {
episode.Spec.CoverImage = item.Image.Href
}
}
}
// SetupWithManager sets up the controller with the Manager.
func (r *RSSReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.recorder = mgr.GetEventRecorderFor("rss")
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.RSS{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Complete(r)
}
func (r *RSSReconciler) errorAndRecord(object runtime.Object, eventType, reason, msg string) error {
r.recorder.Eventf(object, eventType, reason, msg)
return fmt.Errorf(msg)
}