V1PersistentVolumeClaim ExtractPvc(Mount mount, IDictionary <string, string> labels)
        {
            string name     = KubeUtils.SanitizeK8sValue(mount.Source);
            bool   readOnly = mount.ReadOnly;
            var    persistentVolumeClaimSpec = new V1PersistentVolumeClaimSpec()
            {
                // What happens if the PV access mode is not compatible with the access we're requesting?
                // Deployment will be created and will be in a failed state. The user will see this as
                // module running == false.
                AccessModes = new List <string> {
                    readOnly ? "ReadOnlyMany" : "ReadWriteMany"
                },
                Resources = new V1ResourceRequirements()
                {
                    Requests = new Dictionary <string, ResourceQuantity>()
                    {
                        { "storage", new ResourceQuantity($"{this.persistentVolumeClaimSizeMb}Mi") }
                    }
                },
            };

            // prefer persistent volume name to storage class name, if both are set.
            if (this.persistentVolumeName.HasValue)
            {
                this.persistentVolumeName.ForEach(volumeName => persistentVolumeClaimSpec.VolumeName = volumeName);
            }
            else if (this.storageClassName.HasValue)
            {
                this.storageClassName.ForEach(storageClass => persistentVolumeClaimSpec.StorageClassName = storageClass);
            }

            return(new V1PersistentVolumeClaim(metadata: new V1ObjectMeta(name: name, labels: labels), spec: persistentVolumeClaimSpec));
        }
Beispiel #2
0
        V1PersistentVolumeClaim ExtractPvc(KubernetesModule module, Mount mount, IDictionary <string, string> labels)
        {
            string volumeName = KubeUtils.SanitizeK8sValue(mount.Source);
            string pvcName    = KubernetesModule.PvcName(module, mount);
            bool   readOnly   = mount.ReadOnly;

            var persistentVolumeClaimSpec = new V1PersistentVolumeClaimSpec()
            {
                // What happens if the PV access mode is not compatible with the access we're requesting?
                // Deployment will be created and will be in a failed state. The user will see this as
                // module running == false.
                AccessModes = new List <string> {
                    readOnly ? "ReadOnlyMany" : "ReadWriteMany"
                },
                Resources = new V1ResourceRequirements()
                {
                    Requests = new Dictionary <string, ResourceQuantity>()
                    {
                        { "storage", new ResourceQuantity($"{this.persistentVolumeClaimSizeMb}Mi") }
                    }
                },
            };

            if (this.persistentVolumeName.HasValue)
            {
                string pvName = this.persistentVolumeName.OrDefault();
                if (pvName != volumeName)
                {
                    throw new InvalidModuleException(string.Format("The mount name {0} has to be the same as the PV name {1}", volumeName, pvName));
                }

                persistentVolumeClaimSpec.VolumeName = volumeName;
            }

            if (this.storageClassName.HasValue)
            {
                persistentVolumeClaimSpec.StorageClassName = this.storageClassName.OrDefault();
            }

            var pvcMeta = new V1ObjectMeta(
                name: pvcName,
                labels: labels,
                ownerReferences: module.Owner.ToOwnerReferences());

            return(new V1PersistentVolumeClaim(metadata: pvcMeta, spec: persistentVolumeClaimSpec));
        }