static void AssertValuesEqual(ContainerV1 clone, ContainerV2 obj)
 {
     Assert.That(clone.Value1, Is.EqualTo(obj.Value1));
     Assert.That(clone.Value2, Is.EqualTo(obj.Value2));
     Assert.That(clone.Value3, Is.EqualTo(obj.Value3));
     Assert.That(clone.Value4, Is.EqualTo(obj.Value4));
     Assert.That(clone.Value5, Is.EqualTo(obj.Value5));
     Assert.That(clone.Value6, Is.EqualTo(obj.Value6));
 }
        public void ExecuteValuesReversed()
        {
            var         tm  = TypeModel.Create();
            ContainerV2 obj = MakeWithRefs2();

            ContainerV1 clone = tm.ChangeType <ContainerV2, ContainerV1>(obj);

            AssertValuesEqual(clone, obj);

            // was serialized as ref
            Assert.That(clone.Value6, Is.SameAs(clone.Value4));
        }
        public void ExecuteValues()
        {
            var         tm  = TypeModel.Create();
            ContainerV1 obj = MakeWithRefs();

            ContainerV2 clone = tm.ChangeType <ContainerV1, ContainerV2>(obj);

            AssertValuesEqual(clone, obj);

            // was serialized not as ref
            Assert.That(clone.Value6, Is.Not.SameAs(clone.Value4));
        }
        static ContainerV1 MakeWithRefs()
        {
            var obj = new ContainerV1()
            {
                Value1 = "abc",
                Value2 = "def",
                Value4 = new Custom(1),
                Value5 = new Custom(2),
            };

            obj.Value3 = obj.Value1;
            obj.Value6 = obj.Value4;
            return(obj);
        }
        public void ExecuteNullWireTypeCanBeReadEvenWhenSuppressTogglingOff()
        {
            var tm    = TypeModel.Create();
            var tmOld = TypeModel.Create(false, new ProtoCompatibilitySettingsValue()
            {
                SuppressNullWireType = true
            });
            ContainerV1 obj = new ContainerV1()
            {
                Value6 = new Custom(1)
            };

            ContainerV1 clone;

            using (var ms = new MemoryStream())
            {
                tmOld.Serialize(ms, obj);
                ms.Position = 0;
                clone       = tm.Deserialize <ContainerV1>(ms);
            }
            AssertValuesEqual(clone, obj);
        }
Beispiel #6
0
        public void UpdateDeployment(string ns, string name, string env, string image, string cpu, string mem,
                                     bool rsvp, int port, string registrySecret, Plugin config)
        {
            var deploy = _kubeApiClient.DeploymentsV1().Get($"{name}-{env}", ns).Result;

            if (deploy == null)
            {
                Log($"Deployment: {name}-{env} not found, created");


                var templateContainer = new ContainerV1
                {
                    Name  = $"{name}-{env}",
                    Image = image,
                };

                if (!string.IsNullOrEmpty(cpu))
                {
                    templateContainer.Resources = new ResourceRequirementsV1
                    {
                        Limits =
                        {
                            ["cpu"] = cpu
                        }
                    };
                }

                if (!string.IsNullOrEmpty(mem))
                {
                    templateContainer.Resources = new ResourceRequirementsV1
                    {
                        Limits =
                        {
                            ["memory"] = mem
                        }
                    };
                }

                if (!string.IsNullOrEmpty(config.EntryPoint))
                {
                    templateContainer.Command.Add(config.EntryPoint);
                }

                if (!string.IsNullOrEmpty(config.Command))
                {
                    templateContainer.Args.Add(config.Command);
                }

                if (rsvp)
                {
                    if (!string.IsNullOrEmpty(cpu))
                    {
                        templateContainer.Resources.Requests.Add("cpu", cpu);
                    }

                    if (!string.IsNullOrEmpty(mem))
                    {
                        templateContainer.Resources.Requests.Add("memory", mem);
                    }
                }

                if (port > 0)
                {
                    templateContainer.Ports.Add(new ContainerPortV1
                    {
                        Name          = "tcp",
                        ContainerPort = port
                    });
                }

                var res = _kubeApiClient.DeploymentsV1().Create(new DeploymentV1()
                {
                    ApiVersion = "apps/v1",
                    Metadata   = new ObjectMetaV1
                    {
                        Name      = $"{name}-{env}",
                        Namespace = ns,
                        Labels    =
                        {
                            ["simcu-deploy-app"] = $"{ns}-{name}-{env}"
                        }
                    },
                    Spec = new DeploymentSpecV1()
                    {
                        Replicas = 1,
                        Selector = new LabelSelectorV1
                        {
                            MatchLabels =
                            {
                                ["simcu-deploy-app"] = $"{ns}-{name}-{env}"
                            }
                        },

                        Template = new PodTemplateSpecV1
                        {
                            Metadata = new ObjectMetaV1
                            {
                                Labels =
                                {
                                    ["simcu-deploy-app"] = $"{ns}-{name}-{env}"
                                }
                            },
                            Spec = new PodSpecV1
                            {
                                Containers       = { templateContainer },
                                ImagePullSecrets = { new LocalObjectReferenceV1 {
                                                         Name = registrySecret
                                                     } }
                            }
                        }
                    }
                }).Result;
            }
            else
            {
                Log($"Deployment: {name}-{env} already exists, updated");
                var res = _kubeApiClient.DeploymentsV1().Update($"{name}-{env}", kubeNamespace: ns,
                                                                patchAction: patch =>
                {
                    patch.Replace(x => x.Spec.Template.Spec.Containers[0].Image, image);
                    patch.Replace(x => x.Spec.Template.Spec.ImagePullSecrets,
                                  new List <LocalObjectReferenceV1> {
                        new LocalObjectReferenceV1 {
                            Name = registrySecret
                        }
                    });
                    patch.Replace(x => x.Spec.Template.Spec.Containers[0].Resources.Limits,
                                  new Dictionary <string, string> {
                        { "cpu", cpu }, { "memory", mem }
                    });

                    patch.Replace(x => x.Metadata.Annotations, config.Annotations);
                    config.Labels.Add("simcu-deploy-app", $"{ns}-{name}-{env}");
                    patch.Replace(x => x.Metadata.Labels, config.Labels);

                    if (!string.IsNullOrEmpty(config.EntryPoint))
                    {
                        if (deploy.Spec.Template.Spec.Containers[0].Command.Count > 0)
                        {
                            patch.Replace(x => x.Spec.Template.Spec.Containers[0].Command,
                                          new List <string> {
                                config.EntryPoint
                            });
                        }
                        else
                        {
                            patch.Add(x => x.Spec.Template.Spec.Containers[0].Command,
                                      new List <string> {
                                config.EntryPoint
                            });
                        }
                    }
                    else
                    {
                        if (deploy.Spec.Template.Spec.Containers[0].Command.Count > 0)
                        {
                            patch.Remove(x => x.Spec.Template.Spec.Containers[0].Command);
                        }
                    }

                    if (!string.IsNullOrEmpty(config.Command))
                    {
                        if (deploy.Spec.Template.Spec.Containers[0].Args.Count > 0)
                        {
                            patch.Replace(x => x.Spec.Template.Spec.Containers[0].Args,
                                          new List <string> {
                                config.Command
                            });
                        }
                        else
                        {
                            patch.Add(x => x.Spec.Template.Spec.Containers[0].Args,
                                      new List <string> {
                                config.Command
                            });
                        }
                    }
                    else
                    {
                        if (deploy.Spec.Template.Spec.Containers[0].Args.Count > 0)
                        {
                            patch.Remove(x => x.Spec.Template.Spec.Containers[0].Args);
                        }
                    }

                    if (rsvp)
                    {
                        patch.Replace(x => x.Spec.Template.Spec.Containers[0].Resources.Requests,
                                      new Dictionary <string, string> {
                            { "cpu", cpu }, { "memory", mem }
                        });
                    }
                    else
                    {
                        if (deploy.Spec.Template.Spec.Containers[0].Resources.Requests.Count > 0)
                        {
                            patch.Remove(x => x.Spec.Template.Spec.Containers[0].Resources.Requests);
                        }
                    }

                    if (port != 0)
                    {
                        patch.Add(x => x.Spec.Template.Spec.Containers[0].Ports, new List <ContainerPortV1>
                        {
                            new ContainerPortV1
                            {
                                Name          = $"tcp-{port}",
                                ContainerPort = port
                            }
                        });
                    }
                    else
                    {
                        if (deploy.Spec.Template.Spec.Containers[0].Ports.Count > 0)
                        {
                            patch.Remove(x => x.Spec.Template.Spec.Containers[0].Ports, 0);
                        }
                    }
                }).Result;
            }
        }