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 22, 2024
1 parent 8c2e428 commit 569b25b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
13 changes: 8 additions & 5 deletions addon/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,31 +271,34 @@ func (h *Task) pushReport() {
return
}

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

// EnvInjector inject ENVAR into extension metadata.
type EnvInjector struct {
}

// Inject inject ENVAR into extension metadata.
func (r *EnvInjector) Inject(extensions []api.Extension) {
p := regexp.MustCompile(`(\${)([^}]+)(})`)
for i := range extensions {
extension := &extensions[i]
mp := make(map[string]any)
b, _ := json.Marshal(extension.Metadata)
_ = json.Unmarshal(b, &mp)
r.inject(p, mp)
r.inject(mp)
}
}

// inject ENVAR into extension metadata.
func (r *EnvInjector) inject(p *regexp.Regexp, mp map[string]any) {
func (r *EnvInjector) inject(mp map[string]any) {
for k, v := range mp {
switch node := v.(type) {
case map[string]any:
r.inject(p, node)
r.inject(node)
case string:
for {
match := p.FindStringSubmatch(node)
match := EnvRegex.FindStringSubmatch(node)
if len(match) < 3 {
break
}
Expand Down
54 changes: 15 additions & 39 deletions task/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"strconv"
"strings"

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

var (
PortRegex = regexp.MustCompile(`(\$\(port:)([0-9]+)\)`)
)

// Injector macro processor.
Expand All @@ -14,65 +18,37 @@ type Injector struct {
}

// Inject process macros.
func (r *Injector) Inject(extension *crd.Extension) {
container := &extension.Spec.Container
func (r *Injector) Inject(container *core.Container) {
for _, env := range container.Env {
env.Value = r.port.inject(env.Value)
}
r.port.next()
}

// Port port allocation.
type Port struct {
next int
matched int
}

// PortInjector provides $(port:<base>) injection.
type PortInjector struct {
pattern *regexp.Regexp
portMap map[int]Port
}

// init object.
func (r *PortInjector) init() {
if r.pattern != nil {
return
}
r.pattern = regexp.MustCompile(`(\$\(port:)([0-9]+)\)`)
r.portMap = make(map[int]Port)
portMap map[int]int
}

// inject port.
func (r *PortInjector) inject(in string) (out string) {
r.init()
if r.portMap == nil {
r.portMap = make(map[int]int)
}
out = in
for {
match := r.pattern.FindStringSubmatch(out)
match := PortRegex.FindStringSubmatch(out)
if len(match) < 3 {
break
}
base, _ := strconv.Atoi(match[2])
port := r.portMap[base]
offset := r.portMap[base]
out = strings.Replace(
out,
match[0],
strconv.Itoa(base+port.next),
strconv.Itoa(base+offset),
-1)
port.matched++
r.portMap[base] = port
offset++
r.portMap[base] = offset
}
return
}

// next increment offsets and reset matched.
func (r *PortInjector) next() {
r.init()
for base, port := range r.portMap {
if port.matched > 0 {
port.matched = 0
port.next++
r.portMap[base] = port
}
}
}
4 changes: 2 additions & 2 deletions task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,18 +896,18 @@ func (r *Task) containers(
},
},
}
injector := Injector{}
plain = append(plain, addon.Spec.Container)
for i := range extensions {
extension := &extensions[i]
injector.Inject(extension)
container := extension.Spec.Container
plain = append(
plain,
container)
}
injector := Injector{}
for i := range plain {
container := &plain[i]
injector.Inject(container)
container.SecurityContext = &core.SecurityContext{
RunAsUser: &userid,
}
Expand Down

0 comments on commit 569b25b

Please sign in to comment.