class FwdOperator(ObjectOperator): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) cls.__init__(cls, **kwargs) return cls._instance def __init__(self, **kwargs): logger.info(kwargs) self.store = OprStore() config.load_incluster_config() self.obj_api = client.CustomObjectsApi() def query_existing_fwds(self): def list_fwd_obj_fn(name, spec, plurals): logger.info("Bootstrapped fwd {}".format(name)) fwd = Fwd(name, self.obj_api, self.store, spec) if fwd.status == OBJ_STATUS.obj_status_provisioned: self.store.update_obj(fwd) kube_list_obj(self.obj_api, RESOURCES.fwds, list_fwd_obj_fn) self.bootstrapped = True def get_stored_obj(self, name, spec): return Fwd(name, self.obj_api, self.store, spec) def create_default_fwds(self, n, default_dft): for i in range(n): fwd_name = default_dft + '-fwd-' + str(i) fwd = Fwd(fwd_name, self.obj_api, self.store) fwd.dft = default_dft fwd.create_obj()
class DropletOperator(ObjectOperator): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) cls.__init__(cls, **kwargs) return cls._instance def __init__(self, **kwargs): logger.info(kwargs) self.store = OprStore() config.load_incluster_config() self.obj_api = client.CustomObjectsApi() self.allocated_droplets = set() def query_existing_droplets(self): def list_droplet_obj_fn(name, spec, plurals): logger.info("Bootstrapped droplet {}".format(name)) droplet = Droplet(name, self.obj_api, self.store, spec) if droplet.status == OBJ_STATUS.obj_status_provisioned: self.store.update_obj(droplet) kube_list_obj(self.obj_api, RESOURCES.droplets, list_droplet_obj_fn) self.bootstrapped = True def get_stored_obj(self, name, spec): return Droplet(name, self.obj_api, self.store, spec) def get_droplet_by_ip(self, ip): for d in self.store.store["Droplet"]: if self.store.store["Droplet"][d].ip == ip: return self.store.store["Droplet"][d] return None def assign_droplet(self, obj): droplets = set(self.store.get_all_obj_type( KIND.droplet)) - self.allocated_droplets if len(droplets) == 0: return False d = random.sample(droplets, 1)[0] obj.droplet = d self.allocated_droplets.add(d) logger.info("Assigned droplet {} to {}".format(d, obj.name)) return True def unassign_droplet(self, obj): if obj.droplet == "": return False self.allocated_droplets.remove(obj.droplet) obj.droplet = "" logger.info("Unassigned droplet {} from {}".format(d, obj.name)) return True def get_unallocated_droplets(self): return set(self.store.get_all_obj_type( KIND.droplet)) - self.allocated_droplets
class ChainOperator(ObjectOperator): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) cls.__init__(cls, **kwargs) return cls._instance def __init__(self, **kwargs): logger.info(kwargs) self.store = OprStore() config.load_incluster_config() self.obj_api = client.CustomObjectsApi() def query_existing_chains(self): def list_chain_obj_fn(name, spec, plurals): logger.info("Bootstrapped chain {}".format(name)) chain = Chain(name, self.obj_api, self.store, spec) if chain.status == OBJ_STATUS.obj_status_provisioned: self.store.update_obj(chain) kube_list_obj(self.obj_api, RESOURCES.chains, list_chain_obj_fn) self.bootstrapped = True def get_stored_obj(self, name, spec): return Chain(name, self.obj_api, self.store, spec) def create_n_chains(self, dft): for i in range(dft.numchains): chain_name = dft.name + '-chain-' + str(i) chain = Chain(chain_name, self.obj_api, self.store) chain.dft = dft.name chain.size = dft.numchainreplicas chain.create_obj() dft.maglev_table.add(chain.name) dft.table = dft.maglev_table.table
class DftOperator(ObjectOperator): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) cls.__init__(cls, **kwargs) return cls._instance def __init__(self, **kwargs): logger.info(kwargs) self.store = OprStore() config.load_incluster_config() self.obj_api = client.CustomObjectsApi() def query_existing_dfts(self): def list_dft_obj_fn(name, spec, plurals): logger.info("Bootstrapped dft {}".format(name)) dft = Dft(name, self.obj_api, self.store, spec) if dft.status == OBJ_STATUS.obj_status_provisioned: self.store.update_obj(dft) kube_list_obj(self.obj_api, RESOURCES.dfts, list_dft_obj_fn) self.bootstrapped = True def get_stored_obj(self, name, spec): return Dft(name, self.obj_api, self.store, spec) def create_default_dft(self): if self.store.get_obj(OBJ_DEFAULTS.default_dft, KIND.dft): return dft = Dft(OBJ_DEFAULTS.default_dft, self.obj_api, self.store) dft.numchains = OBJ_DEFAULTS.default_n_chains dft.numchainreplicas = OBJ_DEFAULTS.default_n_replicas dft.maglev_table = MaglevTable(OBJ_DEFAULTS.default_maglev_table_size) dft.create_obj()
class ObjectOperator(object): def __init__(self, **kwargs): logger.info(kwargs) self.store = OprStore() def store_get_obj(self, name, kind, spec): obj = self.store.get_obj(name, kind) if not obj: logger.info("{} of type {} not found in store".format(name, kind)) return None obj.set_obj_spec(spec) return obj def store_update_obj(self, obj): self.store.update_obj(obj) obj.update_obj() def store_delete_obj(self, obj): self.store.delete_obj(obj.name, obj.kind) obj.delete_obj() def set_object_provisioned(self, obj): obj.set_status(OBJ_STATUS.obj_status_provisioned) self.store_update_obj(obj)
class FtnOperator(ObjectOperator): _instance = None def __new__(cls, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) cls.__init__(cls, **kwargs) return cls._instance def __init__(self, **kwargs): logger.info(kwargs) self.store = OprStore() config.load_incluster_config() self.obj_api = client.CustomObjectsApi() def query_existing_ftns(self): def list_ftn_obj_fn(name, spec, plurals): logger.info("Bootstrapped ftn {}".format(name)) ftn = Ftn(name, self.obj_api, self.store, spec) if ftn.status == OBJ_STATUS.obj_status_provisioned: self.store.update_obj(ftn) kube_list_obj(self.obj_api, RESOURCES.ftns, list_ftn_obj_fn) self.bootstrapped = True def get_stored_obj(self, name, spec): return Ftn(name, self.obj_api, self.store, spec) def create_n_ftns(self, chain): for i in range(chain.size): ftn_name = chain.name + '-ftn-' + str(i) ftn = Ftn(ftn_name, self.obj_api, self.store) ftn.parent_chain = chain.name ftn.dft = chain.dft ftn.create_obj() chain.ftns.append(ftn.name)