mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
【修改】k8s 配置
This commit is contained in:
85
apps/devops/services/k8s/endpoint/endpoint.go
Normal file
85
apps/devops/services/k8s/endpoint/endpoint.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package endpoint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"pandax/base/global"
|
||||
|
||||
v1 "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"
|
||||
"pandax/apps/devops/entity/k8s"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
)
|
||||
|
||||
type Endpoint struct {
|
||||
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
|
||||
TypeMeta k8s.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: k8s.ListMeta{TotalItems: 0},
|
||||
}
|
||||
|
||||
serviceEndpoints, err := GetEndpoints(client, namespace, name)
|
||||
if err != nil {
|
||||
return endpointList, err
|
||||
}
|
||||
|
||||
endpointList = toEndpointList(serviceEndpoints)
|
||||
global.Log.Info(fmt.Sprintf("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 := &k8scommon.ResourceChannels{
|
||||
EndpointList: k8scommon.GetEndpointListChannelWithOptions(client,
|
||||
k8scommon.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: k8s.NewTypeMeta(k8s.ResourceKindEndpoint),
|
||||
Host: address.IP,
|
||||
Ports: ports,
|
||||
Ready: ready,
|
||||
NodeName: address.NodeName,
|
||||
}
|
||||
}
|
||||
33
apps/devops/services/k8s/endpoint/list.go
Normal file
33
apps/devops/services/k8s/endpoint/list.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package endpoint
|
||||
|
||||
import (
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"pandax/apps/devops/entity/k8s"
|
||||
)
|
||||
|
||||
type EndpointList struct {
|
||||
ListMeta k8s.ListMeta `json:"listMeta"`
|
||||
// List of endpoints
|
||||
Endpoints []Endpoint `json:"endpoints"`
|
||||
}
|
||||
|
||||
// toEndpointList converts array of api events to endpoint List structure
|
||||
func toEndpointList(endpoints []v1.Endpoints) *EndpointList {
|
||||
endpointList := EndpointList{
|
||||
Endpoints: make([]Endpoint, 0),
|
||||
ListMeta: k8s.ListMeta{TotalItems: len(endpoints)},
|
||||
}
|
||||
|
||||
for _, endpoint := range endpoints {
|
||||
for _, subSets := range endpoint.Subsets {
|
||||
for _, address := range subSets.Addresses {
|
||||
endpointList.Endpoints = append(endpointList.Endpoints, *toEndpoint(address, subSets.Ports, true))
|
||||
}
|
||||
for _, notReadyAddress := range subSets.NotReadyAddresses {
|
||||
endpointList.Endpoints = append(endpointList.Endpoints, *toEndpoint(notReadyAddress, subSets.Ports, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &endpointList
|
||||
}
|
||||
Reference in New Issue
Block a user