Esempio n. 1
0
        private async Task AddService(DeployArg arg)
        {
            var body = new V1Service
            {
                Metadata = new V1ObjectMeta
                {
                    Name   = GlobalSetting.RedisServiceName,
                    Labels = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.RedisServiceName }
                    }
                },
                Spec = new V1ServiceSpec
                {
                    Ports = new List <V1ServicePort>
                    {
                        new V1ServicePort(arg.DBArg.Port)
                    },
                    Selector = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.RedisName }
                    },
                    ClusterIP = "None"
                }
            };

            await K8SRequestHelper.GetClient().CreateNamespacedServiceAsync(body, arg.SideChainId);
        }
Esempio n. 2
0
        private async Task AddService(DeployArg arg)
        {
            var body = new V1Service
            {
                Metadata = new V1ObjectMeta
                {
                    Name   = GlobalSetting.MonitorServiceName,
                    Labels = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.MonitorServiceName }
                    }
                },
                Spec = new V1ServiceSpec
                {
                    Type  = "LoadBalancer",
                    Ports = new List <V1ServicePort>
                    {
                        new V1ServicePort(GlobalSetting.MonitorPort, "monitor-port", null, "TCP", GlobalSetting.MonitorPort)
                    },
                    Selector = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.MonitorName }
                    }
                }
            };

            await K8SRequestHelper.GetClient().CreateNamespacedServiceAsync(body, arg.SideChainId);
        }
Esempio n. 3
0
        public void ModifyWorkerCount(string chainId, int workerCount)
        {
            var patch = new JsonPatchDocument <V1Deployment>();

            patch.Replace(e => e.Spec.Replicas, workerCount);
            K8SRequestHelper.GetClient().PatchNamespacedDeployment(new V1Patch(patch), GlobalSetting.WorkerName, chainId);
        }
Esempio n. 4
0
        private void AddService(DeployArg arg)
        {
            var body = new V1Service
            {
                Metadata = new V1ObjectMeta
                {
                    Name   = GlobalSetting.LighthouseServiceName,
                    Labels = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.LighthouseServiceName }
                    }
                },
                Spec = new V1ServiceSpec
                {
                    Ports = new List <V1ServicePort>
                    {
                        new V1ServicePort(Port)
                    },
                    Selector = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.LighthouseName }
                    },
                    ClusterIP = "None"
                }
            };

            K8SRequestHelper.GetClient().CreateNamespacedService(body, arg.SideChainId);
        }
        private async Task <bool> AddService(DeployArg arg)
        {
            var body = new V1Service
            {
                Metadata = new V1ObjectMeta
                {
                    Name   = GlobalSetting.LauncherServiceName,
                    Labels = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.LauncherServiceName }
                    }
                },
                Spec = new V1ServiceSpec
                {
                    Type  = "LoadBalancer",
                    Ports = new List <V1ServicePort>
                    {
                        new V1ServicePort(GlobalSetting.NodePort, "node-port", null, "TCP", GlobalSetting.NodePort),
                        new V1ServicePort(GlobalSetting.RpcPort, "rpc-port", null, "TCP", GlobalSetting.RpcPort),
                        new V1ServicePort(GlobalSetting.GrpcPort, "grpc-port", null, "TCP", GlobalSetting.GrpcPort)
                    },
                    Selector = new Dictionary <string, string>
                    {
                        { "name", GlobalSetting.LauncherName }
                    }
                }
            };

            var result = await K8SRequestHelper.GetClient().CreateNamespacedServiceAsync(body, arg.SideChainId);

            var service = await K8SRequestHelper.GetClient().ReadNamespacedServiceAsync(result.Metadata.Name, arg.SideChainId);

            var retryGetCount = 0;

            while (true)
            {
                arg.LauncherArg.ClusterIp = service.Spec.ClusterIP;
                if (!string.IsNullOrWhiteSpace(arg.LauncherArg.ClusterIp))
                {
                    break;
                }

                if (retryGetCount > GlobalSetting.DeployRetryTime)
                {
                    return(false);
                }

                retryGetCount++;
                Thread.Sleep(3000);
                service = await K8SRequestHelper.GetClient().ReadNamespacedServiceAsync(result.Metadata.Name, arg.SideChainId);
            }

            return(true);
        }
Esempio n. 6
0
        public async Task <List <ChainResult> > GetAllChains()
        {
            var namespaces = await K8SRequestHelper.GetClient().ListNamespaceAsync();

            return((from np in namespaces.Items
                    where np.Metadata.Name != "default" && np.Metadata.Name != "kube-system" && np.Metadata.Name != "kube-public"
                    select new ChainResult
            {
                ChainId = np.Metadata.Name,
                Status = np.Status.Phase,
                CreateTime = np.Metadata.CreationTimestamp
            }).ToList());
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        public async Task <List <LauncherResult> > GetAllLaunchers(string chainId)
        {
            var pods = K8SRequestHelper.GetClient().ListNamespacedPod(chainId, labelSelector: "name=" + GlobalSetting.LauncherName);

            var result = new List <LauncherResult>();

            foreach (var pod in pods.Items)
            {
                result.Add(new LauncherResult
                {
                    NameSpace  = pod.Metadata.NamespaceProperty,
                    Name       = pod.Metadata.Name,
                    Status     = pod.Status.Phase,
                    CreateTime = pod.Metadata.CreationTimestamp
                });
            }

            return(result);
        }
        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);
        }
Esempio n. 10
0
        private async Task <bool> DeployNamespace(DeployArg arg)
        {
            var body = new V1Namespace
            {
                Metadata = new V1ObjectMeta
                {
                    Name = arg.SideChainId
                }
            };

            await K8SRequestHelper.GetClient().CreateNamespaceAsync(body);

            var ns = await K8SRequestHelper.GetClient().ReadNamespaceAsync(arg.SideChainId);

            var retryCount = 0;

            while (true)
            {
                if (ns.Status.Phase == "Active")
                {
                    break;
                }

                if (retryCount > GlobalSetting.DeployRetryTime)
                {
                    break;
                }

                retryCount++;
                Thread.Sleep(3000);
                ns = await K8SRequestHelper.GetClient().ReadNamespaceAsync(arg.SideChainId);
            }

            if (retryCount > GlobalSetting.DeployRetryTime)
            {
                await K8SRequestHelper.GetClient().DeleteNamespaceAsync(new V1DeleteOptions(), arg.SideChainId);

                return(false);
            }

            return(true);
        }
Esempio n. 11
0
        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);
            }
        }
Esempio n. 12
0
        private string GetGrpcRemoteConfigJson(DeployArg arg)
        {
            var config = new GrpcRemoteConfig()
            {
                ParentChain = new Dictionary <string, Uri>(),
                ChildChains = new Dictionary <string, Uri>()
            };

            if (!arg.IsDeployMainChain)
            {
                var service = K8SRequestHelper.GetClient().ReadNamespacedService(GlobalSetting.LauncherServiceName, arg.MainChainId);
                config.ParentChain.Add(arg.MainChainId, new Uri {
                    Port = GlobalSetting.GrpcPort, Address = service.Spec.ClusterIP
                });
            }

            var result = JsonSerializer.Instance.Serialize(config);

            return(result);
        }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        //[Fact]
        public void GetClientTest()
        {
            var name = "0x7ef9c60a1283681b895dbeb1f28c0d0d8559";
            var body = new V1Namespace
            {
                Metadata = new V1ObjectMeta
                {
                    Name = name
                }
            };

            K8SRequestHelper.GetClient().CreateNamespace(body);
            var space1 = K8SRequestHelper.GetClient().ListNamespace();

            Assert.True(space1.Items.Select(n => n.Metadata.Name == name).Any());

            K8SRequestHelper.GetClient().DeleteNamespace(new V1DeleteOptions(), name);
            var space2 = K8SRequestHelper.GetClient().ListNamespace();

            Assert.False(space2.Items.Select(n => n.Metadata.Name == name).Any());
        }
Esempio n. 15
0
        public async Task <List <WorkerResult> > GetAllWorkers(string chainId)
        {
            var configs = await K8SRequestHelper.GetClient().ReadNamespacedConfigMapAsync(GlobalSetting.CommonConfigName, chainId);

            var pods = await K8SRequestHelper.GetClient().ListNamespacedPodAsync(chainId, labelSelector: "name=" + GlobalSetting.WorkerName);

            var result = new List <WorkerResult>();

            foreach (var pod in pods.Items)
            {
                result.Add(new WorkerResult
                {
                    NameSpace  = pod.Metadata.NamespaceProperty,
                    Name       = pod.Metadata.Name,
                    Status     = pod.Status.Phase,
                    CreateTime = pod.Metadata.CreationTimestamp,
                });
            }

            return(result);
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        public List <WorkerResult> GetAllWorkers(string chainId)
        {
            var configs     = K8SRequestHelper.GetClient().ReadNamespacedConfigMap(GlobalSetting.CommonConfigName, chainId);
            var configName  = GetConfigName <ActorConfig>();
            var actorConfig = JsonSerializer.Instance.Deserialize <ActorConfig>(configs.Data[configName]);

            var pods = K8SRequestHelper.GetClient().ListNamespacedPod(chainId, labelSelector: "name=" + GlobalSetting.WorkerName);

            var result = new List <WorkerResult>();

            foreach (var pod in pods.Items)
            {
                result.Add(new WorkerResult
                {
                    NameSpace  = pod.Metadata.NamespaceProperty,
                    Name       = pod.Metadata.Name,
                    Status     = pod.Status.Phase,
                    CreateTime = pod.Metadata.CreationTimestamp,
                    ActorCount = actorConfig.ActorCount
                });
            }

            return(result);
        }
Esempio n. 18
0
 private async Task DeletePod(string chainId, DeployArg arg)
 {
     await K8SRequestHelper.GetClient().DeleteCollectionNamespacedPodAsync(chainId, labelSelector: "name=" + GlobalSetting.MonitorName);
 }
Esempio n. 19
0
 private async Task DeletePod(DeployArg arg)
 {
     await K8SRequestHelper.GetClient().DeleteCollectionNamespacedPodAsync(arg.SideChainId, labelSelector: "name=" + GlobalSetting.WorkerName);
 }
Esempio n. 20
0
 private void DeletePod(string chainId, DeployArg arg)
 {
     K8SRequestHelper.GetClient().DeleteCollectionNamespacedPod(chainId, labelSelector: "name=" + GlobalSetting.RedisName);
 }
 public async Task Action(DeployArg arg)
 {
     await K8SRequestHelper.GetClient().DeleteNamespaceAsync(new V1DeleteOptions(), arg.SideChainId);
 }
Esempio n. 22
0
        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);
        }
Esempio n. 23
0
        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);
        }
Esempio n. 24
0
 public void Action(DeployArg arg)
 {
     K8SRequestHelper.GetClient().DeleteNamespace(new V1DeleteOptions(), arg.SideChainId);
 }
Esempio n. 25
0
        private async Task <bool> AddDeployment(DeployArg arg)
        {
            var body = new V1Deployment
            {
                //ApiVersion = "extensions/v1beta1",
                Kind     = "Deployment",
                Metadata = new V1ObjectMeta
                {
                    Name   = GlobalSetting.MonitorName,
                    Labels = new Dictionary <string, string> {
                        { "name", GlobalSetting.MonitorName }
                    }
                },

                Spec = new V1DeploymentSpec
                {
                    Selector = new V1LabelSelector {
                        MatchLabels = new Dictionary <string, string> {
                            { "name", GlobalSetting.MonitorName }
                        }
                    },
                    Replicas = Replicas,
                    Template = new V1PodTemplateSpec
                    {
                        Metadata = new V1ObjectMeta {
                            Labels = new Dictionary <string, string> {
                                { "name", GlobalSetting.MonitorName }
                            }
                        },
                        Spec = new V1PodSpec
                        {
                            Containers = new List <V1Container>
                            {
                                new V1Container
                                {
                                    Name  = GlobalSetting.MonitorName,
                                    Image = "aelf/node:test",
                                    Ports = new List <V1ContainerPort>
                                    {
                                        new V1ContainerPort(GlobalSetting.MonitorPort),
                                        new V1ContainerPort(ActorPort)
                                    },
                                    ImagePullPolicy = "Always",
                                    Env             = new List <V1EnvVar>
                                    {
                                        new V1EnvVar
                                        {
                                            Name      = "POD_IP",
                                            ValueFrom = new V1EnvVarSource {
                                                FieldRef = new V1ObjectFieldSelector {
                                                    FieldPath = "status.podIP"
                                                }
                                            }
                                        }
                                    },
                                    Command = new List <string> {
                                        "dotnet", "AElf.Monitor.dll"
                                    },
                                    Args = new List <string>
                                    {
                                        "--actor.host",
                                        "$(POD_IP)",
                                        "--actor.port",
                                        ActorPort.ToString()
                                    },
                                    VolumeMounts = new List <V1VolumeMount>
                                    {
                                        new V1VolumeMount
                                        {
                                            MountPath = "/app/aelf/config",
                                            Name      = "config"
                                        }
                                    }
                                }
                            },
                            Volumes = new List <V1Volume>
                            {
                                new V1Volume
                                {
                                    Name      = "config",
                                    ConfigMap = new V1ConfigMapVolumeSource {
                                        Name = "config-common"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var result = await K8SRequestHelper.GetClient().CreateNamespacedDeploymentAsync(body, arg.SideChainId);

            var deploy = await K8SRequestHelper.GetClient().ReadNamespacedDeploymentAsync(result.Metadata.Name, arg.SideChainId);

            var retryGetCount    = 0;
            var retryDeleteCount = 0;

            while (true)
            {
                if (deploy.Status.ReadyReplicas.HasValue && deploy.Status.ReadyReplicas.Value == Replicas)
                {
                    break;
                }

                if (retryGetCount > GlobalSetting.DeployRetryTime)
                {
                    await DeletePod(arg.SideChainId, arg);

                    retryDeleteCount++;
                    retryGetCount = 0;
                }

                if (retryDeleteCount > GlobalSetting.DeployRetryTime)
                {
                    return(false);
                }

                retryGetCount++;
                Thread.Sleep(3000);
                deploy = await K8SRequestHelper.GetClient().ReadNamespacedDeploymentAsync(result.Metadata.Name, arg.SideChainId);
            }

            return(true);
        }
Esempio n. 26
0
        private bool AddDeployment(DeployArg arg)
        {
            var body = new V1Deployment
            {
                //ApiVersion = "extensions/v1beta1",
                Kind     = "Deployment",
                Metadata = new V1ObjectMeta
                {
                    Name   = GlobalSetting.LauncherName,
                    Labels = new Dictionary <string, string> {
                        { "name", GlobalSetting.LauncherName }
                    }
                },

                Spec = new V1DeploymentSpec
                {
                    Selector = new V1LabelSelector {
                        MatchLabels = new Dictionary <string, string> {
                            { "name", GlobalSetting.LauncherName }
                        }
                    },
                    Replicas = Replicas,
                    Template = new V1PodTemplateSpec
                    {
                        Metadata = new V1ObjectMeta {
                            Labels = new Dictionary <string, string> {
                                { "name", GlobalSetting.LauncherName }
                            }
                        },
                        Spec = new V1PodSpec
                        {
                            Containers = new List <V1Container>
                            {
                                new V1Container
                                {
                                    Name  = GlobalSetting.LauncherName,
                                    Image = "aelf/node:test",
                                    Ports = new List <V1ContainerPort>
                                    {
                                        new V1ContainerPort(GlobalSetting.NodePort),
                                        new V1ContainerPort(GlobalSetting.RpcPort),
                                        new V1ContainerPort(ActorPort),
                                        new V1ContainerPort(GlobalSetting.GrpcPort)
                                    },
                                    ImagePullPolicy = "Always",
                                    Env             = new List <V1EnvVar>
                                    {
                                        new V1EnvVar
                                        {
                                            Name      = "POD_IP",
                                            ValueFrom = new V1EnvVarSource {
                                                FieldRef = new V1ObjectFieldSelector {
                                                    FieldPath = "status.podIP"
                                                }
                                            }
                                        }
                                    },
                                    Command = new List <string> {
                                        "dotnet", "AElf.Launcher.dll"
                                    },
                                    Args = new List <string>
                                    {
                                        "--mine.enable",
                                        "true",
                                        "--rpc.host",
                                        "0.0.0.0",
                                        "--rpc.port",
                                        GlobalSetting.RpcPort.ToString(),
                                        "--node.account",
                                        arg.ChainAccount,
                                        "--node.port",
                                        GlobalSetting.NodePort.ToString(),
                                        "--actor.host",
                                        "$(POD_IP)",
                                        "--actor.port",
                                        ActorPort.ToString(),
                                        "--node.accountpassword",
                                        arg.AccountPassword,
                                        "--dpos.generator",
                                        arg.LauncherArg.IsConsensusInfoGenerator.ToString(),
                                        "--chain.id",
                                        arg.SideChainId,
                                        "--node.executor",
                                        arg.LighthouseArg.IsCluster?"akka":"simple"
                                    },
                                    VolumeMounts = new List <V1VolumeMount>
                                    {
                                        new V1VolumeMount
                                        {
                                            MountPath = "/app/aelf/config",
                                            Name      = "config"
                                        },
                                        new V1VolumeMount
                                        {
                                            MountPath = "/app/aelf/keys",
                                            Name      = "key"
                                        },
                                        new V1VolumeMount
                                        {
                                            MountPath = "/app/aelf/certs",
                                            Name      = "cert"
                                        }
                                    }
                                }
                            },
                            Volumes = new List <V1Volume>
                            {
                                new V1Volume
                                {
                                    Name      = "config",
                                    ConfigMap = new V1ConfigMapVolumeSource {
                                        Name = GlobalSetting.CommonConfigName
                                    }
                                },
                                new V1Volume
                                {
                                    Name      = "key",
                                    ConfigMap = new V1ConfigMapVolumeSource
                                    {
                                        Name  = GlobalSetting.KeysConfigName,
                                        Items = new List <V1KeyToPath>
                                        {
                                            new V1KeyToPath {
                                                Key = arg.ChainAccount + ".ak", Path = arg.ChainAccount + ".ak"
                                            }
                                        }
                                    }
                                },
                                new V1Volume
                                {
                                    Name      = "cert",
                                    ConfigMap = new V1ConfigMapVolumeSource {
                                        Name = GlobalSetting.CertsConfigName
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var result = K8SRequestHelper.GetClient().CreateNamespacedDeployment(body, arg.SideChainId);

            var deploy           = K8SRequestHelper.GetClient().ReadNamespacedDeployment(result.Metadata.Name, arg.SideChainId);
            var retryGetCount    = 0;
            var retryDeleteCount = 0;

            while (true)
            {
                if (deploy.Status.ReadyReplicas.HasValue && deploy.Status.ReadyReplicas.Value == Replicas)
                {
                    break;
                }

                if (retryGetCount > GlobalSetting.DeployRetryTime)
                {
                    DeletePod(arg);
                    retryDeleteCount++;
                    retryGetCount = 0;
                }

                if (retryDeleteCount > GlobalSetting.DeployRetryTime)
                {
                    return(false);
                }

                retryGetCount++;
                Thread.Sleep(3000);
                deploy = K8SRequestHelper.GetClient().ReadNamespacedDeployment(result.Metadata.Name, arg.SideChainId);
            }

            return(true);
        }
Esempio n. 27
0
 //[Fact]
 public void Test()
 {
     K8SRequestHelper.GetClient().DeleteCollectionNamespacedPod("0xef23b649fcd3fd35b8389b2aec3da6fa51e9-1", labelSelector: "name=deploy-launcher");
 }
Esempio n. 28
0
 private void DeletePod(DeployArg arg)
 {
     K8SRequestHelper.GetClient().DeleteCollectionNamespacedPod(arg.SideChainId, labelSelector: "name=" + GlobalSetting.LighthouseName);
 }