From 96cf11cf0e0e289178c98d3e5c168920cfa12f74 Mon Sep 17 00:00:00 2001
From: Alex ubuntu vm <alexdecb@yahoo.es>
Date: Wed, 29 Nov 2023 10:35:22 +0100
Subject: [PATCH] general: added bridge interfaces between switches and pods.
 \n This is with the objective  of easing and automating the installation of
 l2sm, without having the user to configure the ip addresses in their machine

---
 K8s/interfaces_definitions/vhost1.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost10.yaml          |  6 +++---
 K8s/interfaces_definitions/vhost2.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost3.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost4.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost5.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost6.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost7.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost8.yaml           |  6 +++---
 K8s/interfaces_definitions/vhost9.yaml           |  6 +++---
 K8s/interfaces_definitions/vpod1.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod10.yaml           | 11 -----------
 K8s/interfaces_definitions/vpod2.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod3.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod4.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod5.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod6.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod7.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod8.yaml            | 11 -----------
 K8s/interfaces_definitions/vpod9.yaml            | 11 -----------
 operator/daemonset/l2sm-switch-amd64.yaml        | 10 +++++-----
 operator/deploy/controller/deployController.yaml |  4 ++++
 operator/deploy/deployOperator.yaml              |  1 +
 operator/src/operator/Dockerfile                 |  2 +-
 operator/src/operator/l2sm-operator.py           |  8 ++++----
 operator/src/switch/main.go                      | 14 +++++++-------
 operator/src/switch/setup_switch.sh              |  2 +-
 27 files changed, 53 insertions(+), 158 deletions(-)
 delete mode 100644 K8s/interfaces_definitions/vpod1.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod10.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod2.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod3.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod4.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod5.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod6.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod7.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod8.yaml
 delete mode 100644 K8s/interfaces_definitions/vpod9.yaml

diff --git a/K8s/interfaces_definitions/vhost1.yaml b/K8s/interfaces_definitions/vhost1.yaml
index 22675e5..3f603cf 100644
--- a/K8s/interfaces_definitions/vhost1.yaml
+++ b/K8s/interfaces_definitions/vhost1.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost1
+  name: veth1
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost1"
+      "type": "bridge",
+      "device": "veth1"
     }'
diff --git a/K8s/interfaces_definitions/vhost10.yaml b/K8s/interfaces_definitions/vhost10.yaml
index 1808449..9975699 100644
--- a/K8s/interfaces_definitions/vhost10.yaml
+++ b/K8s/interfaces_definitions/vhost10.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost10
+  name: veth10
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost10"
+      "type": "bridge",
+      "device": "veth10"
     }'
diff --git a/K8s/interfaces_definitions/vhost2.yaml b/K8s/interfaces_definitions/vhost2.yaml
index b155818..c99a91c 100644
--- a/K8s/interfaces_definitions/vhost2.yaml
+++ b/K8s/interfaces_definitions/vhost2.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost2
+  name: veth2
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost2"
+      "type": "bridge",
+      "device": "veth2"
     }'
diff --git a/K8s/interfaces_definitions/vhost3.yaml b/K8s/interfaces_definitions/vhost3.yaml
index 64e3fad..c05f175 100644
--- a/K8s/interfaces_definitions/vhost3.yaml
+++ b/K8s/interfaces_definitions/vhost3.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost3
+  name: veth3
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost3"
+      "type": "bridge",
+      "device": "veth3"
     }'
diff --git a/K8s/interfaces_definitions/vhost4.yaml b/K8s/interfaces_definitions/vhost4.yaml
index 618369f..a1496cd 100644
--- a/K8s/interfaces_definitions/vhost4.yaml
+++ b/K8s/interfaces_definitions/vhost4.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost4
+  name: veth4
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost4"
+      "type": "bridge",
+      "device": "veth4"
     }'
diff --git a/K8s/interfaces_definitions/vhost5.yaml b/K8s/interfaces_definitions/vhost5.yaml
index d1adb86..81caf02 100644
--- a/K8s/interfaces_definitions/vhost5.yaml
+++ b/K8s/interfaces_definitions/vhost5.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost5
+  name: veth5
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost5"
+      "type": "bridge",
+      "device": "veth5"
     }'
diff --git a/K8s/interfaces_definitions/vhost6.yaml b/K8s/interfaces_definitions/vhost6.yaml
index 51ea0fd..4425010 100644
--- a/K8s/interfaces_definitions/vhost6.yaml
+++ b/K8s/interfaces_definitions/vhost6.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost6
+  name: veth6
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost6"
+      "type": "bridge",
+      "device": "veth6"
     }'
diff --git a/K8s/interfaces_definitions/vhost7.yaml b/K8s/interfaces_definitions/vhost7.yaml
index 12abac8..2bcf5b6 100644
--- a/K8s/interfaces_definitions/vhost7.yaml
+++ b/K8s/interfaces_definitions/vhost7.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost7
+  name: veth7
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost7"
+      "type": "bridge",
+      "device": "veth7"
     }'
diff --git a/K8s/interfaces_definitions/vhost8.yaml b/K8s/interfaces_definitions/vhost8.yaml
index 13df980..2749e31 100644
--- a/K8s/interfaces_definitions/vhost8.yaml
+++ b/K8s/interfaces_definitions/vhost8.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost8
+  name: veth8
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost8"
+      "type": "bridge",
+      "device": "veth8"
     }'
diff --git a/K8s/interfaces_definitions/vhost9.yaml b/K8s/interfaces_definitions/vhost9.yaml
index 0ea4af0..58944e8 100644
--- a/K8s/interfaces_definitions/vhost9.yaml
+++ b/K8s/interfaces_definitions/vhost9.yaml
@@ -2,10 +2,10 @@
 apiVersion: "k8s.cni.cncf.io/v1"
 kind: NetworkAttachmentDefinition
 metadata:
-  name: vhost9
+  name: veth9
 spec:
   config: '{
       "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vhost9"
+      "type": "bridge",
+      "device": "veth9"
     }'
diff --git a/K8s/interfaces_definitions/vpod1.yaml b/K8s/interfaces_definitions/vpod1.yaml
deleted file mode 100644
index af8a907..0000000
--- a/K8s/interfaces_definitions/vpod1.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod1
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod1"
-    }'
diff --git a/K8s/interfaces_definitions/vpod10.yaml b/K8s/interfaces_definitions/vpod10.yaml
deleted file mode 100644
index 14ef5f7..0000000
--- a/K8s/interfaces_definitions/vpod10.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod10
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod10"
-    }'
diff --git a/K8s/interfaces_definitions/vpod2.yaml b/K8s/interfaces_definitions/vpod2.yaml
deleted file mode 100644
index 1aee3a4..0000000
--- a/K8s/interfaces_definitions/vpod2.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod2
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod2"
-    }'
diff --git a/K8s/interfaces_definitions/vpod3.yaml b/K8s/interfaces_definitions/vpod3.yaml
deleted file mode 100644
index af8246b..0000000
--- a/K8s/interfaces_definitions/vpod3.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod3
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod3"
-    }'
diff --git a/K8s/interfaces_definitions/vpod4.yaml b/K8s/interfaces_definitions/vpod4.yaml
deleted file mode 100644
index 2fe22d6..0000000
--- a/K8s/interfaces_definitions/vpod4.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod4
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod4"
-    }'
diff --git a/K8s/interfaces_definitions/vpod5.yaml b/K8s/interfaces_definitions/vpod5.yaml
deleted file mode 100644
index 2d07866..0000000
--- a/K8s/interfaces_definitions/vpod5.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod5
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod5"
-    }'
diff --git a/K8s/interfaces_definitions/vpod6.yaml b/K8s/interfaces_definitions/vpod6.yaml
deleted file mode 100644
index 993fd58..0000000
--- a/K8s/interfaces_definitions/vpod6.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod6
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod6"
-    }'
diff --git a/K8s/interfaces_definitions/vpod7.yaml b/K8s/interfaces_definitions/vpod7.yaml
deleted file mode 100644
index 18e4a37..0000000
--- a/K8s/interfaces_definitions/vpod7.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod7
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod7"
-    }'
diff --git a/K8s/interfaces_definitions/vpod8.yaml b/K8s/interfaces_definitions/vpod8.yaml
deleted file mode 100644
index 152f331..0000000
--- a/K8s/interfaces_definitions/vpod8.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod8
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod8"
-    }'
diff --git a/K8s/interfaces_definitions/vpod9.yaml b/K8s/interfaces_definitions/vpod9.yaml
deleted file mode 100644
index b7459c2..0000000
--- a/K8s/interfaces_definitions/vpod9.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-apiVersion: "k8s.cni.cncf.io/v1"
-kind: NetworkAttachmentDefinition
-metadata:
-  name: vpod9
-spec:
-  config: '{
-      "cniVersion": "0.3.0",
-      "type": "host-device",
-      "device": "vpod9"
-    }'
diff --git a/operator/daemonset/l2sm-switch-amd64.yaml b/operator/daemonset/l2sm-switch-amd64.yaml
index 75f9479..039dad1 100644
--- a/operator/daemonset/l2sm-switch-amd64.yaml
+++ b/operator/daemonset/l2sm-switch-amd64.yaml
@@ -14,7 +14,7 @@ spec:
       labels:
         l2sm-component: l2sm-switch
       annotations:
-        k8s.v1.cni.cncf.io/networks: vhost1@vhost1, vhost2@vhost2, vhost3@vhost3, vhost4@vhost4, vhost5@vhost5, vhost6@vhost6, vhost7@vhost7, vhost8@vhost8, vhost9@vhost9, vhost10@vhost10
+        k8s.v1.cni.cncf.io/networks: veth1, veth2, veth3, veth4, veth5, veth6, veth7, veth8, veth9, veth10
     spec:
       tolerations:
       # this toleration is to have the daemonset runnable on master nodes
@@ -24,21 +24,21 @@ spec:
         effect: NoSchedule
       containers:
       - name: l2sm-switch
-        image: alexdecb/l2sm-ovs:test
+        image: alexdecb/l2sm-switch:2.1
         command: ["sleep","infinity"]
+        #args: ["setup_switch.sh && sleep infinity"]
         env:
         - name: NODENAME
           valueFrom:
             fieldRef:
               fieldPath: spec.nodeName
-        - name: NVPODS
+        - name: NVETHS
           value: "10"  
         - name: CONTROLLERIP
           value: "l2sm-controller-service"
-
-        imagePullPolicy: Always
         securityContext:
           capabilities:
             add: ["NET_ADMIN"]
       nodeSelector:
         kubernetes.io/arch: amd64
+
diff --git a/operator/deploy/controller/deployController.yaml b/operator/deploy/controller/deployController.yaml
index 596091c..6a42776 100644
--- a/operator/deploy/controller/deployController.yaml
+++ b/operator/deploy/controller/deployController.yaml
@@ -15,6 +15,10 @@ spec:
       containers:
       - name: l2sm-controller
         image: alexdecb/l2sm-controller:latest
+        # readinessProbe:
+        #   httpGet:
+        #     path: /onos/v1/l2sm/networks/status   
+        #     port: 8181   
         ports:
         - containerPort: 6633
         - containerPort: 8181
diff --git a/operator/deploy/deployOperator.yaml b/operator/deploy/deployOperator.yaml
index 7afe40d..6a69f52 100644
--- a/operator/deploy/deployOperator.yaml
+++ b/operator/deploy/deployOperator.yaml
@@ -18,6 +18,7 @@ spec:
       containers:
       - image: alexdecb/l2sm-operator:latest
         name: l2sm-opt-pod
+        imagePullPolicy: Always
         env:
           - name: CONTROLLER_IP
             value: l2sm-controller-service
diff --git a/operator/src/operator/Dockerfile b/operator/src/operator/Dockerfile
index 20b735d..e816270 100644
--- a/operator/src/operator/Dockerfile
+++ b/operator/src/operator/Dockerfile
@@ -1,4 +1,4 @@
-FROM python:3.7
+FROM python:3.11.6
 RUN pip install kopf kubernetes PyMySQL cryptography requests
 COPY l2sm-operator.py /l2sm-operator.py
 CMD kopf run --standalone --all-namespaces /l2sm-operator.py
diff --git a/operator/src/operator/l2sm-operator.py b/operator/src/operator/l2sm-operator.py
index b8eef08..3f75af8 100644
--- a/operator/src/operator/l2sm-operator.py
+++ b/operator/src/operator/l2sm-operator.py
@@ -87,7 +87,7 @@ def build_db(body, logger, annotations, **kwargs):
     #MODIFY THE END VALUE TO ADD MORE INTERFACES
     values = []
     for i in range(1,11):
-      values.append(['vpod'+str(i), body['spec']['nodeName'], '-1', ''])
+      values.append(['veth'+str(i), body['spec']['nodeName'], '-1', ''])
     sqlInterfaces = "INSERT INTO interfaces (interface, node, network, pod) VALUES (%s, %s, %s, %s)"
     cur.executemany(sqlInterfaces, values)
     db.commit()
@@ -212,13 +212,13 @@ def pod_vn(body, name, namespace, logger, annotations, **kwargs):
     #    networkN = retrieve[0].strip()
     #    break
 
-    switchId = getSwitchId(cur, node)
+    switchId = getSwitchId(cur, node) # TODO: diferenciar caso en el que es un veth el conectado y el de que es una red de vdd.
 
     if switchId is None:
       logger.info(f"The l2sm switch is not connected to controller. Not connecting the pod")
       return
-    vpodPattern = re.compile(r'\d+$')
-    portNumbers = [int(vpodPattern.search(interface).group()) for interface in interface_to_attach]
+    vethPattern = re.compile(r'\d+$')
+    portNumbers = [int(vethPattern.search(interface).group()) for interface in interface_to_attach]
 
     for m in range(len(network)):
       sql = "UPDATE interfaces SET network = '%s', pod = '%s' WHERE interface = '%s' AND node = '%s'" % (network_array[m], name, interface_to_attach[m], node)
diff --git a/operator/src/switch/main.go b/operator/src/switch/main.go
index c999b42..aec906a 100644
--- a/operator/src/switch/main.go
+++ b/operator/src/switch/main.go
@@ -23,7 +23,7 @@ type Node struct {
 // the second one is the path to the configuration file, in reference to the code.
 func main() {
 
-	configDir, vhostNumber, nodeName, controllerIP, err := takeArguments()
+	configDir, vethNumber, nodeName, controllerIP, err := takeArguments()
 
 	if err != nil {
 		fmt.Println("Error with the arguments. Error:", err)
@@ -41,13 +41,13 @@ func main() {
 	fmt.Println("Switch initialized and connected to the controller.")
 
 	// Set all virtual interfaces up, and connect them to the tunnel bridge:
-	for i := 1; i <= vhostNumber; i++ {
-		vhost := fmt.Sprintf("vhost%d", i)
-		cmd := exec.Command("ip", "link", "set", vhost, "up") // i.e: ip link set vhost1 up
+	for i := 1; i <= vethNumber; i++ {
+		veth := fmt.Sprintf("net%d", i)
+		cmd := exec.Command("ip", "link", "set", veth, "up") // i.e: ip link set veth1 up
 		if err := cmd.Run(); err != nil {
 			fmt.Println("Error:", err)
 		}
-		exec.Command("ovs-vsctl", "add-port", "brtun", vhost).Run() // i.e: ovs-vsctl add-port brtun vhost1
+		exec.Command("ovs-vsctl", "add-port", "brtun", veth).Run() // i.e: ovs-vsctl add-port brtun veth1
 	}
 
 	err = createVxlans(configDir, nodeName)
@@ -61,7 +61,7 @@ func main() {
 func takeArguments() (string, int, string, string, error) {
 	configDir := os.Args[len(os.Args)-1]
 
-	vhostNumber := flag.Int("n_vpods", 0, "number of pod interfaces that are going to be attached to the switch")
+	vethNumber := flag.Int("n_veths", 0, "number of pod interfaces that are going to be attached to the switch")
 	nodeName := flag.String("node_name", "", "name of the node the script is executed in. Required.")
 	controllerIP := flag.String("controller_ip", "", "ip where the SDN controller is listening using the OpenFlow13 protocol. Required")
 
@@ -76,7 +76,7 @@ func takeArguments() (string, int, string, string, error) {
 		return "", 0, "", "", errors.New("Controller IP is not defined")
 	}
 
-	return configDir, *vhostNumber, *nodeName, *controllerIP, nil
+	return configDir, *vethNumber, *nodeName, *controllerIP, nil
 }
 
 func initializeSwitch(controllerIP string) error {
diff --git a/operator/src/switch/setup_switch.sh b/operator/src/switch/setup_switch.sh
index c0c4332..996e62f 100644
--- a/operator/src/switch/setup_switch.sh
+++ b/operator/src/switch/setup_switch.sh
@@ -6,4 +6,4 @@ ovs-vsctl --db=unix:/var/run/openvswitch/db.sock --no-wait init
 
 ovs-vswitchd --pidfile=/var/run/openvswitch/ovs-vswitchd.pid --detach 
 
-l2sm-br --n_vpods=$NVPODS --node_name=$NODENAME --controller_ip=$CONTROLLERIP /etc/l2sm/switchConfig.json
+l2sm-br --n_veths=$NVETHS --node_name=$NODENAME --controller_ip=$CONTROLLERIP /etc/l2sm/switchConfig.json
-- 
GitLab