Ejemplo n.º 1
0
def create_clone_pvc(pvc_values, sc_name, pvc_name, from_pvc_name,
                     created_objects):
    api_instance = client.CoreV1Api()
    pvc_metadata = client.V1ObjectMeta(name=pvc_name)
    pvc_resources = client.V1ResourceRequirements(
        requests={"storage": pvc_values["storage"]})
    pvc_data_source = client.V1TypedLocalObjectReference(
        kind="PersistentVolumeClaim", name=from_pvc_name)

    pvc_spec = client.V1PersistentVolumeClaimSpec(
        access_modes=[pvc_values["access_modes"]],
        resources=pvc_resources,
        storage_class_name=sc_name,
        data_source=pvc_data_source)

    pvc_body = client.V1PersistentVolumeClaim(api_version="v1",
                                              kind="PersistentVolumeClaim",
                                              metadata=pvc_metadata,
                                              spec=pvc_spec)

    try:
        LOGGER.info(
            f'PVC Create from Clone : Creating pvc {pvc_name} with parameters {str(pvc_values)} and storageclass {str(sc_name)} from PVC {from_pvc_name}'
        )
        api_response = api_instance.create_namespaced_persistent_volume_claim(
            namespace=namespace_value, body=pvc_body, pretty=True)
        LOGGER.debug(str(api_response))
        created_objects["clone_pvc"].append(pvc_name)
    except ApiException as e:
        LOGGER.info(f'PVC {pvc_name} creation operation has been failed')
        LOGGER.error(
            f"Exception when calling CoreV1Api->create_namespaced_persistent_volume_claim: {e}"
        )
        cleanup.clean_with_created_objects(created_objects)
        assert False
Ejemplo n.º 2
0
def create_pvc_from_snapshot(pvc_values, sc_name, pvc_name, snap_name,
                             created_objects):
    """
    creates persistent volume claim from snapshot

    Args:
        param1: pvc_values - values required for creation of pvc
        param2: sc_name - name of storage class , pvc associated with
        param3: pvc_name - name of pvc to be created
        param4: snap_name - name of snapshot to recover data from

    Returns:
        None

    Raises:
        Raises an exception on kubernetes client api failure and asserts

    """
    api_instance = client.CoreV1Api()
    pvc_metadata = client.V1ObjectMeta(name=pvc_name)
    pvc_resources = client.V1ResourceRequirements(
        requests={"storage": pvc_values["storage"]})
    pvc_data_source = client.V1TypedLocalObjectReference(
        api_group="snapshot.storage.k8s.io",
        kind="VolumeSnapshot",
        name=snap_name)

    pvc_spec = client.V1PersistentVolumeClaimSpec(
        access_modes=[pvc_values["access_modes"]],
        resources=pvc_resources,
        storage_class_name=sc_name,
        data_source=pvc_data_source)

    pvc_body = client.V1PersistentVolumeClaim(api_version="v1",
                                              kind="PersistentVolumeClaim",
                                              metadata=pvc_metadata,
                                              spec=pvc_spec)

    try:
        LOGGER.info(
            f'PVC Create from snapshot : Creating pvc {pvc_name} with parameters {str(pvc_values)} and storageclass {str(sc_name)}'
        )
        api_response = api_instance.create_namespaced_persistent_volume_claim(
            namespace=namespace_value, body=pvc_body, pretty=True)
        LOGGER.debug(str(api_response))
        created_objects["restore_pvc"].append(pvc_name)
    except ApiException as e:
        LOGGER.info(f'PVC {pvc_name} creation operation has been failed')
        LOGGER.error(
            f"Exception when calling CoreV1Api->create_namespaced_persistent_volume_claim: {e}"
        )
        cleanup.clean_with_created_objects(created_objects)
        assert False
Ejemplo n.º 3
0
        def my_pipeline(param1, param2):
            vol = VolumeOp(
                name="myvol_creation",
                resource_name="myvol",
                size="1Gi",
            )
            snap1 = VolumeSnapshotOp(
                name="mysnap_creation",
                resource_name=param1,
                volume=vol.volume,
            )
            snap2 = VolumeSnapshotOp(name="mysnap_creation",
                                     resource_name="mysnap",
                                     pvc=param2,
                                     attribute_outputs={"size": "test"})
            snap3 = VolumeSnapshotOp(
                name="mysnap_creation2",
                resource_name="mysnap2",
                pvc=param2,
                api_version="snapshot.storage.k8s.io/v1beta1",
                attribute_outputs={"size": "test"})

            self.assertEqual(sorted([x.name for x in snap1.inputs]),
                             ["name", "param1"])
            self.assertEqual(sorted([x.name for x in snap2.inputs]),
                             ["param2"])
            expected_attribute_outputs_1 = {
                "manifest": "{}",
                "name": "{.metadata.name}",
                "size": "{.status.restoreSize}"
            }
            self.assertEqual(snap1.attribute_outputs,
                             expected_attribute_outputs_1)
            expected_attribute_outputs_2 = {
                "manifest": "{}",
                "name": "{.metadata.name}",
                "size": "test"
            }
            self.assertEqual(snap2.attribute_outputs,
                             expected_attribute_outputs_2)
            expected_outputs_1 = {
                "manifest": PipelineParam(name="manifest", op_name=snap1.name),
                "name": PipelineParam(name="name", op_name=snap1.name),
                "size": PipelineParam(name="name", op_name=snap1.name),
            }
            self.assertEqual(snap1.outputs, expected_outputs_1)
            expected_outputs_2 = {
                "manifest": PipelineParam(name="manifest", op_name=snap2.name),
                "name": PipelineParam(name="name", op_name=snap2.name),
                "size": PipelineParam(name="name", op_name=snap2.name),
            }
            self.assertEqual(snap2.outputs, expected_outputs_2)
            self.assertEqual(snap1.output,
                             PipelineParam(name="name", op_name=snap1.name))
            self.assertEqual(snap2.output,
                             PipelineParam(name="size", op_name=snap2.name))
            self.assertEqual(snap1.dependent_names, [])
            self.assertEqual(snap2.dependent_names, [])
            expected_snapshot_1 = k8s_client.V1TypedLocalObjectReference(
                api_group="snapshot.storage.k8s.io",
                kind="VolumeSnapshot",
                name=PipelineParam(name="name", op_name=vol.name))
            self.assertEqual(snap1.snapshot, expected_snapshot_1)
            expected_snapshot_2 = k8s_client.V1TypedLocalObjectReference(
                api_group="snapshot.storage.k8s.io",
                kind="VolumeSnapshot",
                name=PipelineParam(name="param1"))
            assert (snap2.k8s_resource['apiVersion'] ==
                    "snapshot.storage.k8s.io/v1alpha1")
            self.assertEqual(snap2.snapshot, expected_snapshot_2)

            expected_snapshot_3 = k8s_client.V1TypedLocalObjectReference(
                api_group="snapshot.storage.k8s.io",
                kind="VolumeSnapshot",
                name=PipelineParam(name="param1"))
            self.assertEqual(snap3.snapshot, expected_snapshot_3)
            assert (snap3.k8s_resource['apiVersion'] ==
                    "snapshot.storage.k8s.io/v1beta1")
    def test_basic(self):
        """Test basic usage."""
        with Pipeline("somename") as p:
            param1 = PipelineParam("param1")
            param2 = PipelineParam("param2")
            vol = VolumeOp(
                name="myvol_creation",
                resource_name="myvol",
                size="1Gi",
            )
            snap1 = VolumeSnapshotOp(
                name="mysnap_creation",
                resource_name=param1,
                volume=vol.volume,
            )
            snap2 = VolumeSnapshotOp(
                name="mysnap_creation",
                resource_name="mysnap",
                pvc=param2,
                attribute_outputs={"size": "test"}
            )

        self.assertEqual(
            sorted([x.name for x in snap1.inputs]), ["name", "param1"]
        )
        self.assertEqual(
            sorted([x.name for x in snap2.inputs]), ["param2"]
        )
        expected_attribute_outputs_1 = {
            "manifest": "{}",
            "name": "{.metadata.name}",
            "size": "{.status.restoreSize}"
        }
        self.assertEqual(snap1.attribute_outputs, expected_attribute_outputs_1)
        expected_attribute_outputs_2 = {
            "manifest": "{}",
            "name": "{.metadata.name}",
            "size": "test"
        }
        self.assertEqual(snap2.attribute_outputs, expected_attribute_outputs_2)
        expected_outputs_1 = {
            "manifest": PipelineParam(name="manifest", op_name=snap1.name),
            "name": PipelineParam(name="name", op_name=snap1.name),
            "size": PipelineParam(name="name", op_name=snap1.name),
        }
        self.assertEqual(snap1.outputs, expected_outputs_1)
        expected_outputs_2 = {
            "manifest": PipelineParam(name="manifest", op_name=snap2.name),
            "name": PipelineParam(name="name", op_name=snap2.name),
            "size": PipelineParam(name="name", op_name=snap2.name),
        }
        self.assertEqual(snap2.outputs, expected_outputs_2)
        self.assertEqual(
            snap1.output,
            PipelineParam(name="name", op_name=snap1.name)
        )
        self.assertEqual(
            snap2.output,
            PipelineParam(name="size", op_name=snap2.name)
        )
        self.assertEqual(snap1.dependent_names, [])
        self.assertEqual(snap2.dependent_names, [])
        expected_snapshot_1 = k8s_client.V1TypedLocalObjectReference(
            api_group="snapshot.storage.k8s.io",
            kind="VolumeSnapshot",
            name=PipelineParam(name="name", op_name=vol.name)
        )
        self.assertEqual(snap1.snapshot, expected_snapshot_1)
        expected_snapshot_2 = k8s_client.V1TypedLocalObjectReference(
            api_group="snapshot.storage.k8s.io",
            kind="VolumeSnapshot",
            name=PipelineParam(name="param1")
        )
        self.assertEqual(snap2.snapshot, expected_snapshot_2)