Skip to content

Commit ad635da

Browse files
committed
add support logs for namespace k8s.io
Signed-off-by: zhaojizhuang <[email protected]>
1 parent cfe96a9 commit ad635da

File tree

8 files changed

+794
-10
lines changed

8 files changed

+794
-10
lines changed

cmd/nerdctl/logs.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525
"syscall"
2626

2727
"github.com/containerd/containerd"
28+
"github.com/containerd/nerdctl/pkg/api/types/cri"
2829
"github.com/containerd/nerdctl/pkg/clientutil"
2930
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
3031
"github.com/containerd/nerdctl/pkg/labels"
32+
"github.com/containerd/nerdctl/pkg/labels/k8slabels"
3133
"github.com/containerd/nerdctl/pkg/logging"
3234
"github.com/sirupsen/logrus"
3335
"github.com/spf13/cobra"
@@ -62,8 +64,8 @@ func logsAction(cmd *cobra.Command, args []string) error {
6264
}
6365

6466
switch globalOptions.Namespace {
65-
case "moby", "k8s.io":
66-
logrus.Warn("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d`")
67+
case "moby":
68+
logrus.Warn("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d` or CRI")
6769
}
6870
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
6971
if err != nil {
@@ -113,6 +115,12 @@ func logsAction(cmd *cobra.Command, args []string) error {
113115
if err != nil {
114116
return err
115117
}
118+
119+
logPath, err := getLogPath(ctx, found.Container)
120+
if err != nil {
121+
return err
122+
}
123+
116124
task, err := found.Container.Task(ctx, nil)
117125
if err != nil {
118126
return err
@@ -143,13 +151,14 @@ func logsAction(cmd *cobra.Command, args []string) error {
143151
ContainerID: found.Container.ID(),
144152
Namespace: l[labels.Namespace],
145153
DatastoreRootPath: dataStore,
154+
LogPath: logPath,
146155
Follow: follow,
147156
Timestamps: timestamps,
148157
Tail: tail,
149158
Since: since,
150159
Until: until,
151160
}
152-
logViewer, err := logging.InitContainerLogViewer(logViewOpts, stopChannel)
161+
logViewer, err := logging.InitContainerLogViewer(l, logViewOpts, stopChannel)
153162
if err != nil {
154163
return err
155164
}
@@ -167,6 +176,23 @@ func logsAction(cmd *cobra.Command, args []string) error {
167176
return nil
168177
}
169178

179+
func getLogPath(ctx context.Context, container containerd.Container) (string, error) {
180+
extensions, err := container.Extensions(ctx)
181+
if err != nil {
182+
return "", fmt.Errorf("get extensions for container %s,failed: %#v", container.ID(), err)
183+
}
184+
metaData := extensions[k8slabels.ContainerMetadataExtension]
185+
var meta cri.ContainerMetadata
186+
if metaData != nil {
187+
err = meta.UnmarshalJSON(metaData.GetValue())
188+
if err != nil {
189+
return "", fmt.Errorf("unmarshal extensions for container %s,failed: %#v", container.ID(), err)
190+
}
191+
}
192+
193+
return meta.LogPath, nil
194+
}
195+
170196
func logsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
171197
// show container names (TODO: only show containers with logs)
172198
return shellCompleteContainerNames(cmd, nil)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Forked from https://github.com/containerd/containerd/blob/main/pkg/cri/store/container/metadata.go
18+
// Copyright The containerd Authors.
19+
// Licensed under the Apache License, Version 2.0
20+
// NOTE: we just want to get the key information of metadata( such as logpath), not all the metadata.
21+
22+
package cri
23+
24+
import (
25+
"encoding/json"
26+
"fmt"
27+
)
28+
29+
// NOTE(random-liu):
30+
// 1) Metadata is immutable after created.
31+
// 2) Metadata is checkpointed as containerd container label.
32+
33+
// metadataVersion is current version of container metadata.
34+
const metadataVersion = "v1" // nolint
35+
36+
// ContainerVersionedMetadata is the internal versioned container metadata.
37+
// nolint
38+
type ContainerVersionedMetadata struct {
39+
// Version indicates the version of the versioned container metadata.
40+
Version string
41+
// Metadata's type is criContainerMetadataInternal. If not there will be a recursive call in MarshalJSON.
42+
Metadata criContainerMetadataInternal
43+
}
44+
45+
// criContainerMetadataInternal is for internal use.
46+
type criContainerMetadataInternal ContainerMetadata
47+
48+
// ContainerMetadata is the unversioned container metadata.
49+
type ContainerMetadata struct {
50+
// LogPath is the container log path.
51+
LogPath string
52+
}
53+
54+
// MarshalJSON encodes Metadata into bytes in json format.
55+
func (c *ContainerMetadata) MarshalJSON() ([]byte, error) {
56+
return json.Marshal(&ContainerVersionedMetadata{
57+
Version: metadataVersion,
58+
Metadata: criContainerMetadataInternal(*c),
59+
})
60+
}
61+
62+
// UnmarshalJSON decodes Metadata from bytes.
63+
func (c *ContainerMetadata) UnmarshalJSON(data []byte) error {
64+
versioned := &ContainerVersionedMetadata{}
65+
if err := json.Unmarshal(data, versioned); err != nil {
66+
return err
67+
}
68+
// Handle old version after upgrade.
69+
switch versioned.Version {
70+
case metadataVersion:
71+
*c = ContainerMetadata(versioned.Metadata)
72+
return nil
73+
}
74+
return fmt.Errorf("unsupported version: %q", versioned.Version)
75+
}

pkg/labels/k8slabels/k8slabels.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ const (
2121
PodNamespace = "io.kubernetes.pod.namespace"
2222
PodName = "io.kubernetes.pod.name"
2323
ContainerName = "io.kubernetes.container.name"
24+
25+
ContainerMetadataExtension = "io.cri-containerd.container.metadata"
26+
ContainerType = "io.cri-containerd.kind"
2427
)

0 commit comments

Comments
 (0)