Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

buck-ross
Copy link

This feature consists of a new optional flag --hostname HOSTNAME, which can be used when invoking toolbox 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:

  • A new feature, which is backwards compatible with all existing use-cases of toolbox create
  • Associated system tests for the new --hostname flag
  • Associated updates to the toolbox create documentation in the manual.

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
@softwarefactory-project-zuul
Copy link

Build succeeded.

@debarshiray
Copy link
Member

This pull request has been replaced by #1134

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])$")
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

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

Typo alert: netowork

Copy link
Member

@debarshiray debarshiray left a 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",
"",
Copy link
Member

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
Copy link
Member

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.
Copy link
Member

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()) {
Copy link
Member

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

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()
Copy link
Member

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

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.

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

Successfully merging this pull request may close these issues.

3 participants