Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👻 Tasking find refs #673

Merged
merged 9 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
Expand All @@ -11,7 +10,7 @@ import (
"github.com/konveyor/tackle2-hub/assessment"
"github.com/konveyor/tackle2-hub/metrics"
"github.com/konveyor/tackle2-hub/model"
tasking "github.com/konveyor/tackle2-hub/task"
"github.com/konveyor/tackle2-hub/trigger"
"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -250,11 +249,20 @@ func (h ApplicationHandler) Create(ctx *gin.Context) {
return
}

err = h.discover(ctx, m)
rtx := WithContext(ctx)
tr := trigger.Application{
Trigger: trigger.Trigger{
TaskManager: rtx.TaskManager,
Client: rtx.Client,
DB: h.DB(ctx),
},
}
err = tr.Created(m)
if err != nil {
_ = ctx.Error(err)
return
}

h.Respond(ctx, http.StatusCreated, r)
}

Expand Down Expand Up @@ -380,11 +388,20 @@ func (h ApplicationHandler) Update(ctx *gin.Context) {
}
}

err = h.discover(ctx, m)
rtx := WithContext(ctx)
tr := trigger.Application{
Trigger: trigger.Trigger{
TaskManager: rtx.TaskManager,
Client: rtx.Client,
DB: h.DB(ctx),
},
}
err = tr.Updated(m)
if err != nil {
_ = ctx.Error(err)
return
}

h.Status(ctx, http.StatusNoContent)
}

Expand Down Expand Up @@ -1074,31 +1091,6 @@ func (h ApplicationHandler) AssessmentCreate(ctx *gin.Context) {
h.Respond(ctx, http.StatusCreated, r)
}

// discover an application's language and frameworks by launching discovery tasks.
func (h ApplicationHandler) discover(ctx *gin.Context, application *model.Application) (err error) {
rtx := WithContext(ctx)
db := h.DB(ctx)
for _, kind := range Settings.Hub.Discovery.Tasks {
t := Task{}
t.Kind = kind
t.Name = fmt.Sprintf("%s-%s", application.Name, kind)
ref := Ref{ID: application.ID}
t.Application = &ref
t.State = tasking.Ready
taskHandler := TaskHandler{}
err = taskHandler.FindRefs(rtx.Client, &t)
if err != nil {
return
}
task := tasking.Task{Task: t.Model()}
err = rtx.TaskManager.Create(db, &task)
if err != nil {
return
}
}
return
}

// Application REST resource.
type Application struct {
Resource `yaml:",inline"`
Expand Down
41 changes: 0 additions & 41 deletions api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,47 +250,6 @@ func (h *BaseHandler) Attachment(ctx *gin.Context, name string) {
attachment)
}

// Merge maps B into A.
// The B map is the authority.
func (h *BaseHandler) Merge(a, b map[string]any) (out map[string]any) {
if a == nil {
a = map[string]any{}
}
if b == nil {
b = map[string]any{}
}
out = map[string]any{}
for k, v := range a {
out[k] = v
if bv, found := b[k]; found {
out[k] = bv
if av, cast := v.(map[string]any); cast {
if bv, cast := bv.(map[string]any); cast {
out[k] = h.Merge(av, bv)
} else {
out[k] = bv
}
}
}
}
for k, v := range b {
if _, found := a[k]; !found {
out[k] = v
}
}

return
}

// AsMap returns the object as a map.
func (h *BaseHandler) AsMap(object any) (mp map[string]any, isMap bool) {
b, _ := json.Marshal(object)
mp = make(map[string]any)
err := json.Unmarshal(b, &mp)
isMap = err == nil
return
}

// REST resource.
type Resource struct {
ID uint `json:"id,omitempty" yaml:"id,omitempty"`
Expand Down
20 changes: 13 additions & 7 deletions api/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/model"
"github.com/konveyor/tackle2-hub/trigger"
"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -207,13 +208,18 @@ func (h IdentityHandler) Update(ctx *gin.Context) {
return
}

appHandler := ApplicationHandler{}
for i := range m.Applications {
err = appHandler.discover(ctx, &m.Applications[i])
if err != nil {
_ = ctx.Error(err)
return
}
rtx := WithContext(ctx)
tr := trigger.Identity{
Trigger: trigger.Trigger{
TaskManager: rtx.TaskManager,
Client: rtx.Client,
DB: h.DB(ctx),
},
}
err = tr.Updated(m)
if err != nil {
_ = ctx.Error(err)
return
}

h.Status(ctx, http.StatusNoContent)
Expand Down
93 changes: 5 additions & 88 deletions api/task.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"context"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -12,15 +11,12 @@ import (

"github.com/gin-gonic/gin"
qf "github.com/konveyor/tackle2-hub/api/filter"
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
"github.com/konveyor/tackle2-hub/model"
"github.com/konveyor/tackle2-hub/tar"
tasking "github.com/konveyor/tackle2-hub/task"
"gorm.io/gorm"
"gorm.io/gorm/clause"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/utils/strings/slices"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// Routes
Expand Down Expand Up @@ -293,11 +289,6 @@ func (h TaskHandler) Create(ctx *gin.Context) {
return
}
rtx := WithContext(ctx)
err = h.FindRefs(rtx.Client, r)
if err != nil {
_ = ctx.Error(err)
return
}
task := &tasking.Task{}
task.With(r.Model())
task.CreateUser = h.BaseHandler.CurrentUser(ctx)
Expand Down Expand Up @@ -336,7 +327,7 @@ func (h TaskHandler) Delete(ctx *gin.Context) {
// @description Update a task.
// @tags tasks
// @accept json
// @success 202
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and patch probably should go back to being a 204

// @success 200
// @router /tasks/{id} [put]
// @param id path int true "Task ID"
// @param task body Task true "Task data"
Expand Down Expand Up @@ -373,15 +364,17 @@ func (h TaskHandler) Update(ctx *gin.Context) {
return
}

h.Status(ctx, http.StatusAccepted)
r.With(m)

h.Respond(ctx, http.StatusOK, r)
}

// Submit godoc
// @summary Submit a task.
// @description Patch and submit a task.
// @tags tasks
// @accept json
// @success 202
// @success 200
// @router /tasks/{id}/submit [put]
// @param id path int true "Task ID"
// @param task body Task false "Task data (optional)"
Expand Down Expand Up @@ -617,82 +610,6 @@ func (h TaskHandler) GetAttached(ctx *gin.Context) {
}
}

// FindRefs find referenced resources.
// - addon
// - extensions
// - kind
// - priority
// The priority is defaulted to the kind as needed.
func (h *TaskHandler) FindRefs(client k8sclient.Client, r *Task) (err error) {
if r.Addon != "" {
addon := &crd.Addon{}
name := r.Addon
err = client.Get(
context.TODO(),
k8sclient.ObjectKey{
Name: name,
Namespace: Settings.Hub.Namespace,
},
addon)
if err != nil {
if k8serr.IsNotFound(err) {
err = &BadRequestError{
Reason: "Addon: " + name + " not found",
}
}
return
}
}
for _, name := range r.Extensions {
ext := &crd.Extension{}
err = client.Get(
context.TODO(),
k8sclient.ObjectKey{
Name: name,
Namespace: Settings.Hub.Namespace,
},
ext)
if err != nil {
if k8serr.IsNotFound(err) {
err = &BadRequestError{
Reason: "Extension: " + name + " not found",
}
}
return
}
}
if r.Kind != "" {
kind := &crd.Task{}
name := r.Kind
err = client.Get(
context.TODO(),
k8sclient.ObjectKey{
Name: name,
Namespace: Settings.Hub.Namespace,
},
kind)
if err != nil {
if k8serr.IsNotFound(err) {
err = &BadRequestError{
Reason: "Task: " + name + " not found",
}
}
return
}
if r.Priority == 0 {
r.Priority = kind.Spec.Priority
}
mA, castA := h.AsMap(kind.Spec.Data)
mB, castB := r.Data.(map[string]any)
if castA && castB {
r.Data = h.Merge(mA, mB)
} else {
r.Data = mA
}
}
return
}

// TTL time-to-live.
type TTL model.TTL

Expand Down
22 changes: 8 additions & 14 deletions api/taskgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,11 @@ func (h *TaskGroupHandler) findRefs(ctx *gin.Context, r *TaskGroup) (err error)
if r.Priority == 0 {
r.Priority = kind.Spec.Priority
}
mA, castA := h.AsMap(kind.Spec.Data)
mB, castB := r.Data.(map[string]any)
if castA && castB {
r.Data = h.Merge(mA, mB)
} else {
r.Data = mA
data := model.Data{Any: r.Data}
other := model.Data{Any: kind.Data()}
merged := data.Merge(other)
if !merged {
r.Data = other.Any
}
}
return
Expand All @@ -472,14 +471,9 @@ func (h *TaskGroupHandler) Propagate(m *model.TaskGroup) (err error) {
task.Policy = m.Policy
task.State = m.State
task.SetBucket(m.BucketID)
if m.Data.Any != nil {
mA, castA := m.Data.Any.(map[string]any)
mB, castB := task.Data.Any.(map[string]any)
if castA && castB {
task.Data.Any = h.Merge(mA, mB)
} else {
task.Data.Any = m.Data
}
merged := task.Data.Merge(m.Data)
if !merged {
task.Data = m.Data
}
}

Expand Down
4 changes: 0 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func main() {
return
}
}()
err = Settings.FindDiscoveryTasks()
if err != nil {
return
}
}
//
// k8s client.
Expand Down
Loading
Loading