public SampleAppsFixture() { BuildNumber = Environment.GetEnvironmentVariable(BUILD_NUMBER_VAR) ?? Guid.NewGuid().ToString(); BuildNumber = BuildNumber.Replace('.', '_'); // Dots are invalid in Kubernetes service names var storageKey = Environment.GetEnvironmentVariable(STORAGE_KEY_VAR); Console.WriteLine("Using storage key \"{0}...\" from environment variable \"{1}\"", storageKey.Substring(0, 4), STORAGE_KEY_VAR); FolderName = "SampleApps-" + BuildNumber; fileShare = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={STORAGE_ACCOUNT_NAME};AccountKey={storageKey};EndpointSuffix=core.windows.net") .CreateCloudFileClient() .GetShareReference(STORAGE_SHARE_NAME); UploadToFileShare(fileShare, FolderName); KubernetesClientConfiguration config; string kubeConfig = Environment.GetEnvironmentVariable(KUBECONFIG_VAR); if (string.IsNullOrEmpty(kubeConfig)) { config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); } else { byte[] configByteArray = System.Text.Encoding.UTF8.GetBytes(kubeConfig); using (var stream = new MemoryStream(configByteArray)) { config = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream); } } Client = new Kubernetes(config); // Create a PV for our Azure File share and a corresponding claim if they don't already exist // If these fail, make sure that they don't already exist in the cluster: `kubectl delete -n default pvc,pv --all` storage = Client.CreatePersistentVolume(Specs.BuildVolume.GetSpec(VOLUME_NAME, STORAGE_REQUESTED_CAPACITY, STORAGE_SHARE_NAME)); storageClaim = Client.CreateNamespacedPersistentVolumeClaim(Specs.BuildVolumeClaim.GetSpec(STORAGE_REQUESTED_CAPACITY), NAMESPACE); Console.WriteLine("Created PersistentVolume and corresponding PersistentVolumeClaim"); // Create the build pod var podSpec = Specs.BuildPod.GetSpec(BUILD_POD_NAME, VOLUME_NAME, storageClaim.Metadata.Name); BuildPod = k8sHelpers.CreatePodAndWait(Client, podSpec, NAMESPACE, k8sHelpers.IsPodRunning).Result; Console.WriteLine("Build pod is up & running"); }
private async Task Delete(V1PersistentVolume item) { await State.Client.DeletePersistentVolumeAsync(item.Metadata.Name); }
private async Task Update() { Item = await State.Client.ReadPersistentVolumeAsync(Name); StateHasChanged(); }
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); }