-
Notifications
You must be signed in to change notification settings - Fork 97
Add automatic volume creation for local input source #4961
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
package local | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bacalhau-project/bacalhau/pkg/bacerrors" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type CreateStrategy string | ||
|
||
const ( | ||
// Try to infer the type (file vs directory) from path | ||
Infer CreateStrategy = "infer" | ||
|
||
// Create as directory | ||
Dir CreateStrategy = "dir" | ||
|
||
// Create as file | ||
File CreateStrategy = "file" | ||
|
||
// Don't create anything | ||
NoCreate CreateStrategy = "nocreate" | ||
) | ||
|
||
const DefaultCreateStrategy = Infer | ||
const CreateStrategySpecKey = "CreateAs" | ||
|
||
func AllowedCreateStrategies() []string { | ||
return []string{ | ||
Infer.String(), | ||
Dir.String(), | ||
File.String(), | ||
NoCreate.String(), | ||
} | ||
} | ||
|
||
func CreateStrategyFromString(s string) (CreateStrategy, error) { | ||
switch s { | ||
case Infer.String(): | ||
return Infer, nil | ||
case Dir.String(): | ||
return Dir, nil | ||
case File.String(): | ||
return File, nil | ||
case NoCreate.String(): | ||
return NoCreate, nil | ||
case "": | ||
return DefaultCreateStrategy, nil | ||
default: | ||
// TODO: Create a constant for JobSpec to be used in WithComponent for this and similar errors | ||
return "", bacerrors.Newf("invalid CreateAs value %s", s). | ||
WithHint("CreateAs must be one of [%s]", strings.Join(AllowedCreateStrategies(), ", ")). | ||
WithCode(bacerrors.ValidationError) | ||
} | ||
} | ||
|
||
// Attempts to infer whether the given path represents a file or directory. | ||
// This is a best-effort attempt based on common conventions. | ||
func InferCreateStrategyFromPath(path string) CreateStrategy { | ||
// Leverage filepath.Split, it handles edge cases like trailing slashes, no slashes, etc. | ||
// For now this is smart enough, but we can improve it later if needed. | ||
// Note that there are some noticeable exceptions for which this will return the wrong strategy, | ||
// For example folders with a dot in the name (e.g. /etc/conf.d) and without a trailing slash | ||
// will be considered files. However such paths are likely to be non-empty and CreateStrategy will not be called for them. | ||
// Additinally, we can look at Target value to see if it gives more insight on whether we should create a file or directory. | ||
_, file := filepath.Split(path) | ||
var inferredStrategy CreateStrategy | ||
if file == "" { | ||
inferredStrategy = Dir | ||
} else { | ||
inferredStrategy = File | ||
} | ||
log.Debug().Str("path", path).Msgf("inferred create strategy: %s", inferredStrategy) | ||
return inferredStrategy | ||
} | ||
|
||
func (c CreateStrategy) String() string { | ||
return string(c) | ||
} |
This file contains hidden or 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,128 @@ | ||
package local | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAllowedCreateStrategies(t *testing.T) { | ||
expected := []string{Infer.String(), Dir.String(), File.String(), NoCreate.String()} | ||
actual := AllowedCreateStrategies() | ||
|
||
assert.Equal(t, expected, actual, "AllowedCreateStrategies should return all valid strategies") | ||
} | ||
|
||
func TestCreateStrategyFromString(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected CreateStrategy | ||
expectError bool | ||
errorContains string | ||
}{ | ||
{ | ||
name: "infer strategy", | ||
input: "infer", | ||
expected: Infer, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "directory strategy", | ||
input: "dir", | ||
expected: Dir, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "file strategy", | ||
input: "file", | ||
expected: File, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "nocreate strategy", | ||
input: "nocreate", | ||
expected: NoCreate, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "empty string uses default", | ||
input: "", | ||
expected: DefaultCreateStrategy, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "invalid strategy", | ||
input: "invalid", | ||
expected: "", | ||
expectError: true, | ||
errorContains: "invalid CreateAs value", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
strategy, err := CreateStrategyFromString(tc.input) | ||
|
||
if tc.expectError { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), tc.errorContains) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.expected, strategy) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestInferCreateStrategyFromPath(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
path string | ||
expected CreateStrategy | ||
}{ | ||
{ | ||
name: "empty path should be directory", | ||
path: "", | ||
expected: Dir, | ||
}, | ||
{ | ||
name: "path with trailing slash should be directory", | ||
path: "/path/to/dir/", | ||
expected: Dir, | ||
}, | ||
{ | ||
name: "path without trailing slash should be file", | ||
path: "/path/to/file", | ||
expected: File, | ||
}, | ||
{ | ||
name: "path with extension should be file", | ||
path: "/path/to/file.txt", | ||
expected: File, | ||
}, | ||
{ | ||
name: "root directory should be directory", | ||
path: "/", | ||
expected: Dir, | ||
}, | ||
{ | ||
name: "relative path to file", | ||
path: "file.txt", | ||
expected: File, | ||
}, | ||
{ | ||
name: "relative path to directory with trailing slash", | ||
path: "dir/", | ||
expected: Dir, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
strategy := InferCreateStrategyFromPath(tc.path) | ||
assert.Equal(t, tc.expected, strategy) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should this be
noCreate
orNoCreate
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to keep all lowercase for simplicity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I left a comment about supporting case insensitivity, but I might've missed that. Similar to all other specs, we should be case insensitive when parsing and decoding the job spec. Meaning
NoCreate
,noCreate
,nocreate
should all be treated the same.bacalhau/pkg/models/constants.go
Line 58 in 5de7ab8
For readability though when printing things and in our docs,
nocreate
is not really readable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I made the spec decoding case-insensitive and also made sure that whenever
nocreate
is shown to the user it looks likenoCreate
.