Skip to content

Commit

Permalink
Merge pull request #902 from bjwswang/main
Browse files Browse the repository at this point in the history
feat: list cluster nodes with labels
  • Loading branch information
bjwswang authored Mar 20, 2024
2 parents c386035 + 6bce308 commit 6e64f1d
Show file tree
Hide file tree
Showing 8 changed files with 660 additions and 2 deletions.
487 changes: 486 additions & 1 deletion apiserver/graph/generated/generated.go

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions apiserver/graph/generated/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions apiserver/graph/impl/node.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apiserver/graph/schema/entrypoint.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ type Tool {
params: Map
}

union PageNode = Datasource | Model | Embedder | KnowledgeBase | Dataset | VersionedDataset | F | Worker | ApplicationMetadata | LLM | ModelService | RayCluster | RAG | GPT
union PageNode = Datasource | Model | Embedder | KnowledgeBase | Dataset | VersionedDataset | F | Worker | ApplicationMetadata | LLM | ModelService | RayCluster | RAG | GPT | Node
15 changes: 15 additions & 0 deletions apiserver/graph/schema/node.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query listNodes($input: ListNodeInput) {
Node {
listNodes(input: $input) {
totalCount
hasNextPage
nodes {
__typename
... on Node {
name
labels
}
}
}
}
}
31 changes: 31 additions & 0 deletions apiserver/graph/schema/node.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

type Node {
name: String!
labels: Map
}


input ListNodeInput {
"""标签选择器"""
labelSelector: String

"""
分页页码,
规则: 从1开始,默认是1
"""
page: Int

"""
每页数量,
规则: -1,返回全部
"""
pageSize: Int
}

type NodeQuery {
listNodes(input: ListNodeInput): PaginatedResult!
}

extend type Query {
Node: NodeQuery
}
70 changes: 70 additions & 0 deletions apiserver/pkg/node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2024 KubeAGI.
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 node

import (
"context"
"errors"

corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubeagi/arcadia/apiserver/graph/generated"
"github.com/kubeagi/arcadia/apiserver/pkg/common"
graphqlutils "github.com/kubeagi/arcadia/apiserver/pkg/utils"
)

// obj2nodeConverter converts k8s object to page node
func obj2nodeConverter(obj client.Object) (generated.PageNode, error) {
node, ok := obj.(*corev1.Node)
if !ok {
return nil, errors.New("can't convert object to Model")
}
n := &generated.Node{
Name: node.Name,
Labels: graphqlutils.MapStr2Any(node.GetLabels()),
}

return n, nil
}

// ListK8sNodes list k8s nodes by filter
func ListK8sNodes(ctx context.Context, c client.Client, input generated.ListNodeInput) (*generated.PaginatedResult, error) {
page := pointer.IntDeref(input.Page, 1)
pageSize := pointer.IntDeref(input.PageSize, -1)

// list nodes
nodes := &corev1.NodeList{}
opts, err := common.NewListOptions(generated.ListCommonInput{
LabelSelector: input.LabelSelector,
})
if err != nil {
return nil, err
}
err = c.List(ctx, nodes, opts...)
if err != nil {
return nil, err
}

items := make([]client.Object, len(nodes.Items))
for i := range nodes.Items {
items[i] = &nodes.Items[i]
}

return common.ListReources(items, page, pageSize, obj2nodeConverter)
}
4 changes: 4 additions & 0 deletions gqlgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,7 @@ models:
resolver: true
listGPT:
resolver: true
NodeQuery:
fields:
listNodes:
resolver: true

0 comments on commit 6e64f1d

Please sign in to comment.