public async Task DeleteFileShareSecretAsync(V1Secret secret) { V1PersistentVolumeList currentPvs = await k8sClient.ListPersistentVolumeAsync(labelSelector : Constants.LabelSelector); var existingPvSet = new Set <V1PersistentVolume>(currentPvs.Items .Where(pv => pv.Spec?.AzureFile?.SecretName == secret.Metadata.Name) .ToDictionary(pv => pv.Metadata.Name)); var desiredPvSet = Set <V1PersistentVolume> .Empty; var diff = desiredPvSet.Diff(existingPvSet, PvComparer); await this.ManagePvs(diff); }
public async Task ManageFileShareSecretAsync(V1Secret secret) { byte[] accountKeyData; byte[] accountNameData; if (!secret.Data.TryGetValue(AccountKey, out accountKeyData)) { Console.WriteLine($"Secret {secret.Metadata.Name} doesn't have [{AccountKey}] Data"); return; } if (!secret.Data.TryGetValue(AccountName, out accountNameData)) { Console.WriteLine($"Secret {secret.Metadata.Name} doesn't have [{AccountName}] Data"); return; } var pvLabels = new Dictionary <string, string> { [Constants.LabelSelectorKey] = Constants.LabelSelectorValue }; var mountOptions = new List <string> { "dir_mode=0777", "file_mode=0777", "uid=1000", "gid=1000", "mfsymlinks", "nobrl" }; V1PersistentVolumeList currentPvs = await k8sClient.ListPersistentVolumeAsync(labelSelector : Constants.LabelSelector); var existingPvSet = new Set <V1PersistentVolume>(currentPvs.Items .Where(pv => pv.Spec?.AzureFile?.SecretName == secret.Metadata.Name) .ToDictionary(pv => pv.Metadata.Name)); var desiredPvs = new ConcurrentDictionary <string, V1PersistentVolume>(); string accountKey = Encoding.UTF8.GetString(accountKeyData); string accountName = Encoding.UTF8.GetString(accountNameData); string connectionString = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net"; // Open a FileShare client with secret. var serviceClient = new ShareServiceClient(connectionString); var shares = serviceClient.GetSharesAsync(ShareTraits.Metadata, ShareStates.None); await foreach (var share in shares) { // Get all file shares from client that match a trait if ((share.Properties?.Metadata != null) && (share.Properties.Metadata.TryGetValue(Constants.LabelSelectorKey, out string labelValue)) && (labelValue == Constants.LabelSelectorValue)) { // Create a PV from secret and ShareItem Console.WriteLine($"ShareItem {share.Name} found!"); string name = KubeUtils.SanitizeK8sValue($"{accountName}-{share.Name}"); var metadata = new V1ObjectMeta(name: name, labels: pvLabels); var accessModes = new List <string> { AccessMode }; var azurefile = new V1AzureFilePersistentVolumeSource(secret.Metadata.Name, share.Name, readOnlyProperty: false, secret.Metadata.NamespaceProperty); var capacity = new Dictionary <string, ResourceQuantity> { ["storage"] = new ResourceQuantity($"{share.Properties.QuotaInGB}Gi") }; var spec = new V1PersistentVolumeSpec( accessModes: accessModes, azureFile: azurefile, capacity: capacity, storageClassName: StorageClassName, mountOptions: mountOptions); var pv = new V1PersistentVolume(metadata: metadata, spec: spec); if (!desiredPvs.TryAdd(name, pv)) { Console.WriteLine($"Duplicate share name {name}"); } } } var desiredPvSet = new Set <V1PersistentVolume>(desiredPvs); var diff = desiredPvSet.Diff(existingPvSet, PvComparer); await this.ManagePvs(diff); }