Skip to content

Commit

Permalink
Merge pull request #11 from ctrox/bugfixes
Browse files Browse the repository at this point in the history
Improve scheduling
  • Loading branch information
ctrox authored Jun 1, 2024
2 parents f2d618f + 78da8a8 commit 10c1997
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions cmd/installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/services/server/config"
"github.com/coreos/go-systemd/v22/dbus"
"github.com/ctrox/zeropod/zeropod"
nodev1 "k8s.io/api/node/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -62,7 +63,8 @@ network-lock skip
`
runtimeConfig = `
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.zeropod]
runtime_type = "io.containerd.zeropod.v2"
runtime_type = "io.containerd.runc.v2"
runtime_path = "%s/bin/containerd-shim-zeropod-v2"
pod_annotations = [
"zeropod.ctrox.dev/ports-map",
"zeropod.ctrox.dev/container-names",
Expand Down Expand Up @@ -252,7 +254,7 @@ func configureContainerd(runtime containerRuntime, containerdConfig string) (res
return false, err
}

if _, err := cfg.WriteString(runtimeConfig); err != nil {
if _, err := cfg.WriteString(fmt.Sprintf(runtimeConfig, *hostOptPath)); err != nil {
return false, err
}

Expand Down Expand Up @@ -319,6 +321,7 @@ func installRuntimeClass(ctx context.Context, client kubernetes.Interface) error
runtimeClass := &nodev1.RuntimeClass{
ObjectMeta: v1.ObjectMeta{Name: runtimeClassName},
Handler: runtimeHandler,
Scheduling: &nodev1.Scheduling{NodeSelector: map[string]string{zeropod.NodeLabel: "true"}},
}

if _, err := client.NodeV1().RuntimeClasses().Create(ctx, runtimeClass, v1.CreateOptions{}); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions runc/task/service_zeropod.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/containerd/cgroups"
taskAPI "github.com/containerd/containerd/api/runtime/task/v2"
"github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/pkg/cri/annotations"
"github.com/containerd/containerd/pkg/oom"
Expand Down Expand Up @@ -198,6 +199,18 @@ func (w *wrapper) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*emp
return w.service.Exec(ctx, r)
}

func (w *wrapper) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.PidsResponse, error) {
zeropodContainer, ok := w.getZeropodContainer(r.ID)
if ok && zeropodContainer.ScaledDown() {
log.G(ctx).Debugf("pids of scaled down container, returning last known pid: %v", zeropodContainer.Pid())
return &taskAPI.PidsResponse{
Processes: []*task.ProcessInfo{{Pid: uint32(zeropodContainer.Pid())}},
}, nil
}

return w.service.Pids(ctx, r)
}

func (w *wrapper) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) {
zeropodContainer, ok := w.getZeropodContainer(r.ID)
if !ok {
Expand Down
9 changes: 9 additions & 0 deletions zeropod/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/containerd/containerd/namespaces"
"github.com/containerd/log"
"github.com/mitchellh/mapstructure"
"github.com/opencontainers/runtime-spec/specs-go"
Expand All @@ -28,6 +29,7 @@ const (
portsDelim = containersDelim
mappingDelim = ";"
mapDelim = "="
defaultContainerdNS = "k8s.io"
)

type annotationConfig struct {
Expand All @@ -54,6 +56,7 @@ type Config struct {
PodName string
PodNamespace string
PodUID string
ContainerdNamespace string
spec *specs.Spec
}

Expand Down Expand Up @@ -125,6 +128,11 @@ func NewConfig(ctx context.Context, spec *specs.Spec) (*Config, error) {
containerNames = strings.Split(cfg.ZeropodContainerNames, containersDelim)
}

ns, ok := namespaces.Namespace(ctx)
if !ok {
ns = defaultContainerdNS
}

return &Config{
Ports: containerPorts,
ScaleDownDuration: dur,
Expand All @@ -136,6 +144,7 @@ func NewConfig(ctx context.Context, spec *specs.Spec) (*Config, error) {
PodName: cfg.PodName,
PodNamespace: cfg.PodNamespace,
PodUID: cfg.PodUID,
ContainerdNamespace: ns,
spec: spec,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion zeropod/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Container) Restore(ctx context.Context) (*runc.Container, process.Proce
createReq.Checkpoint = ""
}

container, err := runc.NewContainer(namespaces.WithNamespace(ctx, "k8s"), c.platform, createReq)
container, err := runc.NewContainer(namespaces.WithNamespace(ctx, c.cfg.ContainerdNamespace), c.platform, createReq)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 10c1997

Please sign in to comment.