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

feat: workflow status listener - RK-19190 #85

Merged
merged 20 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 34 additions & 6 deletions cmd/piper/piper.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package main

import (
"context"
"fmt"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"k8s.io/apimachinery/pkg/watch"
"log"
"strconv"

rookout "github.com/Rookout/GoSDK"
"github.com/rookout/piper/pkg/clients"
"github.com/rookout/piper/pkg/conf"
"github.com/rookout/piper/pkg/git_provider"
"github.com/rookout/piper/pkg/server"
"github.com/rookout/piper/pkg/utils"
workflowHandler "github.com/rookout/piper/pkg/workflow_handler"

rookout "github.com/Rookout/GoSDK"
)

func main() {
Expand Down Expand Up @@ -51,10 +55,34 @@ func main() {
panic(err)
}

//err = common.Git.UnsetWebhook()
//if err != nil {
// panic(err)
//}
ctx := context.Background()
Copy link
Contributor

@qadiludmer qadiludmer Jul 6, 2023

Choose a reason for hiding this comment

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

I would split the code to 2 files

event_handlers.go

func HandleWorkflowEvents(workflowClient) {
 // create the watch and start listening to events and also handling them
}

piper.go (this is the main)

go HandleWorkflowEvents(globalClients.Workflows)

This way piper.go only deals with initializing stuff and not doing any actual logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

already on it :D
check the new commit @qadiludmer

watcher, err := globalClients.Workflows.Watch(&ctx)
if err != nil {
log.Panicf("Failed to watch workflow error:%s", err)
}
defer watcher.Stop()

go func() {
workflowEventHandler(watcher.ResultChan())
}()

server.Start(cfg, globalClients)
}

func workflowEventHandler(workflowChan <-chan watch.Event) {
for event := range workflowChan {
wf, ok := event.Object.(*v1alpha1.Workflow)
if !ok {
log.Printf("Workflow object is not a v1alpha1.Workflow")
return
}
fmt.Printf(
"evnet are: %s, %s phase: %s completed: %s, message: %s\n",
event.Type,
wf.GetName(),
wf.Status.Phase,
strconv.FormatBool(wf.Status.Phase.Completed()),
wf.Status.Message,
)
}
}
9 changes: 6 additions & 3 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ make helm
To make it easy to develop locally, please run the following

Prerequisites :
1. install ngrok
1. install helm
2. install kubectl
3. isntall docker
4. install ngrok
```bash
brew install ngrok
```
2. install docker
3. install kind
5. install docker
6. install kind
```bash
brew install kind
```
Expand Down
2 changes: 2 additions & 0 deletions pkg/workflow_handler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/rookout/piper/pkg/common"
"k8s.io/apimachinery/pkg/watch"
)

type WorkflowsClient interface {
Expand All @@ -14,4 +15,5 @@ type WorkflowsClient interface {
Lint(wf *v1alpha1.Workflow) error
Submit(ctx *context.Context, wf *v1alpha1.Workflow) error
HandleWorkflowBatch(ctx *context.Context, workflowsBatch *common.WorkflowsBatch) error
Watch(ctx *context.Context) (watch.Interface, error)
}
18 changes: 18 additions & 0 deletions pkg/workflow_handler/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
wfClientSet "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"log"

"github.com/rookout/piper/pkg/common"
Expand Down Expand Up @@ -93,6 +95,7 @@ func (wfc *WorkflowsClientImpl) CreateWorkflow(spec *v1alpha1.WorkflowSpec, work
GenerateName: ConvertToValidString(workflowsBatch.Payload.Repo + "-" + workflowsBatch.Payload.Branch + "-"),
Namespace: wfc.cfg.Namespace,
Labels: map[string]string{
"piper": "true",
"repo": ConvertToValidString(workflowsBatch.Payload.Repo),
"branch": ConvertToValidString(workflowsBatch.Payload.Branch),
"user": ConvertToValidString(workflowsBatch.Payload.User),
Expand Down Expand Up @@ -144,6 +147,7 @@ func (wfc *WorkflowsClientImpl) Submit(ctx *context.Context, wf *v1alpha1.Workfl
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -196,3 +200,17 @@ func (wfc *WorkflowsClientImpl) HandleWorkflowBatch(ctx *context.Context, workfl
log.Printf("submit workflow for branch %s repo %s commit %s", workflowsBatch.Payload.Branch, workflowsBatch.Payload.Repo, workflowsBatch.Payload.Commit)
return nil
}

func (wfc *WorkflowsClientImpl) Watch(ctx *context.Context) (watch.Interface, error) {
workflowsClient := wfc.clientSet.ArgoprojV1alpha1().Workflows(wfc.cfg.Namespace)
opts := v1.ListOptions{
Watch: true,
LabelSelector: "piper=true",
}
watcher, err := workflowsClient.Watch(*ctx, opts)
if err != nil {
return nil, err
}

return watcher, nil
}
1 change: 1 addition & 0 deletions pkg/workflow_handler/workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func TestCreateWorkflow(t *testing.T) {
assert.Equal("my-repo-my-branch-", workflow.ObjectMeta.GenerateName)
assert.Equal(wfcImpl.cfg.Namespace, workflow.ObjectMeta.Namespace)
assert.Equal(map[string]string{
"piper": "true",
"repo": "my-repo",
"branch": "my-branch",
"user": "my-user",
Expand Down