Skip to content

Commit

Permalink
patch kubeadm for dualstack 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea authored and Antonio Ojea committed Apr 1, 2021
1 parent 1485b33 commit b24f7c0
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/cluster/internal/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"time"

"github.com/alessio/shellescape"
"k8s.io/apimachinery/pkg/util/version"

"sigs.k8s.io/kind/pkg/cluster/internal/delete"
"sigs.k8s.io/kind/pkg/cluster/internal/providers"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/internal/apis/config"
"sigs.k8s.io/kind/pkg/internal/apis/config/encoding"
Expand Down Expand Up @@ -106,6 +108,11 @@ func Cluster(logger log.Logger, p providers.Provider, opts *ClusterOptions) erro
return err
}

// TODO: remove once 1.20 is EOL or we no longer want to support dual-stack in 1.20 images
if err := fixupDualStack(p, opts.Config); err != nil {
return err
}

// TODO(bentheelder): make this controllable from the command line?
actionsToRun := []actions.Action{
loadbalancer.NewAction(), // setup external loadbalancer
Expand Down Expand Up @@ -255,3 +262,64 @@ func validateProvider(p providers.Provider) error {
}
return nil
}

// fixupDualStack enables the Kubeadm feature gate if kubernetes version is 1.20
// previous versions doesn't support dual-stack so it returns an error
// 1.21+ enable the feature gate by default
func fixupDualStack(p providers.Provider, cfg *config.Cluster) error {
// return early if is not dualstack
if cfg.Networking.IPFamily != config.DualStackFamily {
return nil
}

// we need to obtain the kubernetes version from the nodes
name := cfg.Name
n, err := p.ListNodes(name)
if err != nil {
return err
}
if len(n) == 0 {
return errors.New("no nodes found")
}
// use the first control plane node available
// we validate against the apiserver version.
node, err := nodeutils.BootstrapControlPlaneNode(n)
if err != nil {
return err
}
kubeVersion, err := nodeutils.KubeVersion(node)
if err != nil {
return errors.Wrapf(err, "failed to get kubernetes version from node %s", node.String())
}
ver, err := version.ParseSemantic(kubeVersion)
if err != nil {
return errors.Wrapf(err, "failed to parse node version %s", kubeVersion)
}

// dual-stack is only supported in 1.20+
if ver.LessThan(version.MustParseSemantic("v1.20.0")) {
return errors.New("failed to create cluster, dual-stack is only supported on Kubernetes versions 1.20+")
}

// dual-stack feature gate is enabled by default since 1.21
if ver.AtLeast(version.MustParseSemantic("v1.21.0")) {
return nil
}

// for 1.20 we have to explicitly enable the feature gate
// we can patch kubeadm here so we avoid to plumb this hack further
dualPatch := `
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
metadata:
name: config
featureGates:
IPv6DualStack: true
`
if len(cfg.KubeadmConfigPatches) > 0 {
cfg.KubeadmConfigPatches = append(cfg.KubeadmConfigPatches, dualPatch)
} else {
cfg.KubeadmConfigPatches = []string{dualPatch}
}
return nil
}

0 comments on commit b24f7c0

Please sign in to comment.