Skip to content

Commit

Permalink
feat: Add transformers defintion for transforming a definition into a…
Browse files Browse the repository at this point in the history
… materialized type
  • Loading branch information
yquansah committed Oct 2, 2023
1 parent 541fba4 commit 9e0538b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (

func New(ctx context.Context, cfg *config.Config) (*api.Configuration, error) {
c := &api.Configuration{
Definitions: containers.MapStore[string, *core.ResourceDefinition]{},
Controllers: containers.MapStore[string, api.Controller]{},
Bindings: containers.MapStore[string, *core.Binding]{},
Definitions: containers.MapStore[string, *core.ResourceDefinition]{},
Controllers: containers.MapStore[string, api.Controller]{},
Bindings: containers.MapStore[string, *core.Binding]{},
Transformers: containers.MapStore[string, *core.Transformer]{},
}

dir := os.DirFS(cfg.API.Resources)
Expand Down Expand Up @@ -109,6 +110,14 @@ func New(ctx context.Context, cfg *config.Config) (*api.Configuration, error) {
}

c.Bindings[binding.Metadata.Name] = &binding
case core.TransformerKind:
var transformer core.Transformer

if err := json.NewDecoder(buf).Decode(&transformer); err != nil {
return fmt.Errorf("parsing transformer: %w", err)
}

c.Transformers[transformer.Spec.Kind] = &transformer
}

return nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/core/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
const (
ResourceDefinitionKind = "ResourceDefinition"
BindingKind = "Binding"
TransformerKind = "Transformer"
)

// Resource is the core API resource definition used to communicate
Expand Down Expand Up @@ -39,3 +40,11 @@ type BindingSpec struct {
Resources []string
Controller string
}

type Transformer Object[TransformerSpec]

type TransformerSpec struct {
Kind string
Controller string
Template string // template to transform a ResourceDefinition into
}
42 changes: 42 additions & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package api

import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"io"
"io/fs"
"log/slog"
Expand Down Expand Up @@ -67,6 +69,7 @@ type Configuration struct {
Definitions containers.MapStore[string, *core.ResourceDefinition]
Controllers containers.MapStore[string, Controller]
Bindings containers.MapStore[string, *core.Binding]
Transformers containers.MapStore[string, *core.Transformer]
TailscaleClient tailscale.Client
}

Expand Down Expand Up @@ -233,6 +236,45 @@ func (s *Server) register(cntl Controller, version string, def *core.ResourceDef
resource.Metadata.Namespace, resource.Metadata.Name,
)

transformer, err := s.cfg.Transformers.Get(resource.Kind)
if err == nil && transformer != nil {
m := map[string]interface{}{}

err := json.Unmarshal(resource.Spec, &m)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

t := template.Must(template.New("").Parse(transformer.Spec.Template))
bb := &bytes.Buffer{}
err = t.Execute(bb, m)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// For transformed values store the transformed payload within the spec, along with the
// definition, that the user has specified.
// Also add a metadata label to mark this resource as transformed. So the clients that
// retrieve the definition are informed.
resource.Metadata.Labels = map[string]string{
"transformed": "yes",
}
a := map[string]json.RawMessage{}

a["definition"] = resource.Spec
a["transformed"] = bb.Bytes()

spec, err := json.Marshal(a)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

resource.Spec = spec
}

result, err := s.fs.Update(r.Context(), s.rev, message, func(f controllers.FSConfig) error {
return cntl.Put(r.Context(), &controllers.PutRequest{
Request: controllers.Request{
Expand Down

0 comments on commit 9e0538b

Please sign in to comment.