|
| 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 petset |
| 16 | + |
| 17 | +import ( |
| 18 | + "log" |
| 19 | + |
| 20 | + "k8s.io/kubernetes/pkg/api" |
| 21 | + "k8s.io/kubernetes/pkg/apis/apps" |
| 22 | + k8sClient "k8s.io/kubernetes/pkg/client/unversioned" |
| 23 | + |
| 24 | + "github.com/kubernetes/dashboard/client" |
| 25 | + "github.com/kubernetes/dashboard/resource/common" |
| 26 | + "github.com/kubernetes/dashboard/resource/pod" |
| 27 | +) |
| 28 | + |
| 29 | +// PetSetDetail is a presentation layer view of Kubernetes Pet Set resource. This means |
| 30 | +// it is Pet Set plus additional augmented data we can get from other sources |
| 31 | +// (like services that target the same pods). |
| 32 | +type PetSetDetail struct { |
| 33 | + ObjectMeta common.ObjectMeta `json:"objectMeta"` |
| 34 | + TypeMeta common.TypeMeta `json:"typeMeta"` |
| 35 | + |
| 36 | + // Aggregate information about pods belonging to this Pet Set. |
| 37 | + PodInfo common.PodInfo `json:"podInfo"` |
| 38 | + |
| 39 | + // Detailed information about Pods belonging to this Pet Set. |
| 40 | + PodList pod.PodList `json:"podList"` |
| 41 | + |
| 42 | + // Container images of the Pet Set. |
| 43 | + ContainerImages []string `json:"containerImages"` |
| 44 | + |
| 45 | + // List of events related to this Pet Set. |
| 46 | + EventList common.EventList `json:"eventList"` |
| 47 | +} |
| 48 | + |
| 49 | +// GetPetSetDetail gets pet set details. |
| 50 | +func GetPetSetDetail(client *k8sClient.Client, heapsterClient client.HeapsterClient, |
| 51 | + namespace, name string) (*PetSetDetail, error) { |
| 52 | + |
| 53 | + log.Printf("Getting details of %s service in %s namespace", name, namespace) |
| 54 | + |
| 55 | + // TODO(floreks): Use channels. |
| 56 | + petSetData, err := client.Apps().PetSets(namespace).Get(name) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + channels := &common.ResourceChannels{ |
| 62 | + PodList: common.GetPodListChannel(client, common.NewSameNamespaceQuery(namespace), 1), |
| 63 | + } |
| 64 | + |
| 65 | + pods := <-channels.PodList.List |
| 66 | + if err := <-channels.PodList.Error; err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + events, err := GetPetSetEvents(client, petSetData.Namespace, petSetData.Name) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + petSet := getPetSetDetail(petSetData, heapsterClient, events, pods.Items) |
| 76 | + return &petSet, nil |
| 77 | +} |
| 78 | + |
| 79 | +func getPetSetDetail(petSet *apps.PetSet, heapsterClient client.HeapsterClient, |
| 80 | + events *common.EventList, pods []api.Pod) PetSetDetail { |
| 81 | + |
| 82 | + matchingPods := common.FilterNamespacedPodsByLabelSelector(pods, petSet.ObjectMeta.Namespace, |
| 83 | + petSet.Spec.Selector) |
| 84 | + |
| 85 | + podInfo := common.GetPodInfo(int32(petSet.Status.Replicas), int32(petSet.Spec.Replicas), |
| 86 | + matchingPods) |
| 87 | + |
| 88 | + return PetSetDetail{ |
| 89 | + ObjectMeta: common.NewObjectMeta(petSet.ObjectMeta), |
| 90 | + TypeMeta: common.NewTypeMeta(common.ResourceKindPetSet), |
| 91 | + ContainerImages: common.GetContainerImages(&petSet.Spec.Template.Spec), |
| 92 | + PodInfo: podInfo, |
| 93 | + PodList: pod.CreatePodList(matchingPods, heapsterClient), |
| 94 | + EventList: *events, |
| 95 | + } |
| 96 | +} |
0 commit comments