コード例 #1
0
        public async Task <K8SResult> DeletePods(string JobId, string jobName, List <string> StatusFilter)
        {
            List <string> deleteLog    = new List <string>();
            K8SResult     resultStatus = new K8SResult()
            {
                Code = HttpStatusCode.OK
            };

            try
            {
                //Get pod list
                JArray podList = await GetK8SItemList(PODAPIPATH);

                if (podList != null)
                {
                    foreach (var currentPod in podList)
                    {
                        string jobStatus = (string)currentPod["status"]["phase"];
                        //Filter by Status, not present
                        if (!StatusFilter.Contains(jobStatus))
                        {
                            string  selfLink = (string)currentPod["metadata"]["selfLink"];
                            JObject labels   = (JObject)currentPod["metadata"]["labels"];
                            string  cjobname = (string)labels["job-name"];
                            string  podName  = (string)currentPod["metadata"]["name"];
                            if (cjobname.IndexOf(jobName) >= 0)
                            {
                                //1. Save POD LOGS by Job Name
                                await SavePodLog(JobId, podName);

                                //2. Delete
                                var subR = await CallK8SXXXAsync(K8SURLTOKEN, selfLink, HttpMethod.Delete);

                                if (!subR.IsSuccessStatusCode)
                                {
                                    deleteLog.Add($"POD {selfLink} Deleted faild: {subR.StatusCode}:{subR.ReasonPhrase}");
                                }
                                else
                                {
                                    deleteLog.Add($"POD {selfLink} Deleted!");
                                }
                            }
                        }
                    }
                    resultStatus.Content = Newtonsoft.Json.JsonConvert.SerializeObject(deleteLog);
                }
            }
            catch (Exception X)
            {
                resultStatus.Code = HttpStatusCode.InternalServerError;
                resultStatus.IsSuccessStatusCode = false;
                resultStatus.Content             = X.Message;
            }

            return(resultStatus);
        }
コード例 #2
0
        public async Task <K8SResult> SubmiteK8SJob(HttpContent yamalJob)
        {
            K8SResult r  = new K8SResult();
            var       rs = await CallK8SPostAsync(yamalJob, K8SURLTOKEN, CREATEJOBAPIPATH);

            r.IsSuccessStatusCode = rs.IsSuccessStatusCode;
            if (rs.IsSuccessStatusCode)
            {
                r.Content = await rs.Content.ReadAsStringAsync();
            }
            else
            {
                r.Content = rs.ReasonPhrase;
            }
            return(r);
        }
コード例 #3
0
        public async Task <K8SResult> SubmiteK8SJob(HttpContent yamalJob)
        {
            K8SResult r  = new K8SResult();
            var       rs = await CallK8SXXXAsync(K8SURLTOKEN, CREATEJOBAPIPATH, yamalJob, HttpMethod.Post);

            r.IsSuccessStatusCode = rs.IsSuccessStatusCode;
            if (rs.IsSuccessStatusCode)
            {
                //TODO: Check Job is really running
                r.Content = await rs.Content.ReadAsStringAsync();
            }
            else
            {
                r.Content = rs.ReasonPhrase;
            }
            return(r);
        }
コード例 #4
0
        public async Task <K8SResult> DeletePods(string jobname, string status = "All")
        {
            List <string> deleteLog = new List <string>();
            K8SResult     r         = new K8SResult()
            {
                Code = HttpStatusCode.OK
            };

            var rs = await CallK8SXXXAsync(K8SURLTOKEN, PODAPIPATH, HttpMethod.Get);

            r.Code = rs.StatusCode;
            r.IsSuccessStatusCode = rs.IsSuccessStatusCode;
            if ((rs.IsSuccessStatusCode))
            {
                JObject j     = Newtonsoft.Json.Linq.JObject.Parse(await rs.Content.ReadAsStringAsync());
                JArray  items = (JArray)j["items"];
                foreach (var item in items)
                {
                    string selfLink = (string)item["metadata"]["selfLink"];

                    if (((string)item["status"]["phase"] == status) || (status == "All"))
                    {
                        JObject labels   = (JObject)item["metadata"]["labels"];
                        string  cjobname = (string)labels["job-name"];
                        if (cjobname.IndexOf(jobname) >= 0)
                        {
                            //Delete
                            var subR = await CallK8SXXXAsync(K8SURLTOKEN, selfLink, HttpMethod.Delete);

                            if (!subR.IsSuccessStatusCode)
                            {
                                //throw new Exception($"{subR.StatusCode}:{subR.ReasonPhrase}");
                                deleteLog.Add($"POD {selfLink} Deleted faild: {subR.StatusCode}:{subR.ReasonPhrase}");
                            }
                            else
                            {
                                deleteLog.Add($"POD {selfLink} Deleted!");
                            }
                        }
                    }
                }
            }
            r.Content = Newtonsoft.Json.JsonConvert.SerializeObject(deleteLog);
            return(r);
        }
コード例 #5
0
        public async Task <K8SResult> GetK8SJobData(string jobName)
        {
            K8SResult r    = new K8SResult();
            string    path = $"/apis/batch/v1/namespaces/default/jobs/{jobName}";
            var       rs   = await CallK8SXXXAsync(K8SURLTOKEN, path, HttpMethod.Get);

            r.Code = rs.StatusCode;
            r.IsSuccessStatusCode = rs.IsSuccessStatusCode;
            if (rs.IsSuccessStatusCode)
            {
                r.Content = await rs.Content.ReadAsStringAsync();
            }
            else
            {
                r.Content = rs.ReasonPhrase;
            }
            return(r);
        }
コード例 #6
0
        public async Task <K8SResult> DeleteJobs(string JobId)
        {
            List <string> deleteLog    = new List <string>();
            K8SResult     masterResult = new K8SResult()
            {
                Code = HttpStatusCode.OK
            };
            JArray podList = null;
            //1. List PODS
            var podListRS = await CallK8SXXXAsync(K8SURLTOKEN, $"{PODBYJOBIDPATH}{JobId}", HttpMethod.Get);

            masterResult.Code = podListRS.StatusCode;
            masterResult.IsSuccessStatusCode = podListRS.IsSuccessStatusCode;
            if ((podListRS.IsSuccessStatusCode))
            {
                JObject jsonPodList = Newtonsoft.Json.Linq.JObject.Parse(await podListRS.Content.ReadAsStringAsync());
                podList = (JArray)jsonPodList["items"];
                foreach (var currentPod in podList)
                {
                    string podName  = (string)currentPod["metadata"]["name"];
                    string selfLink = (string)currentPod["metadata"]["selfLink"];
                    //2. Save information
                    var GetPodLog = await CallK8SXXXAsync(K8SURLTOKEN, $"{selfLink}/log", HttpMethod.Get);

                    masterResult.Code = GetPodLog.StatusCode;
                    masterResult.IsSuccessStatusCode = GetPodLog.IsSuccessStatusCode;
                    if (GetPodLog.IsSuccessStatusCode)
                    {
                        string log = await GetPodLog.Content.ReadAsStringAsync();

                        SaveBlobData(log, $"{JobId}.{podName}.log");
                    }
                    else
                    {
                        //Error to read POD information
                        masterResult.Code = HttpStatusCode.InternalServerError;
                        masterResult.IsSuccessStatusCode = false;
                        Trace.TraceError($"[{JobId}]Error to POD information : {GetPodLog.ReasonPhrase}");
                        deleteLog.Add($"[{JobId}]Error to POD information : {GetPodLog.ReasonPhrase}");
                    }
                }

                //3. Delete Jobs
                if (masterResult.Code == HttpStatusCode.OK)
                {
                    var jobLisRS = await CallK8SXXXAsync(K8SURLTOKEN, $"{JOBBYJOBIDPATH}{JobId}", HttpMethod.Get);

                    masterResult.Code = jobLisRS.StatusCode;
                    masterResult.IsSuccessStatusCode = jobLisRS.IsSuccessStatusCode;
                    if (jobLisRS.IsSuccessStatusCode)
                    {
                        JObject joblistraw = Newtonsoft.Json.Linq.JObject.Parse(await jobLisRS.Content.ReadAsStringAsync());
                        JArray  joblist    = (JArray)joblistraw["items"];
                        foreach (var job in joblist)
                        {
                            string jobName     = (string)job["metadata"]["name"];
                            string jobSelfLink = (string)job["metadata"]["selfLink"];
                            //Delete
                            var deleteRS = await CallK8SXXXAsync(K8SURLTOKEN, jobSelfLink, HttpMethod.Delete);

                            if (!deleteRS.IsSuccessStatusCode)
                            {
                                masterResult.Code = HttpStatusCode.InternalServerError;
                                masterResult.IsSuccessStatusCode = false;
                                deleteLog.Add($"JOB {jobSelfLink} Deleted faild: {deleteRS.StatusCode}:{deleteRS.ReasonPhrase}");
                                Trace.TraceError($"JOB {jobSelfLink} Deleted faild: {deleteRS.StatusCode}:{deleteRS.ReasonPhrase}");
                            }
                            else
                            {
                                deleteLog.Add($"[{JobId}] Deleted JOB {jobSelfLink}");
                                //DELETE POD
                                foreach (var pod2Delete in podList)
                                {
                                    JObject labels   = (JObject)pod2Delete["metadata"]["labels"];
                                    string  cjobname = (string)labels["job-name"];
                                    if (cjobname.IndexOf(jobName) >= 0)
                                    {
                                        var deletePodRS = await CallK8SXXXAsync(K8SURLTOKEN, (string)pod2Delete["metadata"]["selfLink"], HttpMethod.Delete);

                                        if (!deletePodRS.IsSuccessStatusCode)
                                        {
                                            masterResult.Code = HttpStatusCode.InternalServerError;
                                            masterResult.IsSuccessStatusCode = false;
                                            deleteLog.Add($"JOB {(string)pod2Delete["metadata"]["selfLink"]} Deleted POD faild: {deleteRS.StatusCode}:{deleteRS.ReasonPhrase}");
                                            Trace.TraceError($"JOB {(string)pod2Delete["metadata"]["selfLink"]} Deleted POD faild: {deleteRS.StatusCode}:{deleteRS.ReasonPhrase}");
                                        }
                                        else
                                        {
                                            deleteLog.Add($"[{JobId}] Deleted POD {(string)pod2Delete["metadata"]["selfLink"]}");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Error to read JOB LIST information
                        masterResult.Code = HttpStatusCode.InternalServerError;
                        masterResult.IsSuccessStatusCode = false;
                        Trace.TraceError($"[{JobId}]Error reding job list : {jobLisRS.ReasonPhrase}");
                    }
                }
            }
            else
            {
                //Error to read POD information
                masterResult.Code = HttpStatusCode.InternalServerError;
                masterResult.IsSuccessStatusCode = false;
                deleteLog.Add($"[{JobId}]Error to read POD list : {podListRS.ReasonPhrase}");
                Trace.TraceError($"[{JobId}]Error to read POD list : {podListRS.ReasonPhrase}");
            }

            masterResult.Content = Newtonsoft.Json.JsonConvert.SerializeObject(deleteLog);
            return(masterResult);
        }