Skip to content

Commit

Permalink
refactor: handle the error in advance to avoid useless requests
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Sep 25, 2018
1 parent 5bc4afe commit 933a8e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
14 changes: 7 additions & 7 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,9 @@ func (c *CriManager) attachLog(logPath string, containerID string, openStdin boo
func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Container) (*runtime.ContainerStats, error) {
var usedBytes, inodesUsed uint64

stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID)
metadata, err := parseContainerName(meta.Name)
if err != nil {
return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err)
return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err)
}

// snapshot key may not equals container ID later
Expand All @@ -913,11 +913,6 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe
InodesUsed: &runtime.UInt64Value{inodesUsed},
}

metadata, err := parseContainerName(meta.Name)
if err != nil {
return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err)
}

labels, annotations := extractLabels(meta.Config.Labels)

cs.Attributes = &runtime.ContainerAttributes{
Expand All @@ -927,6 +922,11 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe
Annotations: annotations,
}

stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID)
if err != nil {
return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err)
}

if stats != nil {
s, err := typeurl.UnmarshalAny(stats.Data)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,9 @@ func (c *CriManager) attachLog(logPath string, containerID string, openStdin boo
func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Container) (*runtime.ContainerStats, error) {
var usedBytes, inodesUsed uint64

stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID)
metadata, err := parseContainerName(meta.Name)
if err != nil {
return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err)
return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err)
}

sn, err := c.SnapshotStore.Get(meta.ID)
Expand All @@ -938,12 +938,6 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe
UsedBytes: &runtime.UInt64Value{usedBytes},
InodesUsed: &runtime.UInt64Value{inodesUsed},
}

metadata, err := parseContainerName(meta.Name)
if err != nil {
return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err)
}

labels, annotations := extractLabels(meta.Config.Labels)

cs.Attributes = &runtime.ContainerAttributes{
Expand All @@ -953,6 +947,11 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe
Annotations: annotations,
}

stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID)
if err != nil {
return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err)
}

if stats != nil {
s, err := typeurl.UnmarshalAny(stats.Data)
if err != nil {
Expand Down

0 comments on commit 933a8e9

Please sign in to comment.