Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement missing fields in Get #63

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"fmt"
"io"
"text/tabwriter"
"time"

"github.com/iovisor/kubectl-trace/pkg/factory"
"github.com/iovisor/kubectl-trace/pkg/meta"
"github.com/iovisor/kubectl-trace/pkg/tracejob"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/duration"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/cli-runtime/pkg/genericclioptions"
batchv1client "k8s.io/client-go/kubernetes/typed/batch/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
Expand Down Expand Up @@ -187,7 +191,21 @@ func jobsTablePrint(o io.Writer, jobs []tracejob.TraceJob) {
// them as missing.
fmt.Fprintf(w, format, "NAMESPACE", "NODE", "NAME", "STATUS", "AGE")
for _, j := range jobs {
fmt.Fprintf(w, "\n"+format, j.Namespace, j.Hostname, j.Name, "<missing>", "<missing>")
status := j.Status
if status == "" {
status = tracejob.TraceJobUnknown
}
fmt.Fprintf(w, "\n"+format, j.Namespace, j.Hostname, j.Name, status, translateTimestampSince(j.StartTime))
}
fmt.Fprintf(w, "\n")
}

// translateTimestampSince returns the elapsed time since timestamp in
// human-readable approximation.
func translateTimestampSince(timestamp metav1.Time) string {
if timestamp.IsZero() {
return "<unknown>"
}

return duration.HumanDuration(time.Since(timestamp.Time))
}
32 changes: 32 additions & 0 deletions pkg/tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type TraceJob struct {
ImageNameTag string
InitImageNameTag string
FetchHeaders bool
StartTime metav1.Time
Status TraceJobStatus
}

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

return "", fmt.Errorf("hostname not found for job")
}

// TraceJobStatus is a label for the running status of a trace job at the current time.
type TraceJobStatus string

// These are the valid status of traces.
const (
// TraceJobRunning means the trace job has active pods.
TraceJobRunning TraceJobStatus = "Running"
// TraceJobCompleted means the trace job does not have any active pod and has success pods.
TraceJobCompleted TraceJobStatus = "Completed"
// TraceJobFailed means the trace job does not have any active or success pod and has fpods that failed.
TraceJobFailed TraceJobStatus = "Failed"
// TraceJobUnknown means that for some reason we do not have the information to determine the status.
TraceJobUnknown TraceJobStatus = "Unknown"
)

func jobStatus(j batchv1.Job) TraceJobStatus {
if j.Status.Active > 0 {
return TraceJobRunning
}
if j.Status.Succeeded > 0 {
return TraceJobCompleted
}
if j.Status.Failed > 0 {
return TraceJobFailed
}
return TraceJobUnknown
}
89 changes: 89 additions & 0 deletions vendor/k8s.io/apimachinery/pkg/util/duration/duration.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ k8s.io/api/storage/v1beta1
k8s.io/apimachinery/pkg/apis/meta/v1
k8s.io/apimachinery/pkg/types
k8s.io/apimachinery/pkg/util/wait
k8s.io/apimachinery/pkg/util/duration
k8s.io/apimachinery/pkg/util/uuid
k8s.io/apimachinery/pkg/api/meta
k8s.io/apimachinery/pkg/runtime/schema
Expand Down