-
Notifications
You must be signed in to change notification settings - Fork 0
feat: relations #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vertex451
wants to merge
13
commits into
main
Choose a base branch
from
relations-new
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,101
−69
Open
feat: relations #303
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ad375ed
feat: relations
vertex451 20546e2
removed hardcoded versions
vertex451 249f80a
simplidied buildKindRegistry
vertex451 96a22a2
consistentcy
vertex451 de3c7a5
resolver
vertex451 0787ae7
removed circular dep from gateway
vertex451 44afd8b
better sorting
vertex451 9f14440
smart sorting with preffered resource first
vertex451 5cd1b84
use GVK instead of Kind as a kay
vertex451 a9a360c
add test with preferred resource
vertex451 54b8d92
use map native methods
vertex451 f286765
nesting
vertex451 c2bca5b
limit relation resolver by getItem only
vertex451 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,103 @@ | ||
package resolver | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/graphql-go/graphql" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// RelationResolver handles runtime resolution of relation fields | ||
type RelationResolver struct { | ||
service *Service | ||
} | ||
|
||
// NewRelationResolver creates a new relation resolver | ||
func NewRelationResolver(service *Service) *RelationResolver { | ||
return &RelationResolver{ | ||
service: service, | ||
} | ||
} | ||
|
||
// CreateResolver creates a GraphQL resolver for relation fields | ||
func (rr *RelationResolver) CreateResolver(fieldName string, targetGVK schema.GroupVersionKind) graphql.FieldResolveFn { | ||
return func(p graphql.ResolveParams) (interface{}, error) { | ||
parentObj, ok := p.Source.(map[string]interface{}) | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
refInfo := rr.extractReferenceInfo(parentObj, fieldName) | ||
if refInfo.name == "" { | ||
return nil, nil | ||
} | ||
|
||
return rr.resolveReference(p.Context, refInfo, targetGVK) | ||
} | ||
} | ||
|
||
// referenceInfo holds extracted reference details | ||
type referenceInfo struct { | ||
name string | ||
namespace string | ||
kind string | ||
apiGroup string | ||
} | ||
|
||
// extractReferenceInfo extracts reference details from a *Ref object | ||
func (rr *RelationResolver) extractReferenceInfo(parentObj map[string]interface{}, fieldName string) referenceInfo { | ||
name, _ := parentObj["name"].(string) | ||
if name == "" { | ||
return referenceInfo{} | ||
} | ||
|
||
namespace, _ := parentObj["namespace"].(string) | ||
apiGroup, _ := parentObj["apiGroup"].(string) | ||
|
||
kind, _ := parentObj["kind"].(string) | ||
if kind == "" { | ||
// Fallback: infer kind from field name (e.g., "role" -> "Role") | ||
kind = cases.Title(language.English).String(fieldName) | ||
} | ||
|
||
return referenceInfo{ | ||
name: name, | ||
namespace: namespace, | ||
kind: kind, | ||
apiGroup: apiGroup, | ||
} | ||
} | ||
|
||
// resolveReference fetches a referenced Kubernetes resource using provided target GVK | ||
func (rr *RelationResolver) resolveReference(ctx context.Context, ref referenceInfo, targetGVK schema.GroupVersionKind) (interface{}, error) { | ||
gvk := targetGVK | ||
|
||
// Allow overrides from the reference object if specified | ||
if ref.apiGroup != "" { | ||
gvk.Group = ref.apiGroup | ||
} | ||
if ref.kind != "" { | ||
gvk.Kind = ref.kind | ||
} | ||
|
||
// Convert sanitized group to original before calling the client | ||
gvk.Group = rr.service.getOriginalGroupName(gvk.Group) | ||
|
||
obj := &unstructured.Unstructured{} | ||
obj.SetGroupVersionKind(gvk) | ||
|
||
key := client.ObjectKey{Name: ref.name} | ||
if ref.namespace != "" { | ||
key.Namespace = ref.namespace | ||
} | ||
|
||
if err := rr.service.runtimeClient.Get(ctx, key, obj); err == nil { | ||
return obj.Object, nil | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains hidden or 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 hidden or 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,153 @@ | ||
package schema | ||
|
||
import ( | ||
"strings" | ||
|
||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
|
||
"github.com/go-openapi/spec" | ||
"github.com/graphql-go/graphql" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
// RelationEnhancer handles schema enhancement for relation fields | ||
type RelationEnhancer struct { | ||
gateway *Gateway | ||
} | ||
|
||
// NewRelationEnhancer creates a new relation enhancer | ||
func NewRelationEnhancer(gateway *Gateway) *RelationEnhancer { | ||
return &RelationEnhancer{ | ||
gateway: gateway, | ||
} | ||
} | ||
|
||
// AddRelationFields adds relation fields to schemas that contain *Ref fields | ||
func (re *RelationEnhancer) AddRelationFields(fields graphql.Fields, properties map[string]spec.Schema) { | ||
for fieldName := range properties { | ||
if !strings.HasSuffix(fieldName, "Ref") { | ||
continue | ||
} | ||
|
||
baseName := strings.TrimSuffix(fieldName, "Ref") | ||
sanitizedFieldName := sanitizeFieldName(fieldName) | ||
|
||
refField, exists := fields[sanitizedFieldName] | ||
if !exists { | ||
continue | ||
} | ||
|
||
enhancedType := re.enhanceRefTypeWithRelation(refField.Type, baseName) | ||
if enhancedType == nil { | ||
continue | ||
} | ||
|
||
fields[sanitizedFieldName] = &graphql.Field{ | ||
Type: enhancedType, | ||
} | ||
} | ||
} | ||
|
||
// enhanceRefTypeWithRelation adds a relation field to a *Ref object type | ||
func (re *RelationEnhancer) enhanceRefTypeWithRelation(originalType graphql.Output, baseName string) graphql.Output { | ||
objType, ok := originalType.(*graphql.Object) | ||
if !ok { | ||
return originalType | ||
} | ||
|
||
cacheKey := objType.Name() + "_" + baseName + "_Enhanced" | ||
if enhancedType, exists := re.gateway.enhancedTypesCache[cacheKey]; exists { | ||
return enhancedType | ||
} | ||
|
||
enhancedFields := re.copyOriginalFields(objType.Fields()) | ||
re.addRelationField(enhancedFields, baseName) | ||
|
||
enhancedType := graphql.NewObject(graphql.ObjectConfig{ | ||
Name: sanitizeFieldName(cacheKey), | ||
Fields: enhancedFields, | ||
}) | ||
|
||
re.gateway.enhancedTypesCache[cacheKey] = enhancedType | ||
return enhancedType | ||
} | ||
|
||
// copyOriginalFields converts FieldDefinition to Field for reuse | ||
func (re *RelationEnhancer) copyOriginalFields(originalFieldDefs graphql.FieldDefinitionMap) graphql.Fields { | ||
enhancedFields := make(graphql.Fields, len(originalFieldDefs)) | ||
for fieldName, fieldDef := range originalFieldDefs { | ||
enhancedFields[fieldName] = &graphql.Field{ | ||
Type: fieldDef.Type, | ||
Description: fieldDef.Description, | ||
Resolve: fieldDef.Resolve, | ||
} | ||
} | ||
return enhancedFields | ||
} | ||
|
||
// addRelationField adds a single relation field to the enhanced fields | ||
func (re *RelationEnhancer) addRelationField(enhancedFields graphql.Fields, baseName string) { | ||
targetType, targetGVK, ok := re.findRelationTarget(baseName) | ||
if !ok { | ||
return | ||
} | ||
|
||
sanitizedBaseName := sanitizeFieldName(baseName) | ||
enhancedFields[sanitizedBaseName] = &graphql.Field{ | ||
Type: targetType, | ||
Resolve: re.gateway.resolver.RelationResolver(baseName, *targetGVK), | ||
} | ||
} | ||
|
||
// findRelationTarget locates the GraphQL output type and its GVK for a relation target | ||
func (re *RelationEnhancer) findRelationTarget(baseName string) (graphql.Output, *schema.GroupVersionKind, bool) { | ||
targetKind := cases.Title(language.English).String(baseName) | ||
|
||
for defKey, defSchema := range re.gateway.definitions { | ||
if re.matchesTargetKind(defSchema, targetKind) { | ||
// Resolve or build the GraphQL type | ||
var fieldType graphql.Output | ||
if existingType, exists := re.gateway.typesCache[defKey]; exists { | ||
fieldType = existingType | ||
} else { | ||
ft, _, err := re.gateway.convertSwaggerTypeToGraphQL(defSchema, defKey, []string{}, make(map[string]bool)) | ||
if err != nil { | ||
continue | ||
} | ||
fieldType = ft | ||
} | ||
|
||
// Extract GVK from the schema definition | ||
gvk, err := re.gateway.getGroupVersionKind(defKey) | ||
if err != nil || gvk == nil { | ||
continue | ||
} | ||
|
||
return fieldType, gvk, true | ||
} | ||
} | ||
|
||
return nil, nil, false | ||
} | ||
|
||
// matchesTargetKind checks if a schema definition matches the target kind | ||
func (re *RelationEnhancer) matchesTargetKind(defSchema spec.Schema, targetKind string) bool { | ||
gvkExt, ok := defSchema.Extensions["x-kubernetes-group-version-kind"] | ||
if !ok { | ||
return false | ||
} | ||
|
||
gvkSlice, ok := gvkExt.([]any) | ||
if !ok || len(gvkSlice) == 0 { | ||
return false | ||
} | ||
|
||
gvkMap, ok := gvkSlice[0].(map[string]any) | ||
if !ok { | ||
return false | ||
} | ||
|
||
kind, ok := gvkMap["kind"].(string) | ||
return ok && kind == targetKind | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.