Skip to content

Commit 959ad5f

Browse files
authored
Passing Argo context to Argo Workflow calls (#17)
1 parent ac94894 commit 959ad5f

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.0.4 (2021-06-03)
4+
### Fixed
5+
* Passing Argo context to Argo Workflow calls
6+
37
## v0.0.3 (2021-05-27)
48
### Changed
59
* Update environmental variable name to specify workflow execution namespace

service/handlers.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type handler struct {
108108
logger log.Logger
109109
newCredentialsProvider func(a Authorization) (credentialsProvider, error)
110110
argo workflow.Workflow
111+
argoCtx context.Context
111112
config *Config
112113
gitClient gitClient
113114
}
@@ -150,15 +151,14 @@ func (h handler) healthCheck(w http.ResponseWriter, r *http.Request) {
150151
func (h handler) listWorkflows(w http.ResponseWriter, r *http.Request) {
151152
// TODO authenticate user can list this workflow once auth figured out
152153
// TODO fail if project / target does not exist or are not valid format
153-
ctx := r.Context()
154154
vars := mux.Vars(r)
155155
projectName := vars["projectName"]
156156
targetName := vars["targetName"]
157157

158158
l := h.requestLogger(r, "op", "list-workflows", "project", projectName, "target", targetName)
159159

160160
level.Debug(l).Log("message", "listing workflows")
161-
workflowIDs, err := h.argo.List(ctx)
161+
workflowIDs, err := h.argo.List(h.argoCtx)
162162
if err != nil {
163163
level.Error(l).Log("message", "error listing workflows", "error", err)
164164
h.errorResponse(w, "error listing workflows", http.StatusBadRequest, err)
@@ -170,7 +170,7 @@ func (h handler) listWorkflows(w http.ResponseWriter, r *http.Request) {
170170
prefix := fmt.Sprintf("%s-%s", projectName, targetName)
171171
for _, workflowID := range workflowIDs {
172172
if strings.HasPrefix(workflowID, prefix) {
173-
workflow, err := h.argo.Status(ctx, workflowID)
173+
workflow, err := h.argo.Status(h.argoCtx, workflowID)
174174
if err != nil {
175175
level.Error(l).Log("message", "error retrieving workflows", "error", err)
176176
h.errorResponse(w, "error retrieving workflows", http.StatusBadRequest, err)
@@ -400,7 +400,7 @@ func (h handler) createWorkflowFromRequest(ctx context.Context, w http.ResponseW
400400
parameters := workflow.NewParameters(environmentVariablesString, executeCommand, executeContainerImageURI, cwr.TargetName, cwr.ProjectName, cwr.Parameters, credentialsToken)
401401

402402
level.Debug(l).Log("message", "creating workflow")
403-
workflowName, err := h.argo.Submit(ctx, workflowFrom, parameters)
403+
workflowName, err := h.argo.Submit(h.argoCtx, workflowFrom, parameters)
404404
if err != nil {
405405
level.Error(l).Log("message", "error creating workflow", "error", err)
406406
h.errorResponse(w, "error creating workflow", http.StatusInternalServerError, err)
@@ -425,7 +425,6 @@ func (h handler) createWorkflowFromRequest(ctx context.Context, w http.ResponseW
425425

426426
// Gets a workflow
427427
func (h handler) getWorkflow(w http.ResponseWriter, r *http.Request) {
428-
ctx := r.Context()
429428
vars := mux.Vars(r)
430429
workflowName := vars["workflowName"]
431430
// TODO: Workflow name must include -
@@ -436,7 +435,7 @@ func (h handler) getWorkflow(w http.ResponseWriter, r *http.Request) {
436435
l := h.requestLogger(r, "op", "get-workflow", "workflow", workflowName)
437436

438437
level.Debug(l).Log("message", "getting workflow status")
439-
status, err := h.argo.Status(ctx, workflowName)
438+
status, err := h.argo.Status(h.argoCtx, workflowName)
440439
if err != nil {
441440
level.Error(l).Log("message", "error getting workflow", "error", err)
442441
h.errorResponse(w, "error getting workflow", http.StatusBadRequest, err)
@@ -506,7 +505,6 @@ func (h handler) getTarget(w http.ResponseWriter, r *http.Request) {
506505

507506
// Returns the logs for a workflow
508507
func (h handler) getWorkflowLogs(w http.ResponseWriter, r *http.Request) {
509-
ctx := r.Context()
510508
vars := mux.Vars(r)
511509
workflowName := vars["workflowName"]
512510
// TODO: Workflow name must include -
@@ -517,7 +515,7 @@ func (h handler) getWorkflowLogs(w http.ResponseWriter, r *http.Request) {
517515
l := h.requestLogger(r, "op", "get-workflow-logs", "workflow", workflowName)
518516

519517
level.Debug(l).Log("message", "retrieving workflow logs")
520-
argoWorkflowLogs, err := h.argo.Logs(ctx, workflowName)
518+
argoWorkflowLogs, err := h.argo.Logs(h.argoCtx, workflowName)
521519
if err != nil {
522520
level.Error(l).Log("message", "error getting workflow logs", "error", err)
523521
h.errorResponse(w, "error getting workflow logs", http.StatusBadRequest, err)
@@ -535,7 +533,6 @@ func (h handler) getWorkflowLogs(w http.ResponseWriter, r *http.Request) {
535533

536534
// Streams workflow logs
537535
func (h handler) getWorkflowLogStream(w http.ResponseWriter, r *http.Request) {
538-
ctx := r.Context()
539536
w.Header().Set("Content-Type", "text/plain")
540537
vars := mux.Vars(r)
541538
workflowName := vars["workflowName"]
@@ -547,7 +544,7 @@ func (h handler) getWorkflowLogStream(w http.ResponseWriter, r *http.Request) {
547544
l := h.requestLogger(r, "op", "get-workflow-log-stream", "workflow", workflowName)
548545

549546
level.Debug(l).Log("message", "retrieving workflow logs", "workflow", workflowName)
550-
err := h.argo.LogStream(ctx, workflowName, w)
547+
err := h.argo.LogStream(h.argoCtx, workflowName, w)
551548
if err != nil {
552549
level.Error(l).Log("message", "error getting workflow logstream", "error", err)
553550
h.errorResponse(w, "error getting workflow logs", http.StatusBadRequest, err)

service/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ func main() {
7070
panic("error creating git client")
7171
}
7272

73-
_, argoClient := client.NewAPIClient()
73+
// The Argo context is needed for any Argo client method calls or else, nil errors.
74+
argoCtx, argoClient := client.NewAPIClient()
7475
namespace := acoEnv.ArgoNamespace()
7576

77+
// Any Argo Workflow client method calls need the context returned from NewAPIClient, otherwise
78+
// nil errors will occur. Mux sets its params in context, so passing the Argo Workflow context to
79+
// setupRouter and applying it to the request will wipe out Mux vars (or any other data Mux sets in its context).
7680
h := handler{
7781
logger: logger,
7882
newCredentialsProvider: newVaultProvider(vaultSvc),
7983
argo: workflow.NewArgoWorkflow(argoClient.NewWorkflowServiceClient(), namespace),
84+
argoCtx: argoCtx,
8085
config: config,
8186
gitClient: gitClient,
8287
}

0 commit comments

Comments
 (0)