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

Move boot script to cloud init #62

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion pkg/internal/checkup/checkup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"fmt"
"log"
"path"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -226,7 +228,13 @@ func (c *Checkup) waitForVMIDeletion(ctx context.Context) error {
}

func newVMUnderTestConfigMap(checkupConfig config.Config) *corev1.ConfigMap {
return configmap.New(VMUnderTestConfigMapNamePrefix, checkupConfig.PodName, checkupConfig.PodUID)
vmUnderTestConfigData := map[string]string{
config.BootScriptName: generateBootScript(),
}
return configmap.New(VMUnderTestConfigMapNamePrefix,
checkupConfig.PodName,
checkupConfig.PodUID,
vmUnderTestConfigData)
}

func newRealtimeVMI(checkupConfig config.Config) *kvcorev1.VirtualMachineInstance {
Expand Down Expand Up @@ -263,12 +271,43 @@ func newRealtimeVMI(checkupConfig config.Config) *kvcorev1.VirtualMachineInstanc
)
}

func generateBootScript() string {
const isolatedCores = "2-3"
sb := strings.Builder{}

sb.WriteString("#!/bin/bash\n")
sb.WriteString("set -x\n")
sb.WriteString("\n")
sb.WriteString("checkup_tuned_adm_set_marker_full_path=" + config.BootScriptTunedAdmSetMarkerFileFullPath + "\n")
sb.WriteString("\n")
sb.WriteString("if systemctl --type swap list-units | grep -q '.swap'; then\n")
sb.WriteString(" systemctl mask \"$(systemctl --type swap list-units | grep '.swap' | awk '{print $1}')\"\n")
sb.WriteString("fi\n")
sb.WriteString("\n")
sb.WriteString("if [ ! -f \"$checkup_tuned_adm_set_marker_full_path\" ]; then\n")
sb.WriteString(" tuned_conf=\"/etc/tuned/realtime-virtual-guest-variables.conf\"\n")
sb.WriteString(" echo \"isolated_cores=" + isolatedCores + "\" > \"$tuned_conf\"\n")
sb.WriteString(" echo \"isolate_managed_irq=Y\" >> \"$tuned_conf\"\n")
sb.WriteString(" tuned-adm profile realtime-virtual-guest\n")
sb.WriteString(" touch $checkup_tuned_adm_set_marker_full_path\n")
sb.WriteString(" reboot\n")
sb.WriteString(" exit 0\n")
sb.WriteString("fi\n")
sb.WriteString("\n")
sb.WriteString("touch " + config.BootScriptReadinessMarkerFileFullPath + "\n")

return sb.String()
}

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

return []string{
fmt.Sprintf("mkdir %s", configMountDirectory),
fmt.Sprintf("mount /dev/$(lsblk --nodeps -no name,serial | grep %s | cut -f1 -d' ') %s", configDiskSerial, configMountDirectory),
fmt.Sprintf("cp %s %s", path.Join(configMountDirectory, config.BootScriptName), config.BootScriptBinDirectory),
fmt.Sprintf("chmod 744 %s", path.Join(config.BootScriptBinDirectory, config.BootScriptName)),
path.Join(config.BootScriptBinDirectory, config.BootScriptName),
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/checkup/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/apimachinery/pkg/types"
)

func New(name, ownerName, ownerUID string) *k8scorev1.ConfigMap {
func New(name, ownerName, ownerUID string, data map[string]string) *k8scorev1.ConfigMap {
return &k8scorev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -39,5 +39,6 @@ func New(name, ownerName, ownerUID string) *k8scorev1.ConfigMap {
},
},
},
Data: data,
}
}
5 changes: 3 additions & 2 deletions pkg/internal/checkup/configmap/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func TestNew(t *testing.T) {
name := "my-cm"
ownerName := "my-pod"
ownerUID := "1234567890"
data := map[string]string{"key": "value"}

actualConfigMap := configmap.New(name, ownerName, ownerUID)
actualConfigMap := configmap.New(name, ownerName, ownerUID, data)

expectedConfigMap := &k8scorev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -51,7 +52,7 @@ func TestNew(t *testing.T) {
},
},
},
Data: nil,
Data: data,
}

assert.Equal(t, expectedConfigMap, actualConfigMap)
Expand Down
5 changes: 5 additions & 0 deletions pkg/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const (
VMUnderTestDefaultContainerDiskImage = "quay.io/kiagnose/kubevirt-realtime-checkup-vm:main"
OslatDefaultDuration = 5 * time.Minute
OslatDefaultLatencyThreshold = 40 * time.Microsecond

BootScriptName = "realtime-checkup-boot.sh"
BootScriptBinDirectory = "/usr/bin/"
BootScriptTunedAdmSetMarkerFileFullPath = "/var/realtime-checkup-tuned-adm-set-marker"
BootScriptReadinessMarkerFileFullPath = "/tmp/realtime-checkup-ready-marker"
)

var (
Expand Down
76 changes: 0 additions & 76 deletions vms/vm-under-test/scripts/customize-vm
Original file line number Diff line number Diff line change
Expand Up @@ -39,82 +39,6 @@ disable_swap() {
sed -i '/swap/s/^/#/' /etc/fstab
}

# Create and enable the boot checkup service
setup_boot_service() {
local service_name="realtime-checkup-boot.service"
local checkup_boot_script_full_path="/usr/bin/realtime-checkup-boot.sh"
local checkup_boot_service_full_path="/usr/lib/systemd/system/$service_name"

setup_checkup_boot_script "$checkup_boot_script_full_path"

cat <<EOF > "$checkup_boot_service_full_path"
[Unit]
Description=Checkup Boot Script
Before=qemu-guest-agent.service
[Service]
Type=oneshot
ExecStart=$checkup_boot_script_full_path

Restart=no
User=root
Group=root
[Install]
WantedBy=multi-user.target
Wants=first-boot-complete.target
EOF

systemctl enable "$checkup_boot_service_full_path"
systemctl start "$checkup_boot_service_full_path"
}

setup_checkup_boot_script() {
local checkup_boot_script_full_path=$1
cat <<'EOF' > "$checkup_boot_script_full_path"
#!/usr/bin/env bash
#
# This file is part of the kiagnose project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright 2024 Red Hat, Inc.
#

set -x

checkup_tuned_adm_set_marker_full_path="/var/realtime-checkup-tuned-adm-set-marker"

if systemctl --type swap list-units | grep -q '.swap'; then
systemctl mask "$(systemctl --type swap list-units | grep '.swap' | awk '{print $1}')"
fi

if [ ! -f "$checkup_tuned_adm_set_marker_full_path" ]; then
tuned_conf="/etc/tuned/realtime-virtual-guest-variables.conf"
echo "isolated_cores=2-3" > "$tuned_conf"
echo "isolate_managed_irq=Y" >> "$tuned_conf"
tuned-adm profile realtime-virtual-guest
touch $checkup_tuned_adm_set_marker_full_path
reboot
exit 0
fi

boot_script_readiness_marker_full_path="/tmp/realtime-checkup-ready-marker"
touch $boot_script_readiness_marker_full_path
EOF

chmod +x "$checkup_boot_script_full_path"
}

disable_services
install_packages
disable_swap
setup_boot_service
Loading