|
| 1 | +// Copyright 2024 Stefan Prodan. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0 |
| 3 | + |
| 4 | +package reporter |
| 5 | + |
| 6 | +import ( |
| 7 | + "cmp" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/fluxcd/cli-utils/pkg/kstatus/status" |
| 12 | + "github.com/fluxcd/pkg/apis/meta" |
| 13 | + "golang.org/x/exp/slices" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 17 | + kerrors "k8s.io/apimachinery/pkg/util/errors" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + |
| 20 | + fluxcdv1 "github.com/controlplaneio-fluxcd/flux-operator/api/v1" |
| 21 | +) |
| 22 | + |
| 23 | +func (r *FluxStatusReporter) getReconcilersStatus(ctx context.Context, crds []metav1.GroupVersionKind) ([]fluxcdv1.FluxReconcilerStatus, error) { |
| 24 | + var multiErr error |
| 25 | + resStats := make([]fluxcdv1.FluxReconcilerStatus, len(crds)) |
| 26 | + for i, gvk := range crds { |
| 27 | + var total int |
| 28 | + var suspended int |
| 29 | + var failing int |
| 30 | + var totalSize int64 |
| 31 | + |
| 32 | + list := unstructured.UnstructuredList{ |
| 33 | + Object: map[string]interface{}{ |
| 34 | + "apiVersion": gvk.Group + "/" + gvk.Version, |
| 35 | + "kind": gvk.Kind, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + if err := r.List(ctx, &list, client.InNamespace("")); err == nil { |
| 40 | + total = len(list.Items) |
| 41 | + |
| 42 | + for _, item := range list.Items { |
| 43 | + if s, _, _ := unstructured.NestedBool(item.Object, "spec", "suspend"); s { |
| 44 | + suspended++ |
| 45 | + } |
| 46 | + |
| 47 | + if obj, err := status.GetObjectWithConditions(item.Object); err == nil { |
| 48 | + for _, cond := range obj.Status.Conditions { |
| 49 | + if cond.Type == meta.ReadyCondition && cond.Status == corev1.ConditionFalse { |
| 50 | + failing++ |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if size, found, _ := unstructured.NestedInt64(item.Object, "status", "artifact", "size"); found { |
| 56 | + totalSize += size |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + multiErr = kerrors.NewAggregate([]error{multiErr, err}) |
| 61 | + } |
| 62 | + |
| 63 | + resStats[i] = fluxcdv1.FluxReconcilerStatus{ |
| 64 | + APIVersion: gvk.Group + "/" + gvk.Version, |
| 65 | + Kind: gvk.Kind, |
| 66 | + Stats: fluxcdv1.FluxReconcilerStats{ |
| 67 | + Running: total - suspended, |
| 68 | + Failing: failing, |
| 69 | + Suspended: suspended, |
| 70 | + TotalSize: formatSize(totalSize), |
| 71 | + }, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + slices.SortStableFunc(resStats, func(i, j fluxcdv1.FluxReconcilerStatus) int { |
| 76 | + return cmp.Compare(i.APIVersion+i.Kind, j.APIVersion+j.Kind) |
| 77 | + }) |
| 78 | + |
| 79 | + return resStats, multiErr |
| 80 | +} |
| 81 | + |
| 82 | +func formatSize(b int64) string { |
| 83 | + if b == 0 { |
| 84 | + return "" |
| 85 | + } |
| 86 | + const unit = 1024 |
| 87 | + if b < unit { |
| 88 | + return fmt.Sprintf("%d B", b) |
| 89 | + } |
| 90 | + div, exp := int64(unit), 0 |
| 91 | + for n := b / unit; n >= unit; n /= unit { |
| 92 | + div *= unit |
| 93 | + exp++ |
| 94 | + } |
| 95 | + return fmt.Sprintf("%.1f %ciB", |
| 96 | + float64(b)/float64(div), "KMGTPE"[exp]) |
| 97 | +} |
0 commit comments