Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ws-daemon: align to decide if cgroup v2. #9390

Merged
merged 2 commits into from
Apr 19, 2022
Merged
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
61 changes: 47 additions & 14 deletions components/ws-daemon/pkg/iws/iws.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"syscall"
"time"

"github.com/containerd/cgroups"
"github.com/opentracing/opentracing-go"
"golang.org/x/sys/unix"
"golang.org/x/time/rate"
Expand All @@ -28,6 +27,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/gitpod-io/gitpod/common-go/cgroups"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/tracing"
wsinit "github.com/gitpod-io/gitpod/content-service/pkg/initializer"
Expand Down Expand Up @@ -263,6 +263,11 @@ func (wbs *InWorkspaceServiceServer) PrepareForUserNS(ctx context.Context, req *
}

log.WithFields(wbs.Session.OWI()).WithField("configuredShift", wbs.FSShift).WithField("fwb", wbs.Session.FullWorkspaceBackup).Info("fs-shift using fuse")

if err := wbs.createWorkspaceCgroup(ctx, wscontainerID); err != nil {
return nil, err
}

return &api.PrepareForUserNSResponse{
FsShift: api.FSShiftMethod_FUSE,
FullWorkspaceBackup: wbs.Session.FullWorkspaceBackup,
Expand All @@ -287,18 +292,8 @@ func (wbs *InWorkspaceServiceServer) PrepareForUserNS(ctx context.Context, req *
return nil, status.Errorf(codes.Internal, "cannot mount shiftfs mark")
}

if cgroups.Mode() == cgroups.Unified {
cgroupBase, err := rt.ContainerCGroupPath(ctx, wscontainerID)
if err != nil {
log.WithError(err).WithFields(wbs.Session.OWI()).Error("cannot find workspace container CGroup path")
return nil, status.Errorf(codes.NotFound, "cannot find workspace container cgroup")
}

err = evacuateToCGroup(ctx, wbs.CGroupMountPoint, cgroupBase, "workspace")
if err != nil {
log.WithError(err).WithFields(wbs.Session.OWI()).Error("cannot create workspace cgroup")
return nil, status.Errorf(codes.FailedPrecondition, "cannot create workspace cgroup")
}
if err := wbs.createWorkspaceCgroup(ctx, wscontainerID); err != nil {
return nil, err
}

return &api.PrepareForUserNSResponse{
Expand All @@ -307,6 +302,38 @@ func (wbs *InWorkspaceServiceServer) PrepareForUserNS(ctx context.Context, req *
}, nil
}

func (wbs *InWorkspaceServiceServer) createWorkspaceCgroup(ctx context.Context, wscontainerID container.ID) error {
rt := wbs.Uidmapper.Runtime
if rt == nil {
return status.Errorf(codes.FailedPrecondition, "not connected to container runtime")
}

unified, err := cgroups.IsUnifiedCgroupSetup()
if err != nil {
// log error and do not expose it to the user
log.WithError(err).Error("could not determine cgroup setup")
return status.Errorf(codes.FailedPrecondition, "could not determine cgroup setup")
}

if !unified {
return nil
}

cgroupBase, err := rt.ContainerCGroupPath(ctx, wscontainerID)
if err != nil {
log.WithError(err).WithFields(wbs.Session.OWI()).Error("cannot find workspace container CGroup path")
return status.Errorf(codes.NotFound, "cannot find workspace container cgroup")
}

err = evacuateToCGroup(ctx, wbs.CGroupMountPoint, cgroupBase, "workspace")
if err != nil {
log.WithError(err).WithFields(wbs.Session.OWI()).Error("cannot create workspace cgroup")
return status.Errorf(codes.FailedPrecondition, "cannot create workspace cgroup")
}

return nil
}

func (wbs *InWorkspaceServiceServer) SetupPairVeths(ctx context.Context, req *api.SetupPairVethsRequest) (*api.SetupPairVethsResponse, error) {
rt := wbs.Uidmapper.Runtime
if rt == nil {
Expand Down Expand Up @@ -889,7 +916,13 @@ func (wbs *InWorkspaceServiceServer) WriteIDMapping(ctx context.Context, req *ap
// └── workspace drwxr-xr-x 5 gitpodUid gitpodGid
// └── user drwxr-xr-x 5 gitpodUid gitpodGid
func (wbs *InWorkspaceServiceServer) EvacuateCGroup(ctx context.Context, req *api.EvacuateCGroupRequest) (*api.EvacuateCGroupResponse, error) {
if cgroups.Mode() != cgroups.Unified {
unified, err := cgroups.IsUnifiedCgroupSetup()
if err != nil {
// log error and do not expose it to the user
log.WithError(err).Error("could not determine cgroup setup")
return nil, status.Errorf(codes.FailedPrecondition, "could not determine cgroup setup")
}
if !unified {
return &api.EvacuateCGroupResponse{}, nil
}

Expand Down