def set_service_account_token(self, account_name=None, account_uid=None, token=None, kubecfg_data=None, cacert=None): for x in [account_name, account_uid, token]: if not is_valid_string(x): raise SyntaxError('Secret.set_service_account() account_name: [ {} ] is invalid.'.format(x)) if not is_valid_string(account_uid): raise SyntaxError('Secret.set_service_account() account_uid: [ {} ] is invalid.'.format(account_uid)) if not is_valid_string(token): raise SyntaxError('Secret.set_service_account() token: [ {} ] is invalid.'.format(token)) anns = { self.K8s_ANNOTATION_SERVICE_ACCOUNT_NAME: account_name, self.K8s_ANNOTATION_SERVICE_ACCOUNT_UID: account_uid } self.type = self.K8s_TYPE_SERVICE_ACCOUNT self.metadata.annotations = anns self.data = {'token': token} if is_valid_string(kubecfg_data): d = self.data d.update({'kubernetes.kubeconfig': kubecfg_data}) self.data = d if is_valid_string(cacert): d = self.data d.update({'ca.crt': cacert}) self.data = d return self
def set_container_image(self, name=None, image=None): if not is_valid_string(name): raise SyntaxError('PodSpec: name: [ {0} ] is invalid.') if not is_valid_string(image): raise SyntaxError('PodSpec: image: [ {0} ] is invalid.') for c in self.containers: if c.name == name: c.image(image=image) break return self
def untaint(self, key=None, value=None): if key and value: if not is_valid_string(key) or not is_valid_string(value): raise SyntaxError('K8sNode: taint: key: [ {} ] or value: [ {} ] is invalid.'.format(key, value)) remaining_taints = [] for t in self.taints: if key and value: if t.key != key and t.value != value: remaining_taints.append(t) self.taints = remaining_taints self.update() return self
def get_by_name(cls, config=None, name=None): if not is_valid_string(name): raise SyntaxError( 'K8sPod.get_by_name(): name: [ {0} ] is invalid.'.format(name)) return cls.get_by_labels(config=config, labels={ "name": name, })
def path(self, path=None): if not is_valid_string(path): raise SyntaxError('KeyToPath: path: [ {0} ] is invalid.'.format(path)) if re.match('/', path): raise SyntaxError('KeyToPath: path: [ {0} ] is invalid. It may not be an absolute path'.format(path)) if re.search('\.\.', path): raise SyntaxError('KeyToPath: path: [ {0} ] is invalid. It may not contain the string ".."'.format(path)) self._path = path
def mode(self, mode=None): if is_valid_string(mode): try: mode = int(mode) except ValueError: raise SyntaxError('KeyToPath: mode: [ {0} ] is invalid.'.format(mode)) if not isinstance(mode, int): raise SyntaxError('KeyToPath: mode: [ {0} ] is invalid.'.format(mode)) self._mode = mode
def data(self): d = {} for k, v in self._data.items(): d[k] = base64.b64decode(v) if isinstance(d[k], bytes): d[k] = d[k].decode() elif is_valid_string(d[k]): d[k] = d[k].decode() return d
def default_mode(self, mode=None): if is_valid_string(mode): try: mode = int(mode) except ValueError: raise SyntaxError('ConfigMapVolumeSource: defaultMode: [ {0} ] is invalid.'.format(mode)) if not isinstance(mode, int): raise SyntaxError('ConfigMapVolumeSource: defaultMode: [ {0} ] is invalid.'.format(mode)) self._default_mode = mode
def target_port(self, port=None): msg = 'ServicePort: target_port: [ {} ] is invalid.'.format(port) try: p = int(port) except ValueError: if not is_valid_string(port): raise SyntaxError(msg) p = port except TypeError: raise SyntaxError(msg) self._target_port = p
def taint(self, key=None, value=None, effect=None): if not (key and value and effect): raise SyntaxError('K8sNode: taint: you must specify a key, a value and an effect.') if not is_valid_string(key) or not is_valid_string(value): raise SyntaxError('K8sNode: taint: key: [ {} ] or value: [ {} ] is invalid.'.format(key, value)) if effect not in Taint.VALID_TAINT_EFFECTS: raise SyntaxError('K8sNode: taint: effect must be in {}'.format(Taint.VALID_TAINT_EFFECTS)) t = Taint() t.key = key t.value = value t.effect = effect exists = False for existing_taint in self.taints: if existing_taint.key == key and existing_taint.value == value and existing_taint.effect == effect: exists = True if not exists: self.taints.append(t) self.update() return self
def serialize(self): data = super(Secret, self).serialize() if self.data is not None: d = {} for k, v in self.data.items(): if is_valid_string(v): v = bytearray(source=v, encoding='UTF-8') d[k] = base64.b64encode(v) if isinstance(d[k], bytes): d[k] = d[k].decode() data['data'] = d if self.string_data is not None: data['stringData'] = self.string_data if self.type is not None: data['type'] = self.type return data
def get_by_pod_ip(config=None, ip=None): if config is None: config = K8sConfig() if not is_valid_string(ip): raise SyntaxError( 'K8sPod.get_by_pod_ip(): ip: [ {0} ] is invalid.'.format(ip)) found = None pods = K8sPod(config=config, name='throwaway').list() for pod in pods: try: assert isinstance(pod, K8sPod) if pod.pod_ip == ip: found = pod break except NotFoundException: pass return found
def get_by_name(config=None, name=None): if config is not None and not isinstance(config, K8sConfig): raise SyntaxError( 'ReplicationController.get_by_name(): config: [ {0} ] is invalid.'.format(config)) if not is_valid_string(name): raise SyntaxError( 'K8sReplicationController.get_by_name() name: [ {0} ] is invalid.'.format(name)) rc_list = [] data = {'labelSelector': 'name={0}'.format(name)} rcs = K8sReplicationController(config=config, name=name).get_with_params(data=data) for rc in rcs: try: model = ReplicationController(rc) obj = K8sReplicationController(config=config, name=model.metadata.name) rc_list.append(obj.get()) except NotFoundException: pass return rc_list
def data(self, data=None): msg = 'Secret: data: [ {0} ] is invalid.'.format(data) if isinstance(data, string_types): try: data = json.loads(data) except ValueError: raise SyntaxError(msg) if not is_valid_dict(data): raise SyntaxError(msg) for k, v in data.items(): if not is_valid_string(k): raise SyntaxError(msg) if not isinstance(v, bytes): try: v = bytearray(v, 'UTF-8') except: raise SyntaxError('Could not convert [ {0} ] to bytes.'.format(v)) self._data[k] = base64.b64encode(v)
def message(self, msg=None): if not is_valid_string(msg): raise SyntaxError('ContainerStateTerminated: message: [ {0} ] is invalid.'.format(msg)) self._message = msg
def container_id(self, cid=None): if not is_valid_string(cid): raise SyntaxError('ContainerStateTerminated: container_id: [ {0} ] is invalid.'.format(cid)) self._container_id = cid
def deletion_timestamp(self, time=None): if not is_valid_string(time): raise SyntaxError('ObjectMeta: deletion_timestamp: [ {0} ] is invalid.'.format(time)) self._deletion_timestamp = time
def host(self, host=None): if not is_valid_string(host) : raise SyntaxError('HTTPGetAction: host: [ {0} ] is invalid.'.format(host)) self._host = host
def add_header(self, name=None, value=None): if not is_valid_string(name) or not is_valid_string(value): raise SyntaxError('HTTPGetAction: header: [ {0} : {1} ] is invalid.'.format(name, value)) data = {'name': name, 'value': value} self._http_headers.append(data)
def provisioner(self, p=None): if not is_valid_string(p): raise SyntaxError('StorageClass: provisioner: [ {} ] is invalid.'.format(p)) self._provisioner = p
def working_dir(self, wdir=None): if not is_valid_string(wdir): raise SyntaxError( 'Container: working_dir: [ {0} ] is invalid.'.format(wdir)) self._working_dir = wdir
def name(self, name=None): if not is_valid_string(name): raise SyntaxError( 'Container: name: [ {0} ] is invalid.'.format(name)) self._name = name
def image(self, image=None): if not is_valid_string(image): raise SyntaxError( 'Container: image: [ {0} ] is invalid.'.format(image)) self._image = image
def kind(self, k=None): if not is_valid_string(k): raise SyntaxError('BaseModel: kind: [ {} ] is invalid.'.format(k)) self._kind = k
def name(self, name=None): if not is_valid_string(name): raise SyntaxError( 'BaseModel: name: [ {} ] is invalid.'.format(name)) self.metadata.name = name self.metadata.labels.update({'name': name})
def cluster_name(self, name=None): if not is_valid_string(name): raise SyntaxError('ObjectMeta: cluster_name: [ {0} ] is invalid.'.format(name)) self._cluster_name = name
def kind(self, k=None): if not is_valid_string(k): raise SyntaxError('StorageClass: kind: [ {} ] is invalid.'.format(k)) self._kind = k
def last_transition_time(self, time=None): if not is_valid_string(time): raise SyntaxError( 'PodCondition: last_transition_time: [ {0} ] is invalid.'. format(time)) self._last_transition_time = time
def name(self, name=None): if not is_valid_string(name): raise SyntaxError('Container: name: [ {0} ] is invalid.'.format(name)) self._name = name
def reason(self, r=None): if not is_valid_string(r): raise SyntaxError( 'PodCondition: reasons: [ {0} ] is invalid.'.format(r)) self._reason = r
def api_version(self, v=None): if not is_valid_string(v): raise SyntaxError( 'Secret: api_version: [ {0} ] is invalid.'.format(v)) self._api_version = v
def namespace(self, namespace=None): if not is_valid_string(namespace): raise SyntaxError('ObjectMeta: namespace: [ {0} ] is invalid.'.format(namespace)) self._namespace = namespace
def generate_name(self, name=None): if not is_valid_string(name): raise SyntaxError('ObjectMeta: generate_name: [ {0} ] is invalid.'.format(name)) self._generate_name = name
def type(self, t=None): if not is_valid_string(t): raise SyntaxError( 'PodCondition: status: [ {0} ] is invalid.'.format(t)) self._type = t
def message(self, msg=None): if not is_valid_string(msg): raise SyntaxError( 'PodCondition: message: [ {0} ] is invalid.'.format(msg)) self._message = msg
def uid(self, uid=None): if uid is not None: if not is_valid_string(uid): raise SyntaxError('ObjectMeta: uid: [ {0} ] is invalid.'.format(uid)) self._uid = uid
def path(self, path=None): if not is_valid_string(path): raise SyntaxError('HTTPGetAction: path: [ {0} ] is invalid.'.format(path)) self._path = path
def type(self, t=None): if not is_valid_string(t): raise SyntaxError('JobCondition: type: [ {} ] is invalid.'.format(t)) self._type = t
def last_transition_time(self, t=None): if not is_valid_string(t): raise SyntaxError('JobCondition: last_transition_time: [ {} ] is invalid.'.format(t)) self._last_transition_time = t
def topology_key(self, t=None): if not is_valid_string(t): raise SyntaxError('PodAffinityTerm: topology_key: [ {} ] is invalid.'.format(t)) self._topology_key = t
def name(self, name=None): if not is_valid_string(name): raise SyntaxError( 'PersistentVolumeSpec: name: [ {0} ] is invalid.'.format(name)) self._name = name
def status(self, status=None): if not is_valid_string(status): raise SyntaxError( 'PodCondition: status: [ {0} ] is invalid.'.format(status)) self._status = status
def status(self, s=None): if not is_valid_string(s): raise SyntaxError('JobCondition: status: [ {} ] is invalid.'.format(s)) self._status = s
def storage_class_name(self, name=None): if not is_valid_string(name): raise SyntaxError( 'PersistentVolumeSpec: storage_class_name: [ {} ] is invalid.'. format(name)) self._storage_class_name = name
def reason(self, r=None): if not is_valid_string(r): raise SyntaxError('JobCondition: reason: [ {} ] is invalid.'.format(r)) self._reason = r
def message(self, m=None): if not is_valid_string(m): raise SyntaxError("Event: message: [ {} ] is invalid.".format(m)) self._message = m
def finished_at(self, time=None): if not is_valid_string(time): raise SyntaxError('ContainerStateTerminated: finished_at: [ {0} ] is invalid.'.format(time)) self._finished_at = time
def reason(self, r=None): if not is_valid_string(r): raise SyntaxError("Event: reason: [ {} ] is invalid.".format(r)) self._reason = r
def reason(self, msg=None): if not is_valid_string(msg): raise SyntaxError('ContainerStateTerminated: reason: [ {0} ] is invalid.'.format(msg)) self._reason = msg
def type(self, t=None): if not is_valid_string(t): raise SyntaxError("Event: type: [ {} ] is invalid.".format(t)) self._type = t
def type(self, t=None): if not is_valid_string(t): raise SyntaxError('DeploymentStrategy: type: [ {} ] is invalid.'.format(t)) self._type = t
def self_link(self, link=None): if not is_valid_string(link): raise SyntaxError( 'ListMeta: self_link: [ {0} ] is invalid.'.format(link)) self._self_link = link
def schedule(self, s=None): if not is_valid_string(s): raise SyntaxError('CronJobSpec: schedule: [ {} ] is invalid.'.format(s)) self._schedule = s
def name(self, name=None): if not is_valid_string(name): raise SyntaxError( 'ConfigMapVolumeSource: name: [ {0} ] is invalid.'.format( name)) self._name = name
def api_version(self, v=None): if not is_valid_string(v): raise SyntaxError('StorageClass: api_version: [ {} ] is invalid.'.format(v)) self._api_version = v
def type(self, t=None): if not is_valid_string(t): raise SyntaxError('Secret: type: [ {0} ] is invalid.'.format(t)) self._type = t
def image(self, image=None): if not is_valid_string(image): raise SyntaxError('Container: image: [ {0} ] is invalid.'.format(image)) self._image = image
def kind(self, k=None): if not is_valid_string(k): raise SyntaxError('Secret: kind: [ {0} ] is invalid.'.format(k)) self._kind = k
def working_dir(self, wdir=None): if not is_valid_string(wdir): raise SyntaxError('Container: working_dir: [ {0} ] is invalid.'.format(wdir)) self._working_dir = wdir
def subdomain(self, subdomain=None): if not is_valid_string(subdomain): raise SyntaxError( 'PodSpec: subdomain: [ {0} ] is invalid.'.format(subdomain)) self._subdomain = subdomain