Ejemplo n.º 1
0
        private async Task ScaleUp(V1Deployment item)
        {
            var patch = new JsonPatchDocument<V1Deployment>();
            patch.Replace(e => e.Spec.Replicas, item.Spec.Replicas.GetValueOrDefault() + 1);

            await State.Client.PatchNamespacedDeploymentScaleAsync(new V1Patch(patch), item.Metadata.Name, item.Metadata.NamespaceProperty);
        }
Ejemplo n.º 2
0
        public async Task <Response> InstallModule(V1Deployment moduleDeployment, LoadBalancerConfig loadBalancerConfig, string nameSpace = "default")
        {
            // First check if the moduleDeployment is already deployed in the system
            var modules = await GetDeployment(moduleDeployment.Metadata.Name);

            if (modules != null)
            {
                // There is already a module running under the specified deploymentName
                return(Response.Unsuccessful($"There is already a module running under the specified deploymentName: {moduleDeployment}"));
            }
            try
            {
                await _client.CreateNamespacedDeploymentWithHttpMessagesAsync(moduleDeployment, nameSpace);

                if (loadBalancerConfig.NeedLoadBalancer)
                {
                    var port = await FetchAvailablePort();

                    var targetPort = loadBalancerConfig.TargetPort == 0 ? 80 : loadBalancerConfig.TargetPort;

                    await _client.CreateNamespacedServiceWithHttpMessagesAsync(
                        CreateV1Service(moduleDeployment.Metadata.Name, ServiceType.LoadBalancer, targetPort : targetPort, port), nameSpace);
                }
            }
            catch (Exception e)
            {
                return(Response.Unsuccessful(e.Message));
            }

            return(Response.Success());
        }
        public static async Task ReplaceModuleImageAsync(this KubernetesClient client, string name, string image)
        {
            V1Deployment deployment = await client.Kubernetes.ReadNamespacedDeploymentAsync(name, client.DeviceNamespace);

            deployment.Spec.Template.Spec.Containers[0].Image = image;

            await client.Kubernetes.ReplaceNamespacedDeploymentAsync(deployment, name, client.DeviceNamespace);
        }
Ejemplo n.º 4
0
        public static bool ImageEquals(V1Deployment self, V1Deployment other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }

            if (ReferenceEquals(self, other))
            {
                return(true);
            }

            return(string.Equals(self.Kind, other.Kind) && self.Spec.ImageEquals(other.Spec));
        }
Ejemplo n.º 5
0
        public static async Task AddModuleDeploymentAsync(this KubernetesClient client, string name, IDictionary <string, string> labels, IDictionary <string, string> annotations)
        {
            var deployment = new V1Deployment
            {
                Metadata = new V1ObjectMeta
                {
                    Name = name,
                    NamespaceProperty = client.DeviceNamespace,
                    Labels            = labels
                },
                Spec = new V1DeploymentSpec
                {
                    Template = new V1PodTemplateSpec
                    {
                        Metadata = new V1ObjectMeta
                        {
                            Name        = name,
                            Labels      = labels,
                            Annotations = annotations
                        },
                        Spec = new V1PodSpec
                        {
                            Containers = new[]
                            {
                                new V1Container
                                {
                                    Image   = "busybox:latest",
                                    Name    = name,
                                    Command = new[] { "/bin/sh" },
                                    Args    = new[] { "-c", "while true; do echo hello; sleep 10;done" }
                                }
                            },
                            ServiceAccountName = name
                        }
                    },
                    Selector = new V1LabelSelector
                    {
                        MatchLabels = labels
                    }
                }
            };

            await client.Kubernetes.CreateNamespacedDeploymentAsync(deployment, client.DeviceNamespace);
        }
        public MTADeployModel(V1Deployment v1Deployment)
        {
            DeployName      = v1Deployment.Metadata.Name;
            DeployNamespace = v1Deployment.Metadata.NamespaceProperty;
            Replicas        = (int)(v1Deployment.Spec.Replicas);
            Annotations     = (Dictionary <string, string>)(v1Deployment.Metadata.Annotations);
            Labels          = (Dictionary <string, string>)(v1Deployment.Metadata.Labels);

            Image           = v1Deployment.Spec.Template.Spec.Containers[0].Image;
            ImagePullPolicy = v1Deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy;

            var container = v1Deployment.Spec.Template.Spec.Containers[0];

            ContainerName = container.Name;

            foreach (var containerPort in container.Ports)
            {
                Ports.Add(containerPort.ContainerPort);
            }
        }