Skip to content

Commit

Permalink
MGMT-16843: Ensure valid hostname during install
Browse files Browse the repository at this point in the history
https://issues.redhat.com/browse/MGMT-16843
Some processes during install time depend on the hostname and an
invalid hostname (e.g. 'localhost', having capital letters, etc.)
that could cause the install to fail.

Extra validation is added when checking the hostname to ensure
it is valid. A random hostname will be assigned if the current
hostname is invalid.
  • Loading branch information
CrystalChun committed Feb 21, 2024
1 parent 7a2ea37 commit af644c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/installer/installer.go
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/google/uuid"
"github.com/openshift/assisted-installer/src/main/drymock"
"github.com/openshift/assisted-service/pkg/secretdump"
"github.com/openshift/assisted-service/pkg/validations"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
Expand Down Expand Up @@ -370,7 +371,7 @@ func (i *installer) startBootstrap() error {
remove the work done by 30-local-dns-prepender. This will cause DNS issue in bootkube and it will fail to complete
successfully
*/
err = i.checkLocalhostName()
err = i.checkHostname()
if err != nil {
i.log.Error(err)
return err
Expand Down Expand Up @@ -935,24 +936,24 @@ func (i *installer) createSingleNodeMasterIgnition() (string, error) {
return singleNodeMasterIgnitionPath, nil
}

func (i *installer) checkLocalhostName() error {
func (i *installer) checkHostname() error {
if i.DryRunEnabled {
return nil
}

i.log.Infof("Start checking localhostname")
i.log.Infof("Start checking hostname")
hostname, err := i.ops.GetHostname()
if err != nil {
i.log.Errorf("Failed to get hostname from kernel, err %s157", err)
return err
}
if hostname != "localhost" {
i.log.Infof("hostname is not localhost, no need to do anything")

if err := validations.ValidateHostname(hostname); err == nil && hostname != "localhost" {
i.log.Infof("hostname [%s] is not localhost or invalid, no need to do anything", hostname)
return nil
}

data := fmt.Sprintf("random-hostname-%s", uuid.New().String())
i.log.Infof("write data into /etc/hostname")
i.log.Infof("Hostname [%s] is invalid, generated random hostname [%s] and writing data into /etc/hostname")
return i.ops.CreateRandomHostname(data)
}

Expand Down
10 changes: 5 additions & 5 deletions src/installer/installer_test.go
Expand Up @@ -369,7 +369,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
{string(models.HostStageRebooting)},
})
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
Expand Down Expand Up @@ -463,7 +463,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
})
extractIgnitionToFS("extract failure", fmt.Errorf("extract failed"))
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
Expand Down Expand Up @@ -517,7 +517,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
{string(models.HostStageWaitingForControlPlane), waitingForBootstrapToPrepare},
})
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
err := fmt.Errorf("Failed to restart NetworkManager")
restartNetworkManager(err)
//HostRoleMaster flow:
Expand Down Expand Up @@ -576,7 +576,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
extractIgnitionToFS("extract failure", fmt.Errorf("extract failed"))

bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
Expand Down Expand Up @@ -1084,7 +1084,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
// single node bootstrap flow
singleNodeBootstrapSetup()
err := fmt.Errorf("Failed to restart NetworkManager")
checkLocalHostname("not localhost", err)
checkLocalHostname("notlocalhost", err)
ret := installerObj.InstallNode()
Expect(ret).Should(Equal(err))
})
Expand Down

0 comments on commit af644c5

Please sign in to comment.