Skip to content

Commit a93b5bd

Browse files
committed
bugfix: container log path map not initialized for existing businesses
Signed-off-by: Starnop <[email protected]>
1 parent 7bac615 commit a93b5bd

File tree

4 files changed

+12
-69
lines changed

4 files changed

+12
-69
lines changed

cri/v1alpha2/cri.go

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const (
5353
containerTypeLabelSandbox = "sandbox"
5454
containerTypeLabelContainer = "container"
5555
sandboxIDLabelKey = "io.kubernetes.sandbox.id"
56+
containerLogPathLabelKey = "io.kubernetes.container.logpath"
5657

5758
// sandboxContainerName is a string to include in the pouch container so
5859
// that users can easily identify the sandboxes.
@@ -251,8 +252,7 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
251252
return nil, err
252253
}
253254
sandboxMeta := &SandboxMeta{
254-
ID: id,
255-
ContainerLogMap: make(map[string]string),
255+
ID: id,
256256
}
257257
if err := c.SandboxStore.Put(sandboxMeta); err != nil {
258258
return nil, err
@@ -681,6 +681,12 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
681681
labels[containerTypeLabelKey] = containerTypeLabelContainer
682682
// Write the sandbox ID in the labels.
683683
labels[sandboxIDLabelKey] = podSandboxID
684+
// Get container log.
685+
var logPath string
686+
if config.GetLogPath() != "" {
687+
logPath = filepath.Join(sandboxConfig.GetLogDirectory(), config.GetLogPath())
688+
labels[containerLogPathLabelKey] = logPath
689+
}
684690

685691
image := ""
686692
if iSpec := config.GetImage(); iSpec != nil {
@@ -762,14 +768,7 @@ func (c *CriManager) CreateContainer(ctx context.Context, r *runtime.CreateConta
762768
}
763769
}()
764770

765-
// Get container log.
766-
if config.GetLogPath() != "" {
767-
logPath := filepath.Join(sandboxConfig.GetLogDirectory(), config.GetLogPath())
768-
sandboxMeta.ContainerLogMap[containerID] = logPath
769-
if err := c.SandboxStore.Put(sandboxMeta); err != nil {
770-
return nil, err
771-
}
772-
771+
if logPath != "" {
773772
if err := c.ContainerMgr.AttachCRILog(ctx, containerID, logPath); err != nil {
774773
return nil, err
775774
}
@@ -978,15 +977,7 @@ func (c *CriManager) ContainerStatus(ctx context.Context, r *runtime.ContainerSt
978977
imageRef = imageInfo.RepoDigests[0]
979978
}
980979

981-
podSandboxID := container.Config.Labels[sandboxIDLabelKey]
982-
res, err := c.SandboxStore.Get(podSandboxID)
983-
if err != nil {
984-
return nil, fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
985-
}
986-
sandboxMeta := res.(*SandboxMeta)
987-
logDirectory := sandboxMeta.Config.GetLogDirectory()
988-
// TODO: let the container manager handle the log stuff for CRI.
989-
logPath := makeupLogPath(logDirectory, metadata)
980+
logPath := labels[containerLogPathLabelKey]
990981

991982
resources := container.HostConfig.Resources
992983
diskQuota := container.Config.DiskQuota
@@ -1142,23 +1133,9 @@ func (c *CriManager) ReopenContainerLog(ctx context.Context, r *runtime.ReopenCo
11421133
return nil, errors.Wrap(errtypes.ErrPreCheckFailed, "container is not running")
11431134
}
11441135

1145-
// get the container's podSandbox id.
1146-
podSandboxID, ok := container.Config.Labels[sandboxIDLabelKey]
1147-
if !ok {
1148-
return nil, fmt.Errorf("failed to get the sandboxId of container %q", containerID)
1149-
}
1150-
11511136
// get logPath of container
1152-
res, err := c.SandboxStore.Get(podSandboxID)
1153-
if err != nil {
1154-
return nil, fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
1155-
}
1156-
sandboxMeta, ok := res.(*SandboxMeta)
1157-
if !ok {
1158-
return nil, fmt.Errorf("failed to type asseration for sandboxMeta: %v", res)
1159-
}
1160-
logPath, ok := sandboxMeta.ContainerLogMap[containerID]
1161-
if !ok {
1137+
logPath := container.Config.Labels[containerLogPathLabelKey]
1138+
if logPath == "" {
11621139
logrus.Warnf("log path of container: %q is empty", containerID)
11631140
return &runtime.ReopenContainerLogResponse{}, nil
11641141
}

cri/v1alpha2/cri_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ type SandboxMeta struct {
2020

2121
// NetNS is the sandbox's network namespace
2222
NetNS string
23-
24-
// ContainerLogMap store the mapping of container id and CRI logPath.
25-
ContainerLogMap map[string]string
2623
}
2724

2825
// Key returns sandbox's id.

cri/v1alpha2/cri_utils.go

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

555-
// makeupLogPath makes up the log path of container from log directory and its metadata.
556-
func makeupLogPath(logDirectory string, metadata *runtime.ContainerMetadata) string {
557-
return filepath.Join(logDirectory, metadata.Name, fmt.Sprintf("%d.log", metadata.Attempt))
558-
}
559-
560555
// modifyContainerNamespaceOptions apply namespace options for container.
561556
func modifyContainerNamespaceOptions(nsOpts *runtime.NamespaceOption, podSandboxID string, hostConfig *apitypes.HostConfig) {
562557
sandboxNSMode := fmt.Sprintf("container:%v", podSandboxID)

cri/v1alpha2/cri_utils_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -510,32 +510,6 @@ func Test_parseContainerName(t *testing.T) {
510510
}
511511
}
512512

513-
func Test_makeupLogPath(t *testing.T) {
514-
testCases := []struct {
515-
logDirectory string
516-
containerMeta *runtime.ContainerMetadata
517-
expected string
518-
}{
519-
{
520-
logDirectory: "/var/log/pods/099f1c2b79126109140a1f77e211df00",
521-
containerMeta: &runtime.ContainerMetadata{Name: "kube-scheduler", Attempt: 0},
522-
expected: "/var/log/pods/099f1c2b79126109140a1f77e211df00/kube-scheduler/0.log",
523-
},
524-
{
525-
logDirectory: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/",
526-
containerMeta: &runtime.ContainerMetadata{Name: "kube-proxy", Attempt: 10},
527-
expected: "/var/log/pods/d875aada-9920-11e8-bfef-0242ac11001e/kube-proxy/10.log",
528-
},
529-
}
530-
531-
for _, test := range testCases {
532-
logPath := makeupLogPath(test.logDirectory, test.containerMeta)
533-
if !reflect.DeepEqual(test.expected, logPath) {
534-
t.Fatalf("unexpected logPath returned by makeupLogPath")
535-
}
536-
}
537-
}
538-
539513
func Test_toCriContainerState(t *testing.T) {
540514
testCases := []struct {
541515
input apitypes.Status

0 commit comments

Comments
 (0)