Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Check podID and containerID function arguments #163

Merged
merged 6 commits into from
Apr 12, 2017

Conversation

jodh-intel
Copy link
Collaborator

This branch switches the logic around so that rather than passing blank podID and containerID's around and then magically expanding them at quite a low level, the podID and containerID are set as soon as possible by virtc. 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 called resourceNeedsContainerID().

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.

errors.go Outdated
)

// common error objects used for argument checking
var (
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok too bad...

@@ -227,7 +228,14 @@ func buildPodConfig(context *cli.Context) (vc.PodConfig, error) {
Memory: vmMemory,
}

id := context.String("pod-id")
Copy link
Collaborator

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.

Copy link
Collaborator Author

@jodh-intel jodh-intel Mar 22, 2017

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 a uuid? 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 a uuid? 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?)

Copy link
Collaborator

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 UUIDs 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.

Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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 pod uuid automatically).

  • virtc container create --pod-id=$pod_id
    (generates container uuid automatically).

Copy link
Collaborator

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
....
}

Copy link
Collaborator

@sameo sameo Mar 23, 2017

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.

Copy link
Collaborator Author

@jodh-intel jodh-intel Mar 24, 2017

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 {
Copy link
Collaborator

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"

Copy link
Collaborator Author

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 {
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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? :)

@sboeuf
Copy link
Collaborator

sboeuf commented Mar 21, 2017

@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 ?

@jodh-intel
Copy link
Collaborator Author

@sboeuf - volontiers! I thought the crunched up approach was "the golang way" - I much prefer letting the code breathe! 😄

@jodh-intel jodh-intel force-pushed the check-args branch 3 times, most recently from e4e73d7 to 62dda78 Compare March 22, 2017 17:57
@jodh-intel
Copy link
Collaborator Author

Added a new function commonResourceChecks() to reduce the cyclomatic complexity of storeResource() and thus hopefully appease the gods of Travis.

@sboeuf
Copy link
Collaborator

sboeuf commented Mar 22, 2017

@jodh-intel Yeah but you have to fix unit tests now ;)

@jodh-intel
Copy link
Collaborator Author

@sboeuf - Yep - on it ;)

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")
Copy link
Contributor

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"?

Copy link

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

Copy link
Collaborator Author

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 ;)

Copy link
Contributor

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.

Copy link
Collaborator Author

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? 😄

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]>
@jodh-intel
Copy link
Collaborator Author

Ready for re-review.

@jodh-intel
Copy link
Collaborator Author

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).

@jodh-intel jodh-intel changed the title RFC: Check podID and containerID function arguments Check podID and containerID function arguments Apr 12, 2017
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]>
@jodh-intel
Copy link
Collaborator Author

Updated cleanUp() function to stop TestQemuAppendImage from failing due to a missing testImage file. This fix is minimal: I plan to raise follow-up PRs to improve the tests after this PR lands.

@coveralls
Copy link

coveralls commented Apr 12, 2017

Coverage Status

Coverage decreased (-1.05%) to 67.245% when pulling ac92c3e on jodh-intel:check-args into 9c5273c on containers:master.

@sboeuf
Copy link
Collaborator

sboeuf commented Apr 12, 2017

lgtm

Approved with PullApprove

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants