diff --git a/cmd/flux/debug_helmrelease.go b/cmd/flux/debug_helmrelease.go index 040cae8f..327897bb 100644 --- a/cmd/flux/debug_helmrelease.go +++ b/cmd/flux/debug_helmrelease.go @@ -42,7 +42,7 @@ WARNING: This command will print sensitive information if Kubernetes Secrets are # Export the final values of a Helm release composed from referred ConfigMaps and Secrets flux debug hr podinfo --show-values > values.yaml - # Print the reconciliation history of a Helm release + # Print the reconciliation history of a Helm release flux debug hr podinfo --show-history`, RunE: debugHelmReleaseCmdRun, Args: cobra.ExactArgs(1), @@ -77,7 +77,6 @@ func debugHelmReleaseCmdRun(cmd *cobra.Command, args []string) error { if debugHelmReleaseArgs.showHistory { flagsSet++ } - if flagsSet != 1 { return fmt.Errorf("exactly one of --show-status, --show-values, or --show-history must be set") } @@ -134,9 +133,10 @@ func debugHelmReleaseCmdRun(cmd *cobra.Command, args []string) error { return err } - rootCmd.Println("# History documentation: https://fluxcd.io/flux/components/helm/helmreleases/#helmrelease-status") + rootCmd.Println("# History documentation: https://fluxcd.io/flux/components/helm/helmreleases/#history") rootCmd.Print(string(history)) return nil } + return nil } diff --git a/cmd/flux/debug_kustomization.go b/cmd/flux/debug_kustomization.go index 026b150c..adaaa7fd 100644 --- a/cmd/flux/debug_kustomization.go +++ b/cmd/flux/debug_kustomization.go @@ -24,6 +24,7 @@ import ( "strings" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" + "github.com/fluxcd/pkg/apis/meta" "github.com/fluxcd/pkg/kustomize" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -44,15 +45,19 @@ WARNING: This command will print sensitive information if Kubernetes Secrets are flux debug ks podinfo --show-status # Export the final variables used for post-build substitutions composed from referred ConfigMaps and Secrets - flux debug ks podinfo --show-vars > vars.env`, + flux debug ks podinfo --show-vars > vars.env + + # Print the reconciliation history of a Flux Kustomization + flux debug ks podinfo --show-history`, RunE: debugKustomizationCmdRun, Args: cobra.ExactArgs(1), ValidArgsFunction: resourceNamesCompletionFunc(kustomizev1.GroupVersion.WithKind(kustomizev1.KustomizationKind)), } type debugKustomizationFlags struct { - showStatus bool - showVars bool + showStatus bool + showVars bool + showHistory bool } var debugKustomizationArgs debugKustomizationFlags @@ -60,15 +65,25 @@ var debugKustomizationArgs debugKustomizationFlags func init() { debugKustomizationCmd.Flags().BoolVar(&debugKustomizationArgs.showStatus, "show-status", false, "print the status of the Flux Kustomization") debugKustomizationCmd.Flags().BoolVar(&debugKustomizationArgs.showVars, "show-vars", false, "print the final vars of the Flux Kustomization in dot env format") + debugKustomizationCmd.Flags().BoolVar(&debugKustomizationArgs.showHistory, "show-history", false, "print the reconciliation history of the Flux Kustomization") debugCmd.AddCommand(debugKustomizationCmd) } func debugKustomizationCmdRun(cmd *cobra.Command, args []string) error { name := args[0] - if (!debugKustomizationArgs.showStatus && !debugKustomizationArgs.showVars) || - (debugKustomizationArgs.showStatus && debugKustomizationArgs.showVars) { - return fmt.Errorf("either --show-status or --show-vars must be set") + flagsSet := 0 + if debugKustomizationArgs.showStatus { + flagsSet++ + } + if debugKustomizationArgs.showVars { + flagsSet++ + } + if debugKustomizationArgs.showHistory { + flagsSet++ + } + if flagsSet != 1 { + return fmt.Errorf("exactly one of --show-status, --show-vars, or --show-history must be set") } ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) @@ -130,5 +145,20 @@ func debugKustomizationCmdRun(cmd *cobra.Command, args []string) error { } } + if debugKustomizationArgs.showHistory { + if len(ks.Status.History) == 0 { + ks.Status.History = meta.History{} + } + + history, err := yaml.Marshal(ks.Status.History) + if err != nil { + return err + } + + rootCmd.Println("# History documentation: https://fluxcd.io/flux/components/kustomize/kustomizations/#history") + rootCmd.Print(string(history)) + return nil + } + return nil } diff --git a/cmd/flux/debug_kustomization_test.go b/cmd/flux/debug_kustomization_test.go index d3742ed3..902efbac 100644 --- a/cmd/flux/debug_kustomization_test.go +++ b/cmd/flux/debug_kustomization_test.go @@ -55,6 +55,17 @@ func TestDebugKustomization(t *testing.T) { "debug ks test-from --show-vars --show-status=false", "testdata/debug_kustomization/vars-from.golden.env", tmpl, + }, { + "debug history", + "debug ks test-with-history --show-history --show-status=false", + "testdata/debug_kustomization/history.golden.yaml", + tmpl, + }, + { + "debug history empty", + "debug ks test --show-history --show-status=false", + "testdata/debug_kustomization/history-empty.golden.yaml", + tmpl, }, } diff --git a/cmd/flux/testdata/debug_helmrelease/history-empty.golden.yaml b/cmd/flux/testdata/debug_helmrelease/history-empty.golden.yaml index c6ee02fa..20630747 100644 --- a/cmd/flux/testdata/debug_helmrelease/history-empty.golden.yaml +++ b/cmd/flux/testdata/debug_helmrelease/history-empty.golden.yaml @@ -1,2 +1,2 @@ -# History documentation: https://fluxcd.io/flux/components/helm/helmreleases/#helmrelease-status +# History documentation: https://fluxcd.io/flux/components/helm/helmreleases/#history [] diff --git a/cmd/flux/testdata/debug_helmrelease/history.golden.yaml b/cmd/flux/testdata/debug_helmrelease/history.golden.yaml index 5215ed77..507736c4 100644 --- a/cmd/flux/testdata/debug_helmrelease/history.golden.yaml +++ b/cmd/flux/testdata/debug_helmrelease/history.golden.yaml @@ -1,4 +1,4 @@ -# History documentation: https://fluxcd.io/flux/components/helm/helmreleases/#helmrelease-status +# History documentation: https://fluxcd.io/flux/components/helm/helmreleases/#history - appVersion: 6.0.0 chartName: podinfo chartVersion: 6.0.0 diff --git a/cmd/flux/testdata/debug_kustomization/history-empty.golden.yaml b/cmd/flux/testdata/debug_kustomization/history-empty.golden.yaml new file mode 100644 index 00000000..3db1cdb6 --- /dev/null +++ b/cmd/flux/testdata/debug_kustomization/history-empty.golden.yaml @@ -0,0 +1,2 @@ +# History documentation: https://fluxcd.io/flux/components/kustomize/kustomizations/#history +[] diff --git a/cmd/flux/testdata/debug_kustomization/history.golden.yaml b/cmd/flux/testdata/debug_kustomization/history.golden.yaml new file mode 100644 index 00000000..5d521404 --- /dev/null +++ b/cmd/flux/testdata/debug_kustomization/history.golden.yaml @@ -0,0 +1,17 @@ +# History documentation: https://fluxcd.io/flux/components/kustomize/kustomizations/#history +- digest: sha256:def456 + firstReconciled: "2024-01-01T09:00:00Z" + lastReconciled: "2024-01-01T10:00:00Z" + lastReconciledDuration: 300ms + lastReconciledStatus: success + metadata: + originRevision: abc123 + totalReconciliations: 1 +- digest: sha256:ghi012 + firstReconciled: "2024-02-01T09:00:00Z" + lastReconciled: "2024-02-01T10:00:00Z" + lastReconciledDuration: 500ms + lastReconciledStatus: failure + metadata: + originRevision: xyz789 + totalReconciliations: 10 diff --git a/cmd/flux/testdata/debug_kustomization/objects.yaml b/cmd/flux/testdata/debug_kustomization/objects.yaml index b724d7d3..3c4d3245 100644 --- a/cmd/flux/testdata/debug_kustomization/objects.yaml +++ b/cmd/flux/testdata/debug_kustomization/objects.yaml @@ -44,6 +44,47 @@ spec: - kind: Secret name: test --- +apiVersion: kustomize.toolkit.fluxcd.io/v1 +kind: Kustomization +metadata: + name: test-with-history + namespace: {{ .fluxns }} +spec: + sourceRef: + kind: GitRepository + name: test + interval: 1m + path: "./" + prune: true + postBuild: + substitute: + TEST_OVERRIDE: "in-line" + TEST_INLINE: "in-line" + substituteFrom: + - kind: ConfigMap + name: test + - kind: Secret + name: test +status: + observedGeneration: 1 + history: + - digest: sha256:def456 + firstReconciled: "2024-01-01T09:00:00Z" + lastReconciled: "2024-01-01T10:00:00Z" + lastReconciledDuration: 300ms + lastReconciledStatus: success + metadata: + originRevision: abc123 + totalReconciliations: 1 + - digest: sha256:ghi012 + firstReconciled: "2024-02-01T09:00:00Z" + lastReconciled: "2024-02-01T10:00:00Z" + lastReconciledDuration: 500ms + lastReconciledStatus: failure + metadata: + originRevision: xyz789 + totalReconciliations: 10 +--- apiVersion: v1 kind: ConfigMap metadata: