【修改】k8s 配置

This commit is contained in:
PandaGoAdmin
2022-01-22 17:07:04 +08:00
parent c6ebe89865
commit 33cc74711d
439 changed files with 9936 additions and 21687 deletions

View File

@@ -0,0 +1,29 @@
package service
import (
"fmt"
"pandax/base/global"
client "k8s.io/client-go/kubernetes"
"pandax/apps/devops/entity/k8s"
k8scommon "pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
"pandax/apps/devops/services/k8s/event"
)
// GetServiceEvents returns model events for a service with the given name in the given namespace.
func GetServiceEvents(client *client.Clientset, dsQuery *dataselect.DataSelectQuery, namespace, name string) (*k8scommon.EventList, error) {
eventList := k8scommon.EventList{
Events: make([]k8scommon.Event, 0),
ListMeta: k8s.ListMeta{TotalItems: 0},
}
serviceEvents, err := event.GetEvents(client, namespace, name)
if err != nil {
return &eventList, err
}
eventList = event.CreateEventList(event.FillEventsType(serviceEvents), dsQuery)
global.Log.Info(fmt.Sprintf("Found %d events related to %s service in %s namespace", len(eventList.Events), name, namespace))
return &eventList, nil
}

View File

@@ -0,0 +1,52 @@
package service
import (
"context"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
k8scommon "pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
"pandax/apps/devops/services/k8s/event"
"pandax/apps/devops/services/k8s/pods"
)
// GetServicePods gets list of pods targeted by given label selector in given namespace.
func GetServicePods(client *kubernetes.Clientset, namespace, name string, dsQuery *dataselect.DataSelectQuery) (*pods.PodList, error) {
podList := pods.PodList{
Pods: []pods.Pod{},
}
service, err := client.CoreV1().Services(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return &podList, err
}
if service.Spec.Selector == nil {
return &podList, nil
}
labelSelector := labels.SelectorFromSet(service.Spec.Selector)
channels := &k8scommon.ResourceChannels{
PodList: k8scommon.GetPodListChannelWithOptions(client, k8scommon.NewSameNamespaceQuery(namespace),
metaV1.ListOptions{
LabelSelector: labelSelector.String(),
FieldSelector: fields.Everything().String(),
}, 1),
}
apiPodList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return &podList, err
}
events, err := event.GetPodsEvents(client, namespace, apiPodList.Items)
if err != nil {
return &podList, err
}
podList = pods.ToPodList(apiPodList.Items, events, dsQuery)
return &podList, nil
}

View File

@@ -0,0 +1,149 @@
package service
import (
"context"
"errors"
"fmt"
"pandax/base/global"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"pandax/apps/devops/entity/k8s"
k8scommon "pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
"strings"
)
// Service is a representation of a service.
type Service struct {
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
TypeMeta k8s.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 k8scommon.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 []k8scommon.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, ExternalName
Type v1.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"`
}
// ServiceList contains a list of services in the cluster.
type ServiceList struct {
ListMeta k8s.ListMeta `json:"listMeta"`
// Unordered list of services.
Services []Service `json:"services"`
}
// GetServiceList returns a list of all services in the cluster.
func GetServiceList(client *kubernetes.Clientset, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*ServiceList, error) {
global.Log.Info("Getting list of all services in the cluster")
channels := &k8scommon.ResourceChannels{
ServiceList: k8scommon.GetServiceListChannel(client, nsQuery, 1),
}
return GetServiceListFromChannels(channels, dsQuery)
}
// GetServiceListFromChannels returns a list of all services in the cluster.
func GetServiceListFromChannels(channels *k8scommon.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*ServiceList, error) {
services := <-channels.ServiceList.List
err := <-channels.ServiceList.Error
if err != nil {
return nil, err
}
return CreateServiceList(services.Items, dsQuery), nil
}
func ToService(service *v1.Service) Service {
return Service{
ObjectMeta: k8s.NewObjectMeta(service.ObjectMeta),
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindService),
InternalEndpoint: k8scommon.GetInternalEndpoint(service.Name, service.Namespace, service.Spec.Ports),
ExternalEndpoints: k8scommon.GetExternalEndpoints(service),
Selector: service.Spec.Selector,
ClusterIP: service.Spec.ClusterIP,
Type: service.Spec.Type,
}
}
// CreateServiceList returns paginated service list based on given service array and pagination query.
func CreateServiceList(services []v1.Service, dsQuery *dataselect.DataSelectQuery) *ServiceList {
serviceList := &ServiceList{
Services: make([]Service, 0),
ListMeta: k8s.ListMeta{TotalItems: len(services)},
}
serviceCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(services), dsQuery)
services = fromCells(serviceCells)
serviceList.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
for _, service := range services {
serviceList.Services = append(serviceList.Services, ToService(&service))
}
return serviceList
}
func DeleteService(client *kubernetes.Clientset, ns string, serviceName string) error {
global.Log.Info(fmt.Sprintf("请求删除Service: %v, namespace: %v", serviceName, ns))
return client.CoreV1().Services(ns).Delete(
context.TODO(),
serviceName,
metav1.DeleteOptions{},
)
}
func DeleteCollectionService(client *kubernetes.Clientset, serviceList []k8s.ServiceData) (err error) {
global.Log.Info("批量删除service开始")
for _, v := range serviceList {
global.Log.Info(fmt.Sprintf("delete service%v, ns: %v", v.Name, v.Namespace))
err := client.CoreV1().Services(v.Namespace).Delete(
context.TODO(),
v.Name,
metav1.DeleteOptions{},
)
if err != nil {
global.Log.Error(err.Error())
return err
}
}
global.Log.Info("删除service已完成")
return nil
}
func GetToService(client *kubernetes.Clientset, namespace string, name string) (*ServiceList, error) {
serviceList := &ServiceList{
Services: make([]Service, 0),
}
svcList, err := client.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
global.Log.Info("开始获取svc")
if err != nil {
return nil, err
}
for _, svc := range svcList.Items {
if strings.Contains(svc.Name, name) {
serviceList.Services = append(serviceList.Services, ToService(&svc))
serviceList.ListMeta = k8s.ListMeta{
TotalItems: len(serviceList.Services),
}
return serviceList, nil
}
}
global.Log.Warn(fmt.Sprintf("没有找到所关联的SVCnamespace: %s, name: %s", namespace, name))
return nil, errors.New("没有找到所关联的SVC")
}

View File

@@ -0,0 +1,42 @@
package service
import (
v1 "k8s.io/api/core/v1"
"pandax/apps/devops/services/k8s/dataselect"
)
// The code below allows to perform complex data section on []api.Service
type ServiceCell v1.Service
func (self ServiceCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
switch name {
case dataselect.NameProperty:
return dataselect.StdComparableString(self.ObjectMeta.Name)
case dataselect.CreationTimestampProperty:
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
case dataselect.NamespaceProperty:
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
case dataselect.TypeProperty:
return dataselect.StdComparableString(self.Spec.Type)
default:
// if name is not supported then just return a constant dummy value, sort will have no effect.
return nil
}
}
func toCells(std []v1.Service) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = ServiceCell(std[i])
}
return cells
}
func fromCells(cells []dataselect.DataCell) []v1.Service {
std := make([]v1.Service, len(cells))
for i := range std {
std[i] = v1.Service(cells[i].(ServiceCell))
}
return std
}

View File

@@ -0,0 +1,67 @@
package service
import (
"context"
"fmt"
"pandax/base/global"
v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
k8scommon "pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
"pandax/apps/devops/services/k8s/endpoint"
"pandax/apps/devops/services/k8s/pods"
)
// ServiceDetail is a representation of a service.
type ServiceDetail struct {
// Extends list item structure.
Service `json:",inline"`
// List of Endpoint obj. that are endpoints of this Service.
EndpointList endpoint.EndpointList `json:"endpointList"`
// Show the value of the SessionAffinity of the Service.
SessionAffinity v1.ServiceAffinity `json:"sessionAffinity"`
EventList *k8scommon.EventList `json:"eventList"`
PodList *pods.PodList `json:"podList"`
}
// GetServiceDetail gets service details.
func GetServiceDetail(client *kubernetes.Clientset, namespace, name string, dsQuery *dataselect.DataSelectQuery) (*ServiceDetail, error) {
global.Log.Info(fmt.Sprintf("Getting details of %s service in %s namespace", name, namespace))
serviceData, err := client.CoreV1().Services(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}
endpointList, err := endpoint.GetServiceEndpoints(client, namespace, name)
if err != nil {
return nil, err
}
podList, err := GetServicePods(client, namespace, name, dsQuery)
if err != nil {
return nil, err
}
eventList, err := GetServiceEvents(client, dataselect.DefaultDataSelect, namespace, name)
if err != nil {
return nil, err
}
service := toServiceDetail(serviceData, *endpointList, podList, eventList)
return &service, nil
}
func toServiceDetail(service *v1.Service, endpointList endpoint.EndpointList, podList *pods.PodList, eventList *k8scommon.EventList) ServiceDetail {
return ServiceDetail{
Service: ToService(service),
EndpointList: endpointList,
PodList: podList,
EventList: eventList,
SessionAffinity: service.Spec.SessionAffinity,
}
}