Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <jortel@redhat.com>
  • Loading branch information
jortel committed Mar 26, 2024
1 parent fa4ac9d commit 26b2fab
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 47 deletions.
114 changes: 71 additions & 43 deletions cmd/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: <kind>:<key>=<value>
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
Expand Down Expand Up @@ -101,40 +129,40 @@ 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
}
}
return
}

// 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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion hack/addon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spec:
value: $(port:8000)
metadata:
resources:
- kind: identity=maven
- kind: identity:kind=maven
fields:
- name: settings
path: /tmp/settings.xml
Expand Down

0 comments on commit 26b2fab

Please sign in to comment.