-
Notifications
You must be signed in to change notification settings - Fork 43
Check podID and containerID function arguments #163
Conversation
errors.go
Outdated
) | ||
|
||
// common error objects used for argument checking | ||
var ( |
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.
Those ones could be const instead, right ?
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.
That would be good, but you can't create const
errors
afaik.
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 too bad...
hack/virtc/main.go
Outdated
@@ -227,7 +228,14 @@ func buildPodConfig(context *cli.Context) (vc.PodConfig, error) { | |||
Memory: vmMemory, | |||
} | |||
|
|||
id := context.String("pod-id") |
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 am fine with this change, but could you create the corresponding flag ? Indeed, there is no pod-id flag for the create command. And could you call it "id" instead of "pod-id" ? It is not needed to use pod-id for the naming because we are in the command pod ... thus it is pretty obvious.
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.
Added. This raises the question though of what is considered a valid podID
and containerID
. Since these end up being encoded in pathnames, we should take care here.
Currently containerID (via --id=
) is free-form, whereas podID
default to a uuid
(although this branch would potentially allow that to change).
@sameo - Could you clarify what the approach should be here by answering a couple of questions:
- should
podID
be restricted to being auuid
? I'm guessing yes. If so, maybe we shouldn't allow a user to specify a uuid value manually? - should
containerID
be restricted to being auuid
? I'm guessing no, but how about we restrict the name to only be able to contain characters from the set[a-ZA-Z0-9-_.]
? (Do we want unicode support?)
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.
@jodh-intel podID
should be a UUID
, yes. I agree they should just be assigned by virtcontainers and not exported.
I think containerID
should also be a UUID
, not to be confused with a container name (That we don't have atm).
So overall we should stop exporting the ID
field from both ContainerConfig
and PodConfig
and only generate valid UUID
s for those.
And we should add a Name
exported field to both ContainerConfig
and PodConfig
that ican be defined by the caller or we'd generate a readable one. Here only [a-ZA-Z0-9-_.]
would be acceptable. This can be done as a separate PR.
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.
@sameo according to your comment, the generation of the UUID in virtc done in this PR is not valid, right ?
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.
@sameo - In terms of the command-line API then, are you saying that we should not have a --id=
for pod create
or container create
? In other words, we should have the following behaviour:
-
virtc pod create
(generates poduuid
automatically). -
virtc container create --pod-id=$pod_id
(generates containeruuid
automatically).
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.
Yes, but he also means that virtcontainers should be responsible for this, and we could stop to export ID in PodConfig.
type PodConfig struct {
id
....
}
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.
virtc pod create
(generates pod uuid automatically).
Correct.
virtc container create --pod-id=$pod_id
(generates container uuid automatically).
Correct.
And obviously the UUID
generation should happen in virtcontainers.
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've raised #171 to make these ID
fields private.
filesystem.go
Outdated
// pod-specific resources | ||
return false | ||
default: | ||
if podSpecific == true { |
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.
You could simplify this with "return !podSpecific"
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.
Agreed.
pod.go
Outdated
@@ -280,19 +287,26 @@ type PodConfig struct { | |||
|
|||
// valid checks that the pod configuration is valid. | |||
func (podConfig *PodConfig) valid() bool { | |||
if _, err := newHypervisor(podConfig.HypervisorType); err != nil { | |||
podConfig.HypervisorType = QemuHypervisor | |||
if podConfig == nil { |
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.
Can it happen ? I don't think so because you need podConfig to be valid (not nil) to be able to call this function, right ?
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.
Oops - how did that get in there? :)
@jodh-intel I have few comments, let me know what you think about. And as a global comment, could you please add some new lines after all the new closing brackets corresponding to new if statements ? |
@sboeuf - volontiers! I thought the crunched up approach was "the golang way" - I much prefer letting the code breathe! 😄 |
e4e73d7
to
62dda78
Compare
Added a new function |
@jodh-intel Yeah but you have to fix unit tests now ;) |
@sboeuf - Yep - on it ;) |
76f1770
to
b41245d
Compare
errors.go
Outdated
ErrNeedPodID = errors.New("Pod ID cannot be empty") | ||
ErrNeedContainerID = errors.New("Container ID cannot be empty") | ||
ErrNeedFile = errors.New("file cannot be empty") |
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.
Maybe "file" should be "File"?
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.
actually, it seems usual to have errors starting with a lower case character in golang, so the other ones should be changed if any (a separate commit though). For reference I grepped through github.com/golang/go:
$ git grep -p 'errors.New.*"[A-Z]' src/ | wc -l
96
$ git grep -p 'errors.New.*"[a-z]' src/ | wc -l
1364
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.
Hi @wcwxyz - thanks for reminding me about this. I had planned for these errors to refer to the name of the object originally. file
didn't fit because that error is used for a file path. But looking at this list again, it's all rather inconsistent since it should be containerID
and podID
. I think as you say, just capitalising file
would be the simplest fix ;)
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.
Thanks for looking into it. I figure it's a typo.
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.
@dlespiau - interesting, but looking at the virtcontainers code:
$ git grep -p 'fmt.Errorf.*"[A-Z]' | wc -l
220
$ git grep -p 'fmt.Errorf.*"[a-z]' | wc -l
40
Maybe an opportunity here to write a new linter for Error
's to plug into https://github.com/alecthomas/gometalinter? 😄
0cbc28c
to
7c3c612
Compare
Return error if argument checks fail. Signed-off-by: James O. D. Hunt <[email protected]>
Added additional variables for standard error conditions. Signed-off-by: James O. D. Hunt <[email protected]>
Previously, blank containerID was handled by ContainerConfig.valid() and a blank podID was handled by PodConfig.valid(). However, this meant function arguments could not be checked reliably (since some functions accepted blank values, knowning that they would be expanded by a lower level). The behaviour was also rather confusing since valid() connotes isValid(), not makeValid(). This change Ensures that both podID and containerID are set at the highest level (in virtc) which will allow all lower-level functions to assert these values are non-blank in future. It also adds an explicit "--id=" for "pod create". Signed-off-by: James O. D. Hunt <[email protected]>
Ready for re-review. |
Note that once we've decided on a strategy for #187, I can raise a follow-up PR to simplify the test changes (using a test setup fixture). |
Where possible, standard errors are used. Note that a few parameter names have changed slightly to ensure the returned standard errors make sense. Signed-off-by: James O. D. Hunt <[email protected]>
Explain which podResource types relate only to pods. Signed-off-by: James O. D. Hunt <[email protected]>
Updated usage to show that if the user does not specify a container name using "--id=", one will be chosen automatically. Signed-off-by: James O. D. Hunt <[email protected]>
Updated |
This branch switches the logic around so that rather than passing blank
podID
andcontainerID
's around and then magically expanding them at quite a low level, thepodID
andcontainerID
are set as soon as possible byvirtc
. This allows all functions to validate the parameters are not blank and return an error if they are [1].The logic is complicated by the fact that in certain contexts -- for example when creating particular
podResource
's --containerID
may be blank. To handle this, I've created a new function calledresourceNeedsContainerID()
.Note that this is a proof-of-concept branch - the tests have not been updated so will fail.
[1] - This allows "fail-fast" behaviour which is less confusing when attempting to debug issues.