Skip to content
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

feat: Add Transformer type #69

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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