private void ProcessData(V1ConfigMap item) { if (item is null) { Logger?.LogWarning("ConfigMap response is null, no data could be processed"); return; } var configMapContents = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); if (item?.Data != null) { foreach (var data in item?.Data) { if (IsAppsettingsKey(data.Key)) { using var stream = GenerateStreamFromString(data.Value); var jsonConfiguration = ParseConfigMapFile(stream); foreach (var jsonKey in jsonConfiguration.Keys) { configMapContents[NormalizeKey(jsonKey)] = jsonConfiguration[jsonKey]; } } else { configMapContents[NormalizeKey(data.Key)] = data.Value; } } } Data = configMapContents; }
/// <summary> /// Constructs an instance from an existing <see cref="V1ConfigMap"/>. /// </summary> /// <param name="configMap">The config map name as it will be persisted to Kubernetes.</param> public TypeSafeConfigMap(V1ConfigMap configMap) { Covenant.Requires <ArgumentNullException>(configMap != null, nameof(configMap)); if (!configMap.Data.TryGetValue(dataPropertyName, out var json)) { throw new InvalidDataException($"Expected the [{configMap}] to have a [{dataPropertyName}] property."); } this.ConfigMap = configMap; this.Config = NeonHelper.JsonDeserialize <TConfig>(json, strict: true); }
public static string SerializeToYaml(this V1ConfigMap apiConfigMap) { var customConfigMap = GetCustomConfigMap(apiConfigMap); var serializer = new SerializerBuilder() .WithNamingConvention(new CamelCaseNamingConvention()) .Build(); var yamlSerializedObject = serializer.Serialize(customConfigMap); return(yamlSerializedObject); }
public void NamespaceAndNameFromResource() { var resource = new V1ConfigMap( apiVersion: V1ConfigMap.KubeApiVersion, kind: V1ConfigMap.KubeKind, metadata: new V1ObjectMeta( name: "the-name", namespaceProperty: "the-namespace")); var nn = NamespacedName.From(resource); Assert.Equal("the-name", nn.Name); Assert.Equal("the-namespace", nn.Namespace); }
public static KubernetesConfigMap GetCustomConfigMap(this V1ConfigMap apiConfigMap) { return(new KubernetesConfigMap { ApiVersion = apiConfigMap?.ApiVersion, Data = apiConfigMap?.Data, Kind = apiConfigMap?.Kind, Metadata = new Metadata { Name = apiConfigMap?.Metadata?.Name, NamespaceProperty = apiConfigMap?.Metadata?.NamespaceProperty } }); }
public void GroupCanBeEmpty() { var resource = new V1ConfigMap( apiVersion: V1ConfigMap.KubeApiVersion, kind: V1ConfigMap.KubeKind, metadata: new V1ObjectMeta( name: "the-name", namespaceProperty: "the-namespace")); var key = GroupKindNamespacedName.From(resource); Assert.Equal("", key.Group); Assert.Equal("ConfigMap", key.Kind); Assert.Equal("the-namespace", key.NamespacedName.Namespace); Assert.Equal("the-name", key.NamespacedName.Name); }
public void Action(DeployArg arg) { var body = new V1ConfigMap { ApiVersion = V1ConfigMap.KubeApiVersion, Kind = V1ConfigMap.KubeKind, Metadata = new V1ObjectMeta { Name = GlobalSetting.KeysConfigName, NamespaceProperty = arg.SideChainId }, Data = GetAndCreateAccountKey(arg) }; K8SRequestHelper.GetClient().CreateNamespacedConfigMap(body, arg.SideChainId); }
public void NamespaceAndNameFromResource() { // arrange var resource = new V1ConfigMap( apiVersion: V1ConfigMap.KubeApiVersion, kind: V1ConfigMap.KubeKind, metadata: new V1ObjectMeta( name: "the-name", namespaceProperty: "the-namespace")); // act var nn = NamespacedName.From(resource); // assert nn.Name.ShouldBe("the-name"); nn.Namespace.ShouldBe("the-namespace"); }
public async Task Action(DeployArg arg) { var body = new V1ConfigMap { ApiVersion = V1ConfigMap.KubeApiVersion, Kind = V1ConfigMap.KubeKind, Metadata = new V1ObjectMeta { Name = GlobalSetting.ChainInfoConfigName, NamespaceProperty = arg.SideChainId }, Data = new Dictionary <string, string> { { "chain.json", "{\"id\":\"" + arg.SideChainId + "\"}" } } }; await K8SRequestHelper.GetClient().CreateNamespacedConfigMapAsync(body, arg.SideChainId); }
public void GroupCanBeEmpty() { // arrange var resource = new V1ConfigMap( apiVersion: V1ConfigMap.KubeApiVersion, kind: V1ConfigMap.KubeKind, metadata: new V1ObjectMeta( name: "the-name", namespaceProperty: "the-namespace")); // act var key = GroupKindNamespacedName.From(resource); // assert key.Group.ShouldBe(""); key.Kind.ShouldBe("ConfigMap"); key.NamespacedName.Namespace.ShouldBe("the-namespace"); key.NamespacedName.Name.ShouldBe("the-name"); }
public async Task AddLicenseAsync_UpdatesExisting_Async() { var client = new Mock <KubernetesClient>(MockBehavior.Strict); var configClient = new Mock <NamespacedKubernetesClient <V1ConfigMap> >(MockBehavior.Strict); client.Setup(c => c.GetClient <V1ConfigMap>()).Returns(configClient.Object); var configMap = new V1ConfigMap(); configClient.Setup(c => c.TryReadAsync("kaponata-license", default)).ReturnsAsync(configMap).Verifiable(); configClient.Setup(c => c.PatchAsync(configMap, It.IsAny <JsonPatchDocument <V1ConfigMap> >(), default)).ReturnsAsync(configMap).Verifiable(); var store = new LicenseStore(client.Object, NullLogger <LicenseStore> .Instance); var license = new XDocument(); await store.AddLicenseAsync(license, default).ConfigureAwait(false); configClient.Verify(); }
public void Action(DeployArg arg) { var body = new V1ConfigMap { ApiVersion = V1ConfigMap.KubeApiVersion, Kind = V1ConfigMap.KubeKind, Metadata = new V1ObjectMeta { Name = GlobalSetting.CommonConfigName, NamespaceProperty = arg.SideChainId }, Data = new Dictionary <string, string> { { "actor.json", GetActorConfigJson(arg) }, { "database.json", GetDatabaseConfigJson(arg) }, { "miners.json", GetMinersConfigJson(arg) }, { "parallel.json", GetParallelConfigJson(arg) }, { "network.json", GetNetworkConfigJson(arg) }, { "grpc-local.json", GetGrpcConfigJson(arg) }, { "grpc-remote.json", GetGrpcRemoteConfigJson(arg) }, { "api-key.json", GetApiKeyConfig(arg) } } }; K8SRequestHelper.GetClient().CreateNamespacedConfigMap(body, arg.SideChainId); if (!arg.IsDeployMainChain) { var config = K8SRequestHelper.GetClient().ReadNamespacedConfigMap(GlobalSetting.CommonConfigName, arg.MainChainId).Data; var grpcRemoteConfig = JsonSerializer.Instance.Deserialize <GrpcRemoteConfig>(config["grpc-remote.json"]); grpcRemoteConfig.ChildChains.Add(arg.SideChainId, new Uri { Port = GlobalSetting.GrpcPort, Address = arg.LauncherArg.ClusterIp }); config["grpc-remote.json"] = JsonSerializer.Instance.Serialize(grpcRemoteConfig); var patch = new JsonPatchDocument <V1ConfigMap>(); patch.Replace(e => e.Data, config); K8SRequestHelper.GetClient().PatchNamespacedConfigMap(new V1Patch(patch), GlobalSetting.CommonConfigName, arg.MainChainId); } }
private void ProcessData(V1ConfigMap item) { if (item is null) { Settings.Logger?.LogWarning("ConfigMap response is null, no data could be processed"); return; } var configMapContents = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); if (item?.Data != null) { foreach (var data in item?.Data) { configMapContents[data.Key] = data.Value; } } Data = configMapContents; }
public async Task WriteConfigMap(V1ConfigMap configMapRoleMap) { var awsAuthConfigMap = await ReadConfigMap(); if (awsAuthConfigMap == null) { await _client.CreateNamespacedConfigMapAsync( body : configMapRoleMap, namespaceParameter : ConfigMapNamespace ); return; } await _client.ReplaceNamespacedConfigMapAsync( body : configMapRoleMap, name : ConfigMapName, namespaceParameter : ConfigMapNamespace ); }
public async Task Action(DeployArg arg) { CreateGrpcKey(arg); var certFileName = arg.SideChainId + ".cert.pem"; var cert = File.ReadAllText(Path.Combine(ApplicationHelper.AppDataPath, "certs", certFileName)); var keyFileName = arg.SideChainId + ".key.pem"; var key = File.ReadAllText(Path.Combine(ApplicationHelper.AppDataPath, "certs", keyFileName)); var configMapData = new Dictionary <string, string> { { certFileName, cert }, { keyFileName, key } }; if (!arg.IsDeployMainChain) { var certMainChain = await K8SRequestHelper.GetClient().ReadNamespacedConfigMapAsync(GlobalSetting.CertsConfigName, arg.MainChainId); var certName = arg.MainChainId + ".cert.pem"; configMapData.Add(certName, certMainChain.Data[certName]); certMainChain.Data.Add(certFileName, cert); var patch = new JsonPatchDocument <V1ConfigMap>(); patch.Replace(e => e.Data, certMainChain.Data); await K8SRequestHelper.GetClient().PatchNamespacedConfigMapAsync(new V1Patch(patch), GlobalSetting.CertsConfigName, arg.MainChainId); } var body = new V1ConfigMap { ApiVersion = V1ConfigMap.KubeApiVersion, Kind = V1ConfigMap.KubeKind, Metadata = new V1ObjectMeta { Name = GlobalSetting.CertsConfigName, NamespaceProperty = arg.SideChainId }, Data = configMapData }; await K8SRequestHelper.GetClient().CreateNamespacedConfigMapAsync(body, arg.SideChainId); }
private void AddConfig(DeployArg arg) { var body = new V1ConfigMap { ApiVersion = V1ConfigMap.KubeApiVersion, Kind = V1ConfigMap.KubeKind, Metadata = new V1ObjectMeta { Name = GlobalSetting.RedisConfigName, NamespaceProperty = arg.SideChainId }, Data = new Dictionary <string, string> { { GlobalSetting.RedisConfigName, string.Concat("port ", arg.DBArg.Port, Environment.NewLine, "bind 0.0.0.0", Environment.NewLine, "appendonly no", Environment.NewLine) } } }; K8SRequestHelper.GetClient().CreateNamespacedConfigMap(body, arg.SideChainId); }
private async Task <bool> EnsureConfigMapAsync(WebPinger pinger) { var name = $"wp-{pinger.Metadata.Name}-config"; var configMaps = await _kubernetes.ListNamespacedConfigMapAsync( Program.NamespaceName, fieldSelector : $"metadata.name={name}"); if (!configMaps.Items.Any()) { var configMap = new V1ConfigMap { Metadata = new V1ObjectMeta { Name = name, Labels = new Dictionary <string, string>() { { "kiamol", "ch20" }, } }, Data = new Dictionary <string, string> { { "logConfig.js", ConfigMapData.logConfig.Replace("\r\n", "\n") } } }; await _kubernetes.CreateNamespacedConfigMapAsync(configMap, Program.NamespaceName); Console.WriteLine($"** Created ConfigMap: {name}, in namespace: {Program.NamespaceName}"); return(true); } else { Console.WriteLine($"** ConfigMap exists: {name}, in namespace: {Program.NamespaceName}"); return(false); } }
private async Task Update() { Item = await State.Client.ReadNamespacedConfigMapAsync(Name, Namespace); StateHasChanged(); }
public Task ReplaceNamespacedConfigMapAsync(V1ConfigMap body, string name, string namespaceParameter) { return(_kubernetes.ReplaceNamespacedConfigMapAsync(body, name, namespaceParameter)); }
private async Task Delete(V1ConfigMap item) { await State.Client.DeleteNamespacedConfigMapAsync(item.Metadata.Name, item.Metadata.NamespaceProperty); }
public Task WriteConfigMap(V1ConfigMap configMapRoleMap) { throw new System.NotImplementedException(); }
public Task ReplaceNamespacedConfigMapAsync(V1ConfigMap body, string name, string namespaceParameter) { return(Task.CompletedTask); }
/// <summary> /// Constructs an instance by parsing a <see cref="V1ConfigMap"/>. /// </summary> /// <param name="configMap">The source config map.</param> /// <returns>The parsed configuration</returns> public static TypeSafeConfigMap <TConfig> From(V1ConfigMap configMap) { Covenant.Requires <ArgumentNullException>(configMap != null, nameof(configMap)); return(new TypeSafeConfigMap <TConfig>(configMap)); }
private async Task CheckDatabase(HostedDatabase resource) { try { await using var host = _connectionsManager.GetHost(resource.Spec.Host); var @namespace = resource.Metadata.NamespaceProperty; var secretName = $"{resource.Metadata.Name}-auth"; var configMapName = $"{resource.Metadata.Name}-config"; var configMap = await Client.Get <V1ConfigMap>(configMapName, @namespace); if (configMap == null) { configMap = new V1ConfigMap( V1ConfigMap.KubeApiVersion, kind: V1ConfigMap.KubeKind, metadata: new V1ObjectMeta { Name = configMapName, NamespaceProperty = @namespace, OwnerReferences = new List <V1OwnerReference> { new V1OwnerReference(resource.ApiVersion, resource.Kind, resource.Metadata.Name, resource.Metadata.Uid, false, true), } }); configMap.Data = new Dictionary <string, string> { ["host"] = host.Config.Host, ["port"] = host.Config.Port.ToString(), ["database"] = host.FormatDatabaseName(resource.Metadata.Name), }; configMap.Metadata.Labels = new Dictionary <string, string> { ["managed-by"] = "hosted-database-operator", ["database-instance"] = host.FormatDatabaseName(resource.Metadata.Name), }; configMap = await Client.Create(configMap); _logger.LogDebug( @"Create config map ""{configName}"" for database ""{database}"".", configMap.Metadata.Name, resource.Metadata.Name); } var secret = await Client.Get <V1Secret>(secretName, @namespace); if (secret == null) { _logger.LogDebug( @"Secret ""{name}"" did not exist. Create Secret and User.", secretName); secret = new V1Secret( V1Secret.KubeApiVersion, kind: V1Secret.KubeKind, metadata: new V1ObjectMeta { Name = secretName, NamespaceProperty = @namespace, OwnerReferences = new List <V1OwnerReference> { new V1OwnerReference(resource.ApiVersion, resource.Kind, resource.Metadata.Name, resource.Metadata.Uid, false, true), } }); secret.Data = new Dictionary <string, byte[]> { ["username"] = Encoding.UTF8.GetBytes(host.FormatUsername(resource.Metadata.Name)), }; secret.Metadata.Labels = new Dictionary <string, string> { ["managed-by"] = "hosted-database-operator", ["database-instance"] = host.FormatDatabaseName(resource.Metadata.Name), }; secret = await Client.Create(secret); } var db = configMap.Data["database"]; resource.Status.DbHost = $"{host.Config.Host}:{host.Config.Port}"; resource.Status.DbName ??= db; resource.Status.SecretName ??= secretName; resource.Status.ConfigMapName ??= configMapName; var user = secret.ReadData("username"); var password = await host.ProcessDatabase(db, user); if (password != null) { _logger.LogInformation( @"User ""{user}"" for database ""{database}"" updated password.", user, resource.Metadata.Name); secret.WriteData("password", password); await Client.Update(secret); } resource.Status.Error = null; } catch (Exception e) { resource.Status.Error = e.Message; _logger.LogError( e, @"Could not create / update / check database ""{database}"".", resource.Metadata.Name); throw; } finally { await Client.UpdateStatus(resource); } }
private void Notification(WatchEventType eventType, V1ConfigMap configMap) { }
public async Task WatchingEmptyNamespaceGrowByTwoItemsWorks() { // make sure namespace "test" contains no configmaps var configmapsToDelete = await _k8S.ListNamespacedConfigMapAsync(TestNamespace); foreach (var configmap in configmapsToDelete.Items) { await _k8S.DeleteNamespacedConfigMapAsync(configmap.Name(), TestNamespace); } using var watch = new WatchedResource <V1ConfigMap, V1ConfigMapList>((b, limit) => _k8S.ListNamespacedConfigMapWithHttpMessagesAsync(TestNamespace, watch: b, limit: limit)); var events = new List <(WatchEventType type, V1ConfigMap entity)>(); watch.EntityChanged += (type, entity) => { if (((V1ConfigMap)entity).Metadata.Name.StartsWith("testconfigmap")) { events.Add((type, (V1ConfigMap)entity)); } }; Thread.Sleep(1000); Assert.Empty(events); var configmaps = watch.GetAll <V1ConfigMap>().ToList(); Assert.Empty(configmaps); // Now create two configmaps var configMap1 = new V1ConfigMap { Metadata = new V1ObjectMeta { Name = "testconfigmap1", NamespaceProperty = TestNamespace }, Data = new Dictionary <string, string> { { "test1", "value1" }, { "test2", "value2" } } }; var configMap2 = new V1ConfigMap { Metadata = new V1ObjectMeta { Name = "testconfigmap2", NamespaceProperty = TestNamespace }, Data = new Dictionary <string, string> { { "test3", "value3" }, { "test4", "value4" } } }; await _k8S.CreateNamespacedConfigMapAsync(configMap1, TestNamespace); await _k8S.CreateNamespacedConfigMapAsync(configMap2, TestNamespace); Thread.Sleep(11000); // we need two loops and some extra time to detect the new entities Assert.Equal(2, events.Count); Assert.Contains(events, ev => ev.entity.Metadata.Name == configMap1.Name()); Assert.Contains(events, ev => ev.entity.Metadata.Name == configMap2.Name()); }