-
Notifications
You must be signed in to change notification settings - Fork 134
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
2946cc3
commit cc982c1
Showing
7 changed files
with
156 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,58 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/tinkerbell/tink/internal/agent/event" | ||
"github.com/tinkerbell/tink/internal/agent/workflow" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// DefaultFileScanPeriod is the default period for scanning. | ||
const DefaultFileScanPeriod = 5 * time.Second | ||
|
||
// File is a transport implementation that monitors a directory for workflow files. When it finds | ||
// files it has yet to process, it will parse them and offload them to its handler. It is intended | ||
// for developmental use only. | ||
type File struct { | ||
// Log is a logger for debugging. | ||
Log logr.Logger | ||
|
||
// Path is a path to a workflow to be 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,23 @@ | ||
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