mirror of
https://github.com/fluxcd/flux2.git
synced 2026-02-22 15:41:47 +00:00
add --since and --since-time
Signed-off-by: Tomas Tulka <tomas.tulka@gmail.com>
This commit is contained in:
parent
cb96bca6aa
commit
f3dd2122be
3 changed files with 31 additions and 2 deletions
|
|
@ -26,12 +26,14 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/kubectl/pkg/util"
|
||||
|
||||
"github.com/fluxcd/flux2/internal/flags"
|
||||
"github.com/fluxcd/flux2/internal/utils"
|
||||
|
|
@ -41,8 +43,11 @@ var logsCmd = &cobra.Command{
|
|||
Use: "logs",
|
||||
Short: "Display formatted logs for Flux components",
|
||||
Long: "The logs command displays formatted logs from various Flux components.",
|
||||
Example: ` # Print the reconciliation logs of all Flux custom resources in your cluster
|
||||
flux logs --all-namespaces
|
||||
Example: ` # Print the reconciliation logs of all Flux custom resources in your cluster
|
||||
flux logs --all-namespaces
|
||||
|
||||
# Print all logs of all Flux custom resources newer than 2 minutes
|
||||
flux logs --all-namespaces --since=2m
|
||||
|
||||
# Stream logs for a particular log level
|
||||
flux logs --follow --level=error --all-namespaces
|
||||
|
|
@ -64,6 +69,8 @@ type logsFlags struct {
|
|||
name string
|
||||
fluxNamespace string
|
||||
allNamespaces bool
|
||||
sinceTime string
|
||||
sinceSeconds time.Duration
|
||||
}
|
||||
|
||||
var logsArgs = &logsFlags{
|
||||
|
|
@ -78,6 +85,8 @@ func init() {
|
|||
logsCmd.Flags().Int64VarP(&logsArgs.tail, "tail", "", logsArgs.tail, "lines of recent log file to display")
|
||||
logsCmd.Flags().StringVarP(&logsArgs.fluxNamespace, "flux-namespace", "", rootArgs.defaults.Namespace, "the namespace where the Flux components are running")
|
||||
logsCmd.Flags().BoolVarP(&logsArgs.allNamespaces, "all-namespaces", "A", false, "displays logs for objects across all namespaces")
|
||||
logsCmd.Flags().DurationVar(&logsArgs.sinceSeconds, "since", logsArgs.sinceSeconds, "Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of since-time / since may be used.")
|
||||
logsCmd.Flags().StringVar(&logsArgs.sinceTime, "since-time", logsArgs.sinceTime, "Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be used.")
|
||||
rootCmd.AddCommand(logsCmd)
|
||||
}
|
||||
|
||||
|
|
@ -115,6 +124,24 @@ func logsCmdRun(cmd *cobra.Command, args []string) error {
|
|||
logOpts.TailLines = &logsArgs.tail
|
||||
}
|
||||
|
||||
if len(logsArgs.sinceTime) > 0 && logsArgs.sinceSeconds != 0 {
|
||||
return fmt.Errorf("at most one of `sinceTime` or `sinceSeconds` may be specified")
|
||||
}
|
||||
|
||||
if len(logsArgs.sinceTime) > 0 {
|
||||
t, err := util.ParseRFC3339(logsArgs.sinceTime, metav1.Now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logOpts.SinceTime = &t
|
||||
}
|
||||
|
||||
if logsArgs.sinceSeconds != 0 {
|
||||
// round up to the nearest second
|
||||
sec := int64(logsArgs.sinceSeconds.Round(time.Second).Seconds())
|
||||
logOpts.SinceSeconds = &sec
|
||||
}
|
||||
|
||||
var requests []rest.ResponseWrapper
|
||||
for _, pod := range pods {
|
||||
req := clientset.CoreV1().Pods(logsArgs.fluxNamespace).GetLogs(pod.Name, logOpts)
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -30,6 +30,7 @@ require (
|
|||
k8s.io/apiextensions-apiserver v0.21.3
|
||||
k8s.io/apimachinery v0.21.3
|
||||
k8s.io/client-go v0.21.3
|
||||
k8s.io/kubectl v0.21.1
|
||||
sigs.k8s.io/cli-utils v0.25.1-0.20210608181808-f3974341173a
|
||||
sigs.k8s.io/controller-runtime v0.9.5
|
||||
sigs.k8s.io/kustomize/api v0.8.10
|
||||
|
|
|
|||
1
go.sum
1
go.sum
|
|
@ -1225,6 +1225,7 @@ k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6/go.mod h1:GRQhZsXIAJ1xR0C
|
|||
k8s.io/kube-openapi v0.0.0-20210305001622-591a79e4bda7/go.mod h1:wXW5VT87nVfh/iLV8FpR2uDvrFyomxbtb1KivDbvPTE=
|
||||
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e h1:KLHHjkdQFomZy8+06csTWZ0m1343QqxZhR2LJ1OxCYM=
|
||||
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
|
||||
k8s.io/kubectl v0.21.1 h1:ySEusoeSgSDSiSBncDMsNrthSa3OSlXqT4R2rf1VFTw=
|
||||
k8s.io/kubectl v0.21.1/go.mod h1:PMYR88MqESuysBM/MX+Vu4JbX/50nY4d4kny+SPEI2U=
|
||||
k8s.io/legacy-cloud-providers v0.18.8/go.mod h1:tgp4xYf6lvjrWnjQwTOPvWQE9IVqSBGPF4on0IyICQE=
|
||||
k8s.io/metrics v0.21.1/go.mod h1:pyDVLsLe++FIGDBFU80NcW4xMFsuiVTWL8Zfi7+PpNo=
|
||||
|
|
|
|||
Loading…
Reference in a new issue