Skip to content

Commit b15605a

Browse files
authored
Merge pull request #268 from feiskyer/fix-trunc
Avoid panic when runtimes are using truncated IDs
2 parents c01e20f + 0829411 commit b15605a

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

cmd/crictl/container.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,10 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
623623
if !opts.verbose {
624624
id := c.Id
625625
if !opts.noTrunc {
626-
id = strings.TrimPrefix(c.Id, "")[:truncatedIDLen]
626+
id = strings.TrimPrefix(c.Id, "")
627+
if len(id) > truncatedIDLen {
628+
id = id[:truncatedIDLen]
629+
}
627630
}
628631
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
629632
id, c.Image.Image, ctm, c.State, c.Metadata.Name, c.Metadata.Attempt)

cmd/crictl/image.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ var listImageCommand = cli.Command{
154154
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
155155
id := image.Id
156156
if !noTrunc {
157-
id = strings.TrimPrefix(image.Id, "sha256:")[:truncatedIDLen]
157+
id = strings.TrimPrefix(image.Id, "sha256:")
158+
if len(id) > truncatedIDLen {
159+
id = id[:truncatedIDLen]
160+
}
158161
}
159162
for _, repoTagPair := range repoTagPairs {
160163
if showDigest {

cmd/crictl/sandbox.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
431431
ctm := units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
432432
id := pod.Id
433433
if !opts.noTrunc {
434-
id = strings.TrimPrefix(pod.Id, "")[:truncatedIDLen]
434+
id = strings.TrimPrefix(pod.Id, "")
435+
if len(id) > truncatedIDLen {
436+
id = id[:truncatedIDLen]
437+
}
435438
}
436439
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
437440
id, ctm, pod.State, pod.Metadata.Name, pod.Metadata.Namespace, pod.Metadata.Attempt)

cmd/crictl/stats.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ func ContainerStats(client pb.RuntimeServiceClient, opts statsOptions) error {
162162
// Use `+` to work around go vet bug
163163
fmt.Fprintln(w, "CONTAINER\tCPU %"+"\tMEM\tDISK\tINODES")
164164
for _, s := range r.GetStats() {
165-
id := strings.TrimPrefix(s.Attributes.Id, "")[:truncatedIDLen]
165+
id := strings.TrimPrefix(s.Attributes.Id, "")
166+
if len(id) > truncatedIDLen {
167+
id = id[:truncatedIDLen]
168+
}
166169
cpu := s.GetCpu().GetUsageCoreNanoSeconds().GetValue()
167170
mem := s.GetMemory().GetWorkingSetBytes().GetValue()
168171
disk := s.GetWritableLayer().GetUsedBytes().GetValue()

0 commit comments

Comments
 (0)