mirror of
https://github.com/fluxcd/flux2.git
synced 2025-10-25 19:54:33 +08:00
Add --show-history flag to debug kustomization
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
[]
|
||||
|
||||
@ -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
|
||||
|
||||
2
cmd/flux/testdata/debug_kustomization/history-empty.golden.yaml
vendored
Normal file
2
cmd/flux/testdata/debug_kustomization/history-empty.golden.yaml
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# History documentation: https://fluxcd.io/flux/components/kustomize/kustomizations/#history
|
||||
[]
|
||||
17
cmd/flux/testdata/debug_kustomization/history.golden.yaml
vendored
Normal file
17
cmd/flux/testdata/debug_kustomization/history.golden.yaml
vendored
Normal file
@ -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
|
||||
@ -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:
|
||||
|
||||
Reference in New Issue
Block a user