Skip to content

Commit d73ff34

Browse files
committed
bugfix: makeup logpath from sandbox & container mata
Signed-off-by: YaoZengzeng <[email protected]>
1 parent f76d3bc commit d73ff34

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

cri/v1alpha1/cri.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,16 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
714714
imageRef = imageInfo.RepoDigests[0]
715715
}
716716

717+
podSandboxID := container.Config.Labels[sandboxIDLabelKey]
718+
res, err := c.SandboxStore.Get(podSandboxID)
719+
if err != nil {
720+
return nil, fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
721+
}
722+
sandboxMeta := res.(*SandboxMeta)
723+
logDirectory := sandboxMeta.Config.GetLogDirectory()
724+
// TODO: let the container manager handle the log stuff for CRI.
725+
logPath := makeupLogPath(logDirectory, metadata)
726+
717727
status := &runtime.ContainerStatus{
718728
Id: container.ID,
719729
Metadata: metadata,
@@ -729,7 +739,7 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
729739
Message: message,
730740
Labels: labels,
731741
Annotations: annotations,
732-
// TODO: LogPath.
742+
LogPath: logPath,
733743
}
734744

735745
return &runtime.ContainerStatusResponse{Status: status}, nil

cri/v1alpha1/cri_utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ func parseContainerName(name string) (*runtime.ContainerMetadata, error) {
449449
}, nil
450450
}
451451

452+
// makeupLogPath makes up the log path of container from log directory and its metadata.
453+
func makeupLogPath(logDirectory string, metadata *runtime.ContainerMetadata) string {
454+
return filepath.Join(logDirectory, fmt.Sprintf("%s_%d.log", metadata.Name, metadata.Attempt))
455+
}
456+
452457
// modifyContainerNamespaceOptions apply namespace options for container.
453458
func modifyContainerNamespaceOptions(nsOpts *runtime.NamespaceOption, podSandboxID string, hostConfig *apitypes.HostConfig) {
454459
sandboxNSMode := fmt.Sprintf("container:%v", podSandboxID)

cri/v1alpha1/cri_utils_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,32 @@ func Test_parseContainerName(t *testing.T) {
573573
}
574574
}
575575

576+
func Test_makeupLogPath(t *testing.T) {
577+
testCases := []struct {
578+
logDirectory string
579+
containerMeta *runtime.ContainerMetadata
580+
expected string
581+
}{
582+
{
583+
logDirectory: "/var/log/pods/099f1c2b79126109140a1f77e211df00",
584+
containerMeta: &runtime.ContainerMetadata{"kube-scheduler", 0},
585+
expected: "/var/log/pods/099f1c2b79126109140a1f77e211df00/kube-scheduler_0.log",
586+
},
587+
{
588+
logDirectory: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/",
589+
containerMeta: &runtime.ContainerMetadata{"kube-proxy", 10},
590+
expected: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/kube-proxy_10.log",
591+
},
592+
}
593+
594+
for _, test := range testCases {
595+
logPath := makeupLogPath(test.logDirectory, test.containerMeta)
596+
if !reflect.DeepEqual(expected, logPath) {
597+
t.Fatalf("unexpected logPath returned by makeupLogPath")
598+
}
599+
}
600+
}
601+
576602
func Test_toCriContainerState(t *testing.T) {
577603
testCases := []struct {
578604
input apitypes.Status

cri/v1alpha2/cri.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,16 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
713713
imageRef = imageInfo.RepoDigests[0]
714714
}
715715

716+
podSandboxID := container.Config.Labels[sandboxIDLabelKey]
717+
res, err := c.SandboxStore.Get(podSandboxID)
718+
if err != nil {
719+
return nil, fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
720+
}
721+
sandboxMeta := res.(*SandboxMeta)
722+
logDirectory := sandboxMeta.Config.GetLogDirectory()
723+
// TODO: let the container manager handle the log stuff for CRI.
724+
logPath := makeupLogPath(logDirectory, metadata)
725+
716726
resources := container.HostConfig.Resources
717727
diskQuota := container.Config.DiskQuota
718728
status := &runtime.ContainerStatus{
@@ -730,7 +740,7 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
730740
Message: message,
731741
Labels: labels,
732742
Annotations: annotations,
733-
LogPath: container.LogPath,
743+
LogPath: logPath,
734744
Volumes: parseVolumesFromPouch(container.Config.Volumes),
735745
Resources: parseResourcesFromPouch(resources, diskQuota),
736746
}

cri/v1alpha2/cri_utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ func parseContainerName(name string) (*runtime.ContainerMetadata, error) {
444444
}, nil
445445
}
446446

447+
// makeupLogPath makes up the log path of container from log directory and its metadata.
448+
func makeupLogPath(logDirectory string, metadata *runtime.ContainerMetadata) string {
449+
return filepath.Join(logDirectory, metadata.Name, fmt.Sprintf("%d.log", metadata.Attempt))
450+
}
451+
447452
// modifyContainerNamespaceOptions apply namespace options for container.
448453
func modifyContainerNamespaceOptions(nsOpts *runtime.NamespaceOption, podSandboxID string, hostConfig *apitypes.HostConfig) {
449454
sandboxNSMode := fmt.Sprintf("container:%v", podSandboxID)

cri/v1alpha2/cri_utils_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,32 @@ func Test_parseContainerName(t *testing.T) {
560560
}
561561
}
562562

563+
func Test_makeupLogPath(t *testing.T) {
564+
testCases := []struct {
565+
logDirectory string
566+
containerMeta *runtime.ContainerMetadata
567+
expected string
568+
}{
569+
{
570+
logDirectory: "/var/log/pods/099f1c2b79126109140a1f77e211df00",
571+
containerMeta: &runtime.ContainerMetadata{"kube-scheduler", 0},
572+
expected: "/var/log/pods/099f1c2b79126109140a1f77e211df00/kube-scheduler/0.log",
573+
},
574+
{
575+
logDirectory: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/",
576+
containerMeta: &runtime.ContainerMetadata{"kube-proxy", 10},
577+
expected: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/kube-proxy/10.log",
578+
},
579+
}
580+
581+
for _, test := range testCases {
582+
logPath := makeupLogPath(test.logDirectory, test.containerMeta)
583+
if !reflect.DeepEqual(expected, logPath) {
584+
t.Fatalf("unexpected logPath returned by makeupLogPath")
585+
}
586+
}
587+
}
588+
563589
func Test_toCriContainerState(t *testing.T) {
564590
testCases := []struct {
565591
input apitypes.Status

0 commit comments

Comments
 (0)