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

Make CNI configurable and add Calico as an option #308

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func RunClusterCreate(cmd *cobra.Command, args []string) {
datacenters, _ := cmd.Flags().GetStringSlice("datacenters")
nodeCidr, _ := cmd.Flags().GetString("node-cidr")
cloudInit, _ := cmd.Flags().GetString("cloud-init")
cni, _ := cmd.Flags().GetString("cni")

hetznerProvider := hetzner.NewHetznerProvider(AppConf.Context, AppConf.Client, clustermanager.Cluster{
Name: clusterName,
Expand Down Expand Up @@ -103,7 +104,7 @@ func RunClusterCreate(cmd *cobra.Command, args []string) {

coordinator := pkg.NewProgressCoordinator()

clusterManager := clustermanager.NewClusterManager(hetznerProvider, sshClient, coordinator, clusterName, haEnabled, isolatedEtcd, cloudInit)
clusterManager := clustermanager.NewClusterManager(hetznerProvider, sshClient, coordinator, clusterName, haEnabled, isolatedEtcd, cloudInit, cni)
cluster := clusterManager.Cluster()
saveCluster(&cluster)
renderProgressBars(&cluster, coordinator)
Expand Down Expand Up @@ -194,7 +195,7 @@ func computeMasterSteps(numMaster int, cluster *clustermanager.Cluster) int {
func validateClusterCreateFlags(cmd *cobra.Command, args []string) error {

var (
sshKey, masterServerType, workerServerType, cloudInit string
sshKey, masterServerType, workerServerType, cloudInit, cni string
)

if sshKey, _ = cmd.Flags().GetString("ssh-key"); sshKey == "" {
Expand Down Expand Up @@ -223,6 +224,10 @@ func validateClusterCreateFlags(cmd *cobra.Command, args []string) error {
}
}

if cni, _ = cmd.Flags().GetString("cni"); cni != "canal" && cni != "calico" {
return errors.New("flag --cni only allows canal or calico")
}

if _, err := AppConf.Config.FindSSHKeyByName(sshKey); err != nil {
return fmt.Errorf("SSH key '%s' not found", sshKey)
}
Expand Down Expand Up @@ -271,6 +276,7 @@ func init() {
clusterCreateCmd.Flags().IntP("worker-count", "w", 1, "Number of worker nodes for the cluster")
clusterCreateCmd.Flags().StringP("cloud-init", "", "", "Cloud-init file for server preconfiguration")
clusterCreateCmd.Flags().StringP("node-cidr", "", "10.0.1.0/24", "the CIDR for the nodes wireguard IPs")
clusterCreateCmd.Flags().StringP("cni", "", "canal", "The CNI you want to use")

// get default datacenters
dcs := []string{}
Expand Down
1 change: 1 addition & 0 deletions cmd/cluster_phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func getCommonPhaseDependencies(steps int, cmd *cobra.Command, args []string) (c
cluster.HaEnabled,
cluster.IsolatedEtcd,
cluster.CloudInitFile,
cluster.Cni,
)

return provider, clusterManager, coordinator
Expand Down
1 change: 1 addition & 0 deletions cmd/cluster_phase_install_masters.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var installMastersPhaseCommand = &cobra.Command{
cluster.HaEnabled,
cluster.IsolatedEtcd,
cluster.CloudInitFile,
cluster.Cni,
)
phase := phases.NewInstallMastersPhase(clusterManager, phaseOptions)

Expand Down
1 change: 1 addition & 0 deletions cmd/cluster_phase_install_workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var installWorkersCommand = &cobra.Command{
cluster.HaEnabled,
cluster.IsolatedEtcd,
cluster.CloudInitFile,
cluster.Cni,
)
phase := phases2.NewInstallWorkersPhase(clusterManager)

Expand Down
1 change: 1 addition & 0 deletions cmd/cluster_phase_setup_ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var setupHAPhaseCommand = &cobra.Command{
cluster.HaEnabled,
cluster.IsolatedEtcd,
cluster.CloudInitFile,
cluster.Cni,
)

phase := phases.NewSetupHighAvailabilityPhase(clusterManager)
Expand Down
1 change: 1 addition & 0 deletions docs/cluster-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ The following custom options are available for the cluster create command:
- `--worker-count`,`-w`: Number of worker nodes for the cluster , *default: 1*
- `--cloud-init`: Cloud-init file for server preconfiguration
- `--datacenters`: Can be used to filter datacenters by their name, *options: fsn-dc8, nbg1-dc3, hel1-dc2, fsn1-dc14*
- `--cni string`: The CNI you want to use, *default: canal*, *options: canal, calico*
14 changes: 12 additions & 2 deletions pkg/clustermanager/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Manager struct {
nodes []Node
clusterName string
cloudInitFile string
cni string
eventService EventService
nodeCommunicator NodeCommunicator
clusterProvider ClusterProvider
Expand All @@ -36,7 +37,7 @@ const (
)

// NewClusterManager create a new manager for the cluster
func NewClusterManager(provider ClusterProvider, nodeCommunicator NodeCommunicator, eventService EventService, name string, haEnabled bool, isolatedEtcd bool, cloudInitFile string) *Manager {
func NewClusterManager(provider ClusterProvider, nodeCommunicator NodeCommunicator, eventService EventService, name string, haEnabled bool, isolatedEtcd bool, cloudInitFile string, cni string) *Manager {
manager := &Manager{
clusterName: name,
haEnabled: haEnabled,
Expand All @@ -45,6 +46,7 @@ func NewClusterManager(provider ClusterProvider, nodeCommunicator NodeCommunicat
eventService: eventService,
nodeCommunicator: nodeCommunicator,
clusterProvider: provider,
cni: cni,
nodes: provider.GetAllNodes(),
}

Expand Down Expand Up @@ -75,6 +77,7 @@ func (manager *Manager) Cluster() Cluster {
CloudInitFile: manager.cloudInitFile,
NodeCIDR: manager.clusterProvider.GetNodeCidr(),
KubernetesVersion: "1.16.4",
Cni: manager.cni,
}
}

Expand Down Expand Up @@ -171,7 +174,14 @@ func (manager *Manager) InstallMasters(keepCerts KeepCerts) error {
commands := []NodeCommand{
{"kubeadm init", "kubectl version > /dev/null &> /dev/null || kubeadm init --ignore-preflight-errors=all --config /root/master-config.yaml"},
{"configure kubectl", "rm -rf $HOME/.kube && mkdir -p $HOME/.kube && cp -i /etc/kubernetes/admin.conf $HOME/.kube/config && chown $(id -u):$(id -g) $HOME/.kube/config"},
{"install canal", "kubectl apply -f https://docs.projectcalico.org/v3.10/manifests/canal.yaml"},
}

if manager.cni == "canal" {
commands = append(commands, NodeCommand{"install canal", "kubectl apply -f https://docs.projectcalico.org/v3.10/manifests/canal.yaml"})
} else if manager.cni == "calico" {
// We had to change the Pod CIDR to match the one defined in the kubeadm config file
// We had to tweak MTU for our install with Wireguard and the overhead it needs. Calculation is 1500 (eth0) - 80 (wirguard) - 20 (Calico IPinIP)
commands = append(commands, NodeCommand{"install calico", "cd /tmp && curl https://docs.projectcalico.org/v3.11/manifests/calico.yaml -O && sed -i -e \"s?192.168.0.0/16?10.244.0.0/16?g\" calico.yaml && sed -i -e \"s?1440?1400?g\" calico.yaml && kubectl apply -f calico.yaml"})
}

// inject custom commands
Expand Down
1 change: 1 addition & 0 deletions pkg/clustermanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Cluster struct {
CloudInitFile string `json:"cloud_init_file"`
NodeCIDR string `json:"node_cidr"`
KubernetesVersion string `json:"kubernetes_version"`
Cni string `json:"cni"`
}

// NodeCommand is the structure used to define acommand to execute on a node
Expand Down