Coverage for appr/formats/appr/kub.py: 25%

# Coverage for appr/formats/appr/kub.py : 25%

## 119 statements 30 run 89 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | from __future__ import absolute_import, division, print_function import json import logging import os import os.path from collections import OrderedDict import jsonpatch import yaml from appr.display import print_deploy_result from appr.formats.appr.kub_base import KubBase from appr.platforms.kubernetes import ANNOTATIONS, Kubernetes, get_endpoint from appr.utils import colorize, mkdir_p logger = logging.getLogger(__name__) _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG class Kub(KubBase): media_type = 'appr' platform = "kubernetes" def _resource_name(self, resource): return resource.get('name', resource['value']['metadata']['name']) def _resource_build(self, kub, resource): self._annotate_resource(kub, resource) kind = resource['value']['kind'].lower() return { "file": resource.get('file', "%s-%s.yaml" % (self._resource_name(resource), kind)), "update_mode": resource.get('update_mode', 'update'), "hash": resource['value']['metadata']['annotations'].get(ANNOTATIONS['hash'], None), "protected": resource['protected'], "name": self._resource_name(resource), "kind": kind, "endpoint": get_endpoint(resource['value']['kind'].lower()).format(namespace=self.namespace), "body": json.dumps(resource['value'])} # @TODO do it in jsonnet def _annotate_resource(self, kub, resource): if 'annotations' not in resource['value']['metadata']: resource['value']['metadata']['annotations'] = {} if resource.get('hash', True): # Hash is calculated later resource['value']['metadata']['annotations'][ANNOTATIONS['hash']] = None annotation = resource['value']['metadata']['annotations'] annotation[ANNOTATIONS['version']] = kub.version annotation[ANNOTATIONS['package']] = kub.name annotation[ANNOTATIONS['parent']] = self.name annotation[ANNOTATIONS['update-mode']] = str( resource.get('update_mode', annotation.get(ANNOTATIONS['update-mode'], 'update'))).lower() annotation[ANNOTATIONS['protected']] = str( resource.get('protected', annotation.get(ANNOTATIONS['protected'], 'false'))).lower() return resource def _create_namespaces(self): if self.namespace: ns = self.create_namespace(self.namespace) self._resources.insert(0, ns) def resources(self): """ Override resources to auto-create namespace""" if self._resources is None: self._resources = self.manifest.resources self._create_namespaces() return self._resources def _apply_patches(self, resources): for _, resource in resources.iteritems(): if self.namespace: if 'namespace' in resource['value']['metadata']: op = 'replace' else: op = 'add' resource['patch'].append({ "op": op, "path": "/metadata/namespace", "value": self.namespace}) if len(resource['patch']): patch = jsonpatch.JsonPatch(resource['patch']) result = patch.apply(resource['value']) resource['value'] = result return resources @property def kubClass(self): return Kub def create_namespace(self, namespace): value = {"apiVersion": "v1", "kind": "Namespace", "metadata": {"name": namespace}} resource = { "file": "%s-ns.yaml" % namespace, "name": namespace, "generated": True, "order": -1, "hash": False, "protected": True, "update_mode": 'update', "value": value, "patch": [], "variables": {}, "type": "namespace"} return resource def build(self): result = [] for kub in self.dependencies: result.append(self._dep_build(kub)) return {"deploy": result, "package": {"name": self.name, "version": self.version}} def _dep_build(self, kub): package = { "package": kub.name, "version": kub.version, "namespace": kub.namespace, "resources": []} for resource in kub.resources(): package['resources'].\ append(self._resource_build(kub, resource)) return package def _process_deploy(self, dry=False, force=False, fmt="txt", proxy=None, action="create", dest="/tmp/appr"): def output_progress(kubsource, status, fmt="text"): if fmt == 'text': print(" --> %s (%s): %s" % (kubsource.name, kubsource.kind, colorize(status))) dest = os.path.join(dest, self.name, self.version) mkdir_p(dest) table = [] results = [] if fmt == "text": print("%s %s " % (action, self.name)) i = 0 for kub in self.dependencies: package = self._dep_build(kub) i += 1 pname = package["package"] version = package["version"] namespace = package["namespace"] if fmt == "text": print("\n %02d - %s:" % (i, package["package"])) for resource in package["resources"]: body = resource["body"] endpoint = resource["endpoint"] # Use API instead of kubectl with open( os.path.join(dest, "%s-%s" % (resource['name'], resource['file'].replace("/", "_"))), 'wb') as f: f.write(body) kubresource = Kubernetes(namespace=namespace, body=body, endpoint=endpoint, proxy=proxy) status = getattr(kubresource, action)(force=force, dry=dry, strategy=resource.get( 'update_mode', 'update')) if fmt == "text": output_progress(kubresource, status) result_line = OrderedDict( [("package", pname), ("version", version), ("kind", kubresource.kind), ("dry", dry), ("name", kubresource.name), ("namespace", kubresource.namespace), ("status", status)]) if status != 'ok' and action == 'create': kubresource.wait(os.getenv("APPR_KUB_SECONDS", 0.1)) results.append(result_line) if fmt == "text": header = ["package", "version", "kind", "name", "namespace", "status"] display_line = [] for k in header: display_line.append(result_line[k]) table.append(display_line) if fmt == "text": print(print_deploy_result(table)) return results def deploy(self, *args, **kwargs): kwargs['action'] = 'create' return self._process_deploy(*args, **kwargs) def delete(self, *args, **kwargs): kwargs['action'] = 'delete' return self._process_deploy(*args, **kwargs)