Skip to content

Commit

Permalink
Add cilium support
Browse files Browse the repository at this point in the history
  • Loading branch information
fjammes committed Apr 21, 2024
1 parent ccb60d2 commit a4af557
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .ciux
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v1alpha1
registry: k8sschool/k8s-toolbox

2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Install k8s using kind
run: |
ktbx install kind
ktbx create --single --calico
ktbx create --single -c calico
kubectl get pods -n kube-system
kubectl get nodes
- name: Start nginx
Expand Down
28 changes: 19 additions & 9 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/viper"

"github.com/k8s-school/ktbx/internal"
"github.com/k8s-school/ktbx/resources"
)

// createCmd represents the create command
Expand Down Expand Up @@ -40,9 +41,9 @@ func init() {
createCmd.PersistentFlags().BoolP(single, "s", false, "create a single node k8s cluster, take precedence over configuration file 'workers' parameter")
viper.BindPFlag(single, createCmd.PersistentFlags().Lookup(single))

calico := "calico"
createCmd.PersistentFlags().BoolP(calico, "c", false, "install calico CNI, take precedence over configuration file 'usecalico' parameter")
viper.BindPFlag("kind."+calico, createCmd.PersistentFlags().Lookup(calico))
cni := "cni"
createCmd.PersistentFlags().StringP(cni, "c", "", "install custom CNI (cilium, calico), take precedence over configuration file 'cni' parameter")
viper.BindPFlag("kind."+cni, createCmd.PersistentFlags().Lookup(cni))

auditlog := "auditlog"
createCmd.PersistentFlags().BoolP(auditlog, "a", false, "enable audit log inside API server, take precedence over configuration file 'auditlog' parameter")
Expand Down Expand Up @@ -76,13 +77,22 @@ func createCluster() {
os.Exit(1)
}

if c.Calico {
slog.Info("Install Calico CNI")
cmd = `kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/tigera-operator.yaml &&
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/custom-resources.yaml`
_, _, err = ExecCmd(cmd, false)
if len(c.Cni) != 0 {
slog.Info("Install custom CNI", "name", c.Cni)

switch c.Cni {
case "calico":
cmd = `kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/tigera-operator.yaml &&
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/custom-resources.yaml`
_, _, err = ExecCmd(cmd, false)
case "cilium":
_, _, err = ExecCmd(resources.CiliumInstallScript, false)

default:
err = fmt.Errorf("unsupported cni plugin %s", c.Cni)
}
if err != nil {
slog.Error("calico installation failed", "error", err)
slog.Error("Error while installing cni", "error", err)
os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Examples:
ktbx configgen
# Create a single-node cluster using calico CNI
ktbx create --single --calico
ktbx create --single -c calico
# Delete kind cluster
ktbx delete
Expand Down
2 changes: 1 addition & 1 deletion dot-config.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ kind:
localcertsans: false

# Use calico CNI instead of kindnet
# calico: true
# cni: calico
# Number of worker nodes
workers: 1

Expand Down
2 changes: 1 addition & 1 deletion internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type KtbxConfig struct {
ExtraMountPath string `mapstructure:"extramountpath" default:""`
LocalCertSANs bool `mapstructure:"localcertsans" default:"false"`
PrivateRegistry string `mapstructure:"privateregistry" default:""`
Calico bool `mapstructure:"calico" default:"false"`
Cni string `mapstructure:"cni" default:""`
Workers uint `mapstructure:"workers" default:"3"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func TestGetConfig(t *testing.T) {
c := GetConfig()
t.Logf("Config: %+v", c)
assert.Equal(uint(1), c.Workers)
assert.Equal(false, c.Calico)
assert.Equal("", c.Cni)

}
3 changes: 3 additions & 0 deletions resources/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var ArgoCDInstallScript string
//go:embed install-argoworkflows.sh
var ArgoWorkflowInstallScript string

//go:embed install-cilium.sh
var CiliumInstallScript string

//go:embed desk.sh
var DeskRunScript string

Expand Down
20 changes: 20 additions & 0 deletions resources/install-cilium.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Install Cilium cli and CNI plugin

# @author Fabrice Jammes

set -euxo pipefail

# Retrieve Cilium clie version with:
# curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
CILIUM_CLI_VERSION="v0.16.4"
CILIUM_VERSION="1.15.2"
CLI_ARCH=amd64
if [ "$(uname -m)" = "aarch64" ]; then CLI_ARCH=arm64; fi
curl -L --fail --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
sha256sum --check cilium-linux-${CLI_ARCH}.tar.gz.sha256sum
sudo tar xzvfC cilium-linux-${CLI_ARCH}.tar.gz /usr/local/bin
rm cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}

cilium install --version "$CILIUM_VERSION"

0 comments on commit a4af557

Please sign in to comment.