Пример #1
0
        public void Create(PipelineRun pipelineRun)
        {
            var pvc = CreatePVC(pipelineRun.Metadata.Name, pipelineRun.Metadata.Namespace);

            _client.CreateNamespacedPersistentVolumeClaim(pvc, pipelineRun.Metadata.Namespace);

            JsonSerializerSettings jss = new JsonSerializerSettings();

            jss.NullValueHandling = NullValueHandling.Ignore;
            jss.Formatting        = Formatting.Indented;
            jss.ContractResolver  = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

            var pr = JObject.Parse(JsonConvert.SerializeObject(pipelineRun, Formatting.None, jss));

            _client.CreateNamespacedCustomObject(pr, "tekton.dev", "v1beta1", pipelineRun.Metadata.Namespace, "pipelineruns");
        }
Пример #2
0
            public SampleAppsFixture()
            {
                BuildNumber = Environment.GetEnvironmentVariable(BUILD_NUMBER_VAR) ?? Guid.NewGuid().ToString();
                BuildNumber = BuildNumber.Replace('.', '_'); // Dots are invalid in Kubernetes service names

                var storageKey = Environment.GetEnvironmentVariable(STORAGE_KEY_VAR);

                Console.WriteLine("Using storage key \"{0}...\" from environment variable \"{1}\"", storageKey.Substring(0, 4), STORAGE_KEY_VAR);

                FolderName = "SampleApps-" + BuildNumber;

                fileShare = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={STORAGE_ACCOUNT_NAME};AccountKey={storageKey};EndpointSuffix=core.windows.net")
                            .CreateCloudFileClient()
                            .GetShareReference(STORAGE_SHARE_NAME);

                UploadToFileShare(fileShare, FolderName);

                KubernetesClientConfiguration config;
                string kubeConfig = Environment.GetEnvironmentVariable(KUBECONFIG_VAR);

                if (string.IsNullOrEmpty(kubeConfig))
                {
                    config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
                }
                else
                {
                    byte[] configByteArray = System.Text.Encoding.UTF8.GetBytes(kubeConfig);
                    using (var stream = new MemoryStream(configByteArray))
                    {
                        config = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream);
                    }
                }
                Client = new Kubernetes(config);

                // Create a PV for our Azure File share and a corresponding claim if they don't already exist
                // If these fail, make sure that they don't already exist in the cluster: `kubectl delete -n default pvc,pv --all`
                storage      = Client.CreatePersistentVolume(Specs.BuildVolume.GetSpec(VOLUME_NAME, STORAGE_REQUESTED_CAPACITY, STORAGE_SHARE_NAME));
                storageClaim = Client.CreateNamespacedPersistentVolumeClaim(Specs.BuildVolumeClaim.GetSpec(STORAGE_REQUESTED_CAPACITY), NAMESPACE);
                Console.WriteLine("Created PersistentVolume and corresponding PersistentVolumeClaim");

                // Create the build pod
                var podSpec = Specs.BuildPod.GetSpec(BUILD_POD_NAME, VOLUME_NAME, storageClaim.Metadata.Name);

                BuildPod = k8sHelpers.CreatePodAndWait(Client, podSpec, NAMESPACE, k8sHelpers.IsPodRunning).Result;
                Console.WriteLine("Build pod is up & running");
            }
Пример #3
0
        internal void AddServicePod(string podName)
        {
            podName = podName.ToLower();
            var appLabel = new Dictionary <string, string> {
                { "app", podName }
            };

            using (Stream stream = KubeConfigStream)
            {
                var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream);

                using (IKubernetes client = new Kubernetes(config))
                {
                    var deployment = new Appsv1beta1Deployment("apps/v1beta1", "Deployment")
                    {
                        Metadata = new V1ObjectMeta()
                        {
                            Name = podName
                        },
                        Spec = new Appsv1beta1DeploymentSpec()
                        {
                            Template = new V1PodTemplateSpec()
                            {
                                Metadata = new V1ObjectMeta()
                                {
                                    Labels = appLabel
                                },
                                Spec = new V1PodSpec()
                                {
                                    Containers = new k8s.Models.V1Container[] {
                                        new V1Container
                                        {
                                            Name         = podName,
                                            Image        = "openhackteam5.azurecr.io/minecraft-server:2.0",
                                            VolumeMounts = new V1VolumeMount[]
                                            {
                                                new k8s.Models.V1VolumeMount
                                                {
                                                    Name      = "volume",
                                                    MountPath = "/data"
                                                }
                                            },
                                            Ports = new V1ContainerPort[]
                                            {
                                                new V1ContainerPort(25565, name: "port25565"),
                                                new V1ContainerPort(25575, name: "port25575")
                                            },
                                            Env = new V1EnvVar[] { new V1EnvVar("EULA", true.ToString()) }
                                        }
                                    },
                                    Volumes = new V1Volume[]
                                    {
                                        new V1Volume("volume", persistentVolumeClaim: new V1PersistentVolumeClaimVolumeSource(podName + SuffixPVC))
                                    },
                                    ImagePullSecrets = new V1LocalObjectReference[] { new V1LocalObjectReference("acr-auth") }
                                }
                            }
                        }
                    };
                    var loadBalancer = new V1Service("v1", "Service")
                    {
                        Metadata = new V1ObjectMeta {
                            Name = podName + SuffixLoadBalancer
                        },
                        Spec = new V1ServiceSpec
                        {
                            Type  = "LoadBalancer",
                            Ports = new V1ServicePort[] {
                                new   V1ServicePort(25565, "port25565", targetPort: 25565),
                                new   V1ServicePort(25575, "port25575", targetPort: 25575)
                            },
                            Selector = appLabel
                        }
                    };


                    var persistentVolumeClaim = new V1PersistentVolumeClaim()
                    {
                        Metadata = new V1ObjectMeta()
                        {
                            Name = podName + SuffixPVC, NamespaceProperty = DefaultNamespace
                        },
                        Spec = new V1PersistentVolumeClaimSpec
                        {
                            AccessModes      = new string[] { "ReadWriteMany" },
                            StorageClassName = "azurefile",
                            Resources        = new V1ResourceRequirements(requests: new Dictionary <string, ResourceQuantity> {
                                { "storage", new ResourceQuantity("5Gi") }
                            })
                        },
                        Status = new V1PersistentVolumeClaimStatus()
                    };
                    var pvcs = client.ListDeploymentForAllNamespaces1();
                    client.CreateNamespacedPersistentVolumeClaim(persistentVolumeClaim, DefaultNamespace);
                    client.CreateNamespacedDeployment1(deployment, DefaultNamespace);
                    client.CreateNamespacedService(loadBalancer, DefaultNamespace);
                }
            }
        }