diff --git a/cmd/injector.go b/cmd/injector.go index 951ea2d..41f9469 100644 --- a/cmd/injector.go +++ b/cmd/injector.go @@ -13,44 +13,25 @@ import ( ) var ( - DictRegex = regexp.MustCompile(`(\$\()([^)]+)(\))`) + KeyRegex = regexp.MustCompile(`(\$\()([^)]+)(\))`) ) -// Field injection specification. -type Field struct { - Name string `json:"name"` - Path string `json:"path"` - Key string `json:"key"` +// SelectorNotSupported used to report not supported. +type SelectorNotSupported struct { + Selector string } -// Injector resource injection specification. -type Injector struct { - Kind string `json:"kind"` - Fields []Field `json:"fields"` -} - -// Metadata for provider extensions. -type Metadata struct { - Resources []Injector `json:"resources,omitempty"` - Provider provider.Config `json:"provider"` +func (e *SelectorNotSupported) Error() (s string) { + return fmt.Sprintf("Resource selector='%s', not-supported.", e.Selector) } -// UnknownInjector used to report an unknown injector. -type UnknownInjector struct { - Kind string -} - -func (e *UnknownInjector) Error() (s string) { - return fmt.Sprintf("Resource injector: kind=%s, unknown.", e.Kind) -} - -func (e *UnknownInjector) Is(err error) (matched bool) { - var inst *UnknownInjector +func (e *SelectorNotSupported) Is(err error) (matched bool) { + var inst *SelectorNotSupported matched = errors.As(err, &inst) return } -// FieldNotMatched used to report an un-matched resource field. +// FieldNotMatched used to report resource field not matched. type FieldNotMatched struct { Kind string Field string @@ -66,6 +47,53 @@ func (e *FieldNotMatched) Is(err error) (matched bool) { return } +// Field injection specification. +type Field struct { + Name string `json:"name"` + Path string `json:"path"` + Key string `json:"key"` +} + +// Resource injection specification. +// Format: := +type Resource struct { + Selector string `json:"selector"` + Fields []Field `json:"fields"` +} + +// Metadata for provider extensions. +type Metadata struct { + Resources []Resource `json:"resources,omitempty"` + Provider provider.Config `json:"provider"` +} + +// ParsedSelector - +type ParsedSelector struct { + ns string + kind string + name string + value string +} + +// With parses and populates the selector. +func (p *ParsedSelector) With(s string) { + part := strings.SplitN(s, "/", 2) + if len(part) > 1 { + p.ns = part[0] + s = part[1] + } + part = strings.SplitN(s, ":", 2) + if len(part) > 1 { + p.kind = part[0] + s = part[1] + } + part = strings.SplitN(s, "=", 2) + p.name = part[0] + if len(part) > 1 { + p.value = part[1] + } +} + // ResourceInjector inject resources into extension metadata. type ResourceInjector struct { dict map[string]string @@ -101,27 +129,24 @@ func (r *ResourceInjector) build(md *Metadata) (err error) { if err != nil { return } - for _, injector := range md.Resources { - parsed := strings.Split(injector.Kind, "=") - switch strings.ToLower(parsed[0]) { + for _, resource := range md.Resources { + parsed := ParsedSelector{} + parsed.With(resource.Selector) + switch strings.ToLower(parsed.kind) { case "identity": - kind := "" - if len(parsed) > 1 { - kind = parsed[1] - } - identity, found, nErr := addon.Application.FindIdentity(application.ID, kind) + identity, found, nErr := addon.Application.FindIdentity(application.ID, parsed.value) if nErr != nil { err = nErr return } if found { - err = r.add(&injector, identity) + err = r.add(&resource, identity) if err != nil { return } } default: - err = &UnknownInjector{Kind: parsed[0]} + err = &SelectorNotSupported{Selector: resource.Selector} return } } @@ -129,12 +154,15 @@ func (r *ResourceInjector) build(md *Metadata) (err error) { } // add the resource fields specified in the injector. -func (r *ResourceInjector) add(injector *Injector, object any) (err error) { +func (r *ResourceInjector) add(resource *Resource, object any) (err error) { mp := r.asMap(object) - for _, f := range injector.Fields { + for _, f := range resource.Fields { v, found := mp[f.Name] if !found { - err = &FieldNotMatched{Kind: injector.Kind, Field: f.Name} + err = &FieldNotMatched{ + Kind: resource.Selector, + Field: f.Name, + } return } fv := r.string(v) @@ -154,7 +182,7 @@ func (r *ResourceInjector) add(injector *Injector, object any) (err error) { func (r *ResourceInjector) write(path string, s string) (err error) { f, err := os.Create(path) if err == nil { - _, _ = f.Write([]byte(s)) + _, err = f.Write([]byte(s)) _ = f.Close() } return @@ -201,7 +229,7 @@ func (r *ResourceInjector) inject(in any) (out any) { out = injected case string: for { - match := DictRegex.FindStringSubmatch(node) + match := KeyRegex.FindStringSubmatch(node) if len(match) < 3 { break } diff --git a/go.mod b/go.mod index a5e222a..c2de3da 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed ) -replace github.com/konveyor/tackle2-hub => github.com/jortel/tackle2-hub v0.0.0-20240325221104-71edd2377f6a +replace github.com/konveyor/tackle2-hub => github.com/jortel/tackle2-hub v0.0.0-20240326125423-9b302bd0febe require ( github.com/Nerzal/gocloak/v10 v10.0.1 // indirect diff --git a/go.sum b/go.sum index 18e8c21..4eeb297 100644 --- a/go.sum +++ b/go.sum @@ -128,8 +128,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jortel/go-utils v0.1.2 h1:R0TcGRCcwoL793CymcKC5AF9idWXT2cR6eQ2xpBUsoI= github.com/jortel/go-utils v0.1.2/go.mod h1:sl6vav63ODI0sUfSz8e0pImNmCVFnVsuOFhZmwe9GDk= -github.com/jortel/tackle2-hub v0.0.0-20240325221104-71edd2377f6a h1:XIUvuP5lLEgF5WcvJVLUMfTDVCKaxE2yNvQQnuqITdg= -github.com/jortel/tackle2-hub v0.0.0-20240325221104-71edd2377f6a/go.mod h1:3FinnKk81rJdWAHDCSFxkcfDK/nnqY3tJdgB9uaABsM= +github.com/jortel/tackle2-hub v0.0.0-20240326125423-9b302bd0febe h1:2i6mpDF1UkcrSZE1GSaTNsPQTAgoVy0y62trsRNnnyA= +github.com/jortel/tackle2-hub v0.0.0-20240326125423-9b302bd0febe/go.mod h1:3FinnKk81rJdWAHDCSFxkcfDK/nnqY3tJdgB9uaABsM= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= diff --git a/hack/addon.yaml b/hack/addon.yaml index 3fee41c..011608f 100644 --- a/hack/addon.yaml +++ b/hack/addon.yaml @@ -36,7 +36,7 @@ spec: value: $(port:8000) metadata: resources: - - kind: identity=maven + - kind: identity:kind=maven fields: - name: settings path: /tmp/settings.xml