mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-25 20:48:35 +08:00
【修改】k8s 配置
This commit is contained in:
142
apps/devops/services/k8s/dataselect/dataselect.go
Normal file
142
apps/devops/services/k8s/dataselect/dataselect.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package dataselect
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// GenericDataCell describes the interface of the data cell that contains all the necessary methods needed to perform
|
||||
// complex data selection
|
||||
// GenericDataSelect takes a list of these interfaces and performs selection operation.
|
||||
// Therefore as long as the list is composed of GenericDataCells you can perform any data selection!
|
||||
type DataCell interface {
|
||||
// GetPropertyAtIndex returns the property of this data cell.
|
||||
// Value returned has to have Compare method which is required by Sort functionality of DataSelect.
|
||||
GetProperty(PropertyName) ComparableValue
|
||||
}
|
||||
|
||||
// ComparableValue hold any value that can be compared to its own kind.
|
||||
type ComparableValue interface {
|
||||
// Compares self with other value. Returns 1 if other value is smaller, 0 if they are the same, -1 if other is larger.
|
||||
Compare(ComparableValue) int
|
||||
// Returns true if self value contains or is equal to other value, false otherwise.
|
||||
Contains(ComparableValue) bool
|
||||
}
|
||||
|
||||
// SelectableData contains all the required data to perform data selection.
|
||||
// It implements sort.Interface so its sortable under sort.Sort
|
||||
// You can use its Select method to get selected GenericDataCell list.
|
||||
type DataSelector struct {
|
||||
// GenericDataList hold generic data cells that are being selected.
|
||||
GenericDataList []DataCell
|
||||
// DataSelectQuery holds instructions for data select.
|
||||
DataSelectQuery *DataSelectQuery
|
||||
}
|
||||
|
||||
// GenericDataSelectWithFilter 获取 GenericDataCells 和 DataSelectQuery 的列表,并按照 dsQuery 的指示返回选定的数据。
|
||||
func GenericDataSelectWithFilter(dataList []DataCell, dsQuery *DataSelectQuery) ([]DataCell, int) {
|
||||
SelectableData := DataSelector{
|
||||
GenericDataList: dataList,
|
||||
DataSelectQuery: dsQuery,
|
||||
}
|
||||
// Pipeline is Filter -> Sort -> CollectMetrics -> Paginate
|
||||
filtered := SelectableData.Filter()
|
||||
filteredTotal := len(filtered.GenericDataList)
|
||||
processed := filtered.Sort().Paginate()
|
||||
return processed.GenericDataList, filteredTotal
|
||||
}
|
||||
|
||||
// Filter the data inside as instructed by DataSelectQuery and returns itself to allow method chaining.
|
||||
func (self *DataSelector) Filter() *DataSelector {
|
||||
filteredList := []DataCell{}
|
||||
|
||||
for _, c := range self.GenericDataList {
|
||||
matches := true
|
||||
for _, filterBy := range self.DataSelectQuery.FilterQuery.FilterByList {
|
||||
v := c.GetProperty(filterBy.Property)
|
||||
if v == nil || !v.Contains(filterBy.Value) {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
filteredList = append(filteredList, c)
|
||||
}
|
||||
}
|
||||
|
||||
self.GenericDataList = filteredList
|
||||
return self
|
||||
}
|
||||
|
||||
// Implementation of sort.Interface so that we can use built-in sort function (sort.Sort) for sorting SelectableData
|
||||
|
||||
// Len returns the length of data inside SelectableData.
|
||||
func (self DataSelector) Len() int { return len(self.GenericDataList) }
|
||||
|
||||
// Swap swaps 2 indices inside SelectableData.
|
||||
func (self DataSelector) Swap(i, j int) {
|
||||
self.GenericDataList[i], self.GenericDataList[j] = self.GenericDataList[j], self.GenericDataList[i]
|
||||
}
|
||||
|
||||
// Less compares 2 indices inside SelectableData and returns true if first index is larger.
|
||||
func (self DataSelector) Less(i, j int) bool {
|
||||
for _, sortBy := range self.DataSelectQuery.SortQuery.SortByList {
|
||||
a := self.GenericDataList[i].GetProperty(sortBy.Property)
|
||||
b := self.GenericDataList[j].GetProperty(sortBy.Property)
|
||||
// ignore sort completely if property name not found
|
||||
if a == nil || b == nil {
|
||||
break
|
||||
}
|
||||
cmp := a.Compare(b)
|
||||
if cmp == 0 { // values are the same. Just continue to next sortBy
|
||||
continue
|
||||
} else { // values different
|
||||
return (cmp == -1 && sortBy.Ascending) || (cmp == 1 && !sortBy.Ascending)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Sort sorts the data inside as instructed by DataSelectQuery and returns itself to allow method chaining.
|
||||
func (self *DataSelector) Sort() *DataSelector {
|
||||
sort.Sort(*self)
|
||||
return self
|
||||
}
|
||||
|
||||
// Paginates the data inside as instructed by DataSelectQuery and returns itself to allow method chaining.
|
||||
func (self *DataSelector) Paginate() *DataSelector {
|
||||
pQuery := self.DataSelectQuery.PaginationQuery
|
||||
dataList := self.GenericDataList
|
||||
startIndex, endIndex := pQuery.GetPaginationSettings(len(dataList))
|
||||
|
||||
// Return all items if provided settings do not meet requirements
|
||||
if !pQuery.IsValidPagination() {
|
||||
return self
|
||||
}
|
||||
// Return no items if requested page does not exist
|
||||
if !pQuery.IsPageAvailable(len(self.GenericDataList), startIndex) {
|
||||
self.GenericDataList = []DataCell{}
|
||||
return self
|
||||
}
|
||||
|
||||
self.GenericDataList = dataList[startIndex:endIndex]
|
||||
return self
|
||||
}
|
||||
|
||||
// IsValidPagination returns true if pagination has non negative parameters
|
||||
func (p *PaginationQuery) IsValidPagination() bool {
|
||||
return p.ItemsPerPage >= 0 && p.Page >= 0
|
||||
}
|
||||
|
||||
// IsPageAvailable returns true if at least one element can be placed on page. False otherwise
|
||||
func (p *PaginationQuery) IsPageAvailable(itemsCount, startingIndex int) bool {
|
||||
return itemsCount > startingIndex && p.ItemsPerPage > 0
|
||||
}
|
||||
|
||||
// GenericDataSelect takes a list of GenericDataCells and DataSelectQuery and returns selected data as instructed by dsQuery.
|
||||
func GenericDataSelect(dataList []DataCell, dsQuery *DataSelectQuery) []DataCell {
|
||||
SelectableData := DataSelector{
|
||||
GenericDataList: dataList,
|
||||
DataSelectQuery: dsQuery,
|
||||
}
|
||||
return SelectableData.Sort().Paginate().GenericDataList
|
||||
}
|
||||
118
apps/devops/services/k8s/dataselect/dataselectquery.go
Normal file
118
apps/devops/services/k8s/dataselect/dataselectquery.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package dataselect
|
||||
|
||||
// DataSelectQuery is options for GenericDataSelect which takes []GenericDataCell and returns selected data.
|
||||
// Can be extended to include any kind of selection - for example filtering.
|
||||
// Currently included only Pagination and Sort options.
|
||||
type DataSelectQuery struct {
|
||||
PaginationQuery *PaginationQuery
|
||||
SortQuery *SortQuery
|
||||
FilterQuery *FilterQuery
|
||||
}
|
||||
|
||||
// PaginationQuery structure represents pagination settings
|
||||
type PaginationQuery struct {
|
||||
// How many items per page should be returned
|
||||
ItemsPerPage int
|
||||
// Number of page that should be returned when pagination is applied to the list
|
||||
Page int
|
||||
}
|
||||
|
||||
// SortQuery holds options for sort functionality of data select.
|
||||
type SortQuery struct {
|
||||
SortByList []SortBy
|
||||
}
|
||||
|
||||
// SortBy holds the name of the property that should be sorted and whether order should be ascending or descending.
|
||||
type SortBy struct {
|
||||
Property PropertyName
|
||||
Ascending bool
|
||||
}
|
||||
|
||||
type FilterQuery struct {
|
||||
FilterByList []FilterBy
|
||||
}
|
||||
|
||||
type FilterBy struct {
|
||||
Property PropertyName
|
||||
Value ComparableValue
|
||||
}
|
||||
|
||||
var NoFilter = &FilterQuery{
|
||||
FilterByList: []FilterBy{},
|
||||
}
|
||||
|
||||
// NoSort is as option for no sort.
|
||||
var NoSort = &SortQuery{
|
||||
SortByList: []SortBy{},
|
||||
}
|
||||
|
||||
// NewFilterQuery takes raw filter options list and returns FilterQuery object. For example:
|
||||
// ["parameter1", "value1", "parameter2", "value2"] - means that the data should be filtered by
|
||||
// parameter1 equals value1 and parameter2 equals value2
|
||||
func NewFilterQuery(filterByListRaw []string) *FilterQuery {
|
||||
if filterByListRaw == nil || len(filterByListRaw)%2 == 1 {
|
||||
return NoFilter
|
||||
}
|
||||
filterByList := []FilterBy{}
|
||||
for i := 0; i+1 < len(filterByListRaw); i += 2 {
|
||||
propertyName := filterByListRaw[i]
|
||||
propertyValue := filterByListRaw[i+1]
|
||||
filterBy := FilterBy{
|
||||
Property: PropertyName(propertyName),
|
||||
Value: StdComparableString(propertyValue),
|
||||
}
|
||||
// Add to the filter options.
|
||||
filterByList = append(filterByList, filterBy)
|
||||
}
|
||||
return &FilterQuery{
|
||||
FilterByList: filterByList,
|
||||
}
|
||||
}
|
||||
|
||||
// NewSortQuery takes raw sort options list and returns SortQuery object. For example:
|
||||
// ["a", "parameter1", "d", "parameter2"] - means that the data should be sorted by
|
||||
// parameter1 (ascending) and later - for results that return equal under parameter 1 sort - by parameter2 (descending)
|
||||
func NewSortQuery(sortByListRaw []string) *SortQuery {
|
||||
if sortByListRaw == nil || len(sortByListRaw)%2 == 1 {
|
||||
// Empty sort list or invalid (odd) length
|
||||
return NoSort
|
||||
}
|
||||
sortByList := []SortBy{}
|
||||
for i := 0; i+1 < len(sortByListRaw); i += 2 {
|
||||
// parse order option
|
||||
var ascending bool
|
||||
orderOption := sortByListRaw[i]
|
||||
if orderOption == "a" {
|
||||
ascending = true
|
||||
} else if orderOption == "d" {
|
||||
ascending = false
|
||||
} else {
|
||||
// Invalid order option. Only ascending (a), descending (d) options are supported
|
||||
return NoSort
|
||||
}
|
||||
|
||||
// parse property name
|
||||
propertyName := sortByListRaw[i+1]
|
||||
sortBy := SortBy{
|
||||
Property: PropertyName(propertyName),
|
||||
Ascending: ascending,
|
||||
}
|
||||
// Add to the sort options.
|
||||
sortByList = append(sortByList, sortBy)
|
||||
}
|
||||
return &SortQuery{
|
||||
SortByList: sortByList,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDataSelectQuery creates DataSelectQuery object from simpler data select queries.
|
||||
func NewDataSelectQuery(paginationQuery *PaginationQuery, sortQuery *SortQuery, filterQuery *FilterQuery) *DataSelectQuery {
|
||||
return &DataSelectQuery{
|
||||
PaginationQuery: paginationQuery,
|
||||
SortQuery: sortQuery,
|
||||
FilterQuery: filterQuery,
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultDataSelect downloads first 10 items from page 1 with no sort and no metrics.
|
||||
var DefaultDataSelect = NewDataSelectQuery(DefaultPagination, NoSort, NoFilter)
|
||||
28
apps/devops/services/k8s/dataselect/pagination.go
Normal file
28
apps/devops/services/k8s/dataselect/pagination.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package dataselect
|
||||
|
||||
// By default backend pagination will not be applied.
|
||||
var NoPagination = NewPaginationQuery(-1, -1)
|
||||
|
||||
// No items will be returned
|
||||
var EmptyPagination = NewPaginationQuery(0, 0)
|
||||
|
||||
// Returns 10 items from page 1
|
||||
var DefaultPagination = NewPaginationQuery(10, 0)
|
||||
|
||||
// NewPaginationQuery return pagination query structure based on given parameters
|
||||
func NewPaginationQuery(itemsPerPage, page int) *PaginationQuery {
|
||||
return &PaginationQuery{itemsPerPage, page}
|
||||
}
|
||||
|
||||
// GetPaginationSettings based on number of items and pagination query parameters returns start
|
||||
// and end index that can be used to return paginated list of items.
|
||||
func (p *PaginationQuery) GetPaginationSettings(itemsCount int) (startIndex int, endIndex int) {
|
||||
startIndex = p.ItemsPerPage * p.Page
|
||||
endIndex = startIndex + p.ItemsPerPage
|
||||
|
||||
if endIndex > itemsCount {
|
||||
endIndex = itemsCount
|
||||
}
|
||||
|
||||
return startIndex, endIndex
|
||||
}
|
||||
17
apps/devops/services/k8s/dataselect/propertyname.go
Normal file
17
apps/devops/services/k8s/dataselect/propertyname.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package dataselect
|
||||
|
||||
// PropertyName is used to get the value of certain property of data cell.
|
||||
// For example if we want to get the namespace of certain Deployment we can use DeploymentCell.GetProperty(NamespaceProperty)
|
||||
type PropertyName string
|
||||
|
||||
// List of all property names supported by the UI.
|
||||
const (
|
||||
NameProperty = "name"
|
||||
CreationTimestampProperty = "creationTimestamp"
|
||||
NamespaceProperty = "namespace"
|
||||
StatusProperty = "status"
|
||||
TypeProperty = "type"
|
||||
FirstSeenProperty = "firstSeen"
|
||||
LastSeenProperty = "lastSeen"
|
||||
ReasonProperty = "reason"
|
||||
)
|
||||
38
apps/devops/services/k8s/dataselect/stdcomparabletypes.go
Normal file
38
apps/devops/services/k8s/dataselect/stdcomparabletypes.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package dataselect
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type StdComparableString string
|
||||
|
||||
func (self StdComparableString) Compare(otherV ComparableValue) int {
|
||||
other := otherV.(StdComparableString)
|
||||
return strings.Compare(string(self), string(other))
|
||||
}
|
||||
|
||||
func (self StdComparableString) Contains(otherV ComparableValue) bool {
|
||||
other := otherV.(StdComparableString)
|
||||
return strings.Contains(string(self), string(other))
|
||||
}
|
||||
|
||||
type StdComparableTime time.Time
|
||||
|
||||
func (self StdComparableTime) Compare(otherV ComparableValue) int {
|
||||
other := otherV.(StdComparableTime)
|
||||
return ints64Compare(time.Time(self).Unix(), time.Time(other).Unix())
|
||||
}
|
||||
|
||||
func (self StdComparableTime) Contains(otherV ComparableValue) bool {
|
||||
return self.Compare(otherV) == 0
|
||||
}
|
||||
|
||||
func ints64Compare(a, b int64) int {
|
||||
if a > b {
|
||||
return 1
|
||||
} else if a == b {
|
||||
return 0
|
||||
}
|
||||
return -1
|
||||
}
|
||||
Reference in New Issue
Block a user