Example #1
0
    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
Example #2
0
 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 get_by_name(config=None, name=None, name_label='name'):
        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': '{0}={1}'.format(name_label, 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).from_model(m=model)
                rc_list.append(obj)
            except NotFoundException:
                pass

        return rc_list
Example #4
0
 def add_port(self,
              name=None,
              port=None,
              target_port=None,
              protocol=None,
              node_port=None):
     if not is_valid_string(name):
         raise SyntaxError(
             'Service.add_port() name: [ {} ] is invalid.'.format(name))
     if port is None or not (isinstance(port, str)
                             or isinstance(port, int)):
         raise SyntaxError(
             'Service.add_port() port: [ {} ] is invalid.'.format(name))
     ports = []
     port_found = False
     # exists previously
     for p in self.spec.ports:
         if int(p.port) == int(port) and p.protocol == protocol:
             p.name = name
             p.target_port = target_port
             p.protocol = str(protocol).upper()
             p.node_port = node_port
             port_found = True
         ports.append(p)
     # doesn't exist yet
     if not port_found:
         p = ServicePort()
         p.name = name
         p.port = port
         p.target_port = target_port
         p.protocol = protocol
         p.node_port = node_port
         ports.append(p)
     self.spec.ports = ports
Example #5
0
    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
Example #6
0
 def service_account_name(self, san=None):
     if not is_valid_string(san):
         raise SyntaxError(
             "PodSpec: service_account_name: [ {0} ] is invalid.".format(
                 san))
     self._service_account_name = san
     self._service_account = san
Example #7
0
 def get_by_name(cls, config=None, name=None, name_label='name'):
     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_label: name,
     })
Example #8
0
 def sub_path(self, path=None):
     if path is None:
         path = ""  # default; volume's root
     if not is_valid_string(path):
         raise SyntaxError(
             "VolumeMount: sub_path: [ {0} ] is invalid.".format(path))
     self._sub_path = path
Example #9
0
 def operator(self, o=None):
     if not is_valid_string(
             o) or o not in NodeSelectorRequirement.VALID_OPERATORS:
         raise SyntaxError(
             'NodeSelectorRequirement: operator: [ {} ] is invalid.'.format(
                 o))
     self._operator = o
Example #10
0
 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
Example #11
0
 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
Example #12
0
 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
Example #13
0
 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
Example #14
0
    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_py.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 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
Example #16
0
    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
Example #17
0
    def get_by_pod_ip(config=None, ip=None, labels=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(labels=labels)

        for pod in pods:
            try:
                assert isinstance(pod, K8sPod)
                if pod.pod_ip == ip:
                    found = pod
                    break
            except NotFoundException:
                pass
        return found
Example #18
0
    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 name(self, name=None):
     if not is_valid_string(name):
         raise SyntaxError(
             "DeploymentRollback: name: [ {} ] is invalid.".format(name))
     self._name = name
 def api_version(self, v=None):
     if not is_valid_string(v):
         raise SyntaxError(
             "DeploymentRollback: api_version: [ {} ] is invalid.".format(
                 v))
     self._api_version = v
 def kind(self, kind=None):
     if not is_valid_string(kind):
         raise SyntaxError(
             "DeploymentRollback: kind: [ {} ] is invalid.".format(kind))
     self._kind = kind
Example #22
0
 def key(self, k=None):
     if not is_valid_string(k):
         raise SyntaxError(
             'SecretKeySelector: key: [ {} ] is invalid.'.format(k))
     self._key = k
Example #23
0
 def name(self, n=None):
     if not is_valid_string(n):
         raise SyntaxError(
             'SecretKeySelector: name: [ {} ] is invalid.'.format(n))
     self._name = n
Example #24
0
 def kind(self, k=None):
     if not is_valid_string(k):
         raise SyntaxError("BaseModel: kind: [ {} ] is invalid.".format(k))
     self._kind = k
Example #25
0
 def api_version(self, v=None):
     if not is_valid_string(v):
         raise SyntaxError(
             "BaseModel: api_version: [ {} ] is invalid.".format(v))
     self._api_version = v
Example #26
0
 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})
Example #27
0
 def message(self, msg=None):
     if not is_valid_string(msg):
         raise SyntaxError(
             "ContainerStateTerminated: message: [ {0} ] is invalid.".
             format(msg))
     self._message = msg
Example #28
0
 def reason(self, msg=None):
     if not is_valid_string(msg):
         raise SyntaxError(
             "ContainerStateTerminated: reason: [ {0} ] is invalid.".format(
                 msg))
     self._reason = msg
Example #29
0
 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
Example #30
0
 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