-
Notifications
You must be signed in to change notification settings - Fork 950
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
feature: implement the feature of reopen log about cri #2447
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ import ( | |
|
||
// NOTE: "golang.org/x/net/context" is compatible with standard "context" in golang1.7+. | ||
"github.com/cri-o/ocicni/pkg/ocicni" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
|
@@ -250,7 +251,8 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox | |
return nil, err | ||
} | ||
sandboxMeta := &SandboxMeta{ | ||
ID: id, | ||
ID: id, | ||
ContainerLogMap: make(map[string]string), | ||
} | ||
if err := c.SandboxStore.Put(sandboxMeta); err != nil { | ||
return nil, err | ||
|
@@ -499,28 +501,26 @@ func (c *CriManager) RemovePodSandbox(ctx context.Context, r *runtime.RemovePodS | |
|
||
// Remove all containers in the sandbox. | ||
for _, container := range containers { | ||
err = c.ContainerMgr.Remove(ctx, container.ID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true}) | ||
if err != nil { | ||
if err := c.ContainerMgr.Remove(ctx, container.ID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true}); err != nil { | ||
return nil, fmt.Errorf("failed to remove container %q of sandbox %q: %v", container.ID, podSandboxID, err) | ||
} | ||
|
||
logrus.Infof("success to remove container %q of sandbox %q", container.ID, podSandboxID) | ||
} | ||
|
||
// Remove the sandbox container. | ||
err = c.ContainerMgr.Remove(ctx, podSandboxID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true}) | ||
if err != nil { | ||
if err := c.ContainerMgr.Remove(ctx, podSandboxID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true}); err != nil { | ||
return nil, fmt.Errorf("failed to remove sandbox %q: %v", podSandboxID, err) | ||
} | ||
|
||
// Cleanup the sandbox root directory. | ||
sandboxRootDir := path.Join(c.SandboxBaseDir, podSandboxID) | ||
err = os.RemoveAll(sandboxRootDir) | ||
if err != nil { | ||
|
||
if err := os.RemoveAll(sandboxRootDir); err != nil { | ||
return nil, fmt.Errorf("failed to remove root directory %q: %v", sandboxRootDir, err) | ||
} | ||
|
||
err = c.SandboxStore.Remove(podSandboxID) | ||
if err != nil { | ||
if err := c.SandboxStore.Remove(podSandboxID); err != nil { | ||
return nil, fmt.Errorf("failed to remove meta %q: %v", sandboxRootDir, err) | ||
} | ||
|
||
|
@@ -765,6 +765,11 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta | |
// Get container log. | ||
if config.GetLogPath() != "" { | ||
logPath := filepath.Join(sandboxConfig.GetLogDirectory(), config.GetLogPath()) | ||
sandboxMeta.ContainerLogMap[containerID] = logPath | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @starnop containerLogMap has not been initialized There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. THX, I have updated it. |
||
if err := c.SandboxStore.Put(sandboxMeta); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := c.ContainerMgr.AttachCRILog(ctx, containerID, logPath); err != nil { | ||
return nil, err | ||
} | ||
|
@@ -825,8 +830,7 @@ func (c *CriManager) RemoveContainer(ctx context.Context, r *runtime.RemoveConta | |
|
||
containerID := r.GetContainerId() | ||
|
||
err := c.ContainerMgr.Remove(ctx, containerID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true}) | ||
if err != nil { | ||
if err := c.ContainerMgr.Remove(ctx, containerID, &apitypes.ContainerRemoveOptions{Volumes: true, Force: true}); err != nil { | ||
return nil, fmt.Errorf("failed to remove container %q: %v", containerID, err) | ||
} | ||
|
||
|
@@ -1128,7 +1132,42 @@ func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.Up | |
// to either create a new log file and return nil, or return an error. | ||
// Once it returns error, new container log file MUST NOT be created. | ||
func (c *CriManager) ReopenContainerLog(ctx context.Context, r *runtime.ReopenContainerLogRequest) (*runtime.ReopenContainerLogResponse, error) { | ||
return nil, fmt.Errorf("ReopenContainerLog Not Implemented Yet") | ||
containerID := r.GetContainerId() | ||
|
||
container, err := c.ContainerMgr.Get(ctx, containerID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get container %q with error: %v", containerID, err) | ||
} | ||
if !container.IsRunning() { | ||
return nil, errors.Wrap(errtypes.ErrPreCheckFailed, "container is not running") | ||
} | ||
|
||
// get the container's podSandbox id. | ||
podSandboxID, ok := container.Config.Labels[sandboxIDLabelKey] | ||
if !ok { | ||
return nil, fmt.Errorf("failed to get the sandboxId of container %q", containerID) | ||
} | ||
|
||
// get logPath of container | ||
res, err := c.SandboxStore.Get(podSandboxID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err) | ||
} | ||
sandboxMeta, ok := res.(*SandboxMeta) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to type asseration for sandboxMeta: %v", res) | ||
} | ||
logPath, ok := sandboxMeta.ContainerLogMap[containerID] | ||
if !ok { | ||
logrus.Warnf("log path of container: %q is empty", containerID) | ||
return &runtime.ReopenContainerLogResponse{}, nil | ||
} | ||
|
||
if err := c.ContainerMgr.AttachCRILog(ctx, container.Name, logPath); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &runtime.ReopenContainerLogResponse{}, nil | ||
} | ||
|
||
// ExecSync executes a command in the container, and returns the stdout output. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use
errors.Wrap
here because theRemove
might return the 404 or specific error.We can handle this in next pr. it is not blocked issue.