-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcidn.go
More file actions
171 lines (157 loc) · 3.84 KB
/
Copy pathcidn.go
File metadata and controls
171 lines (157 loc) · 3.84 KB
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
package httpmirror
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
"strings"
"github.com/OpenCIDN/cidn/pkg/apis/task/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)
func getBlobName(urlPath string) string {
m := md5.Sum([]byte(urlPath))
return hex.EncodeToString(m[:])
}
func formatGroup(s string) string {
u, err := url.Parse(s)
if err != nil {
return "unknown"
}
parts := strings.SplitN(u.Path, "/", 3)
if len(parts) > 1 && parts[1] != "" {
u.Path = "/" + parts[1]
} else {
u.Path = ""
}
return u.String()
}
func (m *MirrorHandler) cacheFileWithCIDN(ctx context.Context, sourceFile, cacheFile string) error {
blobs := m.CIDNClient.TaskV1alpha1().Blobs()
name := getBlobName(cacheFile)
blob, err := m.CIDNBlobInformer.Lister().Get(name)
if err != nil {
if !apierrors.IsNotFound(err) {
if m.Logger != nil {
m.Logger.Println("Error getting blob from informer:", err)
}
return err
}
blob, err = blobs.Create(ctx, &v1alpha1.Blob{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: map[string]string{
v1alpha1.WebuiDisplayNameAnnotation: sourceFile,
v1alpha1.ReleaseTTLAnnotation: "1h",
v1alpha1.WebuiTagAnnotation: "file",
v1alpha1.WebuiGroupAnnotation: formatGroup(sourceFile),
},
},
Spec: v1alpha1.BlobSpec{
MaximumRunning: m.CIDNMaximumRunning,
MaximumPending: 1,
MinimumChunkSize: m.CIDNMinimumChunkSize,
MaximumRetry: 2,
Source: []v1alpha1.BlobSource{
{
URL: sourceFile,
},
},
Destination: []v1alpha1.BlobDestination{
{
Name: m.CIDNDestination,
Path: cacheFile,
SkipIfExists: true,
},
},
},
}, metav1.CreateOptions{})
if err != nil &&
!apierrors.IsAlreadyExists(err) {
return err
}
}
switch blob.Status.Phase {
case v1alpha1.BlobPhaseSucceeded:
return nil
case v1alpha1.BlobPhaseFailed:
errorMsg := "blob sync failed"
for _, condition := range blob.Status.Conditions {
if condition.Message != "" {
errorMsg = condition.Message
break
}
}
return fmt.Errorf("failed: %s: %w", errorMsg, ErrNotOK)
}
// Create a channel to receive blob status updates
statusChan := make(chan *v1alpha1.Blob, 1)
defer close(statusChan)
// Add event handler to watch for blob status changes
handler := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
blob, ok := obj.(*v1alpha1.Blob)
if !ok {
return
}
if blob.Name == name {
statusChan <- blob
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
oldBlob, ok := oldObj.(*v1alpha1.Blob)
if !ok {
return
}
newBlob, ok := newObj.(*v1alpha1.Blob)
if !ok {
return
}
if newBlob.Name == name && oldBlob.Status.Phase != newBlob.Status.Phase {
statusChan <- newBlob
}
},
DeleteFunc: func(obj interface{}) {
blob, ok := obj.(*v1alpha1.Blob)
if !ok {
return
}
if blob.Name == name {
statusChan <- nil
}
},
}
rer, err := m.CIDNBlobInformer.Informer().AddEventHandler(handler)
if err != nil {
return err
}
defer m.CIDNBlobInformer.Informer().RemoveEventHandler(rer)
for {
select {
case updatedBlob, ok := <-statusChan:
if !ok {
return fmt.Errorf("blob was cancel before completion")
}
if updatedBlob == nil {
return fmt.Errorf("blob was deleted before completion")
}
switch updatedBlob.Status.Phase {
case v1alpha1.BlobPhaseSucceeded:
return nil
case v1alpha1.BlobPhaseFailed:
errorMsg := "blob sync failed"
for _, condition := range updatedBlob.Status.Conditions {
if condition.Message != "" {
errorMsg = condition.Message
break
}
}
return fmt.Errorf("failed: %s: %w", errorMsg, ErrNotOK)
}
case <-ctx.Done():
return ctx.Err()
}
}
}