MCPcopy
hub / github.com/cortexlabs/cortex / GetAPIByName

Function GetAPIByName

pkg/operator/resources/job/taskapi/api.go:213–300  ·  view source on GitHub ↗

GetAPIByName returns a single task API and its most recently submitted job along with all running task jobs

(deployedResource *operator.DeployedResource)

Source from the content-addressed store, hash-verified

211
212// GetAPIByName returns a single task API and its most recently submitted job along with all running task jobs
213func GetAPIByName(deployedResource *operator.DeployedResource) ([]schema.APIResponse, error) {
214 metadata, err := spec.MetadataFromVirtualService(deployedResource.VirtualService)
215 if err != nil {
216 return nil, err
217 }
218
219 api, err := operator.DownloadAPISpec(deployedResource.Name, metadata.APIID)
220 if err != nil {
221 return nil, err
222 }
223
224 k8sJobs, err := config.K8s.ListJobsByLabel("apiName", deployedResource.Name)
225 if err != nil {
226 return nil, err
227 }
228
229 jobIDToK8sJobMap := map[string]*kbatch.Job{}
230 for i, kJob := range k8sJobs {
231 jobIDToK8sJobMap[kJob.Labels["jobID"]] = &k8sJobs[i]
232 }
233
234 endpoint, err := operator.APIEndpoint(api)
235 if err != nil {
236 return nil, err
237 }
238
239 pods, err := config.K8s.ListPodsByLabel("apiName", deployedResource.Name)
240 if err != nil {
241 return nil, err
242 }
243
244 jobIDToPodsMap := map[string][]kcore.Pod{}
245 for _, pod := range pods {
246 jobIDToPodsMap[pod.Labels["jobID"]] = append(jobIDToPodsMap[pod.Labels["jobID"]], pod)
247 }
248
249 inProgressJobKeys, err := job.ListAllInProgressJobKeysByAPI(userconfig.TaskAPIKind, deployedResource.Name)
250 if err != nil {
251 return nil, err
252 }
253
254 jobStatuses := []status.TaskJobStatus{}
255 jobIDSet := strset.New()
256 for _, jobKey := range inProgressJobKeys {
257 jobStatus, err := getJobStatusFromK8sJob(jobKey, jobIDToK8sJobMap[jobKey.ID], jobIDToPodsMap[jobKey.ID])
258 if err != nil {
259 return nil, err
260 }
261
262 jobStatuses = append(jobStatuses, *jobStatus)
263 jobIDSet.Add(jobKey.ID)
264 }
265
266 if len(jobStatuses) < 10 {
267 jobStates, err := job.GetMostRecentlySubmittedJobStates(deployedResource.Name, 10+len(jobStatuses), userconfig.TaskAPIKind)
268 if err != nil {
269 return nil, err
270 }

Callers 1

GetAPIFunction · 0.92

Calls 14

DownloadAPISpecFunction · 0.92
APIEndpointFunction · 0.92
NewFunction · 0.92
StringFunction · 0.92
getJobStatusFromK8sJobFunction · 0.85
ListJobsByLabelMethod · 0.80
ListPodsByLabelMethod · 0.80
getJobStatusFromJobStateFunction · 0.70
getDashboardURLFunction · 0.70

Tested by

no test coverage detected