Skip to content
Snippets Groups Projects
Commit b0345824 authored by Alex ubuntu vm's avatar Alex ubuntu vm
Browse files

moved source code to independent folder

parent 2590309d
No related branches found
No related tags found
1 merge request!2repo: added new directory where utils scripts will be
FROM python:3.7
RUN pip install kopf kubernetes PyMySQL cryptography
COPY l2sm-operator.py /l2sm-operator.py
CMD kopf run --standalone --all-namespaces /l2sm-operator.py
FROM ubuntu:20.04
COPY ./vswitch.ovsschema /tmp/
RUN apt-get update && \
apt-get install -y net-tools iproute2 netcat dnsutils curl iputils-ping iptables nmap tcpdump openvswitch-switch && \
mkdir /var/run/openvswitch && mkdir -p /etc/openvswitch && ovsdb-tool create /etc/openvswitch/conf.db /tmp/vswitch.ovsschema
\ No newline at end of file
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
type Node struct {
Name string `json:"name"`
NodeIP string `json:"nodeIP"`
NeighborNodes []string `json:"neighborNodes"`
}
// Script that takes two required arguments:
// the first one is the name in the cluster of the node where the script is running
// the second one is the path to the configuration file, in reference to the code.
func main() {
// Read file and save in memory the JSON info
data, err := ioutil.ReadFile(os.Args[2])
if err != nil {
fmt.Println("Error reading input file:", err)
return
}
var nodes []Node
err = json.Unmarshal(data, &nodes)
if err != nil {
fmt.Println("Error unmarshalling JSON:", err)
return
}
// Search for the corresponding node in the configuration, according to the first passed parameter.
// Once the node is found, create a bridge for every neighbour node defined.
// The bridge is created with the nodeIp, neighborNodeIP and VNI. The VNI is generated according to the position of the node in the Json file. The first node will have the number 5
// as a reference, the second a 6, and so on. And if a bridge between node 1 and node 2 is generated, it will have a vni of 5006, the two references with two 0s in between.
// Another example would be node 3 of the cluster and node 9. Node 3 will have the reference 7 (5+3-1), and the Node 9 the reference 13(5 + 9 -1), resulting in the VNI 70013.
// There's up to 2 ^ 24 possible vnis that are reduced to (2 ^24)/100 because of this measure (2 decimal digits are lost). So in total, a number of 167.772 virtual networks can be created.
nodeVniRef := 5
for _, node := range nodes {
if node.Name == os.Args[1] {
nodeIP := strings.TrimSpace(node.NodeIP)
neighborVniRef := 5
for _, neighbor := range node.NeighborNodes {
for _, n := range nodes {
if n.Name == neighbor {
vni := fmt.Sprintf("%d00%d", nodeVniRef, neighborVniRef)
neighborIP := strings.TrimSpace(n.NodeIP)
command := fmt.Sprintf("ovs-vsctl add-port brtun vxlan%d -- set interface vxlan1 type=vxlan options:key=%s options:remote_ip=%s options:local_ip=%s options:dst_port=7000", neighborVniRef, vni, neighborIP, nodeIP)
exec.Command(command).Output()
if err != nil {
fmt.Print(fmt.Errorf("Could not create vxlan between node %s and node %s. OVS responds: %w", node.Name, neighbor, err))
}
}
neighborVniRef++
}
}
}
nodeVniRef++
}
}
{"name": "Open_vSwitch",
"version": "8.4.0",
"cksum": "2738838700 27127",
"tables": {
"Open_vSwitch": {
"columns": {
"datapaths": {
"type": {"key": {"type": "string"},
"value": {"type": "uuid",
"refTable": "Datapath"},
"min": 0, "max": "unlimited"}},
"bridges": {
"type": {"key": {"type": "uuid",
"refTable": "Bridge"},
"min": 0, "max": "unlimited"}},
"manager_options": {
"type": {"key": {"type": "uuid",
"refTable": "Manager"},
"min": 0, "max": "unlimited"}},
"ssl": {
"type": {"key": {"type": "uuid",
"refTable": "SSL"},
"min": 0, "max": 1}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"next_cfg": {
"type": "integer"},
"cur_cfg": {
"type": "integer"},
"statistics": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"ovs_version": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"db_version": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"system_type": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"system_version": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"datapath_types": {
"type": {"key": {"type": "string"},
"min": 0, "max": "unlimited"}},
"iface_types": {
"type": {"key": {"type": "string"},
"min": 0, "max": "unlimited"}},
"dpdk_initialized": {
"type": "boolean"},
"dpdk_version": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}}},
"isRoot": true,
"maxRows": 1},
"Bridge": {
"columns": {
"name": {
"type": "string",
"mutable": false},
"datapath_type": {
"type": "string"},
"datapath_version": {
"type": "string"},
"datapath_id": {
"type": {"key": "string", "min": 0, "max": 1},
"ephemeral": true},
"stp_enable": {
"type": "boolean"},
"rstp_enable": {
"type": "boolean"},
"mcast_snooping_enable": {
"type": "boolean"},
"ports": {
"type": {"key": {"type": "uuid",
"refTable": "Port"},
"min": 0, "max": "unlimited"}},
"mirrors": {
"type": {"key": {"type": "uuid",
"refTable": "Mirror"},
"min": 0, "max": "unlimited"}},
"netflow": {
"type": {"key": {"type": "uuid",
"refTable": "NetFlow"},
"min": 0, "max": 1}},
"sflow": {
"type": {"key": {"type": "uuid",
"refTable": "sFlow"},
"min": 0, "max": 1}},
"ipfix": {
"type": {"key": {"type": "uuid",
"refTable": "IPFIX"},
"min": 0, "max": 1}},
"controller": {
"type": {"key": {"type": "uuid",
"refTable": "Controller"},
"min": 0, "max": "unlimited"}},
"protocols": {
"type": {"key": {"type": "string",
"enum": ["set", ["OpenFlow10",
"OpenFlow11",
"OpenFlow12",
"OpenFlow13",
"OpenFlow14",
"OpenFlow15"]]},
"min": 0, "max": "unlimited"}},
"fail_mode": {
"type": {"key": {"type": "string",
"enum": ["set", ["standalone", "secure"]]},
"min": 0, "max": 1}},
"status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"rstp_status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"flood_vlans": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4095},
"min": 0, "max": 4096}},
"flow_tables": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 254},
"value": {"type": "uuid",
"refTable": "Flow_Table"},
"min": 0, "max": "unlimited"}},
"auto_attach": {
"type": {"key": {"type": "uuid",
"refTable": "AutoAttach"},
"min": 0, "max": 1}}},
"indexes": [["name"]]},
"Port": {
"columns": {
"name": {
"type": "string",
"mutable": false},
"interfaces": {
"type": {"key": {"type": "uuid",
"refTable": "Interface"},
"min": 1, "max": "unlimited"}},
"trunks": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4095},
"min": 0, "max": 4096}},
"cvlans": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4095},
"min": 0, "max": 4096}},
"tag": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4095},
"min": 0, "max": 1}},
"vlan_mode": {
"type": {"key": {"type": "string",
"enum": ["set", ["trunk", "access", "native-tagged",
"native-untagged", "dot1q-tunnel"]]},
"min": 0, "max": 1}},
"qos": {
"type": {"key": {"type": "uuid",
"refTable": "QoS"},
"min": 0, "max": 1}},
"mac": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"bond_mode": {
"type": {"key": {"type": "string",
"enum": ["set", ["balance-tcp", "balance-slb", "active-backup"]]},
"min": 0, "max": 1}},
"lacp": {
"type": {"key": {"type": "string",
"enum": ["set", ["active", "passive", "off"]]},
"min": 0, "max": 1}},
"bond_updelay": {
"type": "integer"},
"bond_downdelay": {
"type": "integer"},
"bond_active_slave": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"bond_fake_iface": {
"type": "boolean"},
"fake_bridge": {
"type": "boolean"},
"status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"rstp_status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"rstp_statistics": {
"type": {"key": "string", "value": "integer",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"statistics": {
"type": {"key": "string", "value": "integer",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"protected": {
"type": "boolean"},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"indexes": [["name"]]},
"Interface": {
"columns": {
"name": {
"type": "string",
"mutable": false},
"type": {
"type": "string"},
"options": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"ingress_policing_rate": {
"type": {"key": {"type": "integer",
"minInteger": 0}}},
"ingress_policing_burst": {
"type": {"key": {"type": "integer",
"minInteger": 0}}},
"ingress_policing_kpkts_rate": {
"type": {"key": {"type": "integer",
"minInteger": 0}}},
"ingress_policing_kpkts_burst": {
"type": {"key": {"type": "integer",
"minInteger": 0}}},
"mac_in_use": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1},
"ephemeral": true},
"mac": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"ifindex": {
"type": {
"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295},
"min": 0,
"max": 1},
"ephemeral": true},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"ofport": {
"type": {"key": "integer", "min": 0, "max": 1}},
"ofport_request": {
"type": {
"key": {"type": "integer",
"minInteger": 1,
"maxInteger": 65279},
"min": 0,
"max": 1}},
"bfd": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"bfd_status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"cfm_mpid": {
"type": {
"key": {"type": "integer"},
"min": 0,
"max": 1}},
"cfm_remote_mpids": {
"type": {
"key": {"type": "integer"},
"min": 0,
"max": "unlimited"},
"ephemeral": true},
"cfm_flap_count": {
"type": {
"key": {"type": "integer"},
"min": 0,
"max": 1}},
"cfm_fault": {
"type": {
"key": { "type": "boolean"},
"min": 0,
"max": 1},
"ephemeral": true},
"cfm_fault_status": {
"type": {
"key": "string", "min": 0, "max": "unlimited"},
"ephemeral": true},
"cfm_remote_opstate": {
"type": {"key": {"type": "string",
"enum": ["set", ["up", "down"]]},
"min": 0, "max": 1},
"ephemeral": true},
"cfm_health": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 100},
"min": 0, "max": 1},
"ephemeral": true},
"lacp_current": {
"type": {"key": {"type": "boolean"},
"min": 0, "max": 1},
"ephemeral": true},
"lldp": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"statistics": {
"type": {"key": "string", "value": "integer",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"admin_state": {
"type": {"key": {"type": "string",
"enum": ["set", ["up", "down"]]},
"min": 0, "max": 1},
"ephemeral": true},
"link_state": {
"type": {"key": {"type": "string",
"enum": ["set", ["up", "down"]]},
"min": 0, "max": 1},
"ephemeral": true},
"link_resets": {
"type": {"key": {"type": "integer"},
"min": 0, "max": 1},
"ephemeral": true},
"link_speed": {
"type": {"key": "integer", "min": 0, "max": 1},
"ephemeral": true},
"duplex": {
"type": {"key": {"type": "string",
"enum": ["set", ["half", "full"]]},
"min": 0, "max": 1},
"ephemeral": true},
"mtu": {
"type": {"key": "integer", "min": 0, "max": 1},
"ephemeral": true},
"mtu_request": {
"type": {
"key": {"type": "integer",
"minInteger": 1},
"min": 0,
"max": 1}},
"error": {
"type": {"key": "string", "min": 0, "max": 1}}},
"indexes": [["name"]]},
"Flow_Table": {
"columns": {
"name": {
"type": {"key": "string", "min": 0, "max": 1}},
"flow_limit": {
"type": {"key": {"type": "integer", "minInteger": 0},
"min": 0, "max": 1}},
"overflow_policy": {
"type": {"key": {"type": "string",
"enum": ["set", ["refuse", "evict"]]},
"min": 0, "max": 1}},
"groups": {
"type": {"key": "string", "min": 0, "max": "unlimited"}},
"prefixes": {
"type": {"key": "string", "min": 0, "max": 3}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"QoS": {
"columns": {
"type": {
"type": "string"},
"queues": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295},
"value": {"type": "uuid",
"refTable": "Queue"},
"min": 0, "max": "unlimited"}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"isRoot": true},
"Queue": {
"columns": {
"dscp": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 63},
"min": 0, "max": 1}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"isRoot": true},
"Mirror": {
"columns": {
"name": {
"type": "string"},
"select_all": {
"type": "boolean"},
"select_src_port": {
"type": {"key": {"type": "uuid",
"refTable": "Port",
"refType": "weak"},
"min": 0, "max": "unlimited"}},
"select_dst_port": {
"type": {"key": {"type": "uuid",
"refTable": "Port",
"refType": "weak"},
"min": 0, "max": "unlimited"}},
"select_vlan": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4095},
"min": 0, "max": 4096}},
"output_port": {
"type": {"key": {"type": "uuid",
"refTable": "Port",
"refType": "weak"},
"min": 0, "max": 1}},
"output_vlan": {
"type": {"key": {"type": "integer",
"minInteger": 1,
"maxInteger": 4095},
"min": 0, "max": 1}},
"snaplen": {
"type": {"key": {"type": "integer",
"minInteger": 14,
"maxInteger": 65535},
"min": 0, "max": 1}},
"statistics": {
"type": {"key": "string", "value": "integer",
"min": 0, "max": "unlimited"},
"ephemeral": true},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"NetFlow": {
"columns": {
"targets": {
"type": {"key": {"type": "string"},
"min": 1, "max": "unlimited"}},
"engine_type": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 255},
"min": 0, "max": 1}},
"engine_id": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 255},
"min": 0, "max": 1}},
"add_id_to_interface": {
"type": "boolean"},
"active_timeout": {
"type": {"key": {"type": "integer",
"minInteger": -1}}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"sFlow": {
"columns": {
"targets": {
"type": {"key": "string", "min": 1, "max": "unlimited"}},
"sampling": {
"type": {"key": "integer", "min": 0, "max": 1}},
"polling": {
"type": {"key": "integer", "min": 0, "max": 1}},
"header": {
"type": {"key": "integer", "min": 0, "max": 1}},
"agent": {
"type": {"key": "string", "min": 0, "max": 1}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"IPFIX": {
"columns": {
"targets": {
"type": {"key": "string", "min": 0, "max": "unlimited"}},
"sampling": {
"type": {"key": {"type": "integer",
"minInteger": 1,
"maxInteger": 4294967295},
"min": 0, "max": 1}},
"obs_domain_id": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295},
"min": 0, "max": 1}},
"obs_point_id": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295},
"min": 0, "max": 1}},
"cache_active_timeout": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4200},
"min": 0, "max": 1}},
"cache_max_flows": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295},
"min": 0, "max": 1}},
"stats_interval": {
"type": {"key": {"type": "integer",
"minInteger": 1,
"maxInteger": 3600},
"min": 0, "max": 1}},
"template_interval": {
"type": {"key": {"type": "integer",
"minInteger": 1,
"maxInteger": 3600},
"min": 0, "max": 1}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"Flow_Sample_Collector_Set": {
"columns": {
"id": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 4294967295},
"min": 1, "max": 1}},
"bridge": {
"type": {"key": {"type": "uuid",
"refTable": "Bridge"},
"min": 1, "max": 1}},
"ipfix": {
"type": {"key": {"type": "uuid",
"refTable": "IPFIX"},
"min": 0, "max": 1}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"isRoot": true,
"indexes": [["id", "bridge"]]},
"Controller": {
"columns": {
"type": {
"type": {"key": {"type": "string",
"enum": ["set", ["primary", "service"]]},
"min": 0, "max": 1}},
"target": {
"type": "string"},
"max_backoff": {
"type": {"key": {"type": "integer",
"minInteger": 1000},
"min": 0, "max": 1}},
"inactivity_probe": {
"type": {"key": "integer", "min": 0, "max": 1}},
"connection_mode": {
"type": {"key": {"type": "string",
"enum": ["set", ["in-band", "out-of-band"]]},
"min": 0, "max": 1}},
"local_ip": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"local_netmask": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"local_gateway": {
"type": {"key": {"type": "string"},
"min": 0, "max": 1}},
"enable_async_messages": {
"type": {"key": {"type": "boolean"},
"min": 0, "max": 1}},
"controller_queue_size": {
"type": {"key": {"type": "integer",
"minInteger": 1,
"maxInteger": 512},
"min": 0, "max": 1}},
"controller_rate_limit": {
"type": {"key": {"type": "integer",
"minInteger": 100},
"min": 0, "max": 1}},
"controller_burst_limit": {
"type": {"key": {"type": "integer",
"minInteger": 25},
"min": 0, "max": 1}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"is_connected": {
"type": "boolean",
"ephemeral": true},
"role": {
"type": {"key": {"type": "string",
"enum": ["set", ["other", "master", "slave"]]},
"min": 0, "max": 1},
"ephemeral": true},
"status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true}}},
"Manager": {
"columns": {
"target": {
"type": "string"},
"max_backoff": {
"type": {"key": {"type": "integer",
"minInteger": 1000},
"min": 0, "max": 1}},
"inactivity_probe": {
"type": {"key": "integer", "min": 0, "max": 1}},
"connection_mode": {
"type": {"key": {"type": "string",
"enum": ["set", ["in-band", "out-of-band"]]},
"min": 0, "max": 1}},
"other_config": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"is_connected": {
"type": "boolean",
"ephemeral": true},
"status": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"},
"ephemeral": true}},
"indexes": [["target"]]},
"Datapath": {
"columns": {
"datapath_version": {
"type": "string"},
"ct_zones": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 65535},
"value": {"type": "uuid",
"refTable": "CT_Zone"},
"min": 0, "max": "unlimited"}},
"capabilities": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"CT_Zone": {
"columns": {
"timeout_policy": {
"type": {"key": {"type": "uuid",
"refTable": "CT_Timeout_Policy"},
"min": 0, "max": 1}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"CT_Timeout_Policy": {
"columns": {
"timeouts": {
"type": {"key": {"type" : "string",
"enum": ["set", ["tcp_syn_sent", "tcp_syn_recv",
"tcp_established", "tcp_fin_wait",
"tcp_close_wait", "tcp_last_ack",
"tcp_time_wait", "tcp_close",
"tcp_syn_sent2", "tcp_retransmit",
"tcp_unack", "udp_first",
"udp_single", "udp_multiple",
"icmp_first", "icmp_reply"]]},
"value": {"type" : "integer",
"minInteger" : 0,
"maxInteger" : 4294967295},
"min": 0, "max": "unlimited"}},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}},
"SSL": {
"columns": {
"private_key": {
"type": "string"},
"certificate": {
"type": "string"},
"ca_cert": {
"type": "string"},
"bootstrap_ca_cert": {
"type": "boolean"},
"external_ids": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"maxRows": 1},
"AutoAttach": {
"columns": {
"system_name": {
"type": "string"},
"system_description": {
"type": "string"},
"mappings": {
"type": {"key": {"type": "integer",
"minInteger": 0,
"maxInteger": 16777215},
"value": {"type": "integer",
"minInteger": 0,
"maxInteger": 4095},
"min": 0, "max": "unlimited"}}}}}}
import kopf
import os
import sys
import json
import subprocess
import secrets
import kubernetes
from subprocess import CalledProcessError
from random import randrange
from kubernetes import client, config
import pymysql
import random
import time
ip = "127.0.0.1"
#POPULATE DATABASE ENTRIES WHEN A NEW L2SM POD IS CREATED (A NEW NODE APPEARS)
@kopf.on.create('pods.v1', labels={'l2sm-component': 'l2-ps'})
def build_db(body, logger, annotations, **kwargs):
db = pymysql.connect(host=ip,user="l2sm",password="l2sm;",db="L2SM")
cur = db.cursor()
#CREATE TABLES IF THEY DO NOT EXIST
table1 = "CREATE TABLE IF NOT EXISTS networks (network TEXT NOT NULL, id TEXT NOT NULL);"
table2 = "CREATE TABLE IF NOT EXISTS interfaces (interface TEXT NOT NULL, node TEXT NOT NULL, network TEXT, pod TEXT);"
cur.execute(table1)
cur.execute(table2)
db.commit()
values = []
#MODIFY THE END VALUE TO ADD MORE INTERFACES
for i in range(1,11):
values.append(['vpod'+str(i), body['spec']['nodeName'], '-1', ''])
sql = "INSERT INTO interfaces (interface, node, network, pod) VALUES (%s, %s, %s, %s)"
cur.executemany(sql, values)
db.commit()
db.close()
logger.info(f"Node {body['spec']['nodeName']} has been registered in the operator")
#UPDATE DATABASE WHEN NETWORK IS CREATED, I.E: IS A MULTUS CRD WITH OUR DUMMY INTERFACE PRESENT IN ITS CONFIG
#@kopf.on.create('NetworkAttachmentDefinition', field="spec.config['device']", value='l2sm-vNet')
@kopf.on.create('NetworkAttachmentDefinition', when=lambda spec, **_: '"device": "l2sm-vNet"' in spec['config'])
def create_vn(spec, name, namespace, logger, **kwargs):
db = pymysql.connect(host=ip,user="l2sm",password="l2sm;",db="L2SM")
cur = db.cursor()
id = secrets.token_hex(32)
sql = "INSERT INTO networks (network, id) VALUES ('%s', '%s')" % (name.strip(), id.strip())
cur.execute(sql)
db.commit()
db.close()
logger.info(f"Network has been created")
#ASSIGN POD TO NETWORK (TRIGGERS ONLY IF ANNOTATION IS PRESENT)
@kopf.on.create('pods.v1', annotations={'k8s.v1.cni.cncf.io/networks': kopf.PRESENT})
def pod_vn(body, name, namespace, logger, annotations, **kwargs):
#GET MULTUS INTERFACES IN THE DESCRIPTOR
#IN QUARANTINE: SLOWER THAN MULTUS!!!!!
time.sleep(random.uniform(0,0.8)) #Make sure the database is not consulted at the same time to avoid overlaping
multusInt = annotations.get('k8s.v1.cni.cncf.io/networks').split(",")
#VERIFY IF NETWORK IS PRESENT IN THE CLUSTER
api = client.CustomObjectsApi()
items = api.list_namespaced_custom_object('k8s.cni.cncf.io', 'v1', namespace, 'network-attachment-definitions').get('items')
resources = []
# NETWORK POSITION IN ANNOTATION
network = []
#FIND OUR NETWORKS IN MULTUS
for i in items:
if '"device": "l2sm-vNet"' in i['spec']['config']:
resources.append(i['metadata']['name'])
for k in range(len(multusInt)):
multusInt[k] = multusInt[k].strip()
if multusInt[k] in resources:
network.append(k)
#IF THERE ARE NO NETWORKS, LET MULTUS HANDLE THIS
if not network:
return
#CHECK IF NODE HAS FREE VIRTUAL INTERFACES LEFT
v1 = client.CoreV1Api()
ret = v1.read_namespaced_pod(name, namespace)
node = body['spec']['nodeName']
db = pymysql.connect(host=ip,user="l2sm",password="l2sm;",db="L2SM")
nsql = "SELECT * FROM interfaces WHERE node = '%s' AND network = '-1'" % (node.strip())
cur = db.cursor()
cur.execute(nsql)
data = cur.fetchall()
if not data or len(data)<len(network):
db.close()
raise kopf.PermanentError("l2sm could not deploy the pod: Node " + node.strip() + "has no free interfaces left")
#IF THERE IS ALREADY A MULTUS ANNOTATION, APPEND IT TO THE END.
interface_to_attach = []
network_array = []
j = 0
for interface in data[0:len(network)]:
network_array.append(multusInt[network[j]])
multusInt[network[j]] = interface[0].strip()
interface_to_attach.append(interface[0].strip())
j = j + 1
ret.metadata.annotations['k8s.v1.cni.cncf.io/networks'] = ', '.join(multusInt)
#PATCH NETWORK WITH ANNOTATION
v1.patch_namespaced_pod(name, namespace, ret)
#GET NETWORK ID'S
#for j in items:
# if network in j['metadata']['name']:
# idsql = "SELECT id FROM networks WHERE network = '%s'" % (network.strip())
# cur.execute(idsql)
# retrieve = cur.fetchone()
# networkN = retrieve[0].strip()
# break
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)
cur.execute(sql)
db.commit()
db.close()
#HERE GOES SDN, THIS IS WHERE THE FUN BEGINS
logger.info(f"Pod {name} attached to network {network_array}")
#UPDATE DATABASE WHEN POD IS DELETED
@kopf.on.delete('pods.v1', annotations={'k8s.v1.cni.cncf.io/networks': kopf.PRESENT})
def dpod_vn(name, logger, **kwargs):
db = pymysql.connect(host=ip,user="l2sm",password="l2sm;",db="L2SM")
cur = db.cursor()
sql = "UPDATE interfaces SET network = '-1', pod = '' WHERE pod = '%s'" % (name)
cur.execute(sql)
db.commit()
db.close()
logger.info(f"Pod {name} removed")
#UPDATE DATABASE WHEN NETWORK IS DELETED
@kopf.on.delete('NetworkAttachmentDefinition', when=lambda spec, **_: '"device": "l2sm-vNet"' in spec['config'])
def delete_vn(spec, name, logger, **kwargs):
db = pymysql.connect(host=ip,user="l2sm",password="l2sm;",db="L2SM")
cur = db.cursor()
sql = "DELETE FROM networks WHERE network = '%s'" % (name)
cur.execute(sql)
db.commit()
db.close()
logger.info(f"Network has been deleted")
#DELETE DATABASE ENTRIES WHEN A NEW L2SM POD IS DELETED (A NEW NODE GETS OUT OF THE CLUSTER)
@kopf.on.delete('pods.v1', labels={'l2sm-component': 'l2-ps'})
def remove_node(body, logger, annotations, **kwargs):
db = pymysql.connect(host=ip,user="l2sm",password="l2sm;",db="L2SM")
cur = db.cursor()
sql = "DELETE FROM interfaces WHERE node = '%s'" % (body['spec']['nodeName'])
cur.execute(sql)
db.commit()
db.close()
logger.info(f"Node {body['spec']['nodeName']} has been deleted from the cluster")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment