From 9b11e8c3bd590a201c3939966aec0c020e89e855 Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Mon, 25 Mar 2024 15:02:34 -0700 Subject: [PATCH] checkpoint Signed-off-by: Jeff Ortel --- addon/adapter.go | 6 ++ addon/injector.go | 144 ++-------------------------------------------- addon/task.go | 16 +++--- 3 files changed, 20 insertions(+), 146 deletions(-) diff --git a/addon/adapter.go b/addon/adapter.go index 6f5d8f147..8e623ea08 100644 --- a/addon/adapter.go +++ b/addon/adapter.go @@ -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) diff --git a/addon/injector.go b/addon/injector.go index 268c3d95d..898966957 100644 --- a/addon/injector.go +++ b/addon/injector.go @@ -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) @@ -153,7 +32,7 @@ func (r *MetaInjector) Inject(extension *api.Extension) { // buildEnv builds the `env`. // Maps EXTENSION__ 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( @@ -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 { @@ -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 { diff --git a/addon/task.go b/addon/task.go index de39deeae..2ffaa46d1 100644 --- a/addon/task.go +++ b/addon/task.go @@ -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 @@ -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 }