def assert_profile(self, profile: dict): keys = {'limits', 'requests'} assert isinstance(profile, dict) and keys.issubset(set(profile)), \ ConfigurationError("Resource profile must be a mapping containing the keys {}".format(keys)) for key in keys: for res, unit in zip(['cpu', 'memory'], ['vCores', 'MB']): num = profile.get(key, {}).get(res) if res == 'memory': assert num is None or isinstance(num, int), NhaDockerError( "Resource {} '{}' must be an integer ({})".format( key.rstrip('s'), res, unit)) else: assert num is None or isinstance( num, (int, float, str) ), NhaDockerError( "Resource {} '{}' must be integer or float or string ({})" .format(key.rstrip('s'), res, unit)) if isinstance(num, str): assert num[-1] == "m", NhaDockerError( "When string, CPU must be in milli notation. Example: 500m" ) num = float(num[:-1]) / 1000 assert num >= 0.001, NhaDockerError( "CPU precision must be at least 0.001, but was: {}" .format(num)) profile.get(key, {})[res] = num return profile
def get_nfs_server(self): nfs = self.conf.get(self.KEY_NFS, {}) assert isinstance(nfs, dict) and sorted(list(nfs.keys())) == ['path', 'server'],\ ConfigurationError("NFS server must be a mapping in the form {'server': 127.0.0.1, 'path': /shared/path}") return nfs
def assert_profile(self, profile: dict): assert isinstance(profile, dict), \ ConfigurationError("Resource profile must be a dictionary, but is: {}".format(type(profile))) if profile.get(self.KEYS_RESOURCES[0], None) or profile.get(self.KEYS_RESOURCES[1], None): profile = self.assert_resources(profile) return profile
def get_resource_profile(self, ref_to_profile: str): prof = self.conf.get(self.KEY_PROFILES, {}).get(ref_to_profile) if prof is None: if ref_to_profile in DockerConst.Section.ALL: return None else: raise ConfigurationError("Resource profile '{}' not found".format(ref_to_profile)) prof = self.assert_profile(prof) return prof
def get_svc_type(self, resource_profile: dict): prof = resource_profile or {} svc_opts = {KubeConst.CLUSTER_IP.lower(): KubeConst.CLUSTER_IP, KubeConst.NODE_PORT.lower(): KubeConst.NODE_PORT, KubeConst.LOAD_BALANCER.lower(): KubeConst.LOAD_BALANCER} prof_svc = prof.get(self.KEY_SVC_TYPE, KubeConst.NODE_PORT) assert svc_opts.get(prof_svc.lower(), None), \ ConfigurationError("Invalid service_type value: '{}' in resource profile definition, use one of these: {} " .format(prof_svc, ",".join(KubeConst.ALL_SVC_TYPES))) return svc_opts[prof_svc.lower()]
def assert_namespace(self): try: with K8sBackend(logging_level=logging.ERROR) as k8s_backend: assert super()._find_sth( what='namespaces', name=self.namespace, method=lambda: k8s_backend.core_api.list_namespace().items, key=lambda i: i.metadata.name == self.namespace ) is not None, ConfigurationError( "Namespace '{}' does not exist".format(self.namespace)) except (ConuException, K8sApiException) as e: msg = "Waiting up to {} seconds to find {} '{}'".format( self.timeout, 'namespace', self.namespace) raise PatientError(wait_callback=lambda: self.LOG.info(msg), original_exception=e)
def load(self): default_conf, user_conf, local_conf = [ None if not os.path.exists(src) else Kaptan( handler=Config.FMT).import_config(src) for src in self.sources ] assert default_conf is not None,\ ConfigurationError("Default configuration not found at {}".format(self.sources[0])) conf_opts = [local_conf, user_conf, {}] # if local conf was found, user conf is ignored child_conf = filter( lambda x: x is not None, conf_opts).__next__() # returns first non empty option super().__init__(**join_dicts(default_conf.get(self.namespace, {}), child_conf.get(self.namespace, {}), allow_overwrite=True)) return self
def get_stg_cls(self, section: str): stg_cls = self.conf.get(self.KEY_STG_CLS, self.DEFAULT_STG_CLS) assert isinstance(stg_cls, str) and len(stg_cls) > 0,\ ConfigurationError("Container manager 'kube' requires an existing storage class to be configured") return stg_cls
def get_namespace(self): namespace = self.conf.get(self.KEY_NAMESPACE, self.DEFAULT_NAMESPACE) assert isinstance(namespace, str) and len(namespace) > 0,\ ConfigurationError("Container manager 'kube' requires an existing namespace to be configured") return namespace
def __init__(self, **kwargs): super().__init__(**kwargs) self.compass: LWWarehouseCompass = self.compass assert self.compass.enabled, ConfigurationError("Lightweight store is disabled") self.connect()