Skip to content

Commit 1daa9c1

Browse files
committed
add get command missing fields STATUS and AGE
1 parent cbb07ad commit 1daa9c1

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

pkg/cmd/get.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import (
44
"fmt"
55
"io"
66
"text/tabwriter"
7+
"time"
78

89
"github.com/iovisor/kubectl-trace/pkg/factory"
910
"github.com/iovisor/kubectl-trace/pkg/meta"
1011
"github.com/iovisor/kubectl-trace/pkg/tracejob"
1112
"github.com/spf13/cobra"
1213
"k8s.io/apimachinery/pkg/types"
14+
"k8s.io/apimachinery/pkg/util/duration"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
1317
"k8s.io/cli-runtime/pkg/genericclioptions"
1418
batchv1client "k8s.io/client-go/kubernetes/typed/batch/v1"
1519
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -187,7 +191,21 @@ func jobsTablePrint(o io.Writer, jobs []tracejob.TraceJob) {
187191
// them as missing.
188192
fmt.Fprintf(w, format, "NAMESPACE", "NODE", "NAME", "STATUS", "AGE")
189193
for _, j := range jobs {
190-
fmt.Fprintf(w, "\n"+format, j.Namespace, j.Hostname, j.Name, "<missing>", "<missing>")
194+
status := j.Status
195+
if status == "" {
196+
status = tracejob.TraceJobUnknown
197+
}
198+
fmt.Fprintf(w, "\n"+format, j.Namespace, j.Hostname, j.Name, status, translateTimestampSince(j.StartTime))
191199
}
192200
fmt.Fprintf(w, "\n")
193201
}
202+
203+
// translateTimestampSince returns the elapsed time since timestamp in
204+
// human-readable approximation.
205+
func translateTimestampSince(timestamp metav1.Time) string {
206+
if timestamp.IsZero() {
207+
return "<unknown>"
208+
}
209+
210+
return duration.HumanDuration(time.Since(timestamp.Time))
211+
}

pkg/tracejob/job.go

+32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type TraceJob struct {
3535
ImageNameTag string
3636
InitImageNameTag string
3737
FetchHeaders bool
38+
StartTime metav1.Time
39+
Status TraceJobStatus
3840
}
3941

4042
// WithOutStream setup a file stream to output trace job operation information
@@ -129,6 +131,8 @@ func (t *TraceJobClient) GetJob(nf TraceJobFilter) ([]TraceJob, error) {
129131
ID: types.UID(id),
130132
Namespace: j.Namespace,
131133
Hostname: hostname,
134+
StartTime: *j.Status.StartTime,
135+
Status: jobStatus(j),
132136
}
133137
tjobs = append(tjobs, tj)
134138
}
@@ -362,3 +366,31 @@ func jobHostname(j batchv1.Job) (string, error) {
362366

363367
return "", fmt.Errorf("hostname not found for job")
364368
}
369+
370+
// TraceJobStatus is a label for the running status of a trace job at the current time.
371+
type TraceJobStatus string
372+
373+
// These are the valid status of traces.
374+
const (
375+
// TraceJobRunning means the trace job has active pods.
376+
TraceJobRunning TraceJobStatus = "Running"
377+
// TraceJobCompleted means the trace job does not have any active pod and has success pods.
378+
TraceJobCompleted TraceJobStatus = "Completed"
379+
// TraceJobCompleted means the trace job does not have any active or success pod and has fpods that failed.
380+
TraceJobFailed TraceJobStatus = "Failed"
381+
// TraceJobUnknown means that for some reason we do not have the information to determine the status.
382+
TraceJobUnknown TraceJobStatus = "Unknown"
383+
)
384+
385+
func jobStatus(j batchv1.Job) TraceJobStatus {
386+
if j.Status.Active > 0 {
387+
return TraceJobRunning
388+
}
389+
if j.Status.Succeeded > 0 {
390+
return TraceJobCompleted
391+
}
392+
if j.Status.Failed > 0 {
393+
return TraceJobFailed
394+
}
395+
return TraceJobUnknown
396+
}

vendor/k8s.io/apimachinery/pkg/util/duration/duration.go

+89
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ k8s.io/api/storage/v1beta1
246246
k8s.io/apimachinery/pkg/apis/meta/v1
247247
k8s.io/apimachinery/pkg/types
248248
k8s.io/apimachinery/pkg/util/wait
249+
k8s.io/apimachinery/pkg/util/duration
249250
k8s.io/apimachinery/pkg/util/uuid
250251
k8s.io/apimachinery/pkg/api/meta
251252
k8s.io/apimachinery/pkg/runtime/schema

0 commit comments

Comments
 (0)