-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Heylosky <2547226479@qq.com>
- Loading branch information
Showing
6 changed files
with
223 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package node | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/karmada-io/dashboard/cmd/api/app/router" | ||
"github.com/karmada-io/dashboard/cmd/api/app/types/common" | ||
"github.com/karmada-io/dashboard/pkg/client" | ||
"github.com/karmada-io/dashboard/pkg/resource/node" | ||
) | ||
|
||
func handleGetClusterNode(c *gin.Context) { | ||
karmadaClient := client.InClusterClientForKarmadactlApiServer(c.Param("clustername")) | ||
dataSelect := common.ParseDataSelectPathParameter(c) | ||
result, err := node.GetNodeList(karmadaClient, dataSelect) | ||
if err != nil { | ||
common.Fail(c, err) | ||
return | ||
} | ||
common.Success(c, result) | ||
} | ||
|
||
func init() { | ||
r := router.V1() | ||
r.GET("/node/:clustername", handleGetClusterNode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package node | ||
|
||
import ( | ||
"github.com/karmada-io/dashboard/pkg/dataselect" | ||
api "k8s.io/api/core/v1" | ||
) | ||
|
||
type NodeCell api.Node | ||
|
||
func (self NodeCell) 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) | ||
default: | ||
// if name is not supported then just return a constant dummy value, sort will have no effect. | ||
return nil | ||
} | ||
} | ||
|
||
func toCells(std []api.Node) []dataselect.DataCell { | ||
cells := make([]dataselect.DataCell, len(std)) | ||
for i := range std { | ||
cells[i] = NodeCell(std[i]) | ||
} | ||
return cells | ||
} | ||
|
||
func fromCells(cells []dataselect.DataCell) []api.Node { | ||
std := make([]api.Node, len(cells)) | ||
for i := range std { | ||
std[i] = api.Node(cells[i].(NodeCell)) | ||
} | ||
return std | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package node | ||
|
||
type NodeAllocatedResources struct { | ||
// CPUCapacity is specified node CPU capacity in milicores. | ||
CPUCapacity int64 `json:"cpuCapacity"` | ||
CPUFraction float64 `json:"cpuFraction"` | ||
|
||
// MemoryCapacity is specified node memory capacity in bytes. | ||
MemoryCapacity int64 `json:"memoryCapacity"` | ||
MemoryFraction float64 `json:"memoryFraction"` | ||
|
||
// AllocatedPods in number of currently allocated pods on the node. | ||
AllocatedPods int64 `json:"allocatedPods"` | ||
|
||
// PodCapacity is maximum number of pods, that can be allocated on the node. | ||
PodCapacity int64 `json:"podCapacity"` | ||
|
||
// PodFraction is a fraction of pods, that can be allocated on given node. | ||
PodFraction float64 `json:"podFraction"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package node | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/karmada-io/dashboard/pkg/common/errors" | ||
"github.com/karmada-io/dashboard/pkg/common/types" | ||
"github.com/karmada-io/dashboard/pkg/dataselect" | ||
"github.com/karmada-io/dashboard/pkg/resource/common" | ||
"github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type Node struct { | ||
ObjectMeta types.ObjectMeta `json:"objectMeta"` | ||
TypeMeta types.TypeMeta `json:"typeMeta"` | ||
Ready metav1.ConditionStatus `json:"ready,omitempty"` | ||
KubernetesVersion string `json:"kubernetesVersion,omitempty"` | ||
NodeSummary *v1alpha1.NodeSummary `json:"nodeSummary,omitempty"` | ||
AllocatedResources NodeAllocatedResources `json:"allocatedResources"` | ||
} | ||
|
||
// NodeList contains a list of node. | ||
type NodeList struct { | ||
ListMeta types.ListMeta `json:"listMeta"` | ||
|
||
// Unordered list of Nodes | ||
Items []Node `json:"items"` | ||
|
||
// List of non-critical errors, that occurred during resource retrieval. | ||
Errors []error `json:"errors"` | ||
} | ||
|
||
// GetNodeList returns a list of all Nodes in all cluster. | ||
func GetNodeList(client kubernetes.Interface, dsQuery *dataselect.DataSelectQuery) (*NodeList, error) { | ||
log.Printf("Getting nodes") | ||
channels := &common.ResourceChannels{ | ||
NodeList: common.GetNodeListChannel(client, 1), | ||
} | ||
|
||
return GetNodeListFromChannels(channels, dsQuery) | ||
} | ||
|
||
// GetNodeListFromChannels returns a list of all Nodes in the cluster reading required resource list once from the channels. | ||
func GetNodeListFromChannels(channels *common.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*NodeList, error) { | ||
nodes := <-channels.NodeList.List | ||
err := <-channels.NodeList.Error | ||
nonCriticalErrors, criticalError := errors.ExtractErrors(err) | ||
if criticalError != nil { | ||
return nil, criticalError | ||
} | ||
|
||
result := toNodeList(nodes.Items, nonCriticalErrors, dsQuery) | ||
|
||
return result, nil | ||
} | ||
|
||
func toNode(meta metav1.ObjectMeta) Node { | ||
return Node{ | ||
ObjectMeta: types.NewObjectMeta(meta), | ||
TypeMeta: types.NewTypeMeta(types.ResourceKindNode), | ||
} | ||
} | ||
|
||
func toNodeList(nodes []v1.Node, nonCriticalErrors []error, dsQuery *dataselect.DataSelectQuery) *NodeList { | ||
result := &NodeList{ | ||
Items: make([]Node, 0), | ||
ListMeta: types.ListMeta{TotalItems: len(nodes)}, | ||
Errors: nonCriticalErrors, | ||
} | ||
|
||
nodeCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(nodes), dsQuery) | ||
nodes = fromCells(nodeCells) | ||
result.ListMeta = types.ListMeta{TotalItems: filteredTotal} | ||
|
||
for _, item := range nodes { | ||
result.Items = append(result.Items, toNode(item.ObjectMeta)) | ||
} | ||
|
||
return result | ||
} |