Skip to content

Commit

Permalink
Merge pull request #148 from orelmisan/attach_cm_to_traffic_gen
Browse files Browse the repository at this point in the history
[traffic-gen-in-vm] Attach traffic-gen's ConfigMap to traffic gen VMI
  • Loading branch information
orelmisan authored Jul 19, 2023
2 parents c20107a + 142c2fd commit 10cb1ce
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
6 changes: 4 additions & 2 deletions pkg/internal/checkup/checkup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ func New(client kubeVirtVMIClient, namespace string, checkupConfig config.Config
const randomStringLen = 5
randomSuffix := rand.String(randomStringLen)

trafficGenCMName := trafficGenConfigMapName(randomSuffix)

return &Checkup{
client: client,
namespace: namespace,
params: checkupConfig,
vmiUnderTest: newVMIUnderTest(vmiUnderTestName(randomSuffix), checkupConfig),
trafficGen: newTrafficGen(trafficGenName(randomSuffix), checkupConfig),
trafficGenConfigMap: newTrafficGenConfigMap(trafficGenConfigMapName(randomSuffix), checkupConfig),
trafficGen: newTrafficGen(trafficGenName(randomSuffix), checkupConfig, trafficGenCMName),
trafficGenConfigMap: newTrafficGenConfigMap(trafficGenCMName, checkupConfig),
executor: executor,
}
}
Expand Down
35 changes: 30 additions & 5 deletions pkg/internal/checkup/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,29 @@ func newVMIUnderTest(name string, checkupConfig config.Config) *kvcorev1.Virtual
vmi.WithSRIOVInterface(eastNetworkName, checkupConfig.DPDKEastMacAddress.String(), config.VMIEastNICPCIAddress),
vmi.WithSRIOVInterface(westNetworkName, checkupConfig.DPDKWestMacAddress.String(), config.VMIWestNICPCIAddress),
vmi.WithContainerDisk(rootDiskName, checkupConfig.VMContainerDiskImage),
vmi.WithCloudInitNoCloudVolume(cloudInitDiskName, CloudInit(config.VMIUsername, config.VMIPassword)),
vmi.WithCloudInitNoCloudVolume(cloudInitDiskName, CloudInit(config.VMIUsername, config.VMIPassword, nil)),
)

return vmi.New(name, optionsToApply...)
}

func newTrafficGen(name string, checkupConfig config.Config) *kvcorev1.VirtualMachineInstance {
func newTrafficGen(name string, checkupConfig config.Config, configMapName string) *kvcorev1.VirtualMachineInstance {
const configDiskSerial = "DEADBEEF"
const configVolumeName = "trex-config"

optionsToApply := baseOptions(checkupConfig)

optionsToApply = append(optionsToApply,
vmi.WithAffinity(Affinity(checkupConfig.TrafficGeneratorNodeLabelSelector, checkupConfig.PodUID)),
vmi.WithSRIOVInterface(eastNetworkName, checkupConfig.TrafficGeneratorEastMacAddress.String(), config.VMIEastNICPCIAddress),
vmi.WithSRIOVInterface(westNetworkName, checkupConfig.TrafficGeneratorWestMacAddress.String(), config.VMIWestNICPCIAddress),
vmi.WithContainerDisk(rootDiskName, checkupConfig.TrafficGeneratorImage),
vmi.WithCloudInitNoCloudVolume(cloudInitDiskName, CloudInit(config.VMIUsername, config.VMIPassword)),
vmi.WithCloudInitNoCloudVolume(
cloudInitDiskName,
CloudInit(config.VMIUsername, config.VMIPassword, trafficGenBootCommands(configDiskSerial)),
),
vmi.WithConfigMapVolume(configVolumeName, configMapName),
vmi.WithConfigMapDisk(configVolumeName, configDiskSerial),
)

return vmi.New(name, optionsToApply...)
Expand Down Expand Up @@ -113,13 +121,30 @@ func Affinity(nodeName, ownerUID string) *k8scorev1.Affinity {
return &affinity
}

func CloudInit(username, password string) string {
func CloudInit(username, password string, bootCommands []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")
sb.WriteString(" expire: false\n")

if len(bootCommands) != 0 {
sb.WriteString("bootcmd:\n")

for _, command := range bootCommands {
sb.WriteString(fmt.Sprintf(" - %q\n", command))
}
}

return sb.String()
}

func trafficGenBootCommands(configDiskSerial string) []string {
const configMountDirectory = "/mnt/app-config"

return []string{
fmt.Sprintf("sudo mkdir %s", configMountDirectory),
fmt.Sprintf("sudo mount /dev/$(lsblk --nodeps -no name,serial | grep %s | cut -f1 -d' ') %s", configDiskSerial, configMountDirectory),
}
}
25 changes: 25 additions & 0 deletions pkg/internal/checkup/vmi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ func WithVirtIODisk(name string) Option {
}
}

func WithConfigMapDisk(name, serial string) Option {
return func(vmi *kvcorev1.VirtualMachineInstance) {
vmi.Spec.Domain.Devices.Disks = append(vmi.Spec.Domain.Devices.Disks,
kvcorev1.Disk{
Name: name,
Serial: serial,
},
)
}
}

func WithSRIOVInterface(name, macAddress, pciAddress string) Option {
return func(vmi *kvcorev1.VirtualMachineInstance) {
vmi.Spec.Domain.Devices.Interfaces = append(vmi.Spec.Domain.Devices.Interfaces, kvcorev1.Interface{
Expand Down Expand Up @@ -199,6 +210,20 @@ func WithContainerDisk(volumeName, imageName string) Option {
}
}

func WithConfigMapVolume(name, configMapName string) Option {
return func(vmi *kvcorev1.VirtualMachineInstance) {
vmi.Spec.Volumes = append(vmi.Spec.Volumes, kvcorev1.Volume{
Name: name,
VolumeSource: kvcorev1.VolumeSource{
ConfigMap: &kvcorev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: configMapName},
Optional: Pointer(false),
},
},
})
}
}

func WithCloudInitNoCloudVolume(name, userData string) Option {
return func(vmi *kvcorev1.VirtualMachineInstance) {
newVolume := kvcorev1.Volume{
Expand Down
33 changes: 29 additions & 4 deletions pkg/internal/checkup/vmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,37 @@ func TestAffinityCalculation(t *testing.T) {
}

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

assert.Equal(t, expectedString, actualString)
assert.Equal(t, expectedString, actualString)
})

t.Run("with boot commands", func(t *testing.T) {
bootCommands := []string{
"sudo mkdir /mnt/app-config",
"sudo mount /dev/$(lsblk --nodeps -no name,serial | grep DEADBEEF | cut -f1 -d' ') /mnt/app-config",
}

actualString := checkup.CloudInit(username, password, bootCommands)
expectedString := `#cloud-config
user: user
password: password
chpasswd:
expire: false
bootcmd:
- "sudo mkdir /mnt/app-config"
- "sudo mount /dev/$(lsblk --nodeps -no name,serial | grep DEADBEEF | cut -f1 -d' ') /mnt/app-config"
`

assert.Equal(t, expectedString, actualString)
})
}

0 comments on commit 10cb1ce

Please sign in to comment.