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

refactor: handle the error in advance to avoid useless requests #2270

Merged
merged 1 commit into from
Sep 27, 2018
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
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