private async Task ScaleUp(V1StatefulSet item) { var patch = new JsonPatchDocument <V1Deployment>(); patch.Replace(e => e.Spec.Replicas, item.Spec.Replicas.GetValueOrDefault() + 1); await State.Client.PatchNamespacedStatefulSetScaleAsync(new V1Patch(patch), item.Metadata.Name, item.Metadata.NamespaceProperty); }
private async Task ScaleDown(V1StatefulSet item) { if (!item.Spec.Replicas.HasValue || item.Spec.Replicas.Value == 0) { return; } var patch = new JsonPatchDocument <V1StatefulSet>(); patch.Replace(e => e.Spec.Replicas, item.Spec.Replicas.Value - 1); await State.Client.PatchNamespacedStatefulSetScaleAsync(new V1Patch(patch), item.Metadata.Name, item.Metadata.NamespaceProperty); }
private async Task Update() { Item = await State.Client.ReadNamespacedStatefulSetAsync(Name, Namespace); StateHasChanged(); }
private bool AddStatefulSet(DeployArg arg) { var body = new V1StatefulSet { Metadata = new V1ObjectMeta { Name = GlobalSetting.LighthouseName, Labels = new Dictionary <string, string> { { "name", GlobalSetting.LighthouseName } } }, Spec = new V1StatefulSetSpec { Selector = new V1LabelSelector { MatchExpressions = new List <V1LabelSelectorRequirement> { new V1LabelSelectorRequirement("name", "In", new List <string> { GlobalSetting.LighthouseName }) } }, ServiceName = GlobalSetting.LighthouseServiceName, Replicas = Replicas, Template = new V1PodTemplateSpec { Metadata = new V1ObjectMeta { Labels = new Dictionary <string, string> { { "name", GlobalSetting.LighthouseName } } }, Spec = new V1PodSpec { Containers = new List <V1Container> { new V1Container { Name = GlobalSetting.LighthouseName, Image = "aelf/node:test", ImagePullPolicy = "Always", Ports = new List <V1ContainerPort> { new V1ContainerPort(Port) }, Env = new List <V1EnvVar> { new V1EnvVar { Name = "POD_NAME", ValueFrom = new V1EnvVarSource { FieldRef = new V1ObjectFieldSelector("metadata.name") } } }, Command = new List <string> { "dotnet", "AElf.Concurrency.Lighthouse.dll" }, Args = new List <string> { "--actor.host", "$(POD_NAME)." + GlobalSetting.LighthouseServiceName, "--actor.port", Port.ToString() }, VolumeMounts = new List <V1VolumeMount> { new V1VolumeMount("/app/aelf/config", "config") } } }, Volumes = new List <V1Volume> { new V1Volume { Name = "config", ConfigMap = new V1ConfigMapVolumeSource { Name = "config-common" } } } } } } }; var result = K8SRequestHelper.GetClient().CreateNamespacedStatefulSet(body, arg.SideChainId); var set = K8SRequestHelper.GetClient().ReadNamespacedStatefulSet(result.Metadata.Name, arg.SideChainId); var retryGetCount = 0; var retryDeleteCount = 0; while (true) { if (set.Status.ReadyReplicas.HasValue && set.Status.ReadyReplicas.Value == Replicas) { break; } if (retryGetCount > GlobalSetting.DeployRetryTime) { DeletePod(arg); retryDeleteCount++; retryGetCount = 0; } if (retryDeleteCount > GlobalSetting.DeployRetryTime) { return(false); } retryGetCount++; Thread.Sleep(3000); set = K8SRequestHelper.GetClient().ReadNamespacedStatefulSet(result.Metadata.Name, arg.SideChainId); } return(true); }
private async Task <string> DescribeObject(Kubernetes client, V1Namespace ns, V1StatefulSet o, StringBuilder buffer) { var fetched = await client.ReadNamespacedStatefulSetAsync(o.Metadata.Name, ns.Metadata.Name).ConfigureAwait(false); buffer.AppendLine($"API Veresion: {fetched.ApiVersion}"); buffer.AppendLine($"Kind: {fetched.Kind}"); buffer.AppendLine(DescribeMetadata(fetched.Metadata)); return($"Stateful Set - {fetched.Metadata.Name}"); }
private bool AddStatefulSet(DeployArg arg) { var body = new V1StatefulSet { Metadata = new V1ObjectMeta { Name = GlobalSetting.RedisName, Labels = new Dictionary <string, string> { { "name", GlobalSetting.RedisName } } }, Spec = new V1StatefulSetSpec { Selector = new V1LabelSelector { MatchExpressions = new List <V1LabelSelectorRequirement> { new V1LabelSelectorRequirement("name", "In", new List <string> { GlobalSetting.RedisName }) } }, ServiceName = GlobalSetting.RedisServiceName, Replicas = Replicas, Template = new V1PodTemplateSpec { Metadata = new V1ObjectMeta { Labels = new Dictionary <string, string> { { "name", GlobalSetting.RedisName } } }, Spec = new V1PodSpec { Containers = new List <V1Container> { new V1Container { Name = GlobalSetting.RedisName, Image = "redis", Ports = new List <V1ContainerPort> { new V1ContainerPort(arg.DBArg.Port) }, Command = new List <string> { "redis-server" }, Args = new List <string> { "/redis/redis.conf" }, VolumeMounts = new List <V1VolumeMount> { new V1VolumeMount("/redisdata", "data"), new V1VolumeMount("/redis", "config") } } }, Volumes = new List <V1Volume> { new V1Volume { Name = "data", EmptyDir = new V1EmptyDirVolumeSource() }, new V1Volume { Name = "config", ConfigMap = new V1ConfigMapVolumeSource { Name = GlobalSetting.RedisConfigName, Items = new List <V1KeyToPath> { new V1KeyToPath { Key = "config-redis", Path = "redis.conf" } } } } } } } } }; var result = K8SRequestHelper.GetClient().CreateNamespacedStatefulSet(body, arg.SideChainId); var set = K8SRequestHelper.GetClient().ReadNamespacedStatefulSet(result.Metadata.Name, arg.SideChainId); var retryGetCount = 0; var retryDeleteCount = 0; while (true) { if (set.Status.ReadyReplicas.HasValue && set.Status.ReadyReplicas.Value == Replicas) { break; } if (retryGetCount > GlobalSetting.DeployRetryTime) { DeletePod(arg.SideChainId, arg); retryDeleteCount++; retryGetCount = 0; } if (retryDeleteCount > GlobalSetting.DeployRetryTime) { return(false); } retryGetCount++; Thread.Sleep(3000); set = K8SRequestHelper.GetClient().ReadNamespacedStatefulSet(result.Metadata.Name, arg.SideChainId); } return(true); }
private async Task Delete(V1StatefulSet item) { await State.Client.DeleteNamespacedDeploymentAsync(item.Metadata.Name, item.Metadata.NamespaceProperty); }
/// <summary> /// Restarts a <see cref="V1StatefulSet"/>. /// </summary> /// <param name="statefulset">The deployment being restarted.</param> /// <param name="k8s">The <see cref="IKubernetes"/> client to be used for the operation.</param> /// <returns>The tracking <see cref="Task"/>.</returns> public static async Task RestartAsync(this V1StatefulSet statefulset, IKubernetes k8s) { await SyncContext.Clear; Covenant.Requires <ArgumentNullException>(k8s != null, nameof(k8s)); // $todo(jefflill): // // Fish out the k8s client from the statefulset so we don't have to pass it in as a parameter. var generation = statefulset.Status.ObservedGeneration; var patchStr = $@" {{ ""spec"": {{ ""template"": {{ ""metadata"": {{ ""annotations"": {{ ""kubectl.kubernetes.io/restartedAt"": ""{DateTime.UtcNow.ToString("s")}"" }} }} }} }} }}"; await k8s.PatchNamespacedStatefulSetAsync(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), statefulset.Name(), statefulset.Namespace()); await NeonHelper.WaitForAsync( async() => { try { var newDeployment = await k8s.ReadNamespacedStatefulSetAsync(statefulset.Name(), statefulset.Namespace()); return(newDeployment.Status.ObservedGeneration > generation); } catch { return(false); } }, timeout : TimeSpan.FromSeconds(300), pollInterval : TimeSpan.FromMilliseconds(500)); await NeonHelper.WaitForAsync( async() => { try { statefulset = await k8s.ReadNamespacedStatefulSetAsync(statefulset.Name(), statefulset.Namespace()); return((statefulset.Status.Replicas == statefulset.Status.ReadyReplicas) && statefulset.Status.UpdatedReplicas == null); } catch { return(false); } }, timeout : TimeSpan.FromSeconds(300), pollInterval : TimeSpan.FromMilliseconds(500)); }