-
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
Merged
Merged
feat: relations #303
Changes from 3 commits
Commits
Show all changes
23 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 e578071
fix conflict
vertex451 b5d9b0b
reproduced kubeconfig conflict resolution logic
vertex451 80f107b
Merge branch 'main' of github.com:openmfp/kubernetes-graphql-gateway …
vertex451 2ece5fa
fix tests
vertex451 e5b5c8f
fix tests 2
vertex451 ba51492
fixed getOperationFromContext bug
vertex451 8897cdf
removed unworked code
vertex451 ce396e4
reduced diff
vertex451 09b357b
improved WithCRDCategories
vertex451 b6d83af
simplified depth control
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ import ( | |
"strings" | ||
|
||
"github.com/graphql-go/graphql" | ||
pkgErrors "github.com/pkg/errors" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
|
@@ -30,6 +29,7 @@ type Provider interface { | |
CustomQueriesProvider | ||
CommonResolver() graphql.FieldResolveFn | ||
SanitizeGroupName(string) string | ||
RelationResolver(fieldName string, gvk schema.GroupVersionKind) graphql.FieldResolveFn | ||
} | ||
|
||
type CrudProvider interface { | ||
|
@@ -50,16 +50,22 @@ type CustomQueriesProvider interface { | |
type Service struct { | ||
log *logger.Logger | ||
// groupNames stores relation between sanitized group names and original group names that are used in the Kubernetes API | ||
groupNames map[string]string // map[sanitizedGroupName]originalGroupName | ||
runtimeClient client.WithWatch | ||
groupNames map[string]string // map[sanitizedGroupName]originalGroupName | ||
runtimeClient client.WithWatch | ||
relationResolver *RelationResolver | ||
} | ||
|
||
func New(log *logger.Logger, runtimeClient client.WithWatch) *Service { | ||
return &Service{ | ||
s := &Service{ | ||
log: log, | ||
groupNames: make(map[string]string), | ||
runtimeClient: runtimeClient, | ||
} | ||
|
||
// Initialize the relation resolver | ||
s.relationResolver = NewRelationResolver(s) | ||
pteich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return s | ||
} | ||
|
||
// ListItems returns a GraphQL CommonResolver function that lists Kubernetes resources of the given GroupVersionKind. | ||
|
@@ -82,16 +88,16 @@ func (r *Service) ListItems(gvk schema.GroupVersionKind, scope v1.ResourceScope) | |
log = r.log | ||
} | ||
|
||
// Create an unstructured list to hold the results | ||
// Create a list of unstructured objects to hold the results | ||
list := &unstructured.UnstructuredList{} | ||
list.SetGroupVersionKind(gvk) | ||
list.SetGroupVersionKind(schema.GroupVersionKind{Group: gvk.Group, Version: gvk.Version, Kind: gvk.Kind + "List"}) | ||
|
||
var opts []client.ListOption | ||
// Handle label selector argument | ||
if labelSelector, ok := p.Args[LabelSelectorArg].(string); ok && labelSelector != "" { | ||
selector, err := labels.Parse(labelSelector) | ||
|
||
if val, ok := p.Args[LabelSelectorArg].(string); ok && val != "" { | ||
selector, err := labels.Parse(val) | ||
if err != nil { | ||
log.Error().Err(err).Str(LabelSelectorArg, labelSelector).Msg("Unable to parse given label selector") | ||
log.Error().Err(err).Str(LabelSelectorArg, val).Msg("Unable to parse given label selector") | ||
return nil, err | ||
} | ||
opts = append(opts, client.MatchingLabelsSelector{Selector: selector}) | ||
|
@@ -108,25 +114,25 @@ func (r *Service) ListItems(gvk schema.GroupVersionKind, scope v1.ResourceScope) | |
} | ||
|
||
if err = r.runtimeClient.List(ctx, list, opts...); err != nil { | ||
log.Error().Err(err).Msg("Unable to list objects") | ||
return nil, pkgErrors.Wrap(err, "unable to list objects") | ||
log.Error().Err(err).Str("scope", string(scope)).Msg("Unable to list objects") | ||
return nil, err | ||
} | ||
|
||
sortBy, err := getStringArg(p.Args, SortByArg, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = validateSortBy(list.Items, sortBy) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this issue, but we should sort only in case of sortBy param only |
||
if err != nil { | ||
log.Error().Err(err).Str(SortByArg, sortBy).Msg("Invalid sortBy field path") | ||
return nil, err | ||
if sortBy != "" { | ||
if err := validateSortBy(list.Items, sortBy); err != nil { | ||
log.Error().Err(err).Str(SortByArg, sortBy).Msg("Invalid sortBy field path") | ||
return nil, err | ||
} | ||
sort.Slice(list.Items, func(i, j int) bool { | ||
return compareUnstructured(list.Items[i], list.Items[j], sortBy) < 0 | ||
}) | ||
} | ||
|
||
sort.Slice(list.Items, func(i, j int) bool { | ||
return compareUnstructured(list.Items[i], list.Items[j], sortBy) < 0 | ||
}) | ||
|
||
items := make([]map[string]any, len(list.Items)) | ||
for i, item := range list.Items { | ||
items[i] = item.Object | ||
|
@@ -456,3 +462,8 @@ func compareNumbers[T int64 | float64](a, b T) int { | |
return 0 | ||
} | ||
} | ||
|
||
// RelationResolver creates a GraphQL resolver for relation fields | ||
func (r *Service) RelationResolver(fieldName string, gvk schema.GroupVersionKind) graphql.FieldResolveFn { | ||
return r.relationResolver.CreateResolver(fieldName, gvk) | ||
} |
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.