|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// To display the loading progress, code from CDI |
| 18 | +// (https://github.com/kubevirt/containerized-data-importer/blob/main/pkg/util/prometheus/prometheus.go) |
| 19 | +// was used. However, due to our modifications, we could not utilize the embedding |
| 20 | +// mechanism, so we had to resort to copying. |
| 21 | + |
| 22 | +package monitoring |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "io" |
| 28 | + "time" |
| 29 | + |
| 30 | + "k8s.io/klog/v2" |
| 31 | + |
| 32 | + "kubevirt.io/containerized-data-importer/pkg/util" |
| 33 | +) |
| 34 | + |
| 35 | +// ProgressReader is a counting reader that reports progress to prometheus. |
| 36 | +type ProgressReader struct { |
| 37 | + util.CountingReader |
| 38 | + metric ProgressMetric |
| 39 | + total uint64 |
| 40 | + final bool |
| 41 | +} |
| 42 | + |
| 43 | +// NewProgressReader creates a new instance of a prometheus updating progress reader. |
| 44 | +func NewProgressReader(r io.ReadCloser, metric ProgressMetric, total uint64) *ProgressReader { |
| 45 | + promReader := &ProgressReader{ |
| 46 | + CountingReader: util.CountingReader{ |
| 47 | + Reader: r, |
| 48 | + Current: 0, |
| 49 | + }, |
| 50 | + metric: metric, |
| 51 | + total: total, |
| 52 | + final: true, |
| 53 | + } |
| 54 | + |
| 55 | + return promReader |
| 56 | +} |
| 57 | + |
| 58 | +// StartTimedUpdate starts the update timer to automatically update every second. |
| 59 | +func (r *ProgressReader) StartTimedUpdate(ctx context.Context) { |
| 60 | + // Start the progress update thread. |
| 61 | + go r.timedUpdateProgress(ctx) |
| 62 | +} |
| 63 | + |
| 64 | +func (r *ProgressReader) timedUpdateProgress(ctx context.Context) { |
| 65 | + for { |
| 66 | + select { |
| 67 | + case <-ctx.Done(): |
| 68 | + return |
| 69 | + case <-time.After(time.Second): |
| 70 | + cont := r.updateProgress() |
| 71 | + if !cont { |
| 72 | + return |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (r *ProgressReader) updateProgress() bool { |
| 79 | + if r.total > 0 { |
| 80 | + finished := r.final && r.Done |
| 81 | + currentProgress := 100.0 |
| 82 | + if !finished && r.Current < r.total { |
| 83 | + currentProgress = float64(r.Current) / float64(r.total) * 100.0 |
| 84 | + } |
| 85 | + progress, err := r.metric.Get() |
| 86 | + if err != nil { |
| 87 | + klog.Errorf("updateProgress: failed to read metric; %v", err) |
| 88 | + return true // true ==> to try again // todo - how to avoid endless loop in case it's a constant error? |
| 89 | + } |
| 90 | + if currentProgress > progress { |
| 91 | + r.metric.Add(currentProgress - progress) |
| 92 | + } |
| 93 | + klog.V(1).Infoln(fmt.Sprintf("%.2f", currentProgress)) |
| 94 | + return !finished |
| 95 | + } |
| 96 | + return false |
| 97 | +} |
0 commit comments