Example #1
0
def getIP(namespace, name):
    try:
        # Will look for a free IP with the provided namespace and service name
        svc_line = ('n', namespace, name, )
        cursor = globalholders.databaseConnection.cursor()
        # Selects from the database IPs that are not in use and have the provided namespace and name
        cursor.execute('SELECT ip FROM extips WHERE inuse=? AND namespace=? AND service=?;' , (svc_line))
        my_ip = cursor.fetchone()
        # If IP was not found with provided namespace and name
        if(my_ip is None):
            # Will look for a free IP with the provided namespace
            svc_line = ('n', namespace, )
            # Selects from the database IPs that are not in use and have the provided namespace
            cursor.execute('SELECT ip FROM extips WHERE inuse=? AND namespace=?;' , (svc_line))
            my_ip = cursor.fetchone()
            if(my_ip is None):
                # No IPs found
                return None
            else:
                # Found IP for the provided Namespace
                return str(my_ip[0])
        else:
            # Found IP for the pair namespace/name
            return str(my_ip[0])
    except Exception as e:
        logutil.printException(e)
        return None
def connectApi():
    try:
        globalholders.coreApi = client.CoreV1Api()
        return True
    except Exception as e:
        logutil.printException(e)
        return False
Example #3
0
def connectDB():
    try:
        # Creates "in-memory" database
        globalholders.databaseConnection = sqlite3.connect(':memory:')
        return True
    except Exception as e:
        logutil.printException(e)
        return False
Example #4
0
def readDB():
    try:
        cursor = globalholders.databaseConnection.cursor()
        # Read everything from the database
        cursor.execute('SELECT * FROM extips;')
        allRoutes = cursor.fetchall()
        return allRoutes
    except Exception as e:
        logutil.printException(e)
        return False
Example #5
0
def consumeIP(ip, name, namespace, dst):
    try:
        cursor = globalholders.databaseConnection.cursor()
        flag = 'y'
        svc_line = (str(flag), str(namespace), str(name), str(dst), str(ip), )
        # Update the line flag to y for the provided parameters: IP, Name, Namespace, Destination
        cursor.execute('UPDATE extips SET inuse=? , namespace=? , service=? , dst=? WHERE ip=?;', (svc_line))
        return True
    except Error as e:
        logutil.printException(e)
        return False
Example #6
0
def createDB():
    try:
        # Get a cursor on the database
        cursor = globalholders.databaseConnection.cursor()
        # Create the table that will hold our routes on the database
        cursor.execute('CREATE TABLE extips (ip VARCHAR(16) NOT NULL PRIMARY KEY, inuse CHAR(1) NOT NULL, namespace VARCHAR(64), service VARCHAR(64), dst VARCHAR(16));')
        # Commits the change to the database
        globalholders.databaseConnection.commit()
        return True
    except Exception as e:
        logutil.printException(e)
        return False
def loadConfig():
    try:
        # Disable SSL Warnings:
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        # Load config from the Cluster (will use ServiceAccount)
        config.load_incluster_config()

        return True

    except Exception as e:
        logutil.printException(e)
        return False
def serviceAddIP(service, ip):
    try:
        # Create an empty list for IPs
        ipList = list()
        if (ip is not None):
            # Append the IP to this list
            ipList.append(ip)
        # Update our local copy of the Service Descriptor
        service.spec.external_i_ps = ipList
        # Patch the cluster copy of the Service Descriptor
        globalholders.coreApi.patch_namespaced_service(
            service.metadata.name, service.metadata.namespace, service)
        return True
    except Exception as e:
        logutil.printException(e)
        return False
Example #9
0
def loadIPList():
    try:
        # List config files
        allNamespaces = os.listdir(globalholders.configPath)
        for namespace in allNamespaces:
            # Skipping internal files not related to our config
            if ".." not in namespace:
                # Building the relative path for the namespace config file
                namespacepath = globalholders.configPath + "/" + namespace
                # Opening the config file
                with open(namespacepath) as ipList:
                    # Reading IPs from file
                    for ip in ipList:
                        cursor = globalholders.databaseConnection.cursor()
                        # Adding the IP to our table
                        cursor.execute('INSERT INTO extips(ip,inuse,namespace) VALUES(?,?,?);' , (ip.strip(), 'n', namespace, ))
                        globalholders.databaseConnection.commit()
        return True
    except Exception as e:
        logutil.printException(e)
        return False
Example #10
0
def launchHandler():
    w = watch.Watch()
    try:
        while True:
            # Watching for event stream on Services
            for event in w.stream(
                    globalholders.coreApi.list_service_for_all_namespaces):
                # A new service of type LoadBalancer was added
                if (event['type'] == "ADDED"
                        and event['object'].spec.type == "LoadBalancer"):
                    my_service = event['object']
                    # Handle new service
                    services.added(my_service)

                # A service of type LoadBalancer got modified
                elif (event['type'] == "MODIFIED"
                      and event['object'].spec.type == "LoadBalancer"):
                    my_service = event['object']
                    # Handle updated service
                    services.modified(my_service)

                # A service of type LoadBalancer was deleted
                elif (event['type'] == "DELETED"
                      and event['object'].spec.type == "LoadBalancer"):
                    my_service = event['object']
                    # Handle deleted service
                    services.deleted(my_service)

                # Get actual Route Table
                route_table = database.readDB()
                # Update Filesystem Route Table
                if (route_table is not False):
                    logutil.printRoutes(route_table)

    except Exception as e:
        logutil.printException(e)
        logutil.printMessage("Event Handler Exception: " + e)