Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions cmd/nerdctl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
"syscall"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/api/types/cri"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/labels/k8slabels"
"github.com/containerd/nerdctl/pkg/logging"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,8 +64,8 @@ func logsAction(cmd *cobra.Command, args []string) error {
}

switch globalOptions.Namespace {
case "moby", "k8s.io":
logrus.Warn("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d`")
case "moby":
logrus.Warn("Currently, `nerdctl logs` only supports containers created with `nerdctl run -d` or CRI")
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
Expand Down Expand Up @@ -113,6 +115,12 @@ func logsAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

logPath, err := getLogPath(ctx, found.Container)
if err != nil {
return err
}

task, err := found.Container.Task(ctx, nil)
if err != nil {
return err
Expand Down Expand Up @@ -143,13 +151,14 @@ func logsAction(cmd *cobra.Command, args []string) error {
ContainerID: found.Container.ID(),
Namespace: l[labels.Namespace],
DatastoreRootPath: dataStore,
LogPath: logPath,
Follow: follow,
Timestamps: timestamps,
Tail: tail,
Since: since,
Until: until,
}
logViewer, err := logging.InitContainerLogViewer(logViewOpts, stopChannel)
logViewer, err := logging.InitContainerLogViewer(l, logViewOpts, stopChannel)
if err != nil {
return err
}
Expand All @@ -167,6 +176,23 @@ func logsAction(cmd *cobra.Command, args []string) error {
return nil
}

func getLogPath(ctx context.Context, container containerd.Container) (string, error) {
extensions, err := container.Extensions(ctx)
if err != nil {
return "", fmt.Errorf("get extensions for container %s,failed: %#v", container.ID(), err)
}
metaData := extensions[k8slabels.ContainerMetadataExtension]
var meta cri.ContainerMetadata
if metaData != nil {
err = meta.UnmarshalJSON(metaData.GetValue())
if err != nil {
return "", fmt.Errorf("unmarshal extensions for container %s,failed: %#v", container.ID(), err)
}
}

return meta.LogPath, nil
}

func logsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// show container names (TODO: only show containers with logs)
return shellCompleteContainerNames(cmd, nil)
Expand Down
75 changes: 75 additions & 0 deletions pkg/api/types/cri/metadata_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Forked from https://github.com/containerd/containerd/blob/main/pkg/cri/store/container/metadata.go
// Copyright The containerd Authors.
// Licensed under the Apache License, Version 2.0
// NOTE: we just want to get the key information of metadata( such as logpath), not all the metadata.

package cri

import (
"encoding/json"
"fmt"
)

// NOTE(random-liu):
// 1) Metadata is immutable after created.
// 2) Metadata is checkpointed as containerd container label.

// metadataVersion is current version of container metadata.
const metadataVersion = "v1" // nolint

// ContainerVersionedMetadata is the internal versioned container metadata.
// nolint
type ContainerVersionedMetadata struct {
// Version indicates the version of the versioned container metadata.
Version string
// Metadata's type is criContainerMetadataInternal. If not there will be a recursive call in MarshalJSON.
Metadata criContainerMetadataInternal
}

// criContainerMetadataInternal is for internal use.
type criContainerMetadataInternal ContainerMetadata

// ContainerMetadata is the unversioned container metadata.
type ContainerMetadata struct {
// LogPath is the container log path.
LogPath string
}

// MarshalJSON encodes Metadata into bytes in json format.
func (c *ContainerMetadata) MarshalJSON() ([]byte, error) {
return json.Marshal(&ContainerVersionedMetadata{
Version: metadataVersion,
Metadata: criContainerMetadataInternal(*c),
})
}

// UnmarshalJSON decodes Metadata from bytes.
func (c *ContainerMetadata) UnmarshalJSON(data []byte) error {
versioned := &ContainerVersionedMetadata{}
if err := json.Unmarshal(data, versioned); err != nil {
return err
}
// Handle old version after upgrade.
switch versioned.Version {
case metadataVersion:
*c = ContainerMetadata(versioned.Metadata)
return nil
}
return fmt.Errorf("unsupported version: %q", versioned.Version)
}
3 changes: 3 additions & 0 deletions pkg/labels/k8slabels/k8slabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ const (
PodNamespace = "io.kubernetes.pod.namespace"
PodName = "io.kubernetes.pod.name"
ContainerName = "io.kubernetes.container.name"

ContainerMetadataExtension = "io.cri-containerd.container.metadata"
ContainerType = "io.cri-containerd.kind"
)
Loading