Skip to content

Commit

Permalink
bugfix: makeup logpath from sandbox & container mata
Browse files Browse the repository at this point in the history
Signed-off-by: YaoZengzeng <yaozengzeng@zju.edu.cn>
  • Loading branch information
YaoZengzeng committed Aug 6, 2018
1 parent f76d3bc commit 0cfa2d0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
imageRef = imageInfo.RepoDigests[0]
}

podSandboxID := container.Config.Labels[sandboxIDLabelKey]
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 := res.(*SandboxMeta)
logDirectory := sandboxMeta.Config.GetLogDirectory()
// TODO: let the container manager handle the log stuff for CRI.
logPath := makeupLogPath(logDirectory, metadata)

status := &runtime.ContainerStatus{
Id: container.ID,
Metadata: metadata,
Expand All @@ -729,7 +739,7 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
Message: message,
Labels: labels,
Annotations: annotations,
// TODO: LogPath.
LogPath: logPath,
}

return &runtime.ContainerStatusResponse{Status: status}, nil
Expand Down
5 changes: 5 additions & 0 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ func parseContainerName(name string) (*runtime.ContainerMetadata, error) {
}, nil
}

// makeupLogPath makes up the log path of container from log directory and its metadata.
func makeupLogPath(logDirectory string, metadata *runtime.ContainerMetadata) string {
return filepath.Join(logDirectory, fmt.Sprintf("%s_%d.log", metadata.Name, metadata.Attempt))
}

// modifyContainerNamespaceOptions apply namespace options for container.
func modifyContainerNamespaceOptions(nsOpts *runtime.NamespaceOption, podSandboxID string, hostConfig *apitypes.HostConfig) {
sandboxNSMode := fmt.Sprintf("container:%v", podSandboxID)
Expand Down
26 changes: 26 additions & 0 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,32 @@ func Test_parseContainerName(t *testing.T) {
}
}

func Test_makeupLogPath(t *testing.T) {
testCases := []struct {
logDirectory string
containerMeta *runtime.ContainerMetadata
expected string
}{
{
logDirectory: "/var/log/pods/099f1c2b79126109140a1f77e211df00",
containerMeta: &runtime.ContainerMetadata{"kube-scheduler", 0},
expected: "/var/log/pods/099f1c2b79126109140a1f77e211df00/kube-scheduler_0.log",
},
{
logDirectory: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/",
containerMeta: &runtime.ContainerMetadata{"kube-proxy", 10},
expected: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/kube-proxy_10.log",
},
}

for _, test := range testCases {
logPath := makeupLogPath(test.logDirectory, test.containerMeta)
if !reflect.DeepEqual(test.expected, logPath) {
t.Fatalf("unexpected logPath returned by makeupLogPath")
}
}
}

func Test_toCriContainerState(t *testing.T) {
testCases := []struct {
input apitypes.Status
Expand Down
12 changes: 11 additions & 1 deletion cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,16 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
imageRef = imageInfo.RepoDigests[0]
}

podSandboxID := container.Config.Labels[sandboxIDLabelKey]
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 := res.(*SandboxMeta)
logDirectory := sandboxMeta.Config.GetLogDirectory()
// TODO: let the container manager handle the log stuff for CRI.
logPath := makeupLogPath(logDirectory, metadata)

resources := container.HostConfig.Resources
diskQuota := container.Config.DiskQuota
status := &runtime.ContainerStatus{
Expand All @@ -730,7 +740,7 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
Message: message,
Labels: labels,
Annotations: annotations,
LogPath: container.LogPath,
LogPath: logPath,
Volumes: parseVolumesFromPouch(container.Config.Volumes),
Resources: parseResourcesFromPouch(resources, diskQuota),
}
Expand Down
5 changes: 5 additions & 0 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ func parseContainerName(name string) (*runtime.ContainerMetadata, error) {
}, nil
}

// makeupLogPath makes up the log path of container from log directory and its metadata.
func makeupLogPath(logDirectory string, metadata *runtime.ContainerMetadata) string {
return filepath.Join(logDirectory, metadata.Name, fmt.Sprintf("%d.log", metadata.Attempt))
}

// modifyContainerNamespaceOptions apply namespace options for container.
func modifyContainerNamespaceOptions(nsOpts *runtime.NamespaceOption, podSandboxID string, hostConfig *apitypes.HostConfig) {
sandboxNSMode := fmt.Sprintf("container:%v", podSandboxID)
Expand Down
26 changes: 26 additions & 0 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,32 @@ func Test_parseContainerName(t *testing.T) {
}
}

func Test_makeupLogPath(t *testing.T) {
testCases := []struct {
logDirectory string
containerMeta *runtime.ContainerMetadata
expected string
}{
{
logDirectory: "/var/log/pods/099f1c2b79126109140a1f77e211df00",
containerMeta: &runtime.ContainerMetadata{"kube-scheduler", 0},
expected: "/var/log/pods/099f1c2b79126109140a1f77e211df00/kube-scheduler/0.log",
},
{
logDirectory: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/",
containerMeta: &runtime.ContainerMetadata{"kube-proxy", 10},
expected: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/kube-proxy/10.log",
},
}

for _, test := range testCases {
logPath := makeupLogPath(test.logDirectory, test.containerMeta)
if !reflect.DeepEqual(test.expected, logPath) {
t.Fatalf("unexpected logPath returned by makeupLogPath")
}
}
}

func Test_toCriContainerState(t *testing.T) {
testCases := []struct {
input apitypes.Status
Expand Down

0 comments on commit 0cfa2d0

Please sign in to comment.