feat: add spec definition api with versioning
All checks were successful
arcad/emissary/pipeline/head This commit looks good
arcad/emissary/pipeline/pr-master This commit looks good

This commit is contained in:
2024-03-12 16:22:35 +01:00
parent 0b34b485da
commit f612721b4e
69 changed files with 1468 additions and 273 deletions

View File

@ -38,7 +38,7 @@ func (c *Controller) Name() string {
func (c *Controller) Reconcile(ctx context.Context, state *agent.State) error {
appSpec := spec.NewSpec()
if err := state.GetSpec(spec.Name, appSpec); err != nil {
if err := state.GetSpec(spec.Name, spec.Version, appSpec); err != nil {
if errors.Is(err, agent.ErrSpecNotFound) {
logger.Info(ctx, "could not find app spec")
@ -50,7 +50,12 @@ func (c *Controller) Reconcile(ctx context.Context, state *agent.State) error {
return errors.WithStack(err)
}
logger.Info(ctx, "retrieved spec", logger.F("spec", appSpec.SpecName()), logger.F("revision", appSpec.SpecRevision()))
logger.Info(
ctx, "retrieved spec",
logger.F("name", appSpec.SpecDefinitionName()),
logger.F("version", appSpec.SpecDefinitionVersion()),
logger.F("revision", appSpec.SpecRevision()),
)
c.updateApps(ctx, appSpec)

View File

@ -11,7 +11,7 @@ import (
var schema []byte
func init() {
if err := spec.Register(Name, schema); err != nil {
if err := spec.Register(string(Name), Version, schema); err != nil {
panic(errors.WithStack(err))
}
}

View File

@ -1,5 +1,5 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://app.edge.emissary.cadoles.com/spec.json",
"title": "AppSpec",
"description": "Emissary 'App' specification",
@ -78,7 +78,9 @@
"type": "string"
}
},
"required": ["defaultUrlTemplate"],
"required": [
"defaultUrlTemplate"
],
"additionalProperties": false
},
"unexpectedHostRedirect": {
@ -94,7 +96,10 @@
"type": "string"
}
},
"required": ["acceptedHostPatterns", "hostTarget"],
"required": [
"acceptedHostPatterns",
"hostTarget"
],
"additionalProperties": false
},
"auth": {
@ -104,7 +109,10 @@
"type": "object",
"properties": {
"key": {
"type": ["object", "string"]
"type": [
"object",
"string"
]
},
"signingAlgorithm": {
"type": "string"

View File

@ -6,7 +6,10 @@ import (
"github.com/lestrrat-go/jwx/v2/jwa"
)
const Name spec.Name = "app.emissary.cadoles.com"
const (
Name string = "app.emissary.cadoles.com"
Version string = "0.0.0"
)
type Spec struct {
Revision int `json:"revision"`
@ -56,10 +59,14 @@ type AppURLResolving struct {
DefaultURLTemplate string `json:"defaultUrlTemplate"`
}
func (s *Spec) SpecName() spec.Name {
func (s *Spec) SpecDefinitionName() string {
return Name
}
func (s *Spec) SpecDefinitionVersion() string {
return Version
}
func (s *Spec) SpecRevision() int {
return s.Revision
}

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"testing"
"forge.cadoles.com/Cadoles/emissary/internal/datastore/memory"
"forge.cadoles.com/Cadoles/emissary/internal/spec"
"github.com/pkg/errors"
)
@ -27,11 +28,15 @@ var validatorTestCases = []validatorTestCase{
func TestValidator(t *testing.T) {
t.Parallel()
validator := spec.NewValidator()
if err := validator.Register(Name, schema); err != nil {
ctx := context.Background()
repo := memory.NewSpecDefinitionRepository()
if _, err := repo.Upsert(ctx, Name, Version, schema); err != nil {
t.Fatalf("+%v", errors.WithStack(err))
}
validator := spec.NewValidator(repo)
for _, tc := range validatorTestCases {
func(tc validatorTestCase) {
t.Run(tc.Name, func(t *testing.T) {