Skip to content

Commit

Permalink
✨ Support extenion compat with addon as regex. (konveyor#782)
Browse files Browse the repository at this point in the history
Both the analyzer and tech-discovery both use the same providers. An
extension having compatibility with multiple addons is not a
common/mainstream use case. To get around this, we have defined a
duplicate set of extensions for each provider. One with compatibility
with the `analyzer` addon and the other for the `tech-discovery` addon.

After a recent refactor of the generic extension into multiple
extensions (go, python, nodejs), it became obvious that maintain the
duplicate set of extensions is painful and likely error prone.

It seems a better approach is to change `Extention.Spec.Addon` to
(optionally) define a regex. This approach caters to the mainstream use
case that extensions have compatibility with one addon. It also avoids
making the field an _array_ requiring a CRD change. This is which is
MUCH simpler.

The existing extensions will need to be updated to be compatible with
both addons.
Example (java extension):
```
addon: ^(analyzer|tech-discovery)$
```

The `discovery-` extensions would be deleted.

---------

Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel authored Feb 12, 2025
1 parent 0986cad commit 6627a2d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions settings/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ func (r *Hub) namespace() (ns string, err error) {
// build returns the hub build version.
// This is expected to be the output of `git describe`.
// Examples:
//
// v0.6.0-ea89gcd
// v0.6.0
func (r *Hub) build() (version string, err error) {
Expand Down
19 changes: 19 additions & 0 deletions task/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,25 @@ func (e *SelectorNotValid) Retry() (r bool) {
return
}

// ExtAddonNotValid reports extension addon ref error.
type ExtAddonNotValid struct {
Extension string
Reason string
}

func (e *ExtAddonNotValid) Error() string {
return fmt.Sprintf(
"Extension '%s' addon ref not valid. reason: %s",
e.Extension,
e.Reason)
}

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

// PriorityNotFound report priority class not found.
type PriorityNotFound struct {
Name string
Expand Down
35 changes: 34 additions & 1 deletion task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -88,6 +89,10 @@ const (
Cache = "cache"
)

var (
IsRegex = regexp.MustCompile("[^0-9A-Za-z_-]")
)

var (
Settings = &settings.Settings
Log = logr.WithName("task-scheduler")
Expand Down Expand Up @@ -555,7 +560,11 @@ func (m *Manager) selectExtensions(task *Task, addon *crd.Addon) (err error) {
matched := false
selector := NewSelector(m.DB, task)
for _, extension := range m.cluster.Extensions() {
if extension.Spec.Addon != addon.Name {
matched, err = m.matchAddon(extension, addon)
if err != nil {
return
}
if !matched {
continue
}
matched, err = selector.Match(extension.Spec.Selector)
Expand All @@ -570,6 +579,30 @@ func (m *Manager) selectExtensions(task *Task, addon *crd.Addon) (err error) {
return
}

// matchAddon - returns true when the extension's `addon`
// (ref) matches the addon name.
// The `ref` is matched as a REGEX when it contains
// characters other than: [0-9A-Za-z_].
func (m *Manager) matchAddon(extension *crd.Extension, addon *crd.Addon) (matched bool, err error) {
ref := strings.TrimSpace(extension.Spec.Addon)
p := IsRegex
if p.MatchString(ref) {
p, err = regexp.Compile(ref)
if err != nil {
err = &ExtAddonNotValid{
Extension: extension.Name,
Reason: err.Error(),
}
return
}
matched = p.MatchString(addon.Name)
} else {

matched = addon.Name == ref
}
return
}

// postpone Postpones a task as needed based on rules.
// postpone order:
// - priority (lower)
Expand Down
31 changes: 31 additions & 0 deletions task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,34 @@ func TestPriorityGraph(t *testing.T) {
deps := pE.graph(ready[0], ready)
g.Expect(len(deps)).To(gomega.Equal(2))
}

func TestAddonRegex(t *testing.T) {
g := gomega.NewGomegaWithT(t)
m := Manager{}
addonA := &crd.Addon{}
addonA.Name = "A"
addonB := &crd.Addon{}
addonB.Name = "B"
// direct.
ext := &crd.Extension{}
ext.Name = "Test"
ext.Spec.Addon = "A"
matched, err := m.matchAddon(ext, addonA)
g.Expect(err).To(gomega.BeNil())
g.Expect(matched).To(gomega.BeTrue())
matched, err = m.matchAddon(ext, addonB)
g.Expect(err).To(gomega.BeNil())
g.Expect(matched).To(gomega.BeFalse())
// regex.
ext.Spec.Addon = "^(A|B)$"
matched, err = m.matchAddon(ext, addonA)
g.Expect(err).To(gomega.BeNil())
g.Expect(matched).To(gomega.BeTrue())
matched, err = m.matchAddon(ext, addonB)
g.Expect(err).To(gomega.BeNil())
g.Expect(matched).To(gomega.BeTrue())
// regex not valid.
ext.Spec.Addon = "(]$"
matched, err = m.matchAddon(ext, addonA)
g.Expect(err).ToNot(gomega.BeNil())
}

0 comments on commit 6627a2d

Please sign in to comment.