Skip to content

Commit

Permalink
add getTruncatedId function
Browse files Browse the repository at this point in the history
Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
  • Loading branch information
yanxuean committed Aug 11, 2018
1 parent 19b7255 commit d2bc823
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
5 changes: 1 addition & 4 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
if !opts.verbose {
id := c.Id
if !opts.noTrunc {
id = strings.TrimPrefix(c.Id, "")
if len(id) > truncatedIDLen {
id = id[:truncatedIDLen]
}
id = getTruncatedID(id, "")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
id, c.Image.Image, ctm, convertContainerState(c.State), c.Metadata.Name, c.Metadata.Attempt)
Expand Down
5 changes: 1 addition & 4 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ var listImageCommand = cli.Command{
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
id := image.Id
if !noTrunc {
id = strings.TrimPrefix(image.Id, "sha256:")
if len(id) > truncatedIDLen {
id = id[:truncatedIDLen]
}
id = getTruncatedID(id, "sha256:")
}
for _, repoTagPair := range repoTagPairs {
if showDigest {
Expand Down
5 changes: 1 addition & 4 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
ctm := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
id := pod.Id
if !opts.noTrunc {
id = strings.TrimPrefix(pod.Id, "")
if len(id) > truncatedIDLen {
id = id[:truncatedIDLen]
}
id = getTruncatedID(id, "")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
id, ctm, convertPodState(pod.State), pod.Metadata.Name, pod.Metadata.Namespace, pod.Metadata.Attempt)
Expand Down
6 changes: 1 addition & 5 deletions cmd/crictl/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"time"

Expand Down Expand Up @@ -163,10 +162,7 @@ func ContainerStats(client pb.RuntimeServiceClient, opts statsOptions) error {
// Use `+` to work around go vet bug
fmt.Fprintln(w, "CONTAINER\tCPU %"+"\tMEM\tDISK\tINODES")
for _, s := range r.GetStats() {
id := strings.TrimPrefix(s.Attributes.Id, "")
if len(id) > truncatedIDLen {
id = id[:truncatedIDLen]
}
id := getTruncatedID(s.Attributes.Id, "")
cpu := s.GetCpu().GetUsageCoreNanoSeconds().GetValue()
mem := s.GetMemory().GetWorkingSetBytes().GetValue()
disk := s.GetWritableLayer().GetUsedBytes().GetValue()
Expand Down
8 changes: 8 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,11 @@ func jsonFieldFromTag(tag reflect.StructTag) string {
}
return field
}

func getTruncatedID(id, prefix string) string {
id = strings.TrimPrefix(id, prefix)
if len(id) > truncatedIDLen {
id = id[:truncatedIDLen]
}
return id
}

0 comments on commit d2bc823

Please sign in to comment.