forked from kubesphere/kubesphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm_repo_controller.go
309 lines (266 loc) · 9.07 KB
/
helm_repo_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
/*
Copyright 2019 The KubeSphere 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 helmrepo
import (
"context"
"math"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"k8s.io/utils/strings"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/hzhhong/kubesphere/pkg/apis/application/v1alpha1"
"github.com/hzhhong/kubesphere/pkg/simple/client/openpitrix/helmrepoindex"
"github.com/hzhhong/kubesphere/pkg/utils/sliceutil"
)
const (
// min sync period in seconds
MinSyncPeriod = 180
MinRetryDuration = 60
MaxRetryDuration = 600
HelmRepoSyncStateLen = 10
StateSuccess = "successful"
StateFailed = "failed"
MessageLen = 512
)
const (
HelmRepoFinalizer = "helmrepo.application.kubesphere.io"
)
// Add creates a new Workspace Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileHelmRepo{Client: mgr.GetClient(), scheme: mgr.GetScheme(),
recorder: mgr.GetEventRecorderFor("workspace-controller"),
config: mgr.GetConfig(),
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("helm-repo-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to HelmRelease
err = c.Watch(&source.Kind{Type: &v1alpha1.HelmRepo{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileHelmRepo{}
// ReconcileWorkspace reconciles a Workspace object
type ReconcileHelmRepo struct {
client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
config *rest.Config
}
// Reconcile reads that state of the cluster for a helmrepoes object and makes changes based on the state read
// and what is in the helmreleases.Spec
// +kubebuilder:rbac:groups=application.kubesphere.io,resources=helmrepos,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=application.kubesphere.io,resources=helmrepos/status,verbs=get;update;patch
func (r *ReconcileHelmRepo) Reconcile(request reconcile.Request) (reconcile.Result, error) {
start := time.Now()
klog.Infof("sync repo: %s", request.Name)
defer func() {
klog.Infof("sync repo end: %s, elapsed: %v", request.Name, time.Now().Sub(start))
}()
// Fetch the helmrepoes instance
instance := &v1alpha1.HelmRepo{}
err := r.Client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
if instance.ObjectMeta.DeletionTimestamp.IsZero() {
if instance.Status.State == "" {
instance.Status.State = v1alpha1.RepoStateSyncing
return reconcile.Result{}, r.Status().Update(context.Background(), instance)
}
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object.
if !sliceutil.HasString(instance.ObjectMeta.Finalizers, HelmRepoFinalizer) {
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, HelmRepoFinalizer)
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
} else {
// The object is being deleted
if sliceutil.HasString(instance.ObjectMeta.Finalizers, HelmRepoFinalizer) {
// remove our finalizer from the list and update it.
instance.ObjectMeta.Finalizers = sliceutil.RemoveString(instance.ObjectMeta.Finalizers, func(item string) bool {
if item == HelmRepoFinalizer {
return true
}
return false
})
if err := r.Update(context.Background(), instance); err != nil {
return reconcile.Result{}, err
}
}
return reconcile.Result{}, nil
}
copyInstance := instance.DeepCopy()
if copyInstance.Spec.SyncPeriod != 0 && copyInstance.Spec.SyncPeriod < MinSyncPeriod {
copyInstance.Spec.SyncPeriod = MinSyncPeriod
}
retryAfter := 0
if syncNow, after := needReSyncNow(copyInstance); syncNow {
// sync repo
syncErr := r.syncRepo(copyInstance)
state := copyInstance.Status.SyncState
now := metav1.Now()
if syncErr != nil {
// failed
state = append([]v1alpha1.HelmRepoSyncState{{
State: v1alpha1.RepoStateFailed,
Message: strings.ShortenString(syncErr.Error(), MessageLen),
SyncTime: &now,
}}, state...)
copyInstance.Status.State = v1alpha1.RepoStateFailed
} else {
state = append([]v1alpha1.HelmRepoSyncState{{
State: v1alpha1.RepoStateSuccessful,
SyncTime: &now,
}}, state...)
copyInstance.Status.Version = instance.Spec.Version
copyInstance.Status.State = v1alpha1.RepoStateSuccessful
}
copyInstance.Status.LastUpdateTime = &now
if len(state) > HelmRepoSyncStateLen {
state = state[0:HelmRepoSyncStateLen]
}
copyInstance.Status.SyncState = state
err = r.Client.Status().Update(context.TODO(), copyInstance)
if err != nil {
klog.Errorf("update status failed, error: %s", err)
return reconcile.Result{
RequeueAfter: MinRetryDuration * time.Second,
}, err
} else {
retryAfter = MinSyncPeriod
if syncErr == nil {
retryAfter = copyInstance.Spec.SyncPeriod
}
}
} else {
retryAfter = after
}
return reconcile.Result{
RequeueAfter: time.Duration(retryAfter) * time.Second,
}, nil
}
// needReSyncNow checks instance whether need resync now
// if resync is true, it should resync not
// if resync is false and after > 0, it should resync in after seconds
func needReSyncNow(instance *v1alpha1.HelmRepo) (syncNow bool, after int) {
now := time.Now()
if instance.Status.SyncState == nil || len(instance.Status.SyncState) == 0 {
return true, 0
}
states := instance.Status.SyncState
failedTimes := 0
for i := range states {
if states[i].State != StateSuccess {
failedTimes += 1
} else {
break
}
}
state := states[0]
if instance.Spec.Version != instance.Status.Version && failedTimes == 0 {
// repo has a successful synchronization
diff := now.Sub(state.SyncTime.Time) / time.Second
if diff > 0 && diff < MinRetryDuration {
return false, int(math.Max(10, float64(MinRetryDuration-diff)))
} else {
return true, 0
}
}
period := 0
if state.State != StateSuccess {
period = MinRetryDuration * failedTimes
if period > MaxRetryDuration {
period = MaxRetryDuration
}
if now.After(state.SyncTime.Add(time.Duration(period) * time.Second)) {
return true, 0
}
} else {
period = instance.Spec.SyncPeriod
if period != 0 {
if period < MinSyncPeriod {
period = MinSyncPeriod
}
if now.After(state.SyncTime.Add(time.Duration(period) * time.Second)) {
return true, 0
}
} else {
// need not to sync
return false, 0
}
}
after = int(state.SyncTime.Time.Add(time.Duration(period) * time.Second).Sub(now).Seconds())
// may be less than 10 second
if after <= 10 {
after = 10
}
return false, after
}
func (r *ReconcileHelmRepo) syncRepo(instance *v1alpha1.HelmRepo) error {
// 1. load index from helm repo
index, err := helmrepoindex.LoadRepoIndex(context.TODO(), instance.Spec.Url, &instance.Spec.Credential)
if err != nil {
klog.Errorf("load index failed, repo: %s, url: %s, err: %s", instance.GetTrueName(), instance.Spec.Url, err)
return err
}
existsSavedIndex := &helmrepoindex.SavedIndex{}
if len(instance.Status.Data) != 0 {
existsSavedIndex, err = helmrepoindex.ByteArrayToSavedIndex([]byte(instance.Status.Data))
if err != nil {
klog.Errorf("json unmarshal failed, repo: %s, error: %s", instance.GetTrueName(), err)
return err
}
}
// 2. merge new index with old index which is stored in crd
savedIndex := helmrepoindex.MergeRepoIndex(index, existsSavedIndex)
// 3. save index in crd
data, err := savedIndex.Bytes()
if err != nil {
klog.Errorf("json marshal failed, error: %s", err)
return err
}
instance.Status.Data = string(data)
return nil
}