-
Notifications
You must be signed in to change notification settings - Fork 220
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
Allow toolbox containers to be created with a custom hostname #1007
Conversation
The new `--hostname` flag is now available when invoking `toolbox create` in order to create a container with a custom hostname. The new flag does not enforce network-resolvability of any custom hostnames, but it does require that the hostname is valid, per RFC 1123. To preserve backward-compatibility, the hostname will still default to "toolbox" if the flag is not specified. Signed-off-by: Buckley Ross <[email protected]> Change-Id: Ib3c0706e3a93a5748453e39998b9893e3e57c305
These tests ensure that the new `--hostname` flag works as intended. Signed-off-by: Buckley Ross <[email protected]> Change-Id: Id7b7775fe73a6600afe2dabb0d905be5ba17bb57
Updated the `toolbox create` manual page to cover the usage of the new `--hostname` flag for specifying custom hostnames when creating new containers. Signed-off-by: Buckley Ross <[email protected]> Change-Id: I16d7f63769c17aec1ff69d34ac8fe28f48827777
Build succeeded.
|
Why was this replaced? Was this rebased against main? |
@@ -95,6 +103,10 @@ func init() { | |||
} | |||
|
|||
func create(cmd *cobra.Command, args []string) error { | |||
// This regex filters out strings which are not valid hostnames, according to RFC-1123. | |||
// Source: https://stackoverflow.com/a/106223 | |||
var hostnameRegexp = regexp.MustCompile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])$") |
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.
Do we really need to have this regular expression directly in the code? Can't we use some Go package for it?
@@ -85,6 +86,11 @@ Create a toolbox container for a different operating system DISTRO than the | |||
host. Cannot be used with `--image`. Has to be coupled with `--release` unless | |||
the selected DISTRO matches the host. | |||
|
|||
**--hostname** HOSTNAME | |||
|
|||
Initializes the netowork hostname of the toolbox container to the specified value. |
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.
Typo alert: netowork
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.
This is an implementation of #563 As I mentioned there already, let's first try to improve the default hostname (see #969). Customizing the hostname might not always be necessary to distinguish one container from another (see #98).
Regardless, here are some comments on the specific changes in this pull request. Maybe they might be of help for future work.
@@ -78,6 +80,12 @@ func init() { | |||
"", | |||
"Create a toolbox container for a different operating system distribution than the host") | |||
|
|||
flags.StringVarP(&createFlags.hostname, | |||
"hostname", | |||
"", |
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.
Nitpick: if you use flags.StringVar (ie., without the trailing P
), then this empty parameter can be dropped.
@@ -71,6 +71,25 @@ teardown() { | |||
assert_output --regexp "Created[[:blank:]]+fedora-toolbox-32" | |||
} | |||
|
|||
@test "create: Create a container with a custom hostname ('test.host-name.local')" { | |||
run $TOOLBOX -y create --hostname test.host-name.local |
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.
It will be good to use --separate-stderr
and be strict about the streams where things get written to.
} | ||
|
||
@test "create: Create a container with an invalid hostname ('test.host-name.local.')" { | ||
run $TOOLBOX -y create --hostname test.host-name.local. |
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.
Ditto about --separate-stderr
, and also use -1
to assert the exact exit code.
@@ -115,8 +127,13 @@ func create(cmd *cobra.Command, args []string) error { | |||
return errors.New("options --image and --release cannot be used together") | |||
} | |||
|
|||
if cmd.Flag("hostname").Changed && !hostnameRegexp.MatchString(cmd.Flag("hostname").Value.String()) { |
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.
Use createFlags.hostname
instead of cmd.Flag("hostname").Value.String()
.
@@ -115,8 +127,13 @@ func create(cmd *cobra.Command, args []string) error { | |||
return errors.New("options --image and --release cannot be used together") | |||
} | |||
|
|||
if cmd.Flag("hostname").Changed && !hostnameRegexp.MatchString(cmd.Flag("hostname").Value.String()) { | |||
return errors.New("invalid hostname") |
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.
It will be good to improve this error. See the examples in src/cmd/utils.go
.
var container string | ||
var containerArg string | ||
var hostname = cmd.Flag("hostname").Value.String() |
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.
This isn't necessary ...
@@ -158,14 +175,14 @@ func create(cmd *cobra.Command, args []string) error { | |||
return err | |||
} | |||
|
|||
if err := createContainer(container, image, release, true); err != nil { | |||
if err := createContainer(container, image, release, hostname, true); err != 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.
... because you can pass createFlags.hostname
here.
This feature consists of a new optional flag
--hostname HOSTNAME
, which can be used when invokingtoolbox create
to create a new container with a custom hostname.This is essentially a re-implementation of #210 from scratch, written in golang.
This may also address many of the concerns brought up in the comments of #771, and could potentially assist in the adoption of a new default hostname, since it allows for more flexibility in that regard.
This PR includes:
toolbox create
--hostname
flagtoolbox create
documentation in the manual.