Skip to content

Commit

Permalink
Add sync id and path to report
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Jun 21, 2024
1 parent 2968dd2 commit 8add4c8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
17 changes: 14 additions & 3 deletions api/v1/fluxreport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type FluxDistributionStatus struct {
// +required
Entitlement string `json:"entitlement"`

// Status is the status of the Flux instance.
// Status is a human-readable message indicating details
// about the distribution observed state.
// +required
Status string `json:"status"`

Expand All @@ -63,7 +64,8 @@ type FluxComponentStatus struct {
// +required
Ready bool `json:"ready"`

// Status is the status of the Flux component.
// Status is a human-readable message indicating details
// about the Flux component observed state.
// +required
Status string `json:"status"`

Expand Down Expand Up @@ -111,11 +113,20 @@ 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"`

// Status is the status of the sync.
// Status is a human-readable message indicating details
// about the sync observed state.
// +required
Status string `json:"status"`

Expand Down
19 changes: 16 additions & 3 deletions config/crd/bases/fluxcd.controlplane.io_fluxreports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ spec:
description: Ready is the readiness status of the Flux component.
type: boolean
status:
description: Status is the status of the Flux component.
description: |-
Status is a human-readable message indicating details
about the Flux component observed state.
type: string
required:
- image
Expand All @@ -93,7 +95,9 @@ spec:
Flux instance.
type: string
status:
description: Status is the status of the Flux instance.
description: |-
Status is a human-readable message indicating details
about the distribution observed state.
type: string
version:
description: Version is the version of the Flux instance.
Expand Down Expand Up @@ -154,16 +158,25 @@ 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
source:
description: Source is the URL of the source repository.
type: string
status:
description: Status is the status of the sync.
description: |-
Status is a human-readable message indicating details
about the sync observed state.
type: string
required:
- id
- ready
- status
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down Expand Up @@ -179,7 +181,7 @@ spec:
displayName: FluxReport
kind: FluxReport
version: v1
description: Flux Report
description: Flux Report (Autogenerated)
install:
strategy: deployment
spec:
Expand Down
11 changes: 8 additions & 3 deletions docs/api/v1/fluxreport.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -231,15 +233,18 @@ 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:

```yaml
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'
```
Expand Down
1 change: 1 addition & 0 deletions internal/controller/fluxreport_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/reporter/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package reporter
import (
"context"
"fmt"
"strings"

"github.com/fluxcd/cli-utils/pkg/kstatus/status"
"github.com/fluxcd/pkg/apis/meta"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 8add4c8

Please sign in to comment.