-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
servicedetail.go
95 lines (73 loc) · 3.31 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
// 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"
"k8s.io/kubernetes/pkg/api"
k8sClient "k8s.io/kubernetes/pkg/client/unversioned"
"github.com/kubernetes/dashboard/client"
"github.com/kubernetes/dashboard/resource/common"
"github.com/kubernetes/dashboard/resource/pod"
)
// 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) (*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, serviceData.Spec.Selector)
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 string, serviceSelector map[string]string) (*pod.PodList, error) {
channels := &common.ResourceChannels{
PodList: common.GetPodListChannel(client, common.NewSameNamespaceQuery(namespace), 1),
}
apiPodList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
apiPods := common.FilterNamespacedPodsBySelector(apiPodList.Items, namespace, serviceSelector)
podList := pod.CreatePodList(apiPods, heapsterClient)
return &podList, nil
}