Skip to content

Commit

Permalink
checkup: Generate cloud-init data
Browse files Browse the repository at this point in the history
Currently, the cloud-init data is hard-coded.
Generate the cloud-init using a function, in order to
be able to share the credentials with the component
that will log in to the VMI in a follow-up commit.

Signed-off-by: Ram Lavi <ralavi@redhat.com>
  • Loading branch information
RamLavi committed Dec 4, 2023
1 parent d905829 commit 3350929
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/internal/checkup/checkup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -189,11 +190,6 @@ func newRealtimeVMI(checkupConfig config.Config) *kvcorev1.VirtualMachineInstanc
guestMemory = "4Gi"
rootDiskName = "rootdisk"
cloudInitDiskName = "cloudinitdisk"
userData = `#cloud-config
password: redhat
chpasswd:
expire: false
user: user`
)

return vmi.New(randomizeName(VMINamePrefix),
Expand All @@ -210,7 +206,7 @@ user: user`
vmi.WithNodeSelector(checkupConfig.VMUnderTestTargetNodeName),
vmi.WithContainerDisk(rootDiskName, checkupConfig.VMUnderTestContainerDiskImage),
vmi.WithVirtIODisk(rootDiskName),
vmi.WithCloudInitNoCloudVolume(cloudInitDiskName, userData),
vmi.WithCloudInitNoCloudVolume(cloudInitDiskName, CloudInit(config.VMIUsername, config.VMIPassword)),
vmi.WithVirtIODisk(cloudInitDiskName),
)
}
Expand All @@ -224,3 +220,14 @@ func randomizeName(prefix string) string {
func ObjectFullName(namespace, name string) string {
return fmt.Sprintf("%s/%s", namespace, name)
}

func CloudInit(username, password string) string {
sb := strings.Builder{}
sb.WriteString("#cloud-config\n")
sb.WriteString(fmt.Sprintf("user: %s\n", username))
sb.WriteString(fmt.Sprintf("password: %s\n", password))
sb.WriteString("chpasswd:\n")
sb.WriteString(" expire: false")

return sb.String()
}
11 changes: 11 additions & 0 deletions pkg/internal/checkup/checkup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ func TestRunFailure(t *testing.T) {
assert.Equal(t, expectedResults, actualResults)
}

func TestCloudInitString(t *testing.T) {
actualString := checkup.CloudInit("user", "password")
expectedString := `#cloud-config
user: user
password: password
chpasswd:
expire: false`

assert.Equal(t, expectedString, actualString)
}

type executorStub struct {
executeErr error
}
Expand Down

0 comments on commit 3350929

Please sign in to comment.