Exemplo n.º 1
0
    def __init__(self,
                 resource_name: str = None,
                 size: str = None,
                 storage_class: str = None,
                 modes: List[str] = None,
                 annotations: Dict[str, str] = None,
                 data_source=None,
                 volume_name=None,
                 **kwargs):
        # Add size to attribute outputs
        self.attribute_outputs = {"size": "{.status.capacity.storage}"}

        if "k8s_resource" in kwargs:
            if resource_name or size or storage_class or modes or annotations:
                raise ValueError("You cannot provide k8s_resource along with "
                                 "other arguments.")
            if not isinstance(kwargs["k8s_resource"], V1PersistentVolumeClaim):
                raise ValueError("k8s_resource in VolumeOp must be an instance"
                                 " of V1PersistentVolumeClaim")
            super().__init__(**kwargs)
            self.volume = PipelineVolume(name=sanitize_k8s_name(self.name),
                                         pvc=self.outputs["name"])
            return

        if not size:
            raise ValueError("Please provide size")
        elif not match_serialized_pipelineparam(str(size)):
            self._validate_memory_string(size)

        if data_source and not isinstance(
                data_source,
            (str, PipelineParam, V1TypedLocalObjectReference)):
            raise ValueError("data_source can be one of (str, PipelineParam, "
                             "V1TypedLocalObjectReference).")
        if data_source and isinstance(data_source, (str, PipelineParam)):
            data_source = V1TypedLocalObjectReference(
                api_group="snapshot.storage.k8s.io",
                kind="VolumeSnapshot",
                name=data_source)

        # Set the k8s_resource
        if not match_serialized_pipelineparam(str(resource_name)):
            resource_name = sanitize_k8s_name(resource_name)
        pvc_metadata = V1ObjectMeta(name="{{workflow.name}}-%s" %
                                    resource_name,
                                    annotations=annotations)
        requested_resources = V1ResourceRequirements(
            requests={"storage": size})
        pvc_spec = V1PersistentVolumeClaimSpec(
            access_modes=modes or VOLUME_MODE_RWM,
            resources=requested_resources,
            storage_class_name=storage_class,
            data_source=data_source,
            volume_name=volume_name)
        k8s_resource = V1PersistentVolumeClaim(api_version="v1",
                                               kind="PersistentVolumeClaim",
                                               metadata=pvc_metadata,
                                               spec=pvc_spec)

        super().__init__(
            k8s_resource=k8s_resource,
            **kwargs,
        )
        self.volume = PipelineVolume(name=sanitize_k8s_name(self.name),
                                     pvc=self.outputs["name"])
Exemplo n.º 2
0
    def __init__(self,
                 resource_name: str = None,
                 pvc: str = None,
                 snapshot_class: str = None,
                 annotations: Dict[str, str] = None,
                 volume: V1Volume = None,
                 api_version: str = "snapshot.storage.k8s.io/v1alpha1",
                 **kwargs):
        # Add size to output params
        self.attribute_outputs = {"size": "{.status.restoreSize}"}
        # Add default success_condition if None provided
        if "success_condition" not in kwargs:
            kwargs["success_condition"] = "status.readyToUse == true"

        if "k8s_resource" in kwargs:
            if resource_name or pvc or snapshot_class or annotations or volume:
                raise ValueError("You cannot provide k8s_resource along with "
                                 "other arguments.")
            # TODO: Check if is VolumeSnapshot
            super().__init__(**kwargs)
            self.snapshot = V1TypedLocalObjectReference(
                api_group="snapshot.storage.k8s.io",
                kind="VolumeSnapshot",
                name=self.outputs["name"])
            return

        if not (pvc or volume):
            raise ValueError("You must provide a pvc or a volume.")
        elif pvc and volume:
            raise ValueError("You can't provide both pvc and volume.")

        source = None
        deps = []
        if pvc:
            source = V1TypedLocalObjectReference(kind="PersistentVolumeClaim",
                                                 name=pvc)
        else:
            if not hasattr(volume, "persistent_volume_claim"):
                raise ValueError("The volume must be referencing a PVC.")
            if hasattr(volume,
                       "dependent_names"):  #TODO: Replace with type check
                deps = list(volume.dependent_names)
            source = V1TypedLocalObjectReference(
                kind="PersistentVolumeClaim",
                name=volume.persistent_volume_claim.claim_name)

        # Set the k8s_resource
        # TODO: Use VolumeSnapshot
        if not match_serialized_pipelineparam(str(resource_name)):
            resource_name = sanitize_k8s_name(resource_name)
        snapshot_metadata = V1ObjectMeta(name="{{workflow.name}}-%s" %
                                         resource_name,
                                         annotations=annotations)
        k8s_resource = {
            "apiVersion": api_version,
            "kind": "VolumeSnapshot",
            "metadata": snapshot_metadata,
            "spec": {
                "source": source
            }
        }
        if snapshot_class:
            k8s_resource["spec"]["snapshotClassName"] = snapshot_class

        super().__init__(k8s_resource=k8s_resource, **kwargs)
        self.dependent_names.extend(deps)
        self.snapshot = V1TypedLocalObjectReference(
            api_group="snapshot.storage.k8s.io",
            kind="VolumeSnapshot",
            name=self.outputs["name"])
Exemplo n.º 3
0
    def __init__(self,
                 resource_name: str = None,
                 size: str = None,
                 storage_class: str = None,
                 modes: List[str] = VOLUME_MODE_RWM,
                 annotations: Dict[str, str] = None,
                 data_source=None,
                 **kwargs):
        """Create a new instance of VolumeOp.

        Args:
            resource_name: A desired name for the PVC which will be created
            size: The size of the PVC which will be created
            storage_class: The storage class to use for the dynamically created
                PVC
            modes: The access modes for the PVC
            annotations: Annotations to be patched in the PVC
            data_source: May be a V1TypedLocalObjectReference, and then it is
                used in the data_source field of the PVC as is. Can also be a
                string/PipelineParam, and in that case it will be used as a
                VolumeSnapshot name (Alpha feature)
            kwargs: See ResourceOp definition
        Raises:
            ValueError: if k8s_resource is provided along with other arguments
                        if k8s_resource is not a V1PersistentVolumeClaim
                        if size is None
                        if size is an invalid memory string (when not a
                            PipelineParam)
                        if data_source is not one of (str, PipelineParam,
                            V1TypedLocalObjectReference)
        """
        # Add size to attribute outputs
        self.attribute_outputs = {"size": "{.status.capacity.storage}"}

        if "k8s_resource" in kwargs:
            if resource_name or size or storage_class or modes or annotations:
                raise ValueError("You cannot provide k8s_resource along with "
                                 "other arguments.")
            if not isinstance(kwargs["k8s_resource"], V1PersistentVolumeClaim):
                raise ValueError("k8s_resource in VolumeOp must be an instance"
                                 " of V1PersistentVolumeClaim")
            super().__init__(**kwargs)
            self.volume = PipelineVolume(name=sanitize_k8s_name(self.name),
                                         pvc=self.outputs["name"])
            return

        if not size:
            raise ValueError("Please provide size")
        elif not match_serialized_pipelineparam(str(size)):
            self._validate_memory_string(size)

        if data_source and not isinstance(
                data_source,
            (str, PipelineParam, V1TypedLocalObjectReference)):
            raise ValueError("data_source can be one of (str, PipelineParam, "
                             "V1TypedLocalObjectReference).")
        if data_source and isinstance(data_source, (str, PipelineParam)):
            data_source = V1TypedLocalObjectReference(
                api_group="snapshot.storage.k8s.io",
                kind="VolumeSnapshot",
                name=data_source)

        # Set the k8s_resource
        if not match_serialized_pipelineparam(str(resource_name)):
            resource_name = sanitize_k8s_name(resource_name)
        pvc_metadata = V1ObjectMeta(name="{{workflow.name}}-%s" %
                                    resource_name,
                                    annotations=annotations)
        requested_resources = V1ResourceRequirements(
            requests={"storage": size})
        pvc_spec = V1PersistentVolumeClaimSpec(
            access_modes=modes,
            resources=requested_resources,
            storage_class_name=storage_class,
            data_source=data_source)
        k8s_resource = V1PersistentVolumeClaim(api_version="v1",
                                               kind="PersistentVolumeClaim",
                                               metadata=pvc_metadata,
                                               spec=pvc_spec)

        super().__init__(
            k8s_resource=k8s_resource,
            **kwargs,
        )
        self.volume = PipelineVolume(name=sanitize_k8s_name(self.name),
                                     pvc=self.outputs["name"])