From cec1512c64f70708394401f0d1326bc3709de84a Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 21 Jun 2024 11:42:48 +0300 Subject: [PATCH] Add sync id and path to report Signed-off-by: Stefan Prodan --- api/v1/fluxreport_types.go | 8 ++++++++ .../crd/bases/fluxcd.controlplane.io_fluxreports.yaml | 7 +++++++ .../flux-operator.clusterserviceversion.yaml | 6 ++++-- docs/api/v1/fluxreport.md | 11 ++++++++--- internal/controller/fluxreport_controller_test.go | 1 + internal/reporter/reporter.go | 2 +- internal/reporter/sync.go | 9 +++++++++ 7 files changed, 38 insertions(+), 6 deletions(-) diff --git a/api/v1/fluxreport_types.go b/api/v1/fluxreport_types.go index 46ab93a..cb2214e 100644 --- a/api/v1/fluxreport_types.go +++ b/api/v1/fluxreport_types.go @@ -111,6 +111,14 @@ type FluxReconcilerStats struct { // FluxSyncStatus defines the observed state of the cluster sync. type FluxSyncStatus struct { + // ID is the identifier of the sync. + // +required + ID string `json:"id"` + + // Path is the kustomize path of the sync. + // +optional + Path string `json:"path,omitempty"` + // Ready is the readiness status of the sync. // +required Ready bool `json:"ready"` diff --git a/config/crd/bases/fluxcd.controlplane.io_fluxreports.yaml b/config/crd/bases/fluxcd.controlplane.io_fluxreports.yaml index 883f7e8..8575ac7 100644 --- a/config/crd/bases/fluxcd.controlplane.io_fluxreports.yaml +++ b/config/crd/bases/fluxcd.controlplane.io_fluxreports.yaml @@ -154,6 +154,12 @@ spec: SyncStatus is the status of the cluster sync Source and Kustomization resources. properties: + id: + description: ID is the identifier of the sync. + type: string + path: + description: Path is the kustomize path of the sync. + type: string ready: description: Ready is the readiness status of the sync. type: boolean @@ -164,6 +170,7 @@ spec: description: Status is the status of the sync. type: string required: + - id - ready - status type: object diff --git a/config/olm/bundle/manifests/flux-operator.clusterserviceversion.yaml b/config/olm/bundle/manifests/flux-operator.clusterserviceversion.yaml index 5e7708b..4993840 100644 --- a/config/olm/bundle/manifests/flux-operator.clusterserviceversion.yaml +++ b/config/olm/bundle/manifests/flux-operator.clusterserviceversion.yaml @@ -89,7 +89,9 @@ metadata: ], "sync": { "ready": true, - "source": "https://github.com/controlplaneio-fluxcd/distribution.git", + "id": "kustomization/flux-system", + "path": "clusters/production", + "source": "https://github.com/my-org/my-fleet.git", "status": "Applied revision: refs/heads/main@sha1:a90cd1ac35de01c175f7199315d3f4cd60195911" } } @@ -179,7 +181,7 @@ spec: displayName: FluxReport kind: FluxReport version: v1 - description: Flux Report + description: Flux Report (Autogenerated) install: strategy: deployment spec: diff --git a/docs/api/v1/fluxreport.md b/docs/api/v1/fluxreport.md index f31f9a4..70a249b 100644 --- a/docs/api/v1/fluxreport.md +++ b/docs/api/v1/fluxreport.md @@ -110,7 +110,9 @@ spec: totalSize: 78.1 KiB sync: ready: true - source: https://github.com/controlplaneio-fluxcd/distribution.git + id: kustomization/flux-system + path: clusters/production + source: https://github.com/my-org/my-fleet.git status: 'Applied revision: refs/heads/main@sha1:a90cd1ac35de01c175f7199315d3f4cd60195911' status: conditions: @@ -231,8 +233,9 @@ spec: ### Cluster sync status -The `.spec.sync` field contains information about the Flux sync status, -including the source URL, the applied revision, and the sync readiness status. +The `.spec.sync` field contains information about the cluster sync status, +including the Flux Kustomization name, source URL, the applied revision, +and the sync readiness status. Example: @@ -240,6 +243,8 @@ Example: spec: sync: ready: true + id: kustomization/flux-system + path: tests/v2.3/sources source: https://github.com/controlplaneio-fluxcd/distribution.git status: 'Applied revision: refs/heads/main@sha1:a90cd1ac35de01c175f7199315d3f4cd60195911' ``` diff --git a/internal/controller/fluxreport_controller_test.go b/internal/controller/fluxreport_controller_test.go index f437252..2f70ac0 100644 --- a/internal/controller/fluxreport_controller_test.go +++ b/internal/controller/fluxreport_controller_test.go @@ -102,6 +102,7 @@ func TestFluxReportReconciler_Reconcile(t *testing.T) { // Check reported sync. g.Expect(report.Spec.SyncStatus).ToNot(BeNil()) g.Expect(report.Spec.SyncStatus.Source).To(Equal(instance.Spec.Sync.URL)) + g.Expect(report.Spec.SyncStatus.ID).To(Equal("kustomization/" + ns.Name)) // Check ready condition. g.Expect(conditions.GetReason(report, meta.ReadyCondition)).To(BeIdenticalTo(meta.SucceededReason)) diff --git a/internal/reporter/reporter.go b/internal/reporter/reporter.go index b05fc56..b36c63f 100644 --- a/internal/reporter/reporter.go +++ b/internal/reporter/reporter.go @@ -100,7 +100,7 @@ func RequestReportUpdate(ctx context.Context, kubeClient client.Client, instance annotations[meta.ReconcileRequestAnnotation] = strconv.FormatInt(metav1.Now().Unix(), 10) report.SetAnnotations(annotations) - if err := kubeClient.Patch(ctx, report, patch); err != nil { + if err := kubeClient.Patch(ctx, report, patch, client.FieldOwner(manager)); err != nil { return fmt.Errorf("failed to annotate %s '%s' error: %w", report.Kind, instance, err) } return nil diff --git a/internal/reporter/sync.go b/internal/reporter/sync.go index 6ec3046..b24af98 100644 --- a/internal/reporter/sync.go +++ b/internal/reporter/sync.go @@ -6,6 +6,7 @@ package reporter import ( "context" "fmt" + "strings" "github.com/fluxcd/cli-utils/pkg/kstatus/status" "github.com/fluxcd/pkg/apis/meta" @@ -44,10 +45,17 @@ func (r *FluxStatusReporter) getSyncStatus(ctx context.Context, crds []metav1.Gr } syncStatus := &fluxcdv1.FluxSyncStatus{ + ID: fmt.Sprintf("%s/%s", strings.ToLower(syncKind), r.namespace), Ready: false, Status: "not initialized", } + // Read spec.path from the sync object. + if path, found, _ := unstructured.NestedString(syncObj.Object, "spec", "path"); found { + syncStatus.Path = path + } + + // Set sync readiness based on the Kustomization object conditions. if obj, err := status.GetObjectWithConditions(syncObj.Object); err == nil { for _, cond := range obj.Status.Conditions { if cond.Type == meta.ReadyCondition { @@ -57,6 +65,7 @@ func (r *FluxStatusReporter) getSyncStatus(ctx context.Context, crds []metav1.Gr } } + // Set source URL and readiness based on the source object conditions. if sourceKind, found, _ := unstructured.NestedString(syncObj.Object, "spec", "sourceRef", "kind"); found { sourceGVK := gvkFor(sourceKind, crds) if sourceGVK == nil {