コード例 #1
0
        public async Task CollectGarbage_Explicit()
        {
            // Get all of our configs
            var configs = new List <TenantConfiguration>();

            try { configs.Add(TestConfigurations.DefaultTargetTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultSecondaryTargetTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultTargetPremiumBlobTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultTargetPreviewBlobTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultTargetOAuthTenant); } catch (InconclusiveException) { }
            foreach (TenantConfiguration config in configs)
            {
                // Blobs
                var blobs = new BlobServiceClient(config.ConnectionString);
                await foreach (BlobContainerItem container in blobs.GetBlobContainersAsync())
                {
                    try
                    {
                        await blobs.DeleteBlobContainerAsync(container.Name);
                    }
                    catch (RequestFailedException ex) when(ex.ErrorCode == BlobErrorCode.LeaseIdMissing)
                    {
                        // Break any lingering leases
                        await blobs.GetBlobContainerClient(container.Name).GetBlobLeaseClient().BreakAsync();
                    }
                    catch (RequestFailedException ex) when(ex.ErrorCode == BlobErrorCode.ContainerBeingDeleted)
                    {
                        // Ignore anything already being deleted
                    }
                }

                // Queues
                var queues = new QueueServiceClient(config.ConnectionString);
                await foreach (QueueItem queue in queues.GetQueuesAsync())
                {
                    try
                    {
                        await queues.DeleteQueueAsync(queue.Name);
                    }
                    catch (RequestFailedException ex) when(ex.ErrorCode == QueueErrorCode.QueueBeingDeleted)
                    {
                        // Ignore anything already being deleted
                    }
                }

                // Files
                var files = new ShareServiceClient(config.ConnectionString);
                await foreach (ShareItem share in files.GetSharesAsync())
                {
                    try
                    {
                        await files.DeleteShareAsync(share.Name);
                    }
                    catch (RequestFailedException ex) when(ex.ErrorCode == ShareErrorCode.ShareBeingDeleted)
                    {
                        // Ignore anything already being deleted
                    }
                }
            }
        }
コード例 #2
0
        public static async Task <List <FileShareWrapper> > ListFileSharesAsync(string account, string key)
        {
            System.Threading.CancellationToken cancellationToken = new System.Threading.CancellationToken();
            ShareServiceClient client = new ShareServiceClient(Client.GetConnectionString(account, key));

            List <FileShareWrapper> items = new List <FileShareWrapper>();

            var shares = client.GetSharesAsync(ShareTraits.None, ShareStates.None, null, cancellationToken);

            await foreach (var share in shares)
            {
                items.Add(new FileShareWrapper {
                    Name = share.Name
                });
            }

            return(items);
        }
コード例 #3
0
        private static async Task ListSharesSample(ShareServiceClient shareServiceClient)
        {
            Console.WriteLine();

            // Keep a list of the file shares so you can compare this list
            //   against the list of shares that we retrieve .
            List <string> fileShareNames = new List <string>();

            try
            {
                // Create 3 file shares.

                // Create the share name -- use a guid in the name so it's unique.
                // This will also be used as the container name for blob storage when copying the file to blob storage.
                string baseShareName = "demotest-" + System.Guid.NewGuid().ToString();

                for (int i = 0; i < 3; i++)
                {
                    // Set the name of the share, then add it to the generic list.
                    string shareName = baseShareName + "-0" + i;
                    fileShareNames.Add(shareName);

                    // Create the share with this name.
                    Console.WriteLine("Creating share with name {0}", shareName);
                    ShareClient shareClient = shareServiceClient.GetShareClient(shareName);
                    try
                    {
                        await shareClient.CreateIfNotExistsAsync();

                        Console.WriteLine("    Share created successfully.");
                    }
                    catch (RequestFailedException exRequest)
                    {
                        Common.WriteException(exRequest);
                        Console.WriteLine(
                            "Please make sure your storage account has storage file endpoint enabled and specified correctly in the app.config - then restart the sample.");
                        Console.WriteLine("Press any key to exit");
                        Console.ReadLine();
                        throw;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("    Exception thrown creating share.");
                        Common.WriteException(ex);
                        throw;
                    }
                }

                Console.WriteLine(string.Empty);
                Console.WriteLine("List of shares in the storage account:");

                // List the file shares for this storage account
                var shareList = shareServiceClient.GetSharesAsync();
                try
                {
                    await foreach (ShareItem share in shareList)
                    {
                        Console.WriteLine("Cloud Share name = {0}", share.Name);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("    Exception thrown listing shares.");
                    Common.WriteException(ex);
                    throw;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("    Exception thrown. Message = {0}{1}    Strack Trace = {2}", ex.Message,
                                  Environment.NewLine, ex.StackTrace);
            }
            finally
            {
                // If it created the file shares, remove them (cleanup).
                if (fileShareNames != null && shareServiceClient != null)
                {
                    // Now clean up after yourself, using the list of shares that you created in case there were other shares in the account.
                    foreach (string fileShareName in fileShareNames)
                    {
                        ShareClient share = shareServiceClient.GetShareClient(fileShareName);
                        share.DeleteIfExists();
                    }
                }
            }

            Console.WriteLine();
        }
コード例 #4
0
        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);
        }