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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ require (
go.opentelemetry.io/otel/sdk v1.34.0
go.opentelemetry.io/otel/trace v1.34.0
go.uber.org/mock v0.5.0
golang.org/x/net v0.34.0
golang.org/x/sync v0.11.0
golang.org/x/sys v0.30.0
google.golang.org/grpc v1.70.0
Expand Down Expand Up @@ -221,7 +222,6 @@ require (
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/oauth2 v0.26.0 // indirect
golang.org/x/term v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions internal/config/device/device_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func (d *Config) LoadDevices(devsFromConfig []string) error {
func (d *Config) Devices() []Device {
return nil
}

func DevicesFromAnnotation(annotation string, allowedDevices []string) ([]Device, error) {
return []Device{}, nil
}
33 changes: 0 additions & 33 deletions internal/factory/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
validate "github.com/opencontainers/runtime-tools/validate/capabilities"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
"github.com/syndtr/gocapability/capability"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
Expand Down Expand Up @@ -523,38 +522,6 @@ func (c *container) ReadOnly(serverIsReadOnly bool) bool {
return serverIsReadOnly
}

// SelinuxLabel returns the container's SelinuxLabel
// it takes the sandbox's label, which it falls back upon.
func (c *container) SelinuxLabel(sboxLabel string) ([]string, error) {
selinuxConfig := c.config.Linux.SecurityContext.SelinuxOptions

labels := map[string]string{}

labelOptions, err := label.DupSecOpt(sboxLabel)
if err != nil {
return nil, err
}

for _, r := range labelOptions {
k := strings.Split(r, ":")[0]
labels[k] = r
}

if selinuxConfig != nil {
for _, r := range utils.GetLabelOptions(selinuxConfig) {
k := strings.Split(r, ":")[0]
labels[k] = r
}
}

ret := []string{}
for _, v := range labels {
ret = append(ret, v)
}

return ret, nil
}

// AddUnifiedResourcesFromAnnotations adds the cgroup-v2 resources specified in the io.kubernetes.cri-o.UnifiedCgroup annotation.
func (c *container) AddUnifiedResourcesFromAnnotations(annotationsMap map[string]string) error {
if c.config == nil || c.config.Labels == nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/factory/container/container_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package container

func (c *container) SelinuxLabel(sboxLabel string) ([]string, error) {
return []string{}, nil
}
41 changes: 41 additions & 0 deletions internal/factory/container/container_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package container

import (
"strings"

"github.com/opencontainers/selinux/go-selinux/label"

"github.com/cri-o/cri-o/utils"
)

// SelinuxLabel returns the container's SelinuxLabel
// it takes the sandbox's label, which it falls back upon.
func (c *container) SelinuxLabel(sboxLabel string) ([]string, error) {
selinuxConfig := c.config.Linux.SecurityContext.SelinuxOptions

labels := map[string]string{}

labelOptions, err := label.DupSecOpt(sboxLabel)
if err != nil {
return nil, err
}

Check warning on line 21 in internal/factory/container/container_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/container_linux.go#L20-L21

Added lines #L20 - L21 were not covered by tests

for _, r := range labelOptions {
k := strings.Split(r, ":")[0]
labels[k] = r
}

if selinuxConfig != nil {
for _, r := range utils.GetLabelOptions(selinuxConfig) {
k := strings.Split(r, ":")[0]
labels[k] = r
}
}

ret := []string{}
for _, v := range labels {
ret = append(ret, v)
}

return ret, nil
}
9 changes: 9 additions & 0 deletions internal/factory/container/device_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package container

import (
devicecfg "github.com/cri-o/cri-o/internal/config/device"
)

func (c *container) SpecAddDevices(configuredDevices, annotationDevices []devicecfg.Device, privilegedWithoutHostDevices, enableDeviceOwnershipFromSecurityContext bool) error {
return nil
}
2 changes: 1 addition & 1 deletion internal/factory/container/device_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux
//go:build !linux && !freebsd

package container

Expand Down
97 changes: 0 additions & 97 deletions internal/factory/container/namespaces.go
Original file line number Diff line number Diff line change
@@ -1,106 +1,9 @@
package container

import (
"errors"
"fmt"

rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
types "k8s.io/cri-api/pkg/apis/runtime/v1"

"github.com/cri-o/cri-o/internal/config/nsmgr"
"github.com/cri-o/cri-o/internal/lib/namespace"
oci "github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/pkg/config"
)

func (c *container) SpecAddNamespaces(sb SandboxIFace, targetCtr *oci.Container, serverConfig *config.Config) error {
// Join the namespace paths for the pod sandbox container.
if err := ConfigureGeneratorGivenNamespacePaths(sb.NamespacePaths(), &c.spec); err != nil {
return fmt.Errorf("failed to configure namespaces in container create: %w", err)
}

sc := c.config.Linux.SecurityContext

if sc.NamespaceOptions.Network == types.NamespaceMode_NODE {
if err := c.spec.RemoveLinuxNamespace(string(rspec.NetworkNamespace)); err != nil {
return err
}
}

switch sc.NamespaceOptions.Pid {
case types.NamespaceMode_NODE:
// kubernetes PodSpec specify to use Host PID namespace
if err := c.spec.RemoveLinuxNamespace(string(rspec.PIDNamespace)); err != nil {
return err
}
case types.NamespaceMode_POD:
pidNsPath := sb.PidNsPath()
if pidNsPath == "" {
if sb.NamespaceOptions().Pid != types.NamespaceMode_POD {
return errors.New("pod level PID namespace requested for the container, but pod sandbox was not similarly configured, and does not have an infra container")
}

return errors.New("PID namespace requested, but sandbox infra container unexpectedly invalid")
}

if err := c.spec.AddOrReplaceLinuxNamespace(string(rspec.PIDNamespace), pidNsPath); err != nil {
return fmt.Errorf("updating container PID namespace to pod: %w", err)
}
case types.NamespaceMode_TARGET:
if targetCtr == nil {
return errors.New("target PID namespace specified with invalid target ID")
}

targetPID, err := targetCtr.Pid()
if err != nil {
return fmt.Errorf("target PID namespace find PID: %w", err)
}

ns, err := serverConfig.NamespaceManager().NamespaceFromProcEntry(targetPID, nsmgr.PIDNS)
if err != nil {
return fmt.Errorf("target PID namespace from proc: %w", err)
}

if err := c.spec.AddOrReplaceLinuxNamespace(string(rspec.PIDNamespace), ns.Path()); err != nil {
return fmt.Errorf("updating container PID namespace to target %s: %w", targetCtr.ID(), err)
}

c.pidns = ns
}

return nil
}

// ConfigureGeneratorGivenNamespacePaths takes a map of nsType -> nsPath. It configures the generator
// to add or replace the defaults to these paths.
func ConfigureGeneratorGivenNamespacePaths(managedNamespaces []*namespace.ManagedNamespace, g *generate.Generator) error {
typeToSpec := map[nsmgr.NSType]rspec.LinuxNamespaceType{
nsmgr.IPCNS: rspec.IPCNamespace,
nsmgr.NETNS: rspec.NetworkNamespace,
nsmgr.UTSNS: rspec.UTSNamespace,
nsmgr.USERNS: rspec.UserNamespace,
}

for _, ns := range managedNamespaces {
// allow for empty paths, as this namespace just shouldn't be configured
if ns.Path() == "" {
continue
}

nsForSpec := typeToSpec[ns.Type()]
if nsForSpec == "" {
return fmt.Errorf("invalid namespace type %s", ns.Type())
}

if err := g.AddOrReplaceLinuxNamespace(string(nsForSpec), ns.Path()); err != nil {
return err
}
}

return nil
}

func (c *container) PidNamespace() nsmgr.Namespace {
return c.pidns
}
20 changes: 20 additions & 0 deletions internal/factory/container/namespaces_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package container

import (
"github.com/cri-o/cri-o/internal/config/nsmgr"
oci "github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/pkg/config"
)

func (c *container) SpecAddNamespaces(sb SandboxIFace, targetCtr *oci.Container, serverConfig *config.Config) error {
// Join the namespace paths for the pod sandbox container.
managedNamespaces := sb.NamespacePaths()

for _, ns := range managedNamespaces {
if ns.Type() == nsmgr.NETNS {
c.Spec().AddAnnotation("org.freebsd.parentJail", ns.Path())
}
}

return nil
}
102 changes: 102 additions & 0 deletions internal/factory/container/namespaces_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package container

import (
"errors"
"fmt"

rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
types "k8s.io/cri-api/pkg/apis/runtime/v1"

"github.com/cri-o/cri-o/internal/config/nsmgr"
"github.com/cri-o/cri-o/internal/lib/namespace"
oci "github.com/cri-o/cri-o/internal/oci"
"github.com/cri-o/cri-o/pkg/config"
)

func (c *container) SpecAddNamespaces(sb SandboxIFace, targetCtr *oci.Container, serverConfig *config.Config) error {
// Join the namespace paths for the pod sandbox container.
if err := ConfigureGeneratorGivenNamespacePaths(sb.NamespacePaths(), &c.spec); err != nil {
return fmt.Errorf("failed to configure namespaces in container create: %w", err)
}

Check warning on line 21 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L20-L21

Added lines #L20 - L21 were not covered by tests

sc := c.config.Linux.SecurityContext

if sc.NamespaceOptions.Network == types.NamespaceMode_NODE {
if err := c.spec.RemoveLinuxNamespace(string(rspec.NetworkNamespace)); err != nil {
return err
}

Check warning on line 28 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}

switch sc.NamespaceOptions.Pid {
case types.NamespaceMode_NODE:
// kubernetes PodSpec specify to use Host PID namespace
if err := c.spec.RemoveLinuxNamespace(string(rspec.PIDNamespace)); err != nil {
return err
}

Check warning on line 36 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L35-L36

Added lines #L35 - L36 were not covered by tests
case types.NamespaceMode_POD:
pidNsPath := sb.PidNsPath()
if pidNsPath == "" {
if sb.NamespaceOptions().Pid != types.NamespaceMode_POD {
return errors.New("pod level PID namespace requested for the container, but pod sandbox was not similarly configured, and does not have an infra container")
}

Check warning on line 42 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L40-L42

Added lines #L40 - L42 were not covered by tests

return errors.New("PID namespace requested, but sandbox infra container unexpectedly invalid")

Check warning on line 44 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L44

Added line #L44 was not covered by tests
}

if err := c.spec.AddOrReplaceLinuxNamespace(string(rspec.PIDNamespace), pidNsPath); err != nil {
return fmt.Errorf("updating container PID namespace to pod: %w", err)
}

Check warning on line 49 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L48-L49

Added lines #L48 - L49 were not covered by tests
case types.NamespaceMode_TARGET:
if targetCtr == nil {
return errors.New("target PID namespace specified with invalid target ID")
}

Check warning on line 53 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L52-L53

Added lines #L52 - L53 were not covered by tests

targetPID, err := targetCtr.Pid()
if err != nil {
return fmt.Errorf("target PID namespace find PID: %w", err)
}

Check warning on line 58 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L57-L58

Added lines #L57 - L58 were not covered by tests

ns, err := serverConfig.NamespaceManager().NamespaceFromProcEntry(targetPID, nsmgr.PIDNS)
if err != nil {
return fmt.Errorf("target PID namespace from proc: %w", err)
}

Check warning on line 63 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L62-L63

Added lines #L62 - L63 were not covered by tests

if err := c.spec.AddOrReplaceLinuxNamespace(string(rspec.PIDNamespace), ns.Path()); err != nil {
return fmt.Errorf("updating container PID namespace to target %s: %w", targetCtr.ID(), err)
}

Check warning on line 67 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L66-L67

Added lines #L66 - L67 were not covered by tests

c.pidns = ns
}

return nil
}

// ConfigureGeneratorGivenNamespacePaths takes a map of nsType -> nsPath. It configures the generator
// to add or replace the defaults to these paths.
func ConfigureGeneratorGivenNamespacePaths(managedNamespaces []*namespace.ManagedNamespace, g *generate.Generator) error {
typeToSpec := map[nsmgr.NSType]rspec.LinuxNamespaceType{
nsmgr.IPCNS: rspec.IPCNamespace,
nsmgr.NETNS: rspec.NetworkNamespace,
nsmgr.UTSNS: rspec.UTSNamespace,
nsmgr.USERNS: rspec.UserNamespace,
}

for _, ns := range managedNamespaces {
// allow for empty paths, as this namespace just shouldn't be configured
if ns.Path() == "" {
continue

Check warning on line 88 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L88

Added line #L88 was not covered by tests
}

nsForSpec := typeToSpec[ns.Type()]
if nsForSpec == "" {
return fmt.Errorf("invalid namespace type %s", ns.Type())
}

Check warning on line 94 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L93-L94

Added lines #L93 - L94 were not covered by tests

if err := g.AddOrReplaceLinuxNamespace(string(nsForSpec), ns.Path()); err != nil {
return err
}

Check warning on line 98 in internal/factory/container/namespaces_linux.go

View check run for this annotation

Codecov / codecov/patch

internal/factory/container/namespaces_linux.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

return nil
}
5 changes: 2 additions & 3 deletions internal/linklogs/link_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/opencontainers/selinux/go-selinux/label"
"golang.org/x/sys/unix"
"k8s.io/apimachinery/pkg/util/validation"
types "k8s.io/cri-api/pkg/apis/runtime/v1"

Expand Down Expand Up @@ -49,7 +48,7 @@ func MountPodLogs(ctx context.Context, kubePodUID, emptyDirVolName, namespace, k

log.Infof(ctx, "Mounting from %s to %s for linked logs", podLogsPath, emptyDirLoggingVolumePath)

if err := unix.Mount(podLogsPath, emptyDirLoggingVolumePath, "bind", unix.MS_BIND|unix.MS_RDONLY, ""); err != nil {
if err := mountLogPath(podLogsPath, emptyDirLoggingVolumePath); err != nil {
return fmt.Errorf("failed to mount %v to %v: %w", podLogsPath, emptyDirLoggingVolumePath, err)
}

Expand All @@ -70,7 +69,7 @@ func UnmountPodLogs(ctx context.Context, kubePodUID, emptyDirVolName string) err
log.Infof(ctx, "Unmounting %s for linked logs", emptyDirLoggingVolumePath)

if _, err := os.Stat(emptyDirLoggingVolumePath); !os.IsNotExist(err) {
if err := unix.Unmount(emptyDirLoggingVolumePath, unix.MNT_DETACH); err != nil {
if err := unmountLogPath(emptyDirLoggingVolumePath); err != nil {
return fmt.Errorf("failed to unmounts logs: %w", err)
}
}
Expand Down
Loading
Loading