Skip to content

Commit

Permalink
Add cli option to choose CNI
Browse files Browse the repository at this point in the history
  • Loading branch information
aojeagarcia committed Mar 7, 2019
1 parent 0e39a65 commit 865397d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
3 changes: 3 additions & 0 deletions cmd/kind/create/cluster/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type flagpole struct {
Name string
Config string
ImageName string
CNI string
Retain bool
IPv6 bool
Wait time.Duration
Expand All @@ -55,6 +56,7 @@ func NewCommand() *cobra.Command {
cmd.Flags().StringVar(&flags.Name, "name", cluster.DefaultName, "cluster context name")
cmd.Flags().StringVar(&flags.Config, "config", "", "path to a kind config file")
cmd.Flags().StringVar(&flags.ImageName, "image", "", "node docker image to use for booting the cluster")
cmd.Flags().StringVar(&flags.CNI, "cni", "", "URL with the CNI installation manifest")
cmd.Flags().BoolVar(&flags.Retain, "retain", false, "retain nodes for debugging when cluster creation fails")
cmd.Flags().BoolVar(&flags.IPv6, "ipv6", false, "creates an IPv6 cluster")
cmd.Flags().DurationVar(&flags.Wait, "wait", time.Duration(0), "Wait for control plane node to be ready (default 0s)")
Expand Down Expand Up @@ -109,6 +111,7 @@ func runE(flags *flagpole, cmd *cobra.Command, args []string) error {
create.Retain(flags.Retain),
create.WaitForReady(flags.Wait),
create.IPv6(flags.IPv6),
create.CNI(flags.CNI),
); err != nil {
return errors.Wrap(err, "failed to create cluster")
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/cluster/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ func IPv6(ipv6 bool) ClusterOption {
return o
}
}

// CNI install a different CNI plugins using the manifest provided
func CNI(cni string) ClusterOption {
return func(o *internalcreate.Options) *internalcreate.Options {
o.CNI = cni
return o
}
}
3 changes: 3 additions & 0 deletions pkg/cluster/internal/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Options struct {
Retain bool
WaitForReady time.Duration
IPv6 bool
CNI string
}

// Cluster creates a cluster
Expand Down Expand Up @@ -85,6 +86,8 @@ func Cluster(c *context.Context, cfg *config.Config, opts *Options) error {
cc.IPv6 = true
}

cc.CNI = opts.CNI

// attempt to explicitly pull the required node images if they doesn't exist locally
// we don't care if this errors, we'll still try to run which also pulls
cc.EnsureNodeImages()
Expand Down
1 change: 1 addition & 0 deletions pkg/cluster/internal/create/createcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Context struct {
*DerivedConfig
Retain bool // if we should retain nodes after failing to create.
IPv6 bool // use IPv6 to configure the cluster.
CNI string // install a different CNI plugin
ExecOptions []ExecOption // options to be forwarded to the exec command.
}

Expand Down
48 changes: 31 additions & 17 deletions pkg/cluster/internal/create/kubeadm-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,39 @@ func runKubeadmInit(ec *execContext, configNode *NodeReplica) error {
// install the CNI network plugin
// TODO(bentheelder): support other overlay networks
// first probe for a pre-installed manifest
haveDefaultCNIManifest := true
if err := node.Command("test", "-f", "/kind/manifests/default-cni.yaml").Run(); err != nil {
haveDefaultCNIManifest = false
}
if haveDefaultCNIManifest {
// we found the default manifest, install that
// the images should already be loaded along with kubernetes
if err := node.Command(
"kubectl", "create", "--kubeconfig=/etc/kubernetes/admin.conf",
"-f", "/kind/manifests/default-cni.yaml",
).Run(); err != nil {
return errors.Wrap(err, "failed to apply overlay network")
log.Debug("CNI option ", ec.Context.CNI, "\n")
if ec.Context.CNI == "" {
haveDefaultCNIManifest := true
if err := node.Command("test", "-f", "/kind/manifests/default-cni.yaml").Run(); err != nil {
haveDefaultCNIManifest = false
}

if haveDefaultCNIManifest {
// we found the default manifest, install that
// the images should already be loaded along with kubernetes
if err := node.Command(
"kubectl", "create", "--kubeconfig=/etc/kubernetes/admin.conf",
"-f", "/kind/manifests/default-cni.yaml",
).Run(); err != nil {
return errors.Wrap(err, "failed to apply overlay network")
}
} else {
// fallback to our old pattern of installing weave using their recommended method
if err := node.Command(
"/bin/sh", "-c",
`kubectl apply --kubeconfig=/etc/kubernetes/admin.conf -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version --kubeconfig=/etc/kubernetes/admin.conf | base64 | tr -d '\n')"`,
).Run(); err != nil {
return errors.Wrap(err, "failed to apply overlay network")
}
}
} else {
// fallback to our old pattern of installing weave using their recommended method
if err := node.Command(
"/bin/sh", "-c",
`kubectl apply --kubeconfig=/etc/kubernetes/admin.conf -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version --kubeconfig=/etc/kubernetes/admin.conf | base64 | tr -d '\n')"`,
).Run(); err != nil {
cmd := node.Command(
"kubectl", "--kubeconfig=/etc/kubernetes/admin.conf",
"apply", "-f", ec.Context.CNI,
)
lines, err := exec.CombinedOutputLines(cmd)
log.Debug(strings.Join(lines, "\n"))
if err != nil {
return errors.Wrap(err, "failed to apply overlay network")
}
}
Expand Down

0 comments on commit 865397d

Please sign in to comment.