/
endpoint.go
97 lines (81 loc) · 2.95 KB
/
endpoint.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
// Copyright 2017 The Kubernetes Authors.
//
// 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 endpoint
import (
"log"
"github.com/kubernetes/dashboard/src/app/backend/api"
"github.com/kubernetes/dashboard/src/app/backend/resource/common"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
k8sClient "k8s.io/client-go/kubernetes"
)
type Endpoint struct {
ObjectMeta api.ObjectMeta `json:"objectMeta"`
TypeMeta api.TypeMeta `json:"typeMeta"`
// Hostname, either as a domain name or IP address.
Host string `json:"host"`
// Name of the node the endpoint is located
NodeName *string `json:"nodeName"`
// Status of the endpoint
Ready bool `json:"ready"`
// Array of endpoint ports
Ports []v1.EndpointPort `json:"ports"`
}
// GetServiceEndpoints gets list of endpoints targeted by given label selector in given namespace.
func GetServiceEndpoints(client k8sClient.Interface, namespace, name string) (*EndpointList, error) {
endpointList := &EndpointList{
Endpoints: make([]Endpoint, 0),
ListMeta: api.ListMeta{TotalItems: 0},
}
serviceEndpoints, err := GetEndpoints(client, namespace, name)
if err != nil {
return endpointList, err
}
endpointList = toEndpointList(serviceEndpoints)
log.Printf("Found %d endpoints related to %s service in %s namespace", len(endpointList.Endpoints), name, namespace)
return endpointList, nil
}
// GetEndpoints gets endpoints associated to resource with given name.
func GetEndpoints(client k8sClient.Interface, namespace, name string) ([]v1.Endpoints, error) {
fieldSelector, err := fields.ParseSelector("metadata.name" + "=" + name)
if err != nil {
return nil, err
}
channels := &common.ResourceChannels{
EndpointList: common.GetEndpointListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace),
metaV1.ListOptions{
LabelSelector: labels.Everything().String(),
FieldSelector: fieldSelector.String(),
},
1),
}
endpointList := <-channels.EndpointList.List
if err := <-channels.EndpointList.Error; err != nil {
return nil, err
}
return endpointList.Items, nil
}
// toEndpoint converts endpoint api Endpoint to Endpoint model object.
func toEndpoint(address v1.EndpointAddress, ports []v1.EndpointPort, ready bool) *Endpoint {
return &Endpoint{
TypeMeta: api.NewTypeMeta(api.ResourceKindEndpoint),
Host: address.IP,
Ports: ports,
Ready: ready,
NodeName: address.NodeName,
}
}