Skip to content

Commit

Permalink
change: enable fixes by default and consolidate lookup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Sep 28, 2023
1 parent 6b96205 commit 4e8b570
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 72 deletions.
53 changes: 0 additions & 53 deletions pkg/client/fixes.go

This file was deleted.

19 changes: 10 additions & 9 deletions pkg/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,11 @@ func NodeStart(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, no

func enableFixes(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, nodeStartOpts *k3d.NodeStartOpts) error {
if node.Role == k3d.ServerRole || node.Role == k3d.AgentRole {
// FIXME: FixCgroupV2 - to be removed when fixed upstream
// auto-enable, if needed
EnableCgroupV2FixIfNeeded(runtime)

enabledFixes, anyEnabled := fixes.GetFixes(runtime)

// early exit if we don't need any fix
if !fixes.FixEnabledAny() {
if !anyEnabled {
l.Log().Debugln("No fix enabled.")
return nil
}
Expand All @@ -517,7 +516,7 @@ func enableFixes(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node,
})

// DNS Fix
if fixes.FixEnabled(fixes.EnvFixDNS) {
if enabledFixes[fixes.EnvFixDNS] {
l.Log().Debugln(">>> enabling dns magic")

for _, v := range node.Volumes {
Expand Down Expand Up @@ -545,7 +544,7 @@ func enableFixes(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node,
}

// CGroupsV2Fix
if fixes.FixEnabled(fixes.EnvFixCgroupV2) {
if enabledFixes[fixes.EnvFixCgroupV2] {
l.Log().Debugf(">>> enabling cgroupsv2 magic")

if nodeStartOpts.NodeHooks == nil {
Expand All @@ -564,7 +563,7 @@ func enableFixes(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node,
})
}

if fixes.FixEnabled(fixes.EnvFixMounts) {
if enabledFixes[fixes.EnvFixMounts] {
l.Log().Debugf(">>> enabling mounts magic")

if nodeStartOpts.NodeHooks == nil {
Expand All @@ -588,8 +587,6 @@ func enableFixes(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node,

// NodeCreate creates a new containerized k3s node
func NodeCreate(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, createNodeOpts k3d.NodeCreateOpts) error {
// FIXME: FixCgroupV2 - to be removed when fixed upstream
EnableCgroupV2FixIfNeeded(runtime)
l.Log().Tracef("Creating node from spec\n%+v", node)

/*
Expand Down Expand Up @@ -619,6 +616,10 @@ func NodeCreate(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, c
}
}

if _, any := fixes.GetFixes(runtime); any {
node.K3dEntrypoint = true
}

// memory limits
if node.Memory != "" {
if runtime != runtimes.Docker {
Expand Down
4 changes: 1 addition & 3 deletions pkg/runtimes/docker/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
l "github.com/k3d-io/k3d/v5/pkg/logger"
runtimeErr "github.com/k3d-io/k3d/v5/pkg/runtimes/errors"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/fixes"

dockercliopts "github.com/docker/cli/opts"
dockerunits "github.com/docker/go-units"
Expand Down Expand Up @@ -69,8 +68,7 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) {
containerConfig.Image = node.Image

/* Command & Arguments */
// FIXME: FixCgroupV2 - to be removed when fixed upstream
if fixes.FixEnabledAny() {
if node.K3dEntrypoint {
if node.Role == k3d.AgentRole || node.Role == k3d.ServerRole {
containerConfig.Entrypoint = []string{
"/bin/k3d-entrypoint.sh",
Expand Down
8 changes: 2 additions & 6 deletions pkg/runtimes/docker/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/fixes"
)

func TestTranslateNodeToContainer(t *testing.T) {
Expand Down Expand Up @@ -99,16 +98,13 @@ func TestTranslateNodeToContainer(t *testing.T) {
},
}

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

actualRepresentation, err := TranslateNodeToContainer(inputNode)
if err != nil {
t.Error(err)
}

actualRepresentation.ContainerConfig.Entrypoint = expectedRepresentation.ContainerConfig.Entrypoint // may change depending on the enabled fixes, so we ignore it here

if diff := deep.Equal(actualRepresentation, expectedRepresentation); diff != nil {
t.Errorf("Actual representation\n%+v\ndoes not match expected representation\n%+v\nDiff:\n%+v", actualRepresentation, expectedRepresentation, diff)
}
Expand Down
57 changes: 56 additions & 1 deletion pkg/types/fixes/fixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"os"
"strconv"

l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
)

Expand All @@ -36,7 +38,7 @@ import (

/*
* 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
* Since we're NOT running K3s as PID 1 (using init), we still need our fix even though we have the fix upstream https://github.com/k3s-io/k3s/pull/4086#issuecomment-931639392
*/

type K3DFixEnv string
Expand Down Expand Up @@ -81,3 +83,56 @@ func FixEnabledAny() bool {
}
return false
}

var EnabledFixes map[K3DFixEnv]bool
var AnyFixEnabled bool

var fixNeeded = map[K3DFixEnv]func(runtime runtimes.Runtime) bool{
EnvFixCgroupV2: func(runtime runtimes.Runtime) bool {
runtimeInfo, err := runtime.Info()
if err != nil {
l.Log().Warnf("Failed to get runtime information: %+v", err)
return false
}
cgroupVersion, err := strconv.Atoi(runtimeInfo.CgroupVersion)
if err != nil {
l.Log().Debugf("Failed to parse cgroupVersion: %+v", err)
return false
}
l.Log().Debugf("[autofix cgroupsv2] cgroupVersion: %d", cgroupVersion)
return cgroupVersion == 2
},
EnvFixDNS: func(_ runtimes.Runtime) bool {
return true
},
EnvFixMounts: func(_ runtimes.Runtime) bool {
return true
},
}

// GetFixes returns a map showing which fixes are enabled and a helper boolean indicating if any fixes are enabled
func GetFixes(runtime runtimes.Runtime) (map[K3DFixEnv]bool, bool) {
if EnabledFixes == nil {
result := make(map[K3DFixEnv]bool, len(FixEnvs))
any := false
for _, fixEnv := range FixEnvs {
enabled := false
if v, isSet := os.LookupEnv(string(fixEnv)); !isSet {
enabled = fixNeeded[fixEnv](runtime)
} else {
var err error
enabled, err = strconv.ParseBool(v)
if err != nil {
enabled = false
}
}
result[fixEnv] = enabled
if enabled {
any = true
}
}
EnabledFixes = result
AnyFixEnabled = any
}
return EnabledFixes, AnyFixEnabled
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ type Node struct {
State NodeState // filled automatically
IP NodeIP // filled automatically -> refers solely to the cluster network
HookActions []NodeHook `json:"hooks,omitempty"`
K3dEntrypoint bool
}

// ServerOpts describes some additional server role specific opts
Expand Down

0 comments on commit 4e8b570

Please sign in to comment.