Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ windows_task:
- powershell hack/configure-windows-ci.ps1
- refreshenv
- go install .\cmd\nerdctl\
- go test -v ./cmd/...
- go test -v ./integration/...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Test suite" section in README.md has to be updated too
https://github.com/containerd/nerdctl/tree/v1.1.0#test-suite

2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ jobs:
run: |
sudo apt-get install -y expect
- name: "Ensure that the integration test suite is compatible with Docker"
run: go test -timeout 20m -v -exec sudo ./cmd/nerdctl/... -args -test.target=docker -test.kill-daemon
run: go test -timeout 20m -v -exec sudo ./integration/... -args -test.target=docker -test.kill-daemon
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build

# vagrant
/.vagrant
!cmd/nerdctl/build
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this? is it because go recognize this as a build cache for cmd/nerdctl? If so, will go really generates something in cmd/nerdctl/build?

6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ RUN curl -L -o nydus-static.tgz "https://github.com/dragonflyoss/image-service/r
tar xzf nydus-static.tgz && \
mv nydus-static/nydus-image nydus-static/nydusd nydus-static/nydusify /usr/bin/ && \
rm nydus-static.tgz
CMD ["gotestsum", "--format=testname", "--rerun-fails=2", "--packages=github.com/containerd/nerdctl/cmd/nerdctl/...", \
CMD ["gotestsum", "--format=testname", "--rerun-fails=2", "--packages=github.com/containerd/nerdctl/integration/...", \
"--", "-timeout=20m", "-args", "-test.kill-daemon"]

FROM test-integration AS test-integration-rootless
Expand All @@ -318,11 +318,11 @@ COPY ./Dockerfile.d/etc_systemd_system_user@.service.d_delegate.conf /etc/system
# ipfs daemon for rootless containerd will be enabled in /test-integration-rootless.sh
RUN systemctl disable test-integration-ipfs-offline
VOLUME /home/rootless/.local/share
RUN go test -o /usr/local/bin/nerdctl.test -c ./cmd/nerdctl
RUN go test -o /usr/local/bin/nerdctl.test -c ./integration
COPY ./Dockerfile.d/test-integration-rootless.sh /
CMD ["/test-integration-rootless.sh", \
"gotestsum", "--format=testname", "--rerun-fails=2", "--raw-command", \
"--", "/usr/local/go/bin/go", "tool", "test2json", "-t", "-p", "github.com/containerd/nerdctl/cmd/nerdctl", \
"--", "/usr/local/go/bin/go", "tool", "test2json", "-t", "-p", "github.com/containerd/nerdctl/integration/...", \
"/usr/local/bin/nerdctl.test", "-test.v", "-test.timeout=20m", "-test.kill-daemon"]

# test for CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER=slirp4netns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,10 @@
limitations under the License.
*/

package main
package apparmor

import (
"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/spf13/cobra"
)
import "github.com/spf13/cobra"

func shellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
profiles, err := apparmorutil.Profiles()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string // nolint: prealloc
for _, f := range profiles {
names = append(names, f.Name)
}
return names, cobra.ShellCompDirectiveNoFileComp
func AddApparmorCommand(rootCmd *cobra.Command) {
// NOP
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package apparmor

import (
"fmt"
Expand All @@ -29,14 +29,14 @@ func newApparmorInspectCommand() *cobra.Command {
Use: "inspect",
Short: fmt.Sprintf("Display the default AppArmor profile %q. Other profiles cannot be displayed with this command.", defaults.AppArmorProfileName),
Args: cobra.NoArgs,
RunE: apparmorInspectAction,
RunE: InspectAction,
SilenceUsage: true,
SilenceErrors: true,
}
return cmd
}

func apparmorInspectAction(cmd *cobra.Command, args []string) error {
func InspectAction(cmd *cobra.Command, args []string) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why exported func ?

b, err := apparmor.DumpDefaultProfile(defaults.AppArmorProfileName)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
limitations under the License.
*/

package main
package apparmor

import (
"github.com/containerd/nerdctl/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/cmd/nerdctl/utils/common"
"github.com/spf13/cobra"
)

func newApparmorCommand() *cobra.Command {
func NewApparmorCommand() *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{Category: Management},
Annotations: map[string]string{common.Category: common.Management},
Use: "apparmor",
Short: "Manage AppArmor profiles",
RunE: unknownSubcommandAction,
RunE: completion.UnknownSubcommandAction,
SilenceUsage: true,
SilenceErrors: true,
}
Expand All @@ -37,3 +39,7 @@ func newApparmorCommand() *cobra.Command {
)
return cmd
}

func AddApparmorCommand(rootCmd *cobra.Command) {
rootCmd.AddCommand(NewApparmorCommand())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the logic of adding new Command(s) the rootCmd should stay in the main file in order to keep consistency with the standard common adding

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package apparmor

import (
"fmt"
Expand All @@ -30,14 +30,14 @@ func newApparmorLoadCommand() *cobra.Command {
Use: "load",
Short: fmt.Sprintf("Load the default AppArmor profile %q. Requires root.", defaults.AppArmorProfileName),
Args: cobra.NoArgs,
RunE: apparmorLoadAction,
RunE: LoadAction,
SilenceUsage: true,
SilenceErrors: true,
}
return cmd
}

func apparmorLoadAction(cmd *cobra.Command, args []string) error {
func LoadAction(cmd *cobra.Command, args []string) error {
logrus.Infof("Loading profile %q", defaults.AppArmorProfileName)
return apparmor.LoadDefaultProfile(defaults.AppArmorProfileName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package apparmor

import (
"bytes"
Expand All @@ -23,6 +23,7 @@ import (
"text/tabwriter"
"text/template"

"github.com/containerd/nerdctl/cmd/nerdctl/utils/fmtutil"
"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/spf13/cobra"
)
Expand All @@ -33,7 +34,7 @@ func newApparmorLsCommand() *cobra.Command {
Aliases: []string{"list"},
Short: "List the loaded AppArmor profiles",
Args: cobra.NoArgs,
RunE: apparmorLsAction,
RunE: LsAction,
SilenceUsage: true,
SilenceErrors: true,
}
Expand All @@ -46,7 +47,7 @@ func newApparmorLsCommand() *cobra.Command {
return cmd
}

func apparmorLsAction(cmd *cobra.Command, args []string) error {
func LsAction(cmd *cobra.Command, args []string) error {
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
Expand All @@ -70,7 +71,7 @@ func apparmorLsAction(cmd *cobra.Command, args []string) error {
return errors.New("format and quiet must not be specified together")
}
var err error
tmpl, err = parseTemplate(format)
tmpl, err = fmtutil.ParseTemplate(format)
if err != nil {
return err
}
Expand All @@ -96,7 +97,7 @@ func apparmorLsAction(cmd *cobra.Command, args []string) error {
fmt.Fprintf(w, "%s\t%s\n", f.Name, f.Mode)
}
}
if f, ok := w.(Flusher); ok {
if f, ok := w.(fmtutil.Flusher); ok {
return f.Flush()
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
limitations under the License.
*/

package main
package apparmor

import (
"fmt"

"github.com/containerd/nerdctl/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/sirupsen/logrus"
Expand All @@ -30,23 +31,19 @@ func newApparmorUnloadCommand() *cobra.Command {
Use: "unload [PROFILE]",
Short: fmt.Sprintf("Unload an AppArmor profile. The target profile name defaults to %q. Requires root.", defaults.AppArmorProfileName),
Args: cobra.MaximumNArgs(1),
RunE: apparmorUnloadAction,
ValidArgsFunction: apparmorUnloadShellComplete,
RunE: UnloadAction,
ValidArgsFunction: completion.ApparmorUnloadShellComplete,
SilenceUsage: true,
SilenceErrors: true,
}
return cmd
}

func apparmorUnloadAction(cmd *cobra.Command, args []string) error {
func UnloadAction(cmd *cobra.Command, args []string) error {
target := defaults.AppArmorProfileName
if len(args) > 0 {
target = args[0]
}
logrus.Infof("Unloading profile %q", target)
return apparmorutil.Unload(target)
}

func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return shellCompleteApparmorProfiles(cmd)
}
23 changes: 23 additions & 0 deletions cmd/nerdctl/apparmor/apparmor_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
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.
*/

package apparmor

import "github.com/spf13/cobra"

func AddApparmorCommand(rootCmd *cobra.Command) {
// NOP
}
34 changes: 24 additions & 10 deletions cmd/nerdctl/build.go → cmd/nerdctl/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package builder

import (
"encoding/json"
Expand All @@ -30,6 +30,9 @@ import (

"github.com/containerd/containerd/errdefs"
dockerreference "github.com/containerd/containerd/reference/docker"
ncclient "github.com/containerd/nerdctl/cmd/nerdctl/client"
"github.com/containerd/nerdctl/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/cmd/nerdctl/utils"
"github.com/containerd/nerdctl/pkg/buildkitutil"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/containerd/nerdctl/pkg/platformutil"
Expand All @@ -39,7 +42,7 @@ import (
"github.com/spf13/cobra"
)

func newBuildCommand() *cobra.Command {
func NewBuildCommand() *cobra.Command {
var buildCommand = &cobra.Command{
Use: "build [flags] PATH",
Short: "Build an image from a Dockerfile. Needs buildkitd to be running.",
Expand All @@ -49,7 +52,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
SilenceUsage: true,
SilenceErrors: true,
}
AddStringFlag(buildCommand, "buildkit-host", nil, defaults.BuildKitHost(), "BUILDKIT_HOST", "BuildKit address")
utils.AddStringFlag(buildCommand, "buildkit-host", nil, defaults.BuildKitHost(), "BUILDKIT_HOST", "BuildKit address")
buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
buildCommand.Flags().String("target", "", "Set the target build stage to build")
Expand All @@ -67,7 +70,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
// #region platform flags
// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
buildCommand.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")")
buildCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
buildCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms)
// #endregion

buildCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS")
Expand All @@ -77,7 +80,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
return buildCommand
}

func getBuildkitHost(cmd *cobra.Command) (string, error) {
func BuildkitHost(cmd *cobra.Command) (string, error) {
if cmd.Flags().Changed("buildkit-host") || os.Getenv("BUILDKIT_HOST") != "" {
// If address is explicitly specified, use it.
buildkitHost, err := cmd.Flags().GetString("buildkit-host")
Expand Down Expand Up @@ -132,7 +135,7 @@ func buildAction(cmd *cobra.Command, args []string) error {
}
platform = strutil.DedupeStrSlice(platform)

buildkitHost, err := getBuildkitHost(cmd)
buildkitHost, err := BuildkitHost(cmd)
if err != nil {
return err
}
Expand All @@ -151,7 +154,7 @@ func buildAction(cmd *cobra.Command, args []string) error {
}
if runIPFSRegistry {
logrus.Infof("Ensuring IPFS registry is running")
nerdctlCmd, nerdctlArgs := globalFlags(cmd)
nerdctlCmd, nerdctlArgs := utils.GlobalFlags(cmd)
out, err := exec.Command(nerdctlCmd, append(nerdctlArgs, "ipfs", "registry", "up")...).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to start IPFS registry: %v: %v", string(out), err)
Expand Down Expand Up @@ -190,7 +193,7 @@ func buildAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err = loadImage(buildctlStdout, cmd, platMC, quiet); err != nil {
if err = utils.LoadImage(buildctlStdout, cmd, platMC, quiet); err != nil {
return err
}
}
Expand All @@ -212,7 +215,7 @@ func buildAction(cmd *cobra.Command, args []string) error {

if len(tags) > 1 {
logrus.Debug("Found more than 1 tag")
client, ctx, cancel, err := newClient(cmd)
client, ctx, cancel, err := ncclient.New(cmd)
if err != nil {
return fmt.Errorf("unable to tag images: %s", err)
}
Expand Down Expand Up @@ -257,7 +260,7 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
return "", nil, false, "", nil, nil, err
}
if output == "" {
client, ctx, cancel, err := newClient(cmd)
client, ctx, cancel, err := ncclient.New(cmd)
if err != nil {
return "", nil, false, "", nil, nil, err
}
Expand Down Expand Up @@ -499,6 +502,17 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
return buildctlBinary, buildctlArgs, needsLoading, metaFile, tags, cleanup, nil
}

func CreateBuildContext(dockerfile string) (string, error) {
tmpDir, err := os.MkdirTemp("", "nerdctl-build-test")
if err != nil {
return "", err
}
if err = os.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
return "", err
}
return tmpDir, nil
}

func getDigestFromMetaFile(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
Expand Down
Loading