Skip to content

Commit

Permalink
fix: tekton task watch (#52)
Browse files Browse the repository at this point in the history
* fix: tekton task watch

* fix: tekton task watch
  • Loading branch information
fandujar authored Sep 3, 2024
1 parent b5d9345 commit fe8d905
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
53 changes: 38 additions & 15 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@ func (c *Controller) Run(ctx context.Context) error {
if err != nil {
return err
}

for {
select {
case <-ctx.Done():
log.Info().Msg("context done")
return nil
case event := <-events:
if err := c.HandleEvent(ctx, event); err != nil {
log.Error().Err(err).Msg("failed to handle event")
case event, ok := <-events:
if !ok {
events, err = c.TektonCD.WatchTasks(ctx, "choregate")
if err != nil {
log.Error().Err(err).Msg("failed to watch tasks")
continue
}
}
if event.Object != nil {
if err := c.HandleEvent(ctx, event); err != nil {
log.Error().Err(err).Msg("failed to handle event")
}
}
}
}
Expand All @@ -51,14 +62,20 @@ func (c *Controller) HandleEvent(ctx context.Context, event watch.Event) error {
switch event.Type {
case "ADDED":
tektonTask := event.Object.(*tektonAPI.Task)
log.Info().Msgf("task %s added", tektonTask.Name)
log.Debug().Msgf("task %s added", tektonTask.Name)

var taskID uuid.UUID
var err error
if _, ok := tektonTask.Labels["choregate.fandujar.dev/task-id"]; ok {
return fmt.Errorf("task %s already has a task-id label", tektonTask.Name)
taskID, err = uuid.Parse(tektonTask.Labels["choregate.fandujar.dev/task-id"])
if err != nil {
return err
}
}

task, err := entities.NewTask(
&entities.TaskConfig{
ID: taskID,
Name: tektonTask.Name,
TaskSpec: &tektonTask.Spec,
},
Expand All @@ -77,18 +94,21 @@ func (c *Controller) HandleEvent(ctx context.Context, event watch.Event) error {
})
case "MODIFIED":
tektonTask := event.Object.(*tektonAPI.Task)
log.Info().Msgf("task %s modified", tektonTask.Name)
log.Debug().Msgf("task %s modified", tektonTask.Name)

taskID := tektonTask.Labels["choregate.fandujar.dev/task-id"]
if taskID == "" {
id := tektonTask.Labels["choregate.fandujar.dev/task-id"]
if id == "" {
return fmt.Errorf("task %s has no task-id label", tektonTask.Name)
}

id := uuid.MustParse(taskID)
taskID, err := uuid.Parse(id)
if err != nil {
return err
}

task, err := entities.NewTask(
&entities.TaskConfig{
ID: id,
ID: taskID,
Name: tektonTask.Name,
TaskSpec: &tektonTask.Spec,
},
Expand All @@ -106,16 +126,19 @@ func (c *Controller) HandleEvent(ctx context.Context, event watch.Event) error {
tektonTask := event.Object.(*tektonAPI.Task)
log.Info().Msgf("task %s deleted", tektonTask.Name)

taskID := tektonTask.Labels["choregate.fandujar.dev/task-id"]
if taskID == "" {
id := tektonTask.Labels["choregate.fandujar.dev/task-id"]
if id == "" {
return fmt.Errorf("task %s has no task-id label", tektonTask.Name)
}

id := uuid.MustParse(taskID)
taskID, err := uuid.Parse(id)
if err != nil {
return err
}

task, err := entities.NewTask(
&entities.TaskConfig{
ID: id,
ID: taskID,
Name: tektonTask.Name,
TaskSpec: &tektonTask.Spec,
},
Expand All @@ -130,7 +153,7 @@ func (c *Controller) HandleEvent(ctx context.Context, event watch.Event) error {
return err
}
default:
log.Error().Msgf("unknown event type: %s", event.Type)
log.Debug().Msgf("event type %s not supported. event: %s", event.Type, event)
}

return nil
Expand Down
17 changes: 12 additions & 5 deletions pkg/providers/tektoncd/tektoncd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ func NewTektonClient() (TektonClient, error) {

// WatchTasks watches all tasks inside a namespace.
func (c *TektonClientImpl) WatchTasks(ctx context.Context, namespace string) (<-chan watch.Event, error) {
timeout := int64(300)
// Watch the tasks.
watcher, err := c.tektonClient.TektonV1().Tasks(namespace).Watch(ctx, metav1.ListOptions{})
watcher, err := c.tektonClient.TektonV1().Tasks(namespace).Watch(ctx, metav1.ListOptions{
TimeoutSeconds: &timeout,
})
if err != nil {
return nil, err
}
Expand All @@ -81,11 +84,15 @@ func (c *TektonClientImpl) WatchTasks(ctx context.Context, namespace string) (<-
// WatchTask watches a task.
func (c *TektonClientImpl) WatchTask(ctx context.Context, task *tektonAPI.Task, id uuid.UUID) (<-chan watch.Event, error) {
namespace := task.Namespace

timeout := int64(30)
// Watch the task.
watcher, err := c.tektonClient.TektonV1().Tasks(namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: "choregate.fandujar.dev/task-id=" + id.String(),
})
watcher, err := c.tektonClient.TektonV1().Tasks(namespace).Watch(
ctx,
metav1.ListOptions{
TimeoutSeconds: &timeout,
LabelSelector: "choregate.fandujar.dev/task-id=" + id.String(),
},
)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fe8d905

Please sign in to comment.