-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservicedetail.go
108 lines (86 loc) · 3.71 KB
/
servicedetail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"log"
"github.com/kubernetes/dashboard/src/app/backend/client"
"github.com/kubernetes/dashboard/src/app/backend/resource/common"
"github.com/kubernetes/dashboard/src/app/backend/resource/pod"
"k8s.io/kubernetes/pkg/api"
k8sClient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
)
// Service is a representation of a service.
type ServiceDetail struct {
ObjectMeta common.ObjectMeta `json:"objectMeta"`
TypeMeta common.TypeMeta `json:"typeMeta"`
// InternalEndpoint of all Kubernetes services that have the same label selector as connected Replication
// Controller. Endpoint is DNS name merged with ports.
InternalEndpoint common.Endpoint `json:"internalEndpoint"`
// ExternalEndpoints of all Kubernetes services that have the same label selector as connected Replication
// Controller. Endpoint is external IP address name merged with ports.
ExternalEndpoints []common.Endpoint `json:"externalEndpoints"`
// Label selector of the service.
Selector map[string]string `json:"selector"`
// Type determines how the service will be exposed. Valid options: ClusterIP, NodePort, LoadBalancer
Type api.ServiceType `json:"type"`
// ClusterIP is usually assigned by the master. Valid values are None, empty string (""), or
// a valid IP address. None can be specified for headless services when proxying is not required
ClusterIP string `json:"clusterIP"`
// PodList represents list of pods targeted by same label selector as this service.
PodList pod.PodList `json:"podList"`
}
// GetServiceDetail gets service details.
func GetServiceDetail(client k8sClient.Interface, heapsterClient client.HeapsterClient,
namespace, name string, dsQuery *dataselect.DataSelectQuery) (*ServiceDetail, error) {
log.Printf("Getting details of %s service in %s namespace", name, namespace)
// TODO(maciaszczykm): Use channels.
serviceData, err := client.Services(namespace).Get(name)
if err != nil {
return nil, err
}
podList, err := GetServicePods(client, heapsterClient, namespace, name, dsQuery)
if err != nil {
return nil, err
}
service := ToServiceDetail(serviceData)
service.PodList = *podList
return &service, nil
}
// GetServicePods gets list of pods targeted by given label selector in given namespace.
func GetServicePods(client k8sClient.Interface, heapsterClient client.HeapsterClient, namespace,
name string, dsQuery *dataselect.DataSelectQuery) (*pod.PodList, error) {
service, err := client.Services(namespace).Get(name)
if err != nil {
return nil, err
}
labelSelector := labels.SelectorFromSet(service.Spec.Selector)
channels := &common.ResourceChannels{
PodList: common.GetPodListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace),
api.ListOptions{
LabelSelector: labelSelector,
FieldSelector: fields.Everything(),
},
1),
}
apiPodList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
podList := pod.CreatePodList(apiPodList.Items, dsQuery, heapsterClient)
return &podList, nil
}