From d9d01f11b874056bfed33cb19e0721c97e63f639 Mon Sep 17 00:00:00 2001
From: Pascal Morillon <pascal.morillon@irisa.fr>
Date: Mon, 21 Jan 2019 21:23:59 +0100
Subject: [PATCH] Add Metallb deployement task

---
 kubernetes/metallb/cm-metallb.yaml.erb | 12 ++++++++++++
 tasks/kube.rb                          | 25 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 kubernetes/metallb/cm-metallb.yaml.erb

diff --git a/kubernetes/metallb/cm-metallb.yaml.erb b/kubernetes/metallb/cm-metallb.yaml.erb
new file mode 100644
index 0000000..8cd1949
--- /dev/null
+++ b/kubernetes/metallb/cm-metallb.yaml.erb
@@ -0,0 +1,12 @@
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: metallb-config
+data:
+  config: |
+    address-pools:
+    - name: default
+      protocol: layer2
+      addresses:
+      - <%= rangeStr %>
\ No newline at end of file
diff --git a/tasks/kube.rb b/tasks/kube.rb
index 5791c1b..9b30177 100644
--- a/tasks/kube.rb
+++ b/tasks/kube.rb
@@ -1,3 +1,5 @@
+require 'erb'
+
 namespace :kube do
 
   desc 'Init master'
@@ -99,4 +101,27 @@ namespace :kube do
     end
   end
 
+  desc 'Deploy metallb'
+  task :deployMetallb do
+    subnet = xp.job_with_name(XP5K::Config[:jobname])['resources_by_type']['subnets'].first
+    ipaddr = IPAddr.new(subnet)
+    range = ipaddr.to_range.to_a[257..510]
+    rangeStr = "#{range.first.to_s}-#{range.last.to_s}"
+    tmpl = ERB.new(File.read('kubernetes/metallb/cm-metallb.yaml.erb'))
+    cm = tmpl.result(binding)
+    File.open("kubernetes/metallb/cm-metallb.yaml", 'w') do |file|
+      file.puts cm
+    end
+    ip = clusterDesc()['master']['kubemaster']
+    repoLocalPath = Dir.pwd
+    repoVmPath = repoLocalPath.sub(ENV['HOME'], "/mnt/home")
+    on(ip, :user => 'root') do
+      [
+        'kubectl create ns metallb',
+        "kubectl -n metallb apply -f #{repoVmPath}/kubernetes/metallb/cm-metallb.yaml",
+        %{helm install -n metallb --namespace metallb stable/metallb}
+      ]
+    end
+  end
+
 end
-- 
GitLab