Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Mar 25, 2024
1 parent 34b808d commit 9b11e8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 146 deletions.
6 changes: 6 additions & 0 deletions addon/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ func (h *Adapter) Run(addon func() error) {
}
}

// Client returns the binding rich-client.
func (h *Adapter) Client() (client binding.Client) {
client = *h.client
return
}

// newAdapter builds a new Addon Adapter object.
func newAdapter() (adapter *Adapter) {
richClient := binding.New(Settings.Addon.Hub.URL)
Expand Down
144 changes: 6 additions & 138 deletions addon/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,146 +2,25 @@ package addon

import (
"encoding/json"
"errors"
"fmt"
"os"
"regexp"
"strings"

"github.com/konveyor/tackle2-hub/api"
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
)

var (
EnvRegex = regexp.MustCompile(`(\${)([^}]+)(})`)
DictRegex = regexp.MustCompile(`(\$\()([^)]+)(\))`)
EnvRegex = regexp.MustCompile(`(\${)([^}]+)(})`)
)

// UnknownInjector used to report an unknown injector.
type UnknownInjector struct {
Kind string
}

func (e *UnknownInjector) Error() (s string) {
return fmt.Sprintf("Injector: %s, unknown.", e.Kind)
}

func (e *UnknownInjector) Is(err error) (matched bool) {
var inst *UnknownInjector
matched = errors.As(err, &inst)
return
}

// UnknownField used to report an unknown resource field..
type UnknownField struct {
Kind string
Field string
}

func (e *UnknownField) Error() (s string) {
return fmt.Sprintf("Field: %s.%s, unknown.", e.Kind, e.Field)
}

func (e *UnknownField) Is(err error) (matched bool) {
var inst *UnknownField
matched = errors.As(err, &inst)
return
}

// ResourceInjector supports hub resource injection.
type ResourceInjector struct {
dict map[string]string
}

// Build returns the built resource dictionary.
func (r *ResourceInjector) Build(task *Task, extension *api.Extension) (dict map[string]string, err error) {
richClient := task.richClient
r.dict = make(map[string]string)
for _, injector := range extension.Resources {
parsed := strings.Split(injector.Kind, "=")
switch strings.ToLower(parsed[0]) {
case "identity":
kind := ""
if len(parsed) > 1 {
kind = parsed[1]
}
id := task.task.Application.ID
identity, found, nErr := richClient.Application.FindIdentity(id, kind)
if nErr != nil {
err = nErr
return
}
if found {
err = r.add(&injector, identity)
if err != nil {
return
}
}
default:
err = &UnknownInjector{Kind: parsed[0]}
return
}
}
dict = r.dict
return
}

// add the resource fields specified in the injector.
func (r *ResourceInjector) add(injector *crd.Injector, object any) (err error) {
objectMap := r.objectMap(object)
for _, f := range injector.Fields {
v, found := objectMap[f.Name]
if !found {
err = &UnknownField{Kind: injector.Kind, Field: f.Name}
return
}
fv := r.string(v)
if f.Path != "" {
err = r.write(f.Path, fv)
if err != nil {
return
}
fv = f.Path
}
r.dict[f.Key] = fv
}
return
}

// write a resource field value to a file.
func (r *ResourceInjector) write(path string, s string) (err error) {
f, err := os.Create(path)
if err == nil {
_, _ = f.Write([]byte(s))
_ = f.Close()
}
return
}

// string returns a string representation of a field value.
func (r *ResourceInjector) string(object any) (s string) {
if object != nil {
s = fmt.Sprintf("%v", object)
}
return
}

// objectMap returns a map for a resource object.
func (r *ResourceInjector) objectMap(object any) (mp map[string]any) {
b, _ := json.Marshal(object)
mp = make(map[string]any)
_ = json.Unmarshal(b, &mp)
return
}

// MetaInjector inject key into extension metadata.
type MetaInjector struct {
// EnvInjector inject key into extension metadata.
type EnvInjector struct {
env map[string]string
dict map[string]string
}

// Inject inject into extension metadata.
func (r *MetaInjector) Inject(extension *api.Extension) {
func (r *EnvInjector) Inject(extension *api.Extension) {
r.buildEnv(extension)
mp := make(map[string]any)
b, _ := json.Marshal(extension.Metadata)
Expand All @@ -153,7 +32,7 @@ func (r *MetaInjector) Inject(extension *api.Extension) {
// buildEnv builds the `env`.
// Maps EXTENSION_<extension>_<envar> found in the addon environment to its
// original unqualified name in the extension environment.
func (r *MetaInjector) buildEnv(extension *api.Extension) {
func (r *EnvInjector) buildEnv(extension *api.Extension) {
r.env = make(map[string]string)
for _, env := range extension.Container.Env {
key := strings.Join(
Expand All @@ -169,7 +48,7 @@ func (r *MetaInjector) buildEnv(extension *api.Extension) {

// inject replaces both `dict` keys and `env` environment
// variables referenced in metadata.
func (r *MetaInjector) inject(in any) (out any) {
func (r *EnvInjector) inject(in any) (out any) {
switch node := in.(type) {
case map[string]any:
for k, v := range node {
Expand All @@ -185,17 +64,6 @@ func (r *MetaInjector) inject(in any) (out any) {
}
out = injected
case string:
for {
match := DictRegex.FindStringSubmatch(node)
if len(match) < 3 {
break
}
node = strings.Replace(
node,
match[0],
r.dict[match[2]],
-1)
}
for {
match := EnvRegex.FindStringSubmatch(node)
if len(match) < 3 {
Expand Down
16 changes: 8 additions & 8 deletions addon/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (h *Task) Load() {
return
}

// Task returns the embedded resource.
func (h *Task) Task() (r api.Task) {
r = *h.task
return
}

// Application returns the application associated with the task.
func (h *Task) Application() (r *api.Application, err error) {
appRef := h.task.Application
Expand Down Expand Up @@ -59,14 +65,8 @@ func (h *Task) Addon(inject bool) (r *api.Addon, err error) {
}
for i := range r.Extensions {
extension := &r.Extensions[i]
rj := ResourceInjector{}
var dict map[string]string
dict, err = rj.Build(h, extension)
if err != nil {
return
}
mj := MetaInjector{dict: dict}
mj.Inject(extension)
injector := EnvInjector{}
injector.Inject(extension)
}
return
}
Expand Down

0 comments on commit 9b11e8c

Please sign in to comment.