Skip to content

Commit

Permalink
fix/workaround: add workaround for cgroupv2 until fixed in k3s
Browse files Browse the repository at this point in the history
- Cgroupv2 fix as per k3s-io/k3s#3237 & k3s-io/k3s#3242
  • Loading branch information
iwilltry42 committed Apr 28, 2021
1 parent a576a0e commit 69456f2
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/actions/nodehooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package actions

import (
"context"
"os"

"github.com/rancher/k3d/v4/pkg/runtimes"
k3d "github.com/rancher/k3d/v4/pkg/types"
Expand All @@ -32,8 +33,9 @@ type WriteFileAction struct {
Runtime runtimes.Runtime
Content []byte
Dest string
Mode os.FileMode
}

func (act WriteFileAction) Run(ctx context.Context, node *k3d.Node) error {
return act.Runtime.WriteToNode(ctx, act.Content, act.Dest, node)
return act.Runtime.WriteToNode(ctx, act.Content, act.Dest, act.Mode, node)
}
20 changes: 20 additions & 0 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package client

import (
"context"
_ "embed"
"errors"
"fmt"
"sort"
Expand All @@ -41,6 +42,7 @@ import (
runtimeErr "github.com/rancher/k3d/v4/pkg/runtimes/errors"
"github.com/rancher/k3d/v4/pkg/types"
k3d "github.com/rancher/k3d/v4/pkg/types"
"github.com/rancher/k3d/v4/pkg/types/fixes"
"github.com/rancher/k3d/v4/pkg/types/k3s"
"github.com/rancher/k3d/v4/pkg/util"
"github.com/rancher/k3d/v4/version"
Expand Down Expand Up @@ -205,6 +207,7 @@ func ClusterPrep(ctx context.Context, runtime k3drt.Runtime, clusterConfig *conf
Runtime: runtime,
Content: regCm,
Dest: k3d.DefaultLocalRegistryHostingConfigmapTempPath,
Mode: 0644,
},
})

Expand Down Expand Up @@ -233,6 +236,23 @@ func ClusterPrep(ctx context.Context, runtime k3drt.Runtime, clusterConfig *conf
Runtime: runtime,
Content: regConfBytes,
Dest: k3d.DefaultRegistriesFilePath,
Mode: 0644,
},
})
}

// FIXME: FixCgroupV2 - to be removed when fixed upstream
if fixes.FixCgroupV2Enabled() {

log.Debugln("experimental cgroupv2 fix enabled")

clusterConfig.ClusterCreateOpts.NodeHooks = append(clusterConfig.ClusterCreateOpts.NodeHooks, k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Content: fixes.CgroupV2Entrypoint,
Dest: "/bin/entrypoint.sh",
Mode: 0744,
},
})
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/runtimes/docker/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/docker/go-connections/nat"
runtimeErr "github.com/rancher/k3d/v4/pkg/runtimes/errors"
k3d "github.com/rancher/k3d/v4/pkg/types"
"github.com/rancher/k3d/v4/pkg/types/fixes"
log "github.com/sirupsen/logrus"

dockercliopts "github.com/docker/cli/opts"
Expand All @@ -54,6 +55,15 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) {
containerConfig.Image = node.Image

/* Command & Arguments */
// FIXME: FixCgroupV2 - to be removed when fixed upstream
if fixes.FixCgroupV2Enabled() {
if node.Role == k3d.AgentRole || node.Role == k3d.ServerRole {
containerConfig.Entrypoint = []string{
"/bin/entrypoint.sh",
}
}
}

containerConfig.Cmd = []string{}

containerConfig.Cmd = append(containerConfig.Cmd, node.Cmd...) // contains k3s command and role-specific required flags/args
Expand Down
6 changes: 6 additions & 0 deletions pkg/runtimes/docker/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
k3d "github.com/rancher/k3d/v4/pkg/types"
"github.com/rancher/k3d/v4/pkg/types/fixes"
)

func TestTranslateNodeToContainer(t *testing.T) {
Expand Down Expand Up @@ -93,6 +94,11 @@ func TestTranslateNodeToContainer(t *testing.T) {
},
}

// TODO: // FIXME: FixCgroupV2 - to be removed when fixed upstream
if fixes.FixCgroupV2Enabled() {
expectedRepresentation.ContainerConfig.Entrypoint = []string{"/bin/entrypoint.sh"}
}

actualRepresentation, err := TranslateNodeToContainer(inputNode)
if err != nil {
t.Error(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/runtimes/docker/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (d Docker) CopyToNode(ctx context.Context, src string, dest string, node *k
}

// WriteToNode writes a byte array to the selected node
func (d Docker) WriteToNode(ctx context.Context, content []byte, dest string, node *k3d.Node) error {
func (d Docker) WriteToNode(ctx context.Context, content []byte, dest string, mode os.FileMode, node *k3d.Node) error {

nodeContainer, err := getNodeContainer(ctx, node)
if err != nil {
Expand All @@ -116,7 +116,7 @@ func (d Docker) WriteToNode(ctx context.Context, content []byte, dest string, no
defer tarWriter.Close()
tarHeader := &tar.Header{
Name: dest,
Mode: 0644,
Mode: int64(mode),
Size: int64(len(content)),
}

Expand All @@ -140,7 +140,6 @@ func (d Docker) WriteToNode(ctx context.Context, content []byte, dest string, no
return nil
}


// GetDockerClient returns a docker client
func GetDockerClient() (*client.Client, error) {
var err error
Expand Down
5 changes: 3 additions & 2 deletions pkg/runtimes/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"io"
"net"
"os"
"time"

"github.com/rancher/k3d/v4/pkg/runtimes/docker"
Expand Down Expand Up @@ -68,8 +69,8 @@ type Runtime interface {
ExecInNodeGetLogs(context.Context, *k3d.Node, []string) (*bufio.Reader, error)
GetNodeLogs(context.Context, *k3d.Node, time.Time) (io.ReadCloser, error)
GetImages(context.Context) ([]string, error)
CopyToNode(context.Context, string, string, *k3d.Node) error // @param context, source, destination, node
WriteToNode(context.Context, []byte, string, *k3d.Node) error // @param context, content, destination, node
CopyToNode(context.Context, string, string, *k3d.Node) error // @param context, source, destination, node
WriteToNode(context.Context, []byte, string, os.FileMode, *k3d.Node) error // @param context, content, destination, filemode, node
GetHostIP(context.Context, string) (net.IP, error)
ConnectNodeToNetwork(context.Context, *k3d.Node, string) error // @param context, node, network name
DisconnectNodeFromNetwork(context.Context, *k3d.Node, string) error // @param context, node, network name
Expand Down
22 changes: 22 additions & 0 deletions pkg/types/fixes/assets/cgroupv2-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

set -o errexit
set -o nounset
set -o pipefail

#########################################################################################################################################
# DISCLAIMER #
# Copied from https://github.com/moby/moby/blob/ed89041433a031cafc0a0f19cfe573c31688d377/hack/dind#L28-L37 #
# Permission granted by Akihiro Suda <[email protected]> (https://github.com/rancher/k3d/issues/493#issuecomment-827405962) #
# Moby License Apache 2.0: https://github.com/moby/moby/blob/ed89041433a031cafc0a0f19cfe573c31688d377/LICENSE #
#########################################################################################################################################
if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
# move the processes from the root group to the /init group,
# otherwise writing subtree_control fails with EBUSY.
mkdir -p /sys/fs/cgroup/init
busybox xargs -rn1 < /sys/fs/cgroup/cgroup.procs > /sys/fs/cgroup/init/cgroup.procs || :
# enable controllers
sed -e 's/ / +/g' -e 's/^/+/' <"/sys/fs/cgroup/cgroup.controllers" >"/sys/fs/cgroup/cgroup.subtree_control"
fi

exec /bin/k3s "$@"
47 changes: 47 additions & 0 deletions pkg/types/fixes/fixes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright © 2020 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package fixes

import (
_ "embed"
"os"
)

/* NOTE
* This file includes types used for workarounds and hotfixes which are subject to change
* and may disappear anytime, e.g. when the fix was included in an upstream project
*/

/*
* Cgroupv2 fix as per https://github.com/k3s-io/k3s/pull/3237 & https://github.com/k3s-io/k3s/pull/3242
* FIXME: FixCgroupV2 - to be removed when fixed upstream
*/

// EnvFixCgroupV2 is the environment variable that k3d will check for to enable/disable the cgroupv2 workaround
const EnvFixCgroupV2 = "K3D_FIX_CGROUPV2"

//go:embed assets/cgroupv2-entrypoint.sh
var CgroupV2Entrypoint []byte

func FixCgroupV2Enabled() bool {
return os.Getenv(EnvFixCgroupV2) == "1" || os.Getenv(EnvFixCgroupV2) == "true"
}

0 comments on commit 69456f2

Please sign in to comment.