def map_context(self, context: TemplateContext) -> TemplateContext: if self._is_ambassador(context): context.append_templates([{ 'apiVersion': 'getambassador.io/v1', 'kind': 'Mapping', 'metadata': { 'name': self.name + '-mapping' }, 'spec': nmap({ 'prefix': '/', 'service': self.service + str( M.map(self.port, (lambda x: ':' + str(x))) | M.value_or_default | '') }).append_if_value( 'host', M.nothing() if self.host == '*' else M.just(self.host)).to_map() }]) return context if self._is_istio(context): context.append_templates([{ 'apiVersion': 'networking.istio.io/v1alpha3', 'kind': 'VirtualService', 'metadata': { 'name': self.name + '-vs' }, 'spec': { 'hosts': [self.host], 'gateways': [self.gateway_name], 'http': nlist([ nmap({ 'route': [{ 'destination': nmap({ 'host': self.service }).append_if_value( 'port', M.map(self.port, (lambda x: { 'number': x }))).to_map() }] }).to_map(), ]).to_list() } }]) return context raise AssertionError('Unsupported gateway')
def with_resources(self, cpu_request: Optional[str] = None, memory_request: Optional[str] = None, cpu_limit: Optional[str] = None, memory_limit: Optional[str] = None): def set_resource(cpu, memory): return N.nmap({}) \ .append_if_value('cpu', cpu) \ .append_if_value('memory', memory) \ .to_maybe() requests = set_resource(cpu_request, memory_request) limits = set_resource(cpu_limit, memory_limit) self.resources = N.nmap({}) \ .append_if_value('requests', requests) \ .append_if_value('limits', limits) \ .to_maybe() return self
def get_template(self, context: TemplateContext): cluster_name = context.get_state('cluster_name') if cluster_name is not None: self.environment_vars = set_environment_variable( self.environment_vars, 'CLUSTER', cluster_name) for (name, action) in self.environment_vars_from_context: self.environment_vars = set_environment_variable( self.environment_vars, name, action(lambda key: context.get_state(key))) return nmap({ 'name': app_name(self.name), 'image': self.image, 'ports': self.app_ports, 'env': self.environment_vars, 'volumeMounts': self.volumes }).append_if_value( 'command', self.command) \ .append_if_value( 'resources', self.resources) \ .to_map()
def _get_templates(name: str, image: str, schedule: str, suspend: bool, env_vars: List[str], command: M.Maybe[List[str]]) -> List[dict]: return nlist([ { 'kind': 'CronJob', 'apiVersion': 'batch/v1beta1', 'metadata': {'name': name}, 'spec': { 'suspend': suspend, 'concurrencyPolicy': 'Forbid', 'schedule': schedule, 'jobTemplate': {'spec': { 'template': { 'metadata': { 'annotations': { 'traffic.sidecar.istio.io/excludeOutboundIPRanges': "0.0.0.0/0", "sidecar.istio.io/inject": "false"} }, 'spec': { 'containers': nlist([ nmap({ 'name': name, 'image': image, 'env': env_vars }).append_if_value( 'command', command).to_map() ]).to_list(), 'volumes': nlist([]).to_list(), 'restartPolicy': 'Never' } }, 'backoffLimit': 0 }}, } } ]).to_list()
def map_context(self, context: TemplateContext) -> TemplateContext: context.append_templates([ { 'kind': 'Deployment', 'apiVersion': 'apps/v1', 'metadata': { 'name': _deployment_name(self.name), 'labels': {'app': app_name(self.name)}, }, 'spec': { 'strategy': self.strategy, 'replicas': self.replicas, 'selector': { 'matchLabels': { 'app': app_name(self.name) } }, 'template': { 'metadata': { 'annotations': { "sidecar.istio.io/inject": "false"}, 'labels': {'app': app_name(self.name)}}, 'spec': nmap({ 'containers': list(map(lambda x: x.get_template(context), self.containers)), 'volumes': self.volumes }).append_if_value('initContainers', M.map( self.init_containers, lambda init_containers: list(map( lambda x: x.get_template(context), init_containers)))) .to_map() } } }, ]) return context
def set_resource(cpu, memory): return N.nmap({}) \ .append_if_value('cpu', cpu) \ .append_if_value('memory', memory) \ .to_maybe()
def map_context(self, context: TemplateContext) -> TemplateContext: if context.get_state('cluster_name') is not None: self.with_environment_variable('CLUSTER', context.get_state('cluster_name')) templates = nlist([{ 'kind': 'Service', 'apiVersion': 'v1', 'metadata': { 'name': self.name }, 'spec': { 'type': self.service_type, 'selector': { 'app': _app_name(self.name) }, 'ports': [{ 'protocol': 'TCP', 'port': self.service_port, 'targetPort': self.app_port }] } }, { 'apiVersion': 'apps/v1', 'kind': 'Deployment', 'metadata': { 'name': _deployment_name(self.name), 'labels': { 'app': _app_name(self.name) }, }, 'spec': { 'replicas': self.min_replicas, 'strategy': self.strategy, 'selector': { 'matchLabels': { 'app': _app_name(self.name) } }, 'template': { 'metadata': { 'annotations': self.annotations, 'labels': { 'app': _app_name(self.name) } }, 'spec': nmap({ 'containers': nlist([ nmap({ 'name': _app_name(self.name), 'image': self.image, 'ports': [{ 'containerPort': self.app_port }], 'env': self.environment_vars, 'volumeMounts': list(map(lambda x: x['mount'], self.volumes)) }).append_if_value('command', self.command).append_if_value( 'resources', self.resources).to_map() ]).append_all( list( map(lambda x: x.get_template(context), self.sidecars))).to_list(), 'volumes': list(map(lambda x: x['volume'], self.volumes)) }).append_if_value('initContainers', self.init_containers).to_map() } } }, { 'apiVersion': 'autoscaling/v1', 'kind': 'HorizontalPodAutoscaler', 'metadata': { 'name': _horizontal_pod_autoscaler_name(self.name) }, 'spec': { 'scaleTargetRef': { 'apiVersion': 'apps/v1', 'kind': 'Deployment', 'name': _deployment_name(self.name) }, 'minReplicas': self.min_replicas, 'maxReplicas': self.max_replicas, 'targetCPUUtilizationPercentage': self.cpu_threshold_percentage } }]).to_list() context.append_templates(templates) return context
def test_can_convert_to_base_map_type(): a_map = nmap({'a': 1}).to_map() assert a_map == {'a': 1} assert type(a_map) == dict
def test_will_get_value_if_key_present(): assert nmap({'a': 1}).get_or_none('a') == 1
def test_will_get_none_if_key_missing(): assert nmap({'a': 1}).get_or_none('b') is None
def test_will_ignore_append_to_map_if_nothing(): assert nmap({'a': 1}).append_if_value('b', M.nothing()) == {'a': 1}
def test_will_append_to_map_if_value(): assert nmap({'a': 1}).append_if_value('b', M.just(2)) == {'a': 1, 'b': 2}