-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chris Doherty <[email protected]>
- Loading branch information
1 parent
1a0edeb
commit aaed2e6
Showing
7 changed files
with
149 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/tinkerbell/tink/internal/agent/event" | ||
"github.com/tinkerbell/tink/internal/agent/workflow" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// File is a transport implementation that executes a single workflow stored as a file. | ||
type File struct { | ||
// Log is a logger for debugging. | ||
Log logr.Logger | ||
|
||
// Path to the workflow to run. | ||
Path string | ||
} | ||
|
||
// Start begins watching f.Dir for files. When it finds a file it hasn't handled before, it | ||
// attempts to parse it and offload to the handler. It will run workflows once where a workflow | ||
// is determined by its file name. | ||
func (f *File) Start(ctx context.Context, _ string, handler WorkflowHandler) error { | ||
path, err := filepath.Abs(f.Path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fh, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var wrkflow workflow.Workflow | ||
if err := yaml.NewDecoder(fh).Decode(&wrkflow); err != nil { | ||
return err | ||
} | ||
|
||
handler.HandleWorkflow(ctx, wrkflow, f) | ||
|
||
return nil | ||
} | ||
|
||
func (f *File) RecordEvent(_ context.Context, e event.Event) error { | ||
// Noop because we don't particularly care about events for File based transports. Maybe | ||
// we'll record this in a dedicated file one day. | ||
f.Log.Info("Recording event", "event", e.GetName()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package transport_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-logr/zerologr" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/rs/zerolog" | ||
"github.com/tinkerbell/tink/internal/agent/event" | ||
"github.com/tinkerbell/tink/internal/agent/transport" | ||
"github.com/tinkerbell/tink/internal/agent/workflow" | ||
) | ||
|
||
func TestFile(t *testing.T) { | ||
logger := zerolog.New(zerolog.NewConsoleWriter()) | ||
|
||
expect := workflow.Workflow{ | ||
ID: "test-workflow-id", | ||
Actions: []workflow.Action{ | ||
{ | ||
ID: "test-action-1", | ||
Name: "my test action", | ||
Image: "docker.io/hub/alpine", | ||
Cmd: "sh -c", | ||
Args: []string{"echo", "action 1"}, | ||
Env: map[string]string{"foo": "bar"}, | ||
Volumes: []string{"mount:/foo/bar:ro"}, | ||
NetworkNamespace: "custom-namespace", | ||
}, | ||
{ | ||
ID: "test-action-2", | ||
Name: "my test action", | ||
Image: "docker.io/hub/alpine", | ||
Cmd: "sh -c", | ||
Args: []string{"echo", "action 2"}, | ||
Env: map[string]string{"foo": "bar"}, | ||
Volumes: []string{"mount:/foo/bar:ro"}, | ||
NetworkNamespace: "custom-namespace", | ||
}, | ||
}, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
|
||
handler := &transport.WorkflowHandlerMock{ | ||
HandleWorkflowFunc: func(contextMoqParam context.Context, workflow workflow.Workflow, recorder event.Recorder) { | ||
if !cmp.Equal(expect, workflow) { | ||
t.Fatalf("Workflow diff:\n%v", cmp.Diff(expect, workflow)) | ||
} | ||
}, | ||
} | ||
|
||
f := transport.File{ | ||
Log: zerologr.New(&logger), | ||
Path: "./testdata/workflow.yml", | ||
} | ||
|
||
err := f.Start(ctx, "agent_id", handler) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
id: "test-workflow-id" | ||
actions: | ||
- id: "test-action-1" | ||
name: "my test action" | ||
image: "docker.io/hub/alpine" | ||
cmd: "sh -c" | ||
args: ["echo", "action 1"] | ||
env: | ||
foo: bar | ||
volumes: | ||
- mount:/foo/bar:ro | ||
networkNamespace: "custom-namespace" | ||
- id: "test-action-2" | ||
name: "my test action" | ||
image: "docker.io/hub/alpine" | ||
cmd: "sh -c" | ||
args: ["echo", "action 2"] | ||
env: | ||
foo: bar | ||
volumes: | ||
- mount:/foo/bar:ro | ||
networkNamespace: "custom-namespace" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters