|
| 1 | +// Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package job |
| 16 | + |
| 17 | +import ( |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/kubernetes/dashboard/client" |
| 21 | + "github.com/kubernetes/dashboard/resource/common" |
| 22 | + "github.com/kubernetes/dashboard/resource/pod" |
| 23 | + "k8s.io/kubernetes/pkg/api" |
| 24 | + "k8s.io/kubernetes/pkg/apis/batch" |
| 25 | + k8sClient "k8s.io/kubernetes/pkg/client/unversioned" |
| 26 | +) |
| 27 | + |
| 28 | +// JobDetail is a presentation layer view of Kubernetes Job resource. This means |
| 29 | +// it is Job plus additional augmented data we can get from other sources |
| 30 | +// (like services that target the same pods). |
| 31 | +type JobDetail struct { |
| 32 | + ObjectMeta common.ObjectMeta `json:"objectMeta"` |
| 33 | + TypeMeta common.TypeMeta `json:"typeMeta"` |
| 34 | + |
| 35 | + // Aggregate information about pods belonging to this Job. |
| 36 | + PodInfo common.PodInfo `json:"podInfo"` |
| 37 | + |
| 38 | + // Detailed information about Pods belonging to this Job. |
| 39 | + PodList pod.PodList `json:"podList"` |
| 40 | + |
| 41 | + // Container images of the Job. |
| 42 | + ContainerImages []string `json:"containerImages"` |
| 43 | + |
| 44 | + // List of events related to this Job. |
| 45 | + EventList common.EventList `json:"eventList"` |
| 46 | +} |
| 47 | + |
| 48 | +// GetJobDetail gets job details. |
| 49 | +func GetJobDetail(client k8sClient.Interface, heapsterClient client.HeapsterClient, |
| 50 | + namespace, name string) (*JobDetail, error) { |
| 51 | + |
| 52 | + log.Printf("Getting details of %s service in %s namespace", name, namespace) |
| 53 | + |
| 54 | + // TODO(floreks): Use channels. |
| 55 | + jobData, err := client.Extensions().Jobs(namespace).Get(name) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + channels := &common.ResourceChannels{ |
| 61 | + PodList: common.GetPodListChannel(client, common.NewSameNamespaceQuery(namespace), 1), |
| 62 | + } |
| 63 | + |
| 64 | + pods := <-channels.PodList.List |
| 65 | + if err := <-channels.PodList.Error; err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + events, err := GetJobEvents(client, jobData.Namespace, jobData.Name) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + job := getJobDetail(jobData, heapsterClient, events, pods.Items) |
| 75 | + return &job, nil |
| 76 | +} |
| 77 | + |
| 78 | +func getJobDetail(job *batch.Job, heapsterClient client.HeapsterClient, |
| 79 | + events *common.EventList, pods []api.Pod) JobDetail { |
| 80 | + |
| 81 | + matchingPods := common.FilterNamespacedPodsBySelector(pods, job.ObjectMeta.Namespace, |
| 82 | + job.Spec.Selector.MatchLabels) |
| 83 | + |
| 84 | + podInfo := getPodInfo(job, matchingPods) |
| 85 | + |
| 86 | + return JobDetail{ |
| 87 | + ObjectMeta: common.NewObjectMeta(job.ObjectMeta), |
| 88 | + TypeMeta: common.NewTypeMeta(common.ResourceKindJob), |
| 89 | + ContainerImages: common.GetContainerImages(&job.Spec.Template.Spec), |
| 90 | + PodInfo: podInfo, |
| 91 | + PodList: pod.CreatePodList(matchingPods, heapsterClient), |
| 92 | + EventList: *events, |
| 93 | + } |
| 94 | +} |
0 commit comments