Coverage for appr/platforms/kubernetes.py : 19%

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
"daemonsets": "apis/extensions/v1beta1/namespaces/{namespace}/daemonsets", "deployments": "apis/extensions/v1beta1/namespaces/{namespace}/deployments", "horizontalpodautoscalers": "apis/extensions/v1beta1/namespaces/{namespace}/horizontalpodautoscalers", "ingresses": "apis/extensions/v1beta1/namespaces/{namespace}/ingresses", "jobs": "apis/extensions/v1beta1/namespaces/{namespace}/jobs", "namespaces": "api/v1/namespaces", "replicasets": "apis/extensions/v1beta1/namespaces/{namespace}/replicasets", "persistentvolumes": "api/v1/namespaces/{namespace}/persistentvolumes", "persistentvolumeclaims": "api/v1/namespaces/{namespace}/persistentvolumeclaims", "services": "api/v1/namespaces/{namespace}/services", "serviceaccounts": "api/v1/namespaces/{namespace}/serviceaccounts", "secrets": "api/v1/namespaces/{namespace}/secrets", "configmaps": "api/v1/namespaces/{namespace}/configmaps", "replicationcontrollers": "api/v1/namespaces/{namespace}/replicationcontrollers", "pods": "api/v1/namespaces/{namespace}/pods", "statefulset": "apis/apps/v1beta1/namespaces/{namespace}/statefulsets", "storageclass": "apis/storage.k8s.io/v1beta1/statefulsets", }
"ds": "daemonsets", "hpa": "horizontalpodautoscalers", "ing": "ingresses", "ingress": "ingresses", "ns": "namespaces", "sc": "storageclasses", "sfs": "statefulsets", "po": "pods", "pv": "persistentvolumes", "pvc": "persistentvolumeclaims", "rc": "replicationcontrollers", "svc": "services" }
'protected': 'resource.appr/protected', 'hash': 'resource.appr/hash', 'version': 'package.appr/version', 'parent': 'package.appr/parent', 'update-mode': 'resource.appr/update-mode', 'package': 'package.appr/package' }
name = None if kind in resource_endpoints: name = kind elif kind in resources_alias: name = resources_alias[kind] elif kind + "s" in resource_endpoints: name = kind + "s" else: return None return resource_endpoints[name]
self.proxy = None if endpoint is not None and endpoint[0] == "/": endpoint = endpoint[1:-1] self.endpoint = endpoint self.body = body self.obj = None self.protected = False self._resource_load()
self.kind = self.obj['kind'].lower() self.name = self.obj['metadata']['name'] self.kpmhash = self._get_kpmhash(self.obj) self.namespace = self._namespace(namespace) self.result = None if proxy: self.proxy = urlparse(proxy)
self.obj = json.loads(self.body) if 'annotations' in self.obj['metadata']: if (ANNOTATIONS['protected'] in self.obj['metadata']['annotations'] and self.obj['metadata']['annotations'][ANNOTATIONS['protected']] == 'true'): self.protected = True
if namespace: return namespace elif 'namespace' in self.obj['metadata']: return self.obj['metadata']['namespace'] else: return 'default'
""" - Check if resource name exists - if it exists check if the kpmhash is the same - if not the same delete the resource and recreate it - if force == true, delete the resource and recreate it - if doesnt exists create it """ r = self.get() f = tempfile.NamedTemporaryFile() method = "apply" if self.proxy: method = "create" strategy = "replace"
cmd = [method, '-f', f.name] f.write(self.body) f.flush() if r is None: self._call(cmd, dry=dry) return 'created' elif (self.kpmhash is None or self._get_kpmhash(r) == self.kpmhash) and force is False: return 'ok' elif self._get_kpmhash(r) != self.kpmhash or force is True: if strategy == 'replace' or force: if self.delete(dry=dry) == 'protected': return 'protected' action = "replaced" elif strategy == "update": action = "updated" else: raise ValueError("Unknown action %s" % action) self._call(cmd, dry=dry) return action
if 'annotations' in r['metadata'] and ANNOTATIONS['hash'] in r['metadata']['annotations']: return r['metadata']['annotations'][ANNOTATIONS['hash']] else: return None
cmd = ['get', self.kind, self.name, '-o', 'json'] try: self.result = json.loads(self._call(cmd)) return self.result except RuntimeError: return None except (requests.exceptions.HTTPError) as e: if e.response.status_code == 404: return None else: raise e
cmd = ['delete', self.kind, self.name] if self.protected: return 'protected' r = self.get() if r is not None: self._call(cmd, dry=dry) return 'deleted' else: return 'absent'
r = 1 time.sleep(seconds) obj = self.get() while (r < retries and obj is None): r += 1 time.sleep(seconds) obj = self.get() return obj
r = self.get() if r is None: return False else: return True
command = ['kubectl'] + cmd + ["--namespace", self.namespace] if not dry: if self.proxy is not None: return self._request(cmd[0]) else: try: return subprocess.check_output(command, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: raise RuntimeError("Kubernetes failed to create %s (%s): " "%s" % (self.name, self.kind, e.output)) else: return True
if method == 'create': headers = {'Content-Type': 'application/json'} method = 'post' url = "%s/%s" % (self.proxy.geturl(), self.endpoint) return requests.post(url, data=self.body, headers=headers) else: url = "%s/%s/%s" % (self.proxy.geturl(), self.endpoint, self.name) query = getattr(requests, method) r = query(url) r.raise_for_status() return r.content |