Skip to content

Commit

Permalink
Simplify executor commands to just 'init' and 'wait'. Improve volumes…
Browse files Browse the repository at this point in the history
… examples
  • Loading branch information
jessesuen committed Dec 2, 2017
1 parent e2bfbc1 commit 32b5e99
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 225 deletions.
162 changes: 0 additions & 162 deletions cmd/argoexec/commands/artifacts.go

This file was deleted.

29 changes: 29 additions & 0 deletions cmd/argoexec/commands/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package commands

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Load artifacts",
Run: loadArtifacts,
}

func loadArtifacts(cmd *cobra.Command, args []string) {
wfExecutor := initExecutor()
// Download input artifacts
err := wfExecutor.LoadScriptSource()
if err != nil {
log.Fatalf("Error loading script: %+v", err)
}
err = wfExecutor.LoadArtifacts()
if err != nil {
log.Fatalf("Error downloading input artifacts: %+v", err)
}
}
89 changes: 88 additions & 1 deletion cmd/argoexec/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package commands

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/argoproj/argo"
wfv1 "github.com/argoproj/argo/api/workflow/v1"
"github.com/argoproj/argo/errors"
"github.com/argoproj/argo/util/cmd"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/executor"
Expand Down Expand Up @@ -60,7 +66,7 @@ func initExecutor() *executor.WorkflowExecutor {
var wfTemplate wfv1.Template

// Read template
err := GetTemplateFromPodAnnotations(podAnnotationsPath, &wfTemplate)
err := getTemplateFromPodAnnotations(podAnnotationsPath, &wfTemplate)
if err != nil {
log.Fatalf("Error getting template %v", err)
}
Expand Down Expand Up @@ -95,3 +101,84 @@ func initExecutor() *executor.WorkflowExecutor {
log.Infof("Executor (version: %s) initialized with template:\n%s", argo.FullVersion, string(yamlBytes))
return &wfExecutor
}

// Open the Kubernetes downward api file and
// read the pod annotation file that contains template and then
// unmarshal the template
func getTemplateFromPodAnnotations(annotationsPath string, template *wfv1.Template) error {
// Read the annotation file
file, err := os.Open(annotationsPath)
if err != nil {
fmt.Printf("ERROR opening annotation file from %s\n", annotationsPath)
return errors.InternalWrapError(err)
}

defer file.Close()
reader := bufio.NewReader(file)

// Prefix of template property in the annotation file
prefix := fmt.Sprintf("%s=", common.AnnotationKeyTemplate)

for {
// Read line-by-line
var buffer bytes.Buffer

var l []byte
var isPrefix bool
for {
l, isPrefix, err = reader.ReadLine()
buffer.Write(l)

// If we've reached the end of the line, stop reading.
if !isPrefix {
break
}

// If we're just at the EOF, break
if err != nil {
break
}
}

// The end of the annotation file
if err == io.EOF {
break
}

line := buffer.String()

// Read template property
if strings.HasPrefix(line, prefix) {
// Trim the prefix
templateContent := strings.TrimPrefix(line, prefix)

// This part is a bit tricky in terms of unmarshalling
// The content in the file will be something like,
// `"{\"type\":\"container\",\"inputs\":{},\"outputs\":{}}"`
// which is required to unmarshal twice

// First unmarshal to a string without escaping characters
var templateString string
err = json.Unmarshal([]byte(templateContent), &templateString)
if err != nil {
fmt.Printf("Error unmarshalling annotation into template string, %s, %v\n", templateContent, err)
return errors.InternalWrapError(err)
}

// Second unmarshal to a template
err = json.Unmarshal([]byte(templateString), template)
if err != nil {
fmt.Printf("Error unmarshalling annotation into template, %s, %v\n", templateString, err)
return errors.InternalWrapError(err)
}
return nil
}
}

if err != io.EOF {
return errors.InternalWrapError(err)
}

// If reaching here, then no template prefix in the file
return errors.InternalErrorf("No template property found from annotation file: %s", annotationsPath)
}
40 changes: 0 additions & 40 deletions cmd/argoexec/commands/sidecar.go

This file was deleted.

46 changes: 46 additions & 0 deletions cmd/argoexec/commands/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

import (
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(waitCmd)
}

var waitCmd = &cobra.Command{
Use: "wait",
Short: "wait for main container to finish and save artifacts",
Run: waitContainer,
}

func waitContainer(cmd *cobra.Command, args []string) {
wfExecutor := initExecutor()
// Wait for main container to complete and kill sidecars
err := wfExecutor.Wait()
if err != nil {
log.Errorf("Error waiting on main container to be ready, %+v", err)
}
err = wfExecutor.SaveArtifacts()
if err != nil {
log.Fatalf("Error saving output artifacts, %+v", err)
}
// Saving output parameters
err = wfExecutor.SaveParameters()
if err != nil {
log.Fatalf("Error saving output parameters, %+v", err)
}
// Capture output script result
err = wfExecutor.CaptureScriptResult()
if err != nil {
log.Fatalf("Error capturing script output, %+v", err)
}
err = wfExecutor.AnnotateOutputs()
if err != nil {
log.Fatalf("Error annotating outputs, %+v", err)
}
os.Exit(0)
}
Loading

0 comments on commit 32b5e99

Please sign in to comment.