Skip to content

Commit

Permalink
feat: added support for degraphql_routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K committed Dec 19, 2024
1 parent 56ae7c1 commit 3997f12
Show file tree
Hide file tree
Showing 17 changed files with 994 additions and 4 deletions.
2 changes: 2 additions & 0 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ func (sc *Syncer) init() error {
types.ServicePackage, types.ServiceVersion, types.Document,

types.FilterChain,

types.DegraphqlRoute,
}

sc.entityDiffers = map[types.EntityType]types.Differ{}
Expand Down
2 changes: 2 additions & 0 deletions pkg/diff/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ L3 +---------------------------> Service <---+ +-> Route |
| | | v |
L4 +----------> Document <---------+ +-> Plugins / <---------+
FilterChains
PluginEntities - DegraphqlRoute
*/

// dependencyOrder defines the order in which entities will be synced by decK.
Expand Down Expand Up @@ -64,6 +65,7 @@ var dependencyOrder = [][]types.EntityType{
types.Plugin,
types.FilterChain,
types.Document,
types.DegraphqlRoute,
},
}

Expand Down
103 changes: 103 additions & 0 deletions pkg/file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (b *stateBuilder) build() (*utils.KongRawState, *utils.KonnectRawState, err
b.consumers()
b.plugins()
b.filterChains()
b.pluginEntities()
b.enterprise()

// konnect
Expand Down Expand Up @@ -1551,6 +1552,108 @@ func filterChainRelations(filterChain *kong.FilterChain) (rID, sID string) {
return
}

func (b *stateBuilder) pluginEntities() {
if b.err != nil {
return
}

supportedPluginEntities := map[string]bool{
degraphqlRoutesType: true,
}

var pluginEntities []FPluginEntity
for _, e := range b.targetContent.PluginEntities {
if !supportedPluginEntities[*e.Type] {
b.err = fmt.Errorf("plugin entity %v is not supported", *e.Type)
return
}

pluginEntities = append(pluginEntities, e)
}

b.ingestPluginEntities(pluginEntities)
}

func (b *stateBuilder) ingestPluginEntities(pluginEntities []FPluginEntity) {
for _, e := range pluginEntities {
switch *e.Type {
case degraphqlRoutesType:
b.ingestDeGraphqlRoute(e)
}
}
}

func (b *stateBuilder) ingestDeGraphqlRoute(degraphqlRouteEntity FPluginEntity) {
degraphqlRoute, err := copyToDegraphqlRoute(degraphqlRouteEntity)
if err != nil {
b.err = err
return
}

if utils.Empty(degraphqlRoute.ID) {
d, err := b.currentState.DegraphqlRoutes.GetByUriQuery(*degraphqlRoute.URI, *degraphqlRoute.Query)
if errors.Is(err, state.ErrNotFound) {
degraphqlRoute.ID = uuid()
} else if err != nil {
b.err = err
return
} else {
degraphqlRoute.ID = kong.String(*d.ID)
}
} else {
degraphqlRoute.ID = kong.String(*degraphqlRoute.ID)
}

b.rawState.DegraphqlRoutes = append(b.rawState.DegraphqlRoutes, &degraphqlRoute.DegraphqlRoute)
}

func copyToDegraphqlRoute(fpEntity FPluginEntity) (DegraphqlRoute, error) {
degraphqlRoute := DegraphqlRoute{}
if fpEntity.ID != nil {
degraphqlRoute.ID = fpEntity.ID
}

if fpEntity.Fields == nil {
return DegraphqlRoute{}, fmt.Errorf("fields are required for degraphql_routes")
}

if fpEntity.Fields["service"] != nil {
if service, ok := fpEntity.Fields["service"].(*string); ok {
degraphqlRoute.Service = &kong.Service{
ID: service,
}
}
}

if fpEntity.Fields["uri"] != nil {
if uri, ok := fpEntity.Fields["uri"].(*string); ok {
degraphqlRoute.URI = uri
}
}

if fpEntity.Fields["query"] != nil {
if query, ok := fpEntity.Fields["query"].(*string); ok {
degraphqlRoute.Query = query
}
}

if fpEntity.Fields["methods"] != nil {
if methods, ok := fpEntity.Fields["methods"].([]*string); ok {
methodsString := make([]string, len(methods))
for i, method := range methods {
methodsString[i] = *method
}
degraphqlRoute.Methods = kong.StringSlice(methodsString...)
}
}

if degraphqlRoute.Methods == nil {
degraphqlRoute.Methods = kong.StringSlice("GET")
}

return degraphqlRoute, nil
}

func defaulter(
ctx context.Context, client *kong.Client, fileContent *Content, disableDynamicDefaults, isKonnect bool,
) (*utils.Defaulter, error) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/file/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func main() {
}
schema.Definitions["MTLSAuth"].Required = []string{"id", "subject_name"}

// plugin entities
schema.Definitions["FPluginEntity"].Required = []string{"type", "plugin"}

// RBAC resources
schema.Definitions["FRBACRole"].Required = []string{"name"}
schema.Definitions["FRBACEndpointPermission"].Required = []string{"workspace", "endpoint"}
Expand Down
38 changes: 34 additions & 4 deletions pkg/file/kong_json_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
},
"type": "array"
},
"plugin_entities": {
"items": {
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/FPluginEntity"
},
"type": "array"
},
"plugins": {
"items": {
"$ref": "#/definitions/FPlugin"
Expand Down Expand Up @@ -745,6 +752,29 @@
"additionalProperties": false,
"type": "object"
},
"FPluginEntity": {
"required": [
"type"
],
"properties": {
"fields": {
"patternProperties": {
".*": {
"additionalProperties": true
}
},
"type": "object"
},
"id": {
"type": "string"
},
"type": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object"
},
"FRBACEndpointPermission": {
"required": [
"workspace",
Expand Down Expand Up @@ -1472,25 +1502,25 @@
},
"LookUpSelectorTags": {
"properties": {
"consumers": {
"consumer_groups": {
"items": {
"type": "string"
},
"type": "array"
},
"routes": {
"consumers": {
"items": {
"type": "string"
},
"type": "array"
},
"services": {
"routes": {
"items": {
"type": "string"
},
"type": "array"
},
"consumer_groups": {
"services": {
"items": {
"type": "string"
},
Expand Down
Loading

0 comments on commit 3997f12

Please sign in to comment.